-
Notifications
You must be signed in to change notification settings - Fork 9
/
requests_test.go
590 lines (513 loc) · 15.8 KB
/
requests_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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package requests
import (
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/jgroeneveld/trial/assert"
"github.com/spf13/afero"
)
func TestPlainTextResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Hello, client")
}))
defer ts.Close()
cli := NewClient(ts.URL).Accept("text/plain")
var text string
var status int
err := cli.NewRequest("GET", "/").
Into(&text).
StatusInto(&status).
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if text != "Hello, client" {
t.Errorf("Failed reading plain text body: got %+q, expected %+q", text, "Hello, client")
}
if status != 200 {
t.Errorf("Failed to read status: got %d, expected 200", status)
}
}
func TestJSONResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "encoding/json")
fmt.Fprintln(w, `{ "message": "Hello, client" }`)
}))
defer ts.Close()
cli := NewClient(ts.URL)
var response struct {
Message string `json:"message"`
}
err := cli.NewRequest("GET", "/").
Into(&response).
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if response.Message != "Hello, client" {
t.Errorf("Failed reading JSON body: got %#v, expected %+q", response, "Hello, client")
}
}
func TestResponseHeader(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Custom-Header", "bla")
w.WriteHeader(http.StatusNoContent)
}))
defer ts.Close()
cli := NewClient(ts.URL)
var customHeader string
err := cli.NewRequest("GET", "/").
HeaderInto("Custom-Header", &customHeader).
ExpectedStatus(http.StatusNoContent).
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if customHeader != "bla" {
t.Errorf("Failed reading custom header: got %+q, expected %+q", customHeader, "bla")
}
}
func TestErrorResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("bad request"))
}))
defer ts.Close()
cli := NewClient(ts.URL)
var status int
err := cli.NewRequest("GET", "/").
StatusInto(&status).
ExpectedStatus(http.StatusBadRequest).
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if status != http.StatusBadRequest {
t.Errorf("Failed reading non-200 status code: got %d, expected %d", status, http.StatusBadRequest)
}
}
//func TestNoTLSVerify(t *testing.T) {
//cli := NewClient("")
//cli.NewRequest("GET", "/")
//transport := cli.httpCli.Transport.(*http.Transport)
//if transport.TLSClientConfig.InsecureSkipVerify {
//t.Fatal("Transport should verify TLS connections but isn't")
//}
//cli.NoTLSVerify(true).NewRequest("GET", "/")
//transport = cli.httpCli.Transport.(*http.Transport)
//if !transport.TLSClientConfig.InsecureSkipVerify {
//t.Fatal("Transport shouldn't verify TLS connections but is")
//}
//}
func TestResponseSizeLimit(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Lorem ipsum dolor sit amet")
}))
defer ts.Close()
var data string
cli := NewClient(ts.URL)
err := cli.NewRequest("GET", "/").SizeLimit(5).Into(&data).Run()
if err != ErrSizeExceeded {
t.Fatal("Response size was not limited as expected")
}
}
func TestRequestTimeout(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Lorem ipsum dolor sit amet")
}))
defer ts.Close()
err := NewClient(ts.URL).
NewRequest("GET", "/").
Timeout(1 * time.Second).
Run()
if err == nil {
t.Fatal("Response succeeded but should have timed out")
} else if err != ErrTimeoutReached {
t.Fatalf("Request failed but not with timeout error: %s", err)
}
}
func TestRequestRetries(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Lorem ipsum dolor sit amet")
}))
ts.Close() // this is on purpose
cli := NewClient(ts.URL)
start := time.Now()
err := cli.NewRequest("GET", "/").RetryLimit(2).Run()
end := time.Now()
if err == nil {
t.Fatal("Request did not fail as expected")
} else if end.Sub(start) < 6*time.Second {
t.Fatal("Request was not retried as expected")
}
}
func TestCustomBodyHandler(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x.rot13")
fmt.Fprintln(w, "Hello, World")
}))
defer ts.Close()
var data []byte
err := NewClient(ts.URL).
NewRequest("GET", "/").
BodyHandler(func(status int, cType string, reader io.Reader, target interface{}) error {
if cType != "application/x.rot13" {
return fmt.Errorf("unexpected content type %s returned", cType)
}
b, err := ioutil.ReadAll(reader)
if err != nil {
return fmt.Errorf("failed reading response body: %w", err)
}
t := target.(*[]byte)
*t = rot13(b[:len(b)-1]) // chomp newline
return nil
}).
Into(&data).
Run()
if err != nil {
t.Fatalf("Failed custom body handler: %s", err)
} else if string(data) != "Uryyb, Jbeyq" {
t.Fatalf("Body handler returned wrong content: %q", string(data))
}
}
func TestTLSConnection(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Lorem ipsum dolor sit amet")
}))
defer ts.Close()
certpem := `-----BEGIN CERTIFICATE-----
MIIC+TCCAeGgAwIBAgIRAJTH5D+Q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Cb/xck2EcKrRp3bxAOErv/1lTnE1R2a5noDafr3q
mNQduXYPqZ8SjNGPy2CBw5iVXl/QsW1YPqx6yxez7w8nVaKxhC+QnWoOq4D6FZ70
tSw0cLzkCNwFx4DYBmAMcQKBgB/ISi5p0qeD26g6szeanUwGQWdFcWR1G2sLsHkO
2Ij4HVx6bpMRPKJwGVxdI4UUWdEPd+rQoCyH6Rk4ySAFbSCJOamCvgj/r+th6iGw
ab//OTVvtgLNev6PhvVKYOFPW9KYZmgZtHokTK0G/HiBmR2leirAbQy3JjWiUzBS
8C9FAoGAfG4or+rOZYYcjZgpgORip97ugPzK0OPp1Z6wPF3Nhv3cxhU/DbcprNjM
aUz5HXBcNYdNTwmMhJeR+2PjQLwK8XKQyR/OXPwgpDhYdpI8jx4AV4VakekES2Mx
0soArFGOSvrfeB0pT7L6hzoCS806+qZPiHpag+h2iFnooNQWvC8=
-----END RSA PRIVATE KEY-----`
cli := NewClient(ts.URL).Accept("text/plain").SetTLS([]byte(certpem),
[]byte(keypem), []byte(certpem))
var text string
var status int
err := cli.NewRequest("GET", "/").
Into(&text).
StatusInto(&status).
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if text != "Lorem ipsum dolor sit amet" {
t.Errorf("Failed reading plain text body: got %+q, expected %+q", text, "Hello, client")
}
if status != 200 {
t.Errorf("Failed to read status: got %d, expected 200", status)
}
}
func rot13(b []byte) []byte {
for i, r := range b {
switch {
case r >= 'A' && r <= 'Z':
b[i] = 'A' + (((r - 'A') + 13) % 26)
case r >= 'a' && r <= 'z':
b[i] = 'a' + (((r - 'a') + 13) % 26)
default:
b[i] = r
}
}
return b
}
func TestCompressedRequest(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
cr, err := gzip.NewReader(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Failed creating gzip reader: %s\n", err)
return
}
defer cr.Close()
content, err := io.ReadAll(cr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Failed reading request body: %s\n", err)
return
}
fmt.Fprintln(w, string(content))
}))
defer ts.Close()
cli := NewClient(ts.URL).
Accept("text/plain").
CompressWith(CompressionAlgorithmGzip)
for i, input := range []string{"hello world", "bla", "AAABBb"} {
var output string
err := cli.NewRequest("GET", "/").
Into(&output).
Body([]byte(input), "text/plain").
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if output != input {
t.Errorf("Compression test %d: got %+q, expected %+q", i, output, input)
}
}
}
func TestBodySources(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
w.WriteHeader(http.StatusOK)
io.ReadAll(io.TeeReader(r.Body, w))
}))
defer ts.Close()
var testFS = afero.NewMemMapFs()
err := afero.WriteFile(testFS, "hello", []byte("Hello world"), 0655)
assert.MustBeNil(t, err, "must create file successfully")
cli := NewClient(ts.URL)
type bodySrcTest struct {
desc string
body interface{}
file string
contentType string
expErr error
expBody string
}
for _, test := range []bodySrcTest{
{
desc: "A []byte value",
body: []byte("Hello world"),
contentType: "text/plain",
expBody: "Hello world",
},
{
desc: "A string value",
body: "Hello world",
contentType: "text/plain",
expBody: "Hello world",
},
{
desc: "A simple io.Reader",
body: bytes.NewReader([]byte("Hello world")),
contentType: "text/plain",
expBody: "Hello world",
},
{
desc: "A simple io.ReadCloser",
body: io.NopCloser(bytes.NewReader([]byte("Hello world"))),
contentType: "text/plain",
expBody: "Hello world",
},
{
desc: "An unsupported value",
body: 3,
contentType: "text/plain",
expErr: ErrUnsupportedBody,
},
{
desc: "A file",
file: "hello",
contentType: "text/plain",
expBody: "Hello world",
},
{
desc: "A non-existent file",
file: "doesnt-exist",
contentType: "text/plain",
expErr: errors.New("file does not exist"),
},
} {
var resBody, resContentType string
req := cli.NewRequest("POST", "/").
Accept(test.contentType).
Into(&resBody).
HeaderInto("Content-Type", &resContentType)
if test.file != "" {
req.FileBody(afero.NewIOFS(testFS), test.file, "text/plain")
} else {
req.Body(test.body, test.contentType)
}
err := req.Run()
if test.expErr != nil {
assert.MustNotBeNil(t, err)
assert.MustBeTrue(
t,
errors.Is(
err,
test.expErr) ||
strings.Contains(err.Error(), test.expErr.Error()),
)
} else {
assert.MustBeNil(t, err)
assert.MustBeEqual(t, test.contentType, resContentType)
assert.MustBeEqual(t, test.expBody, resBody)
}
}
}
func TestMultipartBody(t *testing.T) {
type part struct {
Fieldname string `json:"fieldname"`
Filename string `json:"filename"`
Content string `json:"content"`
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(
w,
"Invalid content type %q: %s",
r.Header.Get("Content-Type"), err,
)
return
}
mr := multipart.NewReader(r.Body, params["boundary"])
var output []part
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Failed getting next part: %s", err)
return
}
b, err := io.ReadAll(p)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Failed reading part body: %s", err)
return
}
output = append(output, part{
Fieldname: p.FormName(),
Filename: p.FileName(),
Content: string(b),
})
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
}))
defer ts.Close()
var testFS = afero.NewMemMapFs()
err := afero.WriteFile(testFS, "hello", []byte("HELLO"), 0655)
assert.MustBeNil(t, err, "must create file successfully")
err = afero.WriteFile(testFS, "world", []byte("WORLD"), 0655)
assert.MustBeNil(t, err, "must create file successfully")
var res []part
iofs := afero.NewIOFS(testFS)
err = NewClient(ts.URL).NewRequest("POST", "/").
Into(&res).
MultipartBody(
MultipartPart("header", "title.txt", "Hello world"),
MultipartFile("body", iofs, "hello"),
MultipartFile("footer", iofs, "world"),
).
Run()
assert.MustBeNil(t, err)
assert.MustBeDeepEqual(t, []part{
{
Fieldname: "header",
Filename: "title.txt",
Content: "Hello world",
},
{
Fieldname: "body",
Filename: "hello",
Content: "HELLO",
},
{
Fieldname: "footer",
Filename: "world",
Content: "WORLD",
},
}, res)
}
func TestRequestBodyProcessor(t *testing.T) {
var body []byte
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
body, err = io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusNoContent)
}
}))
defer ts.Close()
fn := func(r io.Reader, w io.Writer) error {
fmt.Fprint(w, `{"items":`)
tee := io.TeeReader(r, w)
_, err := io.ReadAll(tee)
if err != nil {
return err
}
fmt.Fprint(w, `}`)
return nil
}
cli := NewClient(ts.URL)
// First run without the body processor
err := cli.NewRequest("POST", "/").
JSONBody([]string{"one", "two", "three"}).
ExpectedStatus(http.StatusNoContent).
Run()
assert.MustBeNil(t, err)
assert.MustBeEqual(t, `["one","two","three"]`, string(body))
// Now with the processor
err = cli.NewRequest("POST", "/").
ReqBodyProcessor(fn).
JSONBody([]string{"one", "two", "three"}).
ExpectedStatus(http.StatusNoContent).
Run()
assert.MustBeNil(t, err)
assert.MustBeEqual(t, `{"items":["one","two","three"]}`, string(body))
}