forked from rcrowley/go-tigertonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshaler_test.go
329 lines (297 loc) · 10.6 KB
/
marshaler_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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package tigertonic
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"testing"
)
func TestMarshaledCalm(t *testing.T) {
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return 0, http.Header{}, nil, nil
})
}
func TestMarshaledPanicNumIn(t *testing.T) {
testMarshaledPanic(func() {}, t)
testMarshaledPanic(func(u interface{}) {}, t)
testMarshaledPanic(func(u, h interface{}) {}, t)
testMarshaledPanic(func(u, h, rq, foo, bar interface{}) {}, t)
}
func TestMarshaledPanicIn0(t *testing.T) {
testMarshaledPanic(func(u, h, rq interface{}) {}, t)
}
func TestMarshaledPanicIn1(t *testing.T) {
testMarshaledPanic(func(u *url.URL, h, rq interface{}) {}, t)
}
func TestMarshaledPanicNumOut(t *testing.T) {
testMarshaledPanic(func(u *url.URL, h http.Header) {}, t)
testMarshaledPanic(func(u *url.URL, h http.Header) int {
return 0
}, t)
testMarshaledPanic(func(u *url.URL, h http.Header) (int, int) {
return 0, 0
}, t)
testMarshaledPanic(func(u *url.URL, h http.Header) (int, int, int) {
return 0, 0, 0
}, t)
testMarshaledPanic(func(u *url.URL, h http.Header) (int, int, int, int, int) {
return 0, 0, 0, 0, 0
}, t)
}
func TestMarshaledPanicOut0(t *testing.T) {
testMarshaledPanic(func(u *url.URL, h http.Header, rq *testRequest) (string, int, int, int) {
return "", 0, 0, 0
}, t)
}
func TestMarshaledPanicOut1(t *testing.T) {
testMarshaledPanic(func(u *url.URL, h http.Header, rq *testRequest) (int, int, int, int) {
return 0, 0, 0, 0
}, t)
}
func TestMarshaledPanicOut2(t *testing.T) {
testMarshaledPanic(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, int, int) {
return 0, http.Header{}, 0, 0
}, t)
}
func TestMarshaledPanicOut3(t *testing.T) {
testMarshaledPanic(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, int) {
return 0, http.Header{}, nil, 0
}, t)
}
func TestNotAcceptable(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
r.Header.Set("Accept", "text/plain")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusNoContent, nil, nil, nil
}).ServeHTTP(w, r)
if http.StatusNotAcceptable != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestUnsupportedMediaType(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", nil)
r.Header.Set("Accept", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusNoContent, nil, nil, nil
}).ServeHTTP(w, r)
if http.StatusUnsupportedMediaType != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestBadRequest(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString(""))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusNoContent, nil, nil, nil
}).ServeHTTP(w, r)
if http.StatusBadRequest != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "{\"description\":\"EOF\",\"error\":\"error\"}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestBadRequestSyntaxError(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString("}"))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusNoContent, nil, nil, nil
}).ServeHTTP(w, r)
if http.StatusBadRequest != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "{\"description\":\"invalid character '}' looking for beginning of value\",\"error\":\"json.SyntaxError\"}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestInternalServerError(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
r.Header.Set("Accept", "application/json")
Marshaled(func(u *url.URL, h http.Header) (int, http.Header, *testResponse, error) {
return 0, nil, nil, errors.New("foo")
}).ServeHTTP(w, r)
if http.StatusInternalServerError != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "{\"description\":\"foo\",\"error\":\"error\"}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestHTTPEquivError(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
r.Header.Set("Accept", "application/json")
Marshaled(func(u *url.URL, h http.Header) (int, http.Header, *testResponse, error) {
return 0, nil, nil, ServiceUnavailable{errors.New("foo")}
}).ServeHTTP(w, r)
if http.StatusServiceUnavailable != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "{\"description\":\"foo\",\"error\":\"tigertonic.ServiceUnavailable\"}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestSnakeCaseHTTPEquivError(t *testing.T) {
SnakeCaseHTTPEquivErrors = true
defer func() { SnakeCaseHTTPEquivErrors = false }()
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
r.Header.Set("Accept", "application/json")
Marshaled(func(u *url.URL, h http.Header) (int, http.Header, *testResponse, error) {
return 0, nil, nil, ServiceUnavailable{errors.New("foo")}
}).ServeHTTP(w, r)
if http.StatusServiceUnavailable != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "{\"description\":\"foo\",\"error\":\"service_unavailable\"}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestNamedError(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
r.Header.Set("Accept", "application/json")
Marshaled(func(u *url.URL, h http.Header) (int, http.Header, *testResponse, error) {
return 0, nil, nil, testNamedError("foo")
}).ServeHTTP(w, r)
if http.StatusInternalServerError != w.StatusCode {
t.Fatal(w.StatusCode)
}
if "{\"description\":\"foo\",\"error\":\"foo\"}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestNoContent(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
r.Header.Set("Accept", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusNoContent, nil, nil, nil
}).ServeHTTP(w, r)
if http.StatusNoContent != w.StatusCode {
t.Fatal(w.StatusCode)
}
}
func TestHeader(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
r.Header.Set("Accept", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusNoContent, map[string][]string{
"Foo": {"bar"},
}, nil, nil
}).ServeHTTP(w, r)
if "bar" != w.Header().Get("Foo") {
t.Fatal(w.Header().Get("Foo"))
}
}
func TestBody(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString("{\"foo\":\"bar\"}"))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
if "bar" != rq.Foo {
t.Fatal(rq.Foo)
}
return http.StatusOK, nil, &testResponse{"bar"}, nil
}).ServeHTTP(w, r)
if "{\"foo\":\"bar\"}\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestEmptyBody(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
Marshaled(func(*url.URL, http.Header, interface{}) (int, http.Header, interface{}, error) {
return http.StatusOK, nil, nil, nil
}).ServeHTTP(w, r)
if "" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestMarshaledShortGET(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
Marshaled(func(*url.URL, http.Header) (int, http.Header, interface{}, error) {
return http.StatusOK, nil, nil, nil
}).ServeHTTP(w, r)
if "" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func Test500OnMisconfiguredPost(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString("anything"))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
Marshaled(func(u *url.URL, h http.Header) (int, http.Header, *testResponse, error) {
return http.StatusOK, nil, &testResponse{"bar"}, nil
}).ServeHTTP(w, r)
if http.StatusInternalServerError != w.StatusCode {
t.Fatalf("Server did not 500 when trying to handle a POST to a handler with interface{} as the request type")
}
}
func TestNonPointerMapBody(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString(`{"a": "b"}`))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
Logged(Marshaled(func(u *url.URL, h http.Header, m map[string]string) (int, http.Header, string, error) {
return http.StatusOK, nil, m["a"], nil
}), nil).ServeHTTP(w, r)
if http.StatusOK != w.StatusCode {
t.Fatalf("Server responded %d to a post with a non-pointer map body", w.StatusCode)
}
var result string
_ = json.Unmarshal(w.Body.Bytes(), &result)
if "b" != result {
t.Fatalf("Body should have been 'b', but instead was '%s'", string(w.Body.Bytes()))
}
}
func TestNonPointerSliceBody(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("POST", "http://example.com/foo", bytes.NewBufferString(`["a", "b", "c"]`))
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
Logged(Marshaled(func(u *url.URL, h http.Header, s []string) (int, http.Header, string, error) {
return http.StatusOK, nil, s[1], nil
}), nil).ServeHTTP(w, r)
if http.StatusOK != w.StatusCode {
t.Fatalf("Server responded %d to a post with a non-pointer map body", w.StatusCode)
}
var result string
_ = json.Unmarshal(w.Body.Bytes(), &result)
if "b" != result {
t.Fatalf("Body should have been 'b', but instead was '%s'", string(w.Body.Bytes()))
}
}
func testMarshaledPanic(i interface{}, t *testing.T) {
defer func() {
err := recover()
if nil == err {
t.Fail()
}
if _, ok := err.(MarshalerError); !ok {
t.Error(err)
}
}()
Marshaled(i)
}
type testNamedError string
func (err testNamedError) Error() string { return string(err) }
func (err testNamedError) Name() string { return string(err) }
type testRequest struct {
Foo string `json:"foo"`
}
type testResponse struct {
Foo string `json:"foo"`
}