-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_res_test.go
520 lines (416 loc) · 13 KB
/
t_res_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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package gr_test
import (
"encoding/xml"
"net/http"
"net/url"
"strings"
"testing"
"github.com/mitranim/gr"
)
func TestRes_IsInfo(t *testing.T) {
testIsInfo(t, func(code int) bool {
return (&gr.Res{StatusCode: code}).IsInfo()
})
}
func TestRes_IsOk(t *testing.T) {
testIsOk(t, func(code int) bool {
return (&gr.Res{StatusCode: code}).IsOk()
})
}
func TestRes_IsRedir(t *testing.T) {
testIsRedir(t, func(code int) bool {
return (&gr.Res{StatusCode: code}).IsRedir()
})
}
func TestRes_IsClientErr(t *testing.T) {
testIsClientErr(t, func(code int) bool {
return (&gr.Res{StatusCode: code}).IsClientErr()
})
}
func TestRes_IsServerErr(t *testing.T) {
testIsServerErr(t, func(code int) bool {
return (&gr.Res{StatusCode: code}).IsServerErr()
})
}
func TestRes_OkCatch(t *testing.T) {
eq(t, nil, (&gr.Res{StatusCode: 200}).OkCatch())
eq(t, nil, (&gr.Res{StatusCode: 201}).OkCatch())
eq(t, nil, (&gr.Res{StatusCode: 299}).OkCatch())
eq(t, nil, (&gr.Res{StatusCode: 200, Body: gr.NewStringReadCloser(`hello world`)}).OkCatch())
eq(t, nil, (&gr.Res{StatusCode: 201, Body: gr.NewStringReadCloser(`hello world`)}).OkCatch())
eq(t, nil, (&gr.Res{StatusCode: 299, Body: gr.NewStringReadCloser(`hello world`)}).OkCatch())
errs(
t,
`[gr] error: unexpected non-OK response with empty body`,
new(gr.Res).OkCatch(),
)
errs(
t,
`[gr] error (HTTP status 101): unexpected non-OK response with empty body`,
(&gr.Res{StatusCode: 101}).OkCatch(),
)
errs(
t,
`[gr] error: unexpected non-OK response; body: hello world`,
(&gr.Res{Body: gr.NewStringReadCloser(`hello world`)}).OkCatch(),
)
errs(
t,
`[gr] error (HTTP status 101): unexpected non-OK response; body: hello world`,
(&gr.Res{StatusCode: 101, Body: gr.NewStringReadCloser(`hello world`)}).OkCatch(),
)
t.Run(`closing`, func(t *testing.T) {
t.Run(`success`, func(t *testing.T) {
eq(
t,
nil,
(&gr.Res{StatusCode: 200, Body: FailReadCloser{}}).OkCatch(),
)
})
t.Run(`empty body`, func(t *testing.T) {
body := new(ReadCloseFlag)
errs(
t,
`[gr] error (HTTP status 101): unexpected non-OK response with empty body`,
(&gr.Res{StatusCode: 101, Body: body}).OkCatch(),
)
eq(
t,
&ReadCloseFlag{DidRead: true, DidClose: true},
body,
)
})
t.Run(`non-empty body`, func(t *testing.T) {
body := NewReaderCloseFlag(`hello world`)
errs(
t,
`[gr] error (HTTP status 101): unexpected non-OK response; body: hello world`,
(&gr.Res{StatusCode: 101, Body: body}).OkCatch(),
)
eq(t, true, body.DidClose)
})
})
}
func TestRes_RedirCatch(t *testing.T) {
eq(t, nil, (&gr.Res{StatusCode: 300}).RedirCatch())
eq(t, nil, (&gr.Res{StatusCode: 301}).RedirCatch())
eq(t, nil, (&gr.Res{StatusCode: 399}).RedirCatch())
errs(
t,
`[gr] error: unexpected non-redirect response with empty body`,
new(gr.Res).RedirCatch(),
)
errs(
t,
`[gr] error (HTTP status 101): unexpected non-redirect response with empty body`,
(&gr.Res{StatusCode: 101}).RedirCatch(),
)
errs(
t,
`[gr] error: unexpected non-redirect response; body: hello world`,
(&gr.Res{Body: gr.NewStringReadCloser(`hello world`)}).RedirCatch(),
)
errs(
t,
`[gr] error (HTTP status 101): unexpected non-redirect response; body: hello world`,
(&gr.Res{StatusCode: 101, Body: gr.NewStringReadCloser(`hello world`)}).RedirCatch(),
)
t.Run(`closing`, func(t *testing.T) {
t.Run(`success`, func(t *testing.T) {
eq(
t,
nil,
(&gr.Res{StatusCode: 300, Body: FailReadCloser{}}).RedirCatch(),
)
})
t.Run(`empty body`, func(t *testing.T) {
body := new(ReadCloseFlag)
errs(
t,
`[gr] error (HTTP status 101): unexpected non-redirect response with empty body`,
(&gr.Res{StatusCode: 101, Body: body}).RedirCatch(),
)
eq(
t,
&ReadCloseFlag{DidRead: true, DidClose: true},
body,
)
})
t.Run(`non-empty body`, func(t *testing.T) {
body := NewReaderCloseFlag(`hello world`)
errs(
t,
`[gr] error (HTTP status 101): unexpected non-redirect response; body: hello world`,
(&gr.Res{StatusCode: 101, Body: body}).RedirCatch(),
)
eq(t, true, body.DidClose)
})
})
}
func TestRes_CloseErr(t *testing.T) {
eq(t, nil, new(gr.Res).CloseErr())
body := &ReadCloseFlag{}
eq(t, nil, (&gr.Res{Body: body}).CloseErr())
eq(t, &ReadCloseFlag{DidRead: false, DidClose: true}, body)
}
func TestRes_Type(t *testing.T) {
test := func(exp string, src *gr.Res) {
t.Helper()
eq(t, exp, src.Type())
}
test(``, nil)
test(``, new(gr.Res))
test(gr.TypeJson, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJson}}})
test(gr.TypeJsonUtf8, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJsonUtf8}}})
}
func TestRes_Media(t *testing.T) {
test := func(typExp string, parExp map[string]string, src *gr.Res) {
t.Helper()
typ, par := src.Media()
eq(t, typExp, typ)
eq(t, parExp, par)
}
test(``, nil, nil)
test(``, nil, new(gr.Res))
test(gr.TypeJson, map[string]string{}, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJson}}})
test(gr.TypeJson, map[string]string{`charset`: `utf-8`}, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJsonUtf8}}})
}
func TestRes_MediaType(t *testing.T) {
test := func(exp string, src *gr.Res) {
t.Helper()
eq(t, exp, src.MediaType())
}
test(``, nil)
test(``, new(gr.Res))
test(gr.TypeJson, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJson}}})
test(gr.TypeJson, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJsonUtf8}}})
}
func TestRes_IsJson(t *testing.T) {
test := func(exp bool, src *gr.Res) {
t.Helper()
eq(t, exp, src.IsJson())
}
test(false, nil)
test(false, new(gr.Res))
test(false, &gr.Res{Header: http.Header{gr.Type: {gr.TypeForm}}})
test(false, &gr.Res{Header: http.Header{gr.Type: {gr.TypeFormUtf8}}})
test(true, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJson}}})
test(true, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJsonUtf8}}})
}
func TestRes_IsForm(t *testing.T) {
test := func(exp bool, src *gr.Res) {
t.Helper()
eq(t, exp, src.IsForm())
}
test(false, nil)
test(false, new(gr.Res))
test(false, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJson}}})
test(false, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJsonUtf8}}})
test(true, &gr.Res{Header: http.Header{gr.Type: {gr.TypeForm}}})
test(true, &gr.Res{Header: http.Header{gr.Type: {gr.TypeFormUtf8}}})
}
func TestRes_IsMulti(t *testing.T) {
test := func(exp bool, src *gr.Res) {
t.Helper()
eq(t, exp, src.IsMulti())
}
test(false, nil)
test(false, new(gr.Res))
test(false, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJson}}})
test(false, &gr.Res{Header: http.Header{gr.Type: {gr.TypeJsonUtf8}}})
test(true, &gr.Res{Header: http.Header{gr.Type: {gr.TypeMulti}}})
test(true, &gr.Res{Header: http.Header{gr.Type: {gr.TypeMultiUtf8}}})
}
func TestRes_ReadBytes(t *testing.T) {
eq(t, []byte(nil), new(gr.Res).ReadBytes())
body := NewReaderCloseFlag(`hello world`)
eq(t, `hello world`, string((&gr.Res{Body: body}).ReadBytes()))
eq(t, true, body.DidClose)
eq(t, []byte{}, (&gr.Res{Body: ReaderFailCloser{}}).ReadBytes())
}
func TestRes_ReadString(t *testing.T) {
eq(t, ``, new(gr.Res).ReadString())
body := NewReaderCloseFlag(`hello world`)
eq(t, `hello world`, string((&gr.Res{Body: body}).ReadString()))
eq(t, true, body.DidClose)
eq(t, ``, (&gr.Res{Body: ReaderFailCloser{}}).ReadString())
}
func TestRes_Form(t *testing.T) {
eq(t, url.Values(nil), new(gr.Res).Form())
t.Run(`decode and close body`, func(t *testing.T) {
body := NewReaderCloseFlag(`one=two&three=four&three=five`)
eq(
t,
url.Values{`one`: {`two`}, `three`: {`four`, `five`}},
(&gr.Res{Body: body}).Form(),
)
eq(t, true, body.DidClose)
})
t.Run(`catch decoding error and close body`, func(t *testing.T) {
body := NewReaderCloseFlag(`;`)
val, err := (&gr.Res{Body: body}).FormCatch()
eq(t, url.Values(nil), val)
errs(t, `[gr] failed to form-decode response body: invalid semicolon separator in query`, err)
eq(t, true, body.DidClose)
})
}
func TestRes_JsonCatch(t *testing.T) {
eq(t, nil, new(gr.Res).JsonCatch(nil))
t.Run(`allow nil output and close body`, func(t *testing.T) {
body := &ReadCloseFlag{}
eq(t, nil, (&gr.Res{Body: body}).JsonCatch(nil))
eq(t, &ReadCloseFlag{DidRead: false, DidClose: true}, body)
})
t.Run(`decode and close body`, func(t *testing.T) {
body := NewReaderCloseFlag(`"hello world"`)
var out string
eq(t, nil, (&gr.Res{Body: body}).JsonCatch(&out))
eq(t, `hello world`, out)
eq(t, true, body.DidClose)
})
t.Run(`catch decoding error and close body`, func(t *testing.T) {
body := NewReaderCloseFlag(`null`)
errs(
t,
`[gr] failed to JSON-decode response body: failing to decode`,
(&gr.Res{Body: body}).JsonCatch(new(DecodeFail)),
)
eq(t, true, body.DidClose)
})
}
func TestRes_JsonEither(t *testing.T) {
eq(t, false, new(gr.Res).JsonEither(nil, nil))
eq(t, false, (&gr.Res{StatusCode: 199}).JsonEither(nil, nil))
eq(t, true, (&gr.Res{StatusCode: 200}).JsonEither(nil, nil))
eq(t, false, (&gr.Res{StatusCode: 300}).JsonEither(nil, nil))
testJsonEither(t, Pair{nil, nil}, 0, Pair{nil, nil})
testJsonEither(t, Pair{nil, nil}, 200, Pair{nil, nil})
testJsonEither(t, Pair{nil, spr(`hello world`)}, 0, Pair{nil, spr(``)})
testJsonEither(t, Pair{spr(`hello world`), nil}, 200, Pair{spr(``), nil})
testJsonEither(t, Pair{spr(``), spr(`hello world`)}, 0, Pair{spr(``), spr(``)})
testJsonEither(t, Pair{spr(`hello world`), spr(``)}, 200, Pair{spr(``), spr(``)})
}
func testJsonEither(t testing.TB, exp Pair, status int, inp Pair) {
t.Helper()
body := NewReaderCloseFlag(`"hello world"`)
(&gr.Res{StatusCode: status, Body: body}).JsonEither(inp[0], inp[1])
eq(t, exp[0], inp[0])
eq(t, exp[1], inp[1])
eq(t, true, body.DidClose)
}
func TestRes_XmlCatch(t *testing.T) {
eq(t, nil, new(gr.Res).XmlCatch(nil))
t.Run(`allow nil output and close body`, func(t *testing.T) {
body := &ReadCloseFlag{}
eq(t, nil, (&gr.Res{Body: body}).XmlCatch(nil))
eq(t, &ReadCloseFlag{DidRead: false, DidClose: true}, body)
})
t.Run(`decode and close body`, func(t *testing.T) {
body := NewReaderCloseFlag(
`<string>hello world</string>`,
)
var out string
eq(t, nil, (&gr.Res{Body: body}).XmlCatch(&out))
eq(t, `hello world`, out)
eq(t, true, body.DidClose)
})
t.Run(`catch decoding error and close body`, func(t *testing.T) {
body := NewReaderCloseFlag(
`<string>hello world</string>`,
)
errs(
t,
`[gr] failed to XML-decode response body: failing to decode`,
(&gr.Res{Body: body}).XmlCatch(new(DecodeFail)),
)
eq(t, true, body.DidClose)
})
}
// Most behaviors are tested via `TestRes_XmlCatch`.
// We just need to verify that the customizer function is used.
func TestRes_XmlWithCatch(t *testing.T) {
const src = `<string>hello world`
t.Run(`without customizer`, func(t *testing.T) {
res := &gr.Res{Body: NewReaderCloseFlag(src)}
err := res.XmlWithCatch(new(string), nil)
eq(t, true, err != nil)
// XML syntax errors don't support error wrapping and unwrapping.
// As a result, `errors.Is(err, io.EOF) == false`.
eq(t, true, strings.Contains(err.Error(), `EOF`))
})
t.Run(`with customizer`, func(t *testing.T) {
res := &gr.Res{Body: NewReaderCloseFlag(src)}
fun := func(dec *xml.Decoder) {
dec.Strict = false
dec.AutoClose = []string{`string`}
}
err := res.XmlWithCatch(new(string), fun)
is(t, nil, err)
})
}
// TODO dedup with `TestReq_CloneBody`.
func TestRes_CloneBody(t *testing.T) {
t.Run(`nil`, func(t *testing.T) {
res := new(gr.Res)
eq(t, nil, res.Body)
eq(t, nil, res.CloneBody())
eq(t, nil, res.Body)
})
t.Run(`normal`, func(t *testing.T) {
res := new(gr.Res)
res.Body = gr.NewStringReadCloser(`hello world`)
eq(t, gr.NewStringReadCloser(`hello world`), res.Body)
eq(
t,
gr.NewBytesReadCloser([]byte(`hello world`)),
res.CloneBody(),
)
// The original body got read and replaced.
// Note the difference in type from before.
eq(
t,
gr.NewBytesReadCloser([]byte(`hello world`)),
res.Body,
)
eq(t, `hello world`, readStr(res.CloneBody()))
eq(t, `hello world`, readStr(res.CloneBody()))
// Mutates the request by reading its body, which becomes empty.
eq(t, `hello world`, readStr(res.Body))
eq(t, ``, readStr(res.Body))
// Clones have no choice but to be empty.
eq(t, ``, readStr(res.CloneBody()))
})
}
// TODO dedup with `TestReq_Clone`.
func TestRes_Clone(t *testing.T) {
t.Run(`nil`, func(t *testing.T) {
eq(t, (*gr.Res)(nil), (*gr.Res)(nil).Clone())
is(t, (*gr.Res)(nil), (*gr.Res)(nil).Clone())
})
src := new(gr.Res)
src.Header = H{gr.Type: {gr.TypeForm}}
src.Body = gr.NewStringReadCloser(`hello world`)
tar := src.Clone()
// Mutate the clone to demonstrate that the original is unaffected.
tar.Header.Set(gr.Type, gr.TypeJson)
eq(
t,
(&gr.Res{
Header: H{gr.Type: {gr.TypeForm}},
Body: gr.NewBytesReadCloser([]byte(`hello world`)),
}),
src,
)
eq(
t,
(&gr.Res{
Header: H{gr.Type: {gr.TypeJson}},
Body: gr.NewBytesReadCloser([]byte(`hello world`)),
}),
tar,
)
eq(t, `hello world`, readStr(tar.Body))
eq(t, ``, readStr(tar.Body))
eq(t, `hello world`, readStr(src.Body))
eq(t, ``, readStr(src.Body))
}