Esempio n. 1
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.")
	}
}
Esempio n. 2
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.")
	}
}