✅ Minor test improvements
This commit is contained in:
parent
1c16a893d6
commit
18d987caf7
3 changed files with 59 additions and 50 deletions
|
|
@ -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"))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue