-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_test.go
More file actions
646 lines (551 loc) · 18.6 KB
/
Copy pathrequest_test.go
File metadata and controls
646 lines (551 loc) · 18.6 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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
package rhttp_test
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/oswaldom-code/rhttp"
)
func TestRequestBuilder_Get(t *testing.T) {
var capturedReq *http.Request
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
resp, err := c.R().Get("http://example.com/api")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
if capturedReq.Method != http.MethodGet {
t.Errorf("expected GET, got %s", capturedReq.Method)
}
if capturedReq.URL.String() != "http://example.com/api" {
t.Errorf("expected http://example.com/api, got %s", capturedReq.URL.String())
}
}
func TestRequestBuilder_Post(t *testing.T) {
var capturedReq *http.Request
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: http.StatusCreated, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
resp, err := c.R().
SetBodyString("test body").
Post("http://example.com/api")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp.StatusCode != http.StatusCreated {
t.Fatalf("expected 201, got %d", resp.StatusCode)
}
if capturedReq.Method != http.MethodPost {
t.Errorf("expected POST, got %s", capturedReq.Method)
}
}
func TestRequestBuilder_Headers(t *testing.T) {
var capturedReq *http.Request
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, _ = c.R().
SetHeader("X-Custom", "value1").
SetHeaders(map[string]string{
"X-Another": "value2",
"X-Third": "value3",
}).
AddHeader("X-Multi", "a").
AddHeader("X-Multi", "b").
SetContentType("application/json").
SetAccept("application/json").
SetUserAgent("test-agent").
Get("http://example.com")
if capturedReq.Header.Get("X-Custom") != "value1" {
t.Errorf("expected X-Custom=value1, got %s", capturedReq.Header.Get("X-Custom"))
}
if capturedReq.Header.Get("X-Another") != "value2" {
t.Errorf("expected X-Another=value2, got %s", capturedReq.Header.Get("X-Another"))
}
if capturedReq.Header.Get("Content-Type") != "application/json" {
t.Errorf("expected Content-Type=application/json, got %s", capturedReq.Header.Get("Content-Type"))
}
if capturedReq.Header.Get("Accept") != "application/json" {
t.Errorf("expected Accept=application/json, got %s", capturedReq.Header.Get("Accept"))
}
if capturedReq.Header.Get("User-Agent") != "test-agent" {
t.Errorf("expected User-Agent=test-agent, got %s", capturedReq.Header.Get("User-Agent"))
}
multiVals := capturedReq.Header.Values("X-Multi")
if len(multiVals) != 2 || multiVals[0] != "a" || multiVals[1] != "b" {
t.Errorf("expected X-Multi=[a,b], got %v", multiVals)
}
}
func TestRequestBuilder_QueryParams(t *testing.T) {
var capturedReq *http.Request
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, _ = c.R().
SetQueryParam("page", "1").
SetQueryParams(map[string]string{
"limit": "10",
"sort": "desc",
}).
AddQueryParam("filter", "active").
AddQueryParam("filter", "verified").
Get("http://example.com/users")
query := capturedReq.URL.Query()
if query.Get("page") != "1" {
t.Errorf("expected page=1, got %s", query.Get("page"))
}
if query.Get("limit") != "10" {
t.Errorf("expected limit=10, got %s", query.Get("limit"))
}
if query.Get("sort") != "desc" {
t.Errorf("expected sort=desc, got %s", query.Get("sort"))
}
filters := query["filter"]
if len(filters) != 2 {
t.Errorf("expected 2 filter values, got %d", len(filters))
}
}
func TestRequestBuilder_PathParams(t *testing.T) {
var capturedReq *http.Request
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, _ = c.R().
SetPathParam("org", "acme").
SetPathParams(map[string]string{
"repo": "api",
"id": "123",
}).
Get("http://example.com/{org}/{repo}/issues/{id}")
expected := "http://example.com/acme/api/issues/123"
if capturedReq.URL.String() != expected {
t.Errorf("expected %s, got %s", expected, capturedReq.URL.String())
}
}
func TestRequestBuilder_SetBodyJSON(t *testing.T) {
var capturedReq *http.Request
var capturedBody []byte
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
capturedBody, _ = io.ReadAll(req.Body)
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
payload := map[string]string{"name": "test", "value": "123"}
_, _ = c.R().
SetBodyJSON(payload).
Post("http://example.com/api")
if capturedReq.Header.Get("Content-Type") != "application/json" {
t.Errorf("expected Content-Type=application/json, got %s", capturedReq.Header.Get("Content-Type"))
}
var decoded map[string]string
if err := json.Unmarshal(capturedBody, &decoded); err != nil {
t.Fatalf("failed to decode JSON body: %v", err)
}
if decoded["name"] != "test" || decoded["value"] != "123" {
t.Errorf("unexpected body: %v", decoded)
}
}
func TestRequestBuilder_SetBodyForm(t *testing.T) {
var capturedReq *http.Request
var capturedBody string
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
body, _ := io.ReadAll(req.Body)
capturedBody = string(body)
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, _ = c.R().
SetBodyForm(map[string]string{
"username": "test",
"password": "secret",
}).
Post("http://example.com/login")
if capturedReq.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Errorf("expected Content-Type=application/x-www-form-urlencoded, got %s", capturedReq.Header.Get("Content-Type"))
}
if !strings.Contains(capturedBody, "username=test") {
t.Errorf("expected body to contain username=test, got %s", capturedBody)
}
if !strings.Contains(capturedBody, "password=secret") {
t.Errorf("expected body to contain password=secret, got %s", capturedBody)
}
}
func TestRequestBuilder_SetAuthToken(t *testing.T) {
var capturedReq *http.Request
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, _ = c.R().
SetAuthToken("my-token-123").
Get("http://example.com/api")
expected := "Bearer my-token-123"
if capturedReq.Header.Get("Authorization") != expected {
t.Errorf("expected Authorization=%s, got %s", expected, capturedReq.Header.Get("Authorization"))
}
}
func TestRequestBuilder_SetBasicAuth(t *testing.T) {
var capturedReq *http.Request
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, _ = c.R().
SetBasicAuth("user", "pass").
Get("http://example.com/api")
auth := capturedReq.Header.Get("Authorization")
want := "Basic " + base64.StdEncoding.EncodeToString([]byte("user:pass"))
if auth != want {
t.Errorf("expected Authorization %q, got %q", want, auth)
}
}
func TestRequestBuilder_Timeout(t *testing.T) {
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
// Respect context cancellation
select {
case <-req.Context().Done():
return nil, req.Context().Err()
case <-time.After(200 * time.Millisecond):
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
}
})
c := rhttp.New(rhttp.WithTransport(rt))
_, err := c.R().
SetTimeout(50 * time.Millisecond).
Get("http://example.com/api")
if err == nil {
t.Fatal("expected timeout error")
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("expected context.DeadlineExceeded, got %v", err)
}
}
func TestRequestBuilder_SetTimeoutBodyReadableAfterReturn(t *testing.T) {
const head, tail = "first-chunk-", "second-chunk"
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fl, ok := w.(http.Flusher)
if !ok {
t.Error("ResponseWriter is not a Flusher")
return
}
_, _ = io.WriteString(w, head)
fl.Flush()
time.Sleep(50 * time.Millisecond)
_, _ = io.WriteString(w, tail)
}))
defer srv.Close()
c := rhttp.New()
resp, err := c.R().
SetTimeout(5 * time.Second).
Get(srv.URL)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("reading body after builder execute returned: %v", err)
}
if string(body) != head+tail {
t.Fatalf("expected body %q, got %q", head+tail, body)
}
}
func TestRequestBuilder_Context(t *testing.T) {
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
// Respect context cancellation
select {
case <-req.Context().Done():
return nil, req.Context().Err()
case <-time.After(200 * time.Millisecond):
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
}
})
c := rhttp.New(rhttp.WithTransport(rt))
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
_, err := c.R().
Context(ctx).
Get("http://example.com/api")
if err == nil {
t.Fatal("expected context timeout error")
}
}
func TestRequestBuilder_AllMethods(t *testing.T) {
methods := []struct {
name string
fn func(*rhttp.RequestBuilder, string) (*http.Response, error)
expect string
}{
{"Get", func(rb *rhttp.RequestBuilder, url string) (*http.Response, error) { return rb.Get(url) }, "GET"},
{"Post", func(rb *rhttp.RequestBuilder, url string) (*http.Response, error) { return rb.Post(url) }, "POST"},
{"Put", func(rb *rhttp.RequestBuilder, url string) (*http.Response, error) { return rb.Put(url) }, "PUT"},
{"Patch", func(rb *rhttp.RequestBuilder, url string) (*http.Response, error) { return rb.Patch(url) }, "PATCH"},
{"Delete", func(rb *rhttp.RequestBuilder, url string) (*http.Response, error) { return rb.Delete(url) }, "DELETE"},
{"Head", func(rb *rhttp.RequestBuilder, url string) (*http.Response, error) { return rb.Head(url) }, "HEAD"},
{"Options", func(rb *rhttp.RequestBuilder, url string) (*http.Response, error) { return rb.Options(url) }, "OPTIONS"},
}
for _, m := range methods {
t.Run(m.name, func(t *testing.T) {
var capturedMethod string
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedMethod = req.Method
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, _ = m.fn(c.R(), "http://example.com")
if capturedMethod != m.expect {
t.Errorf("expected %s, got %s", m.expect, capturedMethod)
}
})
}
}
type opaqueReader struct{ r io.Reader }
func (o *opaqueReader) Read(p []byte) (int, error) { return o.r.Read(p) }
func TestRequestBuilder_ReaderBodyIsRetryable(t *testing.T) {
attempts := 0
var bodies []string
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
attempts++
b, _ := io.ReadAll(req.Body)
bodies = append(bodies, string(b))
return &http.Response{StatusCode: http.StatusServiceUnavailable, Body: http.NoBody}, nil
})
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.Retry(rhttp.RetryConfig{
MaxAttempts: 3,
RetryAllMethods: true,
Backoff: rhttp.ConstantBackoff(0),
})),
)
_, _ = c.R().
SetBody(&opaqueReader{r: strings.NewReader("payload")}).
Post("http://example.com")
if attempts != 3 {
t.Fatalf("opaque reader body disabled retries: got %d attempts, want 3", attempts)
}
for i, b := range bodies {
if b != "payload" {
t.Errorf("attempt %d body = %q, want %q", i+1, b, "payload")
}
}
}
func BenchmarkRequestBuilder_Simple(b *testing.B) {
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = c.R().Get("http://example.com")
}
}
func BenchmarkRequestBuilder_WithOptions(b *testing.B) {
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = c.R().
SetHeader("Authorization", "Bearer token").
SetQueryParam("page", "1").
SetPathParam("id", "123").
Get("http://example.com/users/{id}")
}
}
func TestRequestBuilder_SecondExecuteResendsFullBody(t *testing.T) {
var bodies []string
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
data, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
bodies = append(bodies, string(data))
return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
rb := c.R().SetBodyBytes([]byte("payload"))
for i := 0; i < 2; i++ {
resp, err := rb.Post("http://example.com")
if err != nil {
t.Fatalf("execute %d: unexpected error: %v", i, err)
}
resp.Body.Close()
}
if len(bodies) != 2 || bodies[0] != "payload" || bodies[1] != "payload" {
t.Fatalf("expected both executions to send the full body, got %q", bodies)
}
}
func TestRequestBuilder_LargeBodyStreamsWithoutRetry(t *testing.T) {
// One byte over the 10 MB buffering limit forces the streaming path.
const size = 10<<20 + 1
var attempts int32
var received int64
var sawGetBody bool
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&attempts, 1)
sawGetBody = req.GetBody != nil
n, err := io.Copy(io.Discard, req.Body)
if err != nil {
return nil, err
}
atomic.StoreInt64(&received, n)
return &http.Response{StatusCode: http.StatusServiceUnavailable, Body: http.NoBody, Request: req}, nil
})
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.Retry(rhttp.RetryConfig{
MaxAttempts: 3,
Backoff: rhttp.ConstantBackoff(time.Millisecond),
})),
)
opaque := &nonReplayableReader{r: bytes.NewReader(make([]byte, size))}
resp, err := c.R().
SetBody(opaque).
Put("http://example.com/upload")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
resp.Body.Close()
if sawGetBody {
t.Error("expected GetBody to be nil on the streaming path")
}
if received != size {
t.Errorf("expected the transport to receive %d bytes, got %d", size, received)
}
if got := atomic.LoadInt32(&attempts); got != 1 {
t.Errorf("expected a single attempt for a non-replayable streamed body, got %d", got)
}
}
func TestRequestBuilder_MalformedURL(t *testing.T) {
var calls int32
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&calls, 1)
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, err := c.R().Get("http://exa mple.com/api")
if err == nil {
t.Fatal("expected error for malformed URL")
}
if calls != 0 {
t.Errorf("expected the transport to never run, got %d calls", calls)
}
}
func TestRequestBuilder_SetBodyJSONMarshalError(t *testing.T) {
var calls int32
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&calls, 1)
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, err := c.R().
SetBodyJSON(make(chan int)).
Post("http://example.com")
if err == nil {
t.Fatal("expected marshal error for unsupported JSON type")
}
if calls != 0 {
t.Errorf("expected the transport to never run, got %d calls", calls)
}
}
func TestRequestBuilder_SetBodyXML(t *testing.T) {
var capturedReq *http.Request
var capturedBody string
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
body, _ := io.ReadAll(req.Body)
capturedBody = string(body)
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
type User struct {
Name string `xml:"name"`
}
_, err := c.R().
SetBodyXML(User{Name: "John"}).
Post("http://example.com")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got := capturedReq.Header.Get("Content-Type"); got != "application/xml" {
t.Errorf("expected Content-Type=application/xml, got %s", got)
}
if !strings.Contains(capturedBody, "<name>John</name>") {
t.Errorf("unexpected XML body: %s", capturedBody)
}
}
func TestRequestBuilder_SetBodyXMLMarshalError(t *testing.T) {
c := rhttp.New(rhttp.WithTransport(rhttp.RoundTripperFunc(
func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})))
_, err := c.R().
SetBodyXML(map[string]string{"k": "v"}).
Post("http://example.com")
if err == nil {
t.Fatal("expected marshal error: xml does not support maps")
}
}
func TestRequestBuilder_ExecuteCustomMethod(t *testing.T) {
var capturedReq *http.Request
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, err := c.R().Execute("TRACE", "http://example.com/api")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if capturedReq.Method != "TRACE" {
t.Errorf("expected method TRACE, got %s", capturedReq.Method)
}
}
func TestRequestBuilder_PathParamIsEscaped(t *testing.T) {
var capturedReq *http.Request
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
capturedReq = req
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(rhttp.WithTransport(rt))
_, err := c.R().
SetPathParam("id", "a/b c").
Get("http://example.com/items/{id}")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := "http://example.com/items/a%2Fb%20c"
if got := capturedReq.URL.String(); got != want {
t.Errorf("expected escaped path param URL %s, got %s", want, got)
}
}