示例#1
0
func TestRESTResponseWrite(t *testing.T) {
	response := NewRESTResponse()
	res := httptest.NewRecorder()
	response.Generate(res)
	test.Assert("RESTResponse.Write Header Content-Type", "application/json", res.Header().Get("Content-Type"), t)
	test.Assert("RESTResponse.Write status code", http.StatusOK, res.Code, t)

	var js map[string]interface{}
	err := json.Unmarshal(res.Body.Bytes(), &js)
	test.AssertNotErr("RESTResponse.Write", err, t)
}
示例#2
0
func TestDistanceHistogram(t *testing.T) {
	h0 := NewHistogram(openTestImage("img.jpg"))
	h1 := NewHistogram(openTestImage("img.jpg"))
	result := DistanceHistogram(&h0, &h1)
	expected := 749.6217574983835
	test.Assert("HistogramDistance", expected, result, t)
}
示例#3
0
func TestConfigGet(t *testing.T) {
	config := New("../test_fixtures/config.json")
	defer os.Remove("../test_fixtures/config.json")
	expected := "value1"
	result := config.Get("key1")

	test.Assert("Config.Get", expected, result, t)
}
示例#4
0
func TestRouterServeHTTPNotFound(t *testing.T) {
	router := New()
	request, _ := http.NewRequest("DELETE", "/test", nil)
	response := httptest.NewRecorder()
	router.ServeHTTP(response, request)

	test.Assert("Router.ServeHTTP", http.StatusNotFound, response.Code, t)
}
示例#5
0
func TestConfigSet(t *testing.T) {
	config := New("../test_fixtures/config.json")
	defer os.Remove("../test_fixtures/config.json")
	config.Set("newkey", "newval")
	expected := "newval"
	result := config.Get("newkey")

	test.Assert("Config.Set", expected, result, t)
}
示例#6
0
func TestFormatURL(t *testing.T) {
	params := map[string]string{
		"param1": "val1",
		"param2": "val2",
	}
	result, err := formatURL("http://example.com/path", params)
	expected := "http://example.com/path?param1=val1&param2=val2"

	test.AssertNotErr("formatURL", err, t)
	test.Assert("formatURL", expected, result, t)
}
示例#7
0
func TestAuthRandString(t *testing.T) {
	auth, _ := buildAuth()
	rand := auth.randString(10)
	if len(rand) != 10 {
		t.Errorf("Auth.randString() returned a string of length %s, expected 10", len(rand))
	}
	pattern := "[a-z0-9]+"
	match, err := regexp.MatchString(pattern, rand)

	test.AssertNotErr("Auth.randString", err, t)
	test.Assert("Auth.randString", true, match, t)
}
示例#8
0
func TestRouterGet(t *testing.T) {
	router := New()
	handler := NewTestHandler()
	router.Get("/test", handler)
	request, _ := http.NewRequest("GET", "/test", nil)
	response := httptest.NewRecorder()
	router.ServeHTTP(response, request)

	test.Assert("Router.ServeHTTP", http.StatusOK, response.Code, t)
	if !handler.IsCalled {
		t.Error("handler of matched route was not called")
	}
}
示例#9
0
func TestAuthUpdateSession(t *testing.T) {
	auth, _ := buildAuth()
	token := auth.GenerateToken()
	user := model.NewUser(token)
	err := auth.CreateSession(user)
	test.AssertNotErr("Auth.CreateSession", err, t)

	user.ID = "updated"
	errUpd := auth.UpdateSession(user)
	usercheck, errGet := auth.GetSession(token)

	test.AssertNotErr("Auth.UpdateSession", errUpd, t)
	test.AssertNotErr("formatURL", errGet, t)
	test.Assert("Auth.UpdateSession", user.ID, usercheck.ID, t)
}
示例#10
0
func TestRouterDelete(t *testing.T) {
	router := New()
	handler := NewTestHandler()
	router.Delete("/test", handler)
	request, err := http.NewRequest("DELETE", "/test", nil)
	if err != nil {
		t.Fatal(err)
	}
	response := httptest.NewRecorder()
	router.ServeHTTP(response, request)

	test.Assert("Router.ServeHTTP", http.StatusOK, response.Code, t)
	if !handler.IsCalled {
		t.Error("handler of matched route was not called")
	}
}
示例#11
0
func TestHandlerServeHTTPUnauthorized(t *testing.T) {
	ctx, auth, _ := buildContext()
	next := NewTestHandler()
	handler := RequireAuth(ctx, next)
	request, _ := http.NewRequest("GET", "/", nil)

	token := auth.GenerateToken()
	request.Header.Add("authToken", token)

	response := httptest.NewRecorder()
	handler.ServeHTTP(response, request)

	test.Assert("Handler.ServeHTTP", http.StatusUnauthorized, response.Code, t)
	if next.IsCalled != false {
		t.Error("Handler.ServeHTTP() called the next handler for unauthorized user.")
	}
}
示例#12
0
func TestRouterServeHTTP(t *testing.T) {
	router := New()
	handler1 := NewTestHandler()
	handler2 := NewTestHandler()
	router.Get("/", handler1)
	router.Get("/", handler2)
	request, _ := http.NewRequest("GET", "/", nil)
	response := httptest.NewRecorder()
	router.ServeHTTP(response, request)

	test.Assert("Router.ServeHTTP", http.StatusOK, response.Code, t)
	if handler1.IsCalled == false {
		t.Errorf("First registered handler was not matched first.")
	}
	if handler2.IsCalled == true {
		t.Errorf("Last registered handler was matched but shouldn't.")
	}
}
示例#13
0
func TestHandlerServeHTTP(t *testing.T) {
	ctx, auth, _ := buildContext()
	next := NewTestHandler()
	handler := RequireAuth(ctx, next)
	request, _ := http.NewRequest("GET", "/", nil)
	token := auth.GenerateToken()
	user := model.NewUser(token)
	auth.CreateSession(user)
	request.Header.Add("authToken", token)

	response := httptest.NewRecorder()
	handler.ServeHTTP(response, request)

	test.Assert("Handler.ServeHTTP", http.StatusOK, response.Code, t)
	if next.IsCalled != true {
		t.Error("Handler.ServeHTTP() didn't call the next handler on successful authentication.")
	}
	if ctx.User == nil {
		t.Error("Handler.ServeHTTP() didn't set the user to the context on successful authentication.")
	}
}
示例#14
0
func TestAverageColor(t *testing.T) {
	c := AverageColor(openTestImage("target.jpg"))
	test.Assert("AverageColor R", uint32(34676), c.R, t)
	test.Assert("AverageColor G", uint32(57807), c.G, t)
	test.Assert("AverageColor B", uint32(15416), c.B, t)
}
示例#15
0
func TestNewContext(t *testing.T) {
	ctx, _, _ := buildContext()
	_, err := os.Stat(ctx.RootDir)
	test.AssertNotErr("os.Stat(ctx.RootDir)", err, t)
	test.Assert("Context.RootDir", false, os.IsNotExist(err), t)
}