-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
response_test.go
74 lines (67 loc) · 2.06 KB
/
response_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package httpsuite
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
type TestResponse struct {
Key string `json:"key"`
}
func Test_SendResponse(t *testing.T) {
tests := []struct {
name string
code int
data any
errs []Error
meta *Meta
expectedCode int
expectedJSON string
}{
{
name: "200 OK with TestResponse body",
code: http.StatusOK,
data: &TestResponse{Key: "value"},
errs: nil,
expectedCode: http.StatusOK,
expectedJSON: `{"data":{"key":"value"}}`,
},
{
name: "404 Not Found without body",
code: http.StatusNotFound,
data: nil,
errs: []Error{{Code: http.StatusNotFound, Message: "Not Found"}},
expectedCode: http.StatusNotFound,
expectedJSON: `{"errors":[{"code":404,"message":"Not Found"}]}`,
},
{
name: "200 OK with pagination metadata",
code: http.StatusOK,
data: &TestResponse{Key: "value"},
meta: &Meta{TotalPages: 100, Page: 1, PageSize: 10},
expectedCode: http.StatusOK,
expectedJSON: `{"data":{"key":"value"},"meta":{"total_pages":100,"page":1,"page_size":10}}`,
},
{
name: "400 Bad Request with multiple errors",
code: http.StatusBadRequest,
errs: []Error{{Code: 400, Message: "Invalid email"}, {Code: 400, Message: "Invalid password"}},
expectedCode: http.StatusBadRequest,
expectedJSON: `{"errors":[{"code":400,"message":"Invalid email"},{"code":400,"message":"Invalid password"}]}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
switch data := tt.data.(type) {
case TestResponse:
SendResponse[TestResponse](w, tt.code, data, tt.errs, tt.meta)
default:
SendResponse[any](w, tt.code, tt.data, tt.errs, tt.meta)
}
assert.Equal(t, tt.expectedCode, w.Code)
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
assert.JSONEq(t, tt.expectedJSON, w.Body.String())
})
}
}