-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_test.go
More file actions
260 lines (240 loc) · 6.5 KB
/
Copy pathresponse_test.go
File metadata and controls
260 lines (240 loc) · 6.5 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package restapi
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
// newTestContext returns a gin.Context wired to a fresh httptest recorder.
func newTestContext(t *testing.T) (*gin.Context, *httptest.ResponseRecorder) {
t.Helper()
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
return c, w
}
func decodeBody(t *testing.T, w *httptest.ResponseRecorder) ResponseBody {
t.Helper()
var body ResponseBody
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode body %q: %v", w.Body.String(), err)
}
return body
}
func TestWriteResponse_NilContext_NoOp(t *testing.T) {
t.Parallel()
// Must not panic.
Default().WriteResponse(nil, http.StatusOK, false, nil)
}
func TestWriteResponse_AlreadyWritten_NoOp(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
c.Writer.WriteHeader(http.StatusTeapot)
c.Writer.Write([]byte("pre"))
Default().WriteResponse(c, http.StatusOK, false, &ResponseBody{Data: "data"})
if w.Code != http.StatusTeapot {
t.Fatalf("status changed after already-written; got %d", w.Code)
}
if w.Body.String() != "pre" {
t.Fatalf("body mutated after already-written; got %q", w.Body.String())
}
}
func TestWriteResponse_NilBody_NoAbort_StatusOnly(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
Default().WriteResponse(c, http.StatusNoContent, false, nil)
if w.Code != http.StatusNoContent {
t.Fatalf("status = %d; want %d", w.Code, http.StatusNoContent)
}
if w.Body.Len() != 0 {
t.Fatalf("body should be empty; got %q", w.Body.String())
}
}
func TestWriteResponse_NilBody_Abort_StatusOnly(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
Default().WriteResponse(c, http.StatusForbidden, true, nil)
if w.Code != http.StatusForbidden {
t.Fatalf("status = %d; want %d", w.Code, http.StatusForbidden)
}
if !c.IsAborted() {
t.Fatal("context should be aborted")
}
if w.Body.Len() != 0 {
t.Fatalf("body should be empty; got %q", w.Body.String())
}
}
func TestWriteResponse_BodyJSON_NoAbort(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
Default().WriteResponse(c, http.StatusOK, false, &ResponseBody{Data: "x"})
if w.Code != http.StatusOK {
t.Fatalf("status = %d; want 200", w.Code)
}
got := decodeBody(t, w)
if got.Data != "x" {
t.Fatalf("Data = %v; want x", got.Data)
}
if c.IsAborted() {
t.Fatal("context must not be aborted")
}
}
func TestWriteResponse_BodyJSON_Abort(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
Default().WriteResponse(c, http.StatusBadRequest, true, &ResponseBody{Data: "x"})
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d; want 400", w.Code)
}
if !c.IsAborted() {
t.Fatal("context should be aborted")
}
}
func TestSuccess_AppliesOptions_SkipsNil(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
pg := OffsetPaginationResponse{Limit: 10, Offset: 0, Total: 100}
Default().Success(c, http.StatusOK, "data", nil, WithPagination(pg))
if w.Code != http.StatusOK {
t.Fatalf("status = %d", w.Code)
}
body := decodeBody(t, w)
if body.Data != "data" {
t.Fatalf("Data = %v", body.Data)
}
if body.Pagination == nil {
t.Fatal("Pagination missing")
}
}
func TestSuccessBody(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
Default().SuccessBody(c, http.StatusCreated, &ResponseBody{Data: 1})
if w.Code != http.StatusCreated {
t.Fatalf("status = %d; want 201", w.Code)
}
}
func TestError_KnownCode_MappedStatus(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
Default().Error(c, CodeNotFound, WithMessage("missing"))
if w.Code != http.StatusNotFound {
t.Fatalf("status = %d; want 404", w.Code)
}
body := decodeBody(t, w)
if len(body.Errors) != 1 {
t.Fatalf("expected 1 error; got %+v", body.Errors)
}
if body.Errors[0].Code != CodeNotFound || body.Errors[0].Message != "missing" {
t.Fatalf("unexpected: %+v", body.Errors[0])
}
if c.IsAborted() {
t.Fatal("Error must not abort")
}
}
func TestError_UnknownCode_FallsBackToInternal(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
Default().Error(c, 99999)
if w.Code != http.StatusInternalServerError {
t.Fatalf("status = %d; want 500", w.Code)
}
body := decodeBody(t, w)
if body.Errors[0].Code != CodeInternal {
t.Fatalf("Code = %d; want CodeInternal", body.Errors[0].Code)
}
}
func TestErrorBody(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
Default().ErrorBody(c, http.StatusTeapot, &ResponseBody{Data: "x"})
if w.Code != http.StatusTeapot {
t.Fatalf("status = %d", w.Code)
}
}
func TestAbort_Aborts(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
Default().Abort(c, CodeForbidden)
if !c.IsAborted() {
t.Fatal("Abort must set aborted")
}
if w.Code != http.StatusForbidden {
t.Fatalf("status = %d; want 403", w.Code)
}
body := decodeBody(t, w)
if body.Errors[0].Code != CodeForbidden {
t.Fatalf("Code = %d", body.Errors[0].Code)
}
}
func TestAbortBody(t *testing.T) {
t.Parallel()
c, w := newTestContext(t)
Default().AbortBody(c, http.StatusGone, &ResponseBody{Data: "x"})
if !c.IsAborted() {
t.Fatal("AbortBody must set aborted")
}
if w.Code != http.StatusGone {
t.Fatalf("status = %d", w.Code)
}
}
func TestPackageWrappers_DelegateToDefault(t *testing.T) {
t.Parallel()
// WriteResponse
{
c, w := newTestContext(t)
WriteResponse(c, http.StatusOK, false, &ResponseBody{Data: "a"})
if w.Code != http.StatusOK || decodeBody(t, w).Data != "a" {
t.Fatal("WriteResponse wrapper failed")
}
}
// Success
{
c, w := newTestContext(t)
Success(c, http.StatusOK, "d")
if w.Code != http.StatusOK || decodeBody(t, w).Data != "d" {
t.Fatal("Success wrapper failed")
}
}
// SuccessBody
{
c, w := newTestContext(t)
SuccessBody(c, http.StatusAccepted, &ResponseBody{Data: 1})
if w.Code != http.StatusAccepted {
t.Fatal("SuccessBody wrapper failed")
}
}
// Error
{
c, w := newTestContext(t)
Error(c, CodeNotFound)
if w.Code != http.StatusNotFound {
t.Fatal("Error wrapper failed")
}
}
// ErrorBody
{
c, w := newTestContext(t)
ErrorBody(c, http.StatusBadGateway, &ResponseBody{Data: 1})
if w.Code != http.StatusBadGateway {
t.Fatal("ErrorBody wrapper failed")
}
}
// Abort
{
c, w := newTestContext(t)
Abort(c, CodeForbidden)
if !c.IsAborted() || w.Code != http.StatusForbidden {
t.Fatal("Abort wrapper failed")
}
}
// AbortBody
{
c, w := newTestContext(t)
AbortBody(c, http.StatusGone, &ResponseBody{Data: 1})
if !c.IsAborted() || w.Code != http.StatusGone {
t.Fatal("AbortBody wrapper failed")
}
}
}