diff --git a/.golangci.yaml b/.golangci.yaml index 3f33683..5c6dd96 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -23,6 +23,11 @@ linters: - gosec - perfsprint - testifylint + exclusions: + rules: + - path: _test\.go + linters: + - err113 settings: testifylint: enable-all: true diff --git a/helper_test.go b/helper_test.go index 6c2a3ad..5abe8fd 100644 --- a/helper_test.go +++ b/helper_test.go @@ -10,17 +10,31 @@ import ( "github.com/stretchr/testify/assert" ) -var ( - errTest = errors.New("test error") +var errTest = errors.New("test error") + +type handlerType string + +const ( + handlerT handlerType = "Handler" + handlerFuncT handlerType = "HandlerFunc" + respHandlerT handlerType = "ResponseHandler" ) +type testHandler struct { + err error +} + +func (th testHandler) ServeHTTP(http.ResponseWriter, *http.Request) error { + return th.err +} + func TestNewHelper(t *testing.T) { mockErrorHandler := func(w http.ResponseWriter, r *http.Request, err error) {} helper := ezhandler.NewHelper(mockErrorHandler) assert.NotNil(t, helper) } -func runHelperTest(t *testing.T, name string, handlerErr, expectedErr error, expectedErrorHandlerCalled bool, handlerType string) { +func runHelperTest(t *testing.T, name string, handlerErr, expectedErr error, expectedErrorHandlerCalled bool, ht handlerType) { t.Run(name, func(t *testing.T) { var errorHandlerCalled bool mockErrorHandler := func(w http.ResponseWriter, r *http.Request, err error) { @@ -33,18 +47,16 @@ func runHelperTest(t *testing.T, name string, handlerErr, expectedErr error, exp rec := httptest.NewRecorder() var wrappedHandler http.Handler - switch handlerType { - case "Handler": - mockHandler := ezhandler.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error { - return handlerErr - }) + switch ht { + case handlerT: + mockHandler := testHandler{handlerErr} wrappedHandler = helper.Handler(mockHandler) - case "HandlerFunc": + case handlerFuncT: mockHandlerFunc := ezhandler.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error { return handlerErr }) wrappedHandler = helper.HandlerFunc(mockHandlerFunc) - case "ResponderHandler": + case respHandlerT: mockResponseHandler := ezhandler.ResponseHandler(func(r *http.Request) (ezhandler.ResponseHelper, error) { if handlerErr != nil { return nil, handlerErr @@ -52,6 +64,8 @@ func runHelperTest(t *testing.T, name string, handlerErr, expectedErr error, exp return ezhandler.JSONResponse(map[string]string{"status": "ok"}), nil }) wrappedHandler = helper.ResponderHandler(mockResponseHandler) + default: + assert.Fail(t, "Unknown test type: "+string(ht)) } wrappedHandler.ServeHTTP(rec, req) @@ -61,16 +75,16 @@ func runHelperTest(t *testing.T, name string, handlerErr, expectedErr error, exp } func TestHelper_Handler(t *testing.T) { - runHelperTest(t, "no error", nil, nil, false, "Handler") - runHelperTest(t, "with error", errTest, errTest, true, "Handler") + runHelperTest(t, "no error", nil, nil, false, handlerT) + runHelperTest(t, "with error", errTest, errTest, true, handlerT) } func TestHelper_HandlerFunc(t *testing.T) { - runHelperTest(t, "no error", nil, nil, false, "HandlerFunc") - runHelperTest(t, "with error", errTest, errTest, true, "HandlerFunc") + runHelperTest(t, "no error", nil, nil, false, handlerFuncT) + runHelperTest(t, "with error", errTest, errTest, true, handlerFuncT) } func TestHelper_ResponderHandler(t *testing.T) { - runHelperTest(t, "no error", nil, nil, false, "ResponderHandler") - runHelperTest(t, "with error", errTest, errTest, true, "ResponderHandler") + runHelperTest(t, "no error", nil, nil, false, respHandlerT) + runHelperTest(t, "with error", errTest, errTest, true, respHandlerT) } diff --git a/response_test.go b/response_test.go index 53faccb..6f922f2 100644 --- a/response_test.go +++ b/response_test.go @@ -1,17 +1,15 @@ package ezhandler_test import ( - "codeberg.org/danjones000/ezhandler" "encoding/json" "errors" - "github.com/stretchr/testify/assert" - "net/http" "net/http/httptest" "testing" -) -var errCircularMarshal = errors.New("json: unsupported value: encountered a cycle") + "codeberg.org/danjones000/ezhandler" + "github.com/stretchr/testify/assert" +) func TestJSONResponse(t *testing.T) { type testData struct { @@ -76,18 +74,20 @@ func TestJSONResponse_BodyError(t *testing.T) { assert.Nil(t, body) } +type jsonServeTestCase struct { + name string + handler ezhandler.ResponseHandler + expectedStatus int + expectedBody string + expectedError error +} + func TestResponseHandler_ServeHTTP_JSONResponse(t *testing.T) { type testData struct { Message string `json:"message"` } - tests := []struct { - name string - handler ezhandler.ResponseHandler - expectedStatus int - expectedBody string - expectedError error - }{ + tests := []jsonServeTestCase{ { name: "successful JSON response", handler: ezhandler.ResponseHandler(func(r *http.Request) (ezhandler.ResponseHelper, error) { @@ -111,9 +111,7 @@ func TestResponseHandler_ServeHTTP_JSONResponse(t *testing.T) { handler: ezhandler.ResponseHandler(func(r *http.Request) (ezhandler.ResponseHelper, error) { return nil, errTest // Using errTest from helper_test.go }), - expectedStatus: http.StatusOK, // Status won't be set if handler returns error before writing header - expectedBody: "", - expectedError: errTest, + expectedError: errTest, }, { name: "error from Body() method", @@ -125,37 +123,29 @@ func TestResponseHandler_ServeHTTP_JSONResponse(t *testing.T) { data.Self = &data return ezhandler.JSONResponse(data), nil }), - expectedStatus: http.StatusOK, // Status won't be set if Body() returns error - expectedBody: "", - expectedError: errCircularMarshal, // The error is returned by ServeHTTP, not the handler func + expectedError: errors.New("json: unsupported value: encountered a cycle"), // The error is returned by ServeHTTP, not the handler func }, } - for _, tt := range tests { - runResponseHandlerTest(t, tt) + for _, tc := range tests { + runResponseHandlerTest(t, tc) } } -func runResponseHandlerTest(t *testing.T, tt struct { - name string - handler ezhandler.ResponseHandler - expectedStatus int - expectedBody string - expectedError error -}) { - t.Run(tt.name, func(t *testing.T) { +func runResponseHandlerTest(tt *testing.T, tc jsonServeTestCase) { + tt.Run(tc.name, func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/", http.NoBody) rec := httptest.NewRecorder() - err := tt.handler.ServeHTTP(rec, req) + err := tc.handler.ServeHTTP(rec, req) - if tt.expectedError != nil { - assert.ErrorContains(t, err, tt.expectedError.Error()) + if tc.expectedError != nil { + assert.ErrorContains(t, err, tc.expectedError.Error()) } else { assert.NoError(t, err) - assert.Equal(t, tt.expectedStatus, rec.Code) - assert.Equal(t, tt.expectedBody, rec.Body.String()) - if tt.expectedBody != "" { + assert.Equal(t, tc.expectedStatus, rec.Code) + assert.Equal(t, tc.expectedBody, rec.Body.String()) + if tc.expectedBody != "" { assert.Equal(t, "application/json", rec.Header().Get("Content-Type")) } }