-
Notifications
You must be signed in to change notification settings - Fork 2
/
recorder_test.go
593 lines (499 loc) · 15 KB
/
recorder_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
591
592
593
package recorder_test
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"path"
"strings"
"testing"
"github.com/akupila/recorder"
"github.com/google/go-cmp/cmp"
)
func TestMain(m *testing.M) {
code := m.Run()
if err := os.RemoveAll("testdata"); err != nil {
log.Fatalf("Clean up testdata: %v", err)
}
os.Exit(code)
}
func Example() {
// Create a new recorder.
// Data will be saved in testdata/example.yml
rec := recorder.New("testdata/example")
// Create HTTP client with recorder transport
cli := &http.Client{
Transport: rec,
}
// Perform a request
resp, err := cli.Get("https://jsonplaceholder.typicode.com/posts/1")
if err != nil {
log.Fatal(err)
}
// Response is only done if required
b, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
func ExampleFilter_custom() {
rec := recorder.New("testdata/request-header", func(e *recorder.Entry) {
// Modify e.Request and e.Response
})
cli := &http.Client{
Transport: rec,
}
_, err := cli.Get("https://example.com")
if err != nil {
log.Fatal(err)
}
}
func ExampleRemoveRequestHeader() {
rec := recorder.New("testdata/request-header", recorder.RemoveRequestHeader("Authorization"))
cli := &http.Client{
Transport: rec,
}
req, _ := http.NewRequest("https://example.com", "application/json", strings.NewReader("{}"))
req.Header.Add("Authorization", "abcdef")
_, err := cli.Do(req)
if err != nil {
log.Fatal(err)
}
// Authorization header is not saved to disk
}
func ExampleRemoveResponseHeader() {
rec := recorder.New("testdata/secret", recorder.RemoveResponseHeader("Set-Cookie"))
cli := &http.Client{
Transport: rec,
}
_, err := cli.Get("https://example.com")
if err != nil {
log.Fatal(err)
}
// The saved file will not contain the Set-Cookie header that was set by the server.
}
func ExampleNoRequestError() {
rec := recorder.New("notfound")
// Disallow network traffic so this returns an error.
rec.Mode = recorder.ReplayOnly
cli := &http.Client{Transport: rec}
if _, err := cli.Get("https://example.com"); err != nil {
uerr, ok := err.(*url.Error)
if !ok {
log.Fatal("Error is not *url.Error")
}
_, ok = uerr.Err.(recorder.NoRequestError)
if ok {
// Recorded entry was not found.
}
}
}
func TestRoundTrip_Default_replay(t *testing.T) {
requests := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests++
w.WriteHeader(200)
}))
defer ts.Close()
rec := recorder.New("testdata/roundtrip-auto")
cli := &http.Client{Transport: rec}
for i := 0; i < 5; i++ {
_, err := cli.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
}
if requests != 1 {
t.Errorf("Got %d outgoing requests, want %d", requests, 1)
}
}
func TestRoundTrip_RequestBody(t *testing.T) {
body := []byte(`{"hello": "world"}`)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Got method %s, want %s", r.Method, http.MethodPost)
}
if r.Header.Get("Content-Type") != "application/json" {
t.Errorf("Got content-type %s, want %s", r.Header.Get("Content-Type"), "application/json")
}
gotBody, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
return
}
if !bytes.Equal(gotBody, body) {
t.Errorf("Body does not match\nGot %s\nWant %s", gotBody, body)
}
w.WriteHeader(200)
}))
defer ts.Close()
rec := recorder.New("testdata/roundtrip-post")
cli := &http.Client{Transport: rec}
_, err := cli.Post(ts.URL, "application/json", bytes.NewReader(body))
if err != nil {
log.Fatal(err)
}
got, ok := rec.Lookup(http.MethodPost, ts.URL)
if !ok {
t.Fatalf("Entry was not recorded")
}
if got.Request.Body != string(body) {
t.Errorf("Request body does not match\nGot %s\nWant %s", got.Request.Body, string(body))
}
}
func TestRoundTrip_ResponseBody(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("Got method %s, want %s", r.Method, http.MethodGet)
}
w.Write([]byte("hello")) // nolint: errcheck
}))
defer ts.Close()
rec := recorder.New("testdata/roundtrip-get")
cli := &http.Client{Transport: rec}
resp, err := cli.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Read body: %v", err)
}
wantBody := []byte("hello")
if !bytes.Equal(body, wantBody) {
t.Errorf("Returned body does not match\nGot %s\nWant %s", body, wantBody)
}
}
func TestRoundTrip_ReplayOnly(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Error("Request was sent to server")
}))
defer ts.Close()
rec := recorder.New("testdata/roundtrip-replay-only")
rec.Mode = recorder.ReplayOnly
cli := &http.Client{Transport: rec}
_, err := cli.Get(ts.URL)
if err != nil {
uerr, ok := err.(*url.Error)
if !ok {
t.Fatalf("Returned error is %T, not *url.Error", err)
}
_, ok = uerr.Err.(recorder.NoRequestError)
if !ok {
t.Errorf("Got error %T %v, want %T", err, err, recorder.NoRequestError{})
}
}
}
func TestRoundTrip_Record(t *testing.T) {
requests := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests++
w.WriteHeader(200)
}))
defer ts.Close()
rec := recorder.New("testdata/roundtrip-record")
rec.Mode = recorder.Record
cli := &http.Client{Transport: rec}
n := 3
for i := 0; i < n; i++ {
_, err := cli.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
}
if requests != n {
t.Errorf("Got %d outgoing requests, want %d", requests, n)
}
}
func TestRoundTrip_Passthrough(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello")) // nolint: errcheck
}))
defer ts.Close()
rec := recorder.New("testdata/passthrough")
rec.Mode = recorder.Passthrough
cli := &http.Client{Transport: rec}
_, err := cli.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
_, ok := rec.Lookup(http.MethodGet, ts.URL)
if !ok {
t.Fatalf("Entry was not recorded")
}
// Nothing should be saved
_, err = os.Open("testdata/passthrough")
if !os.IsNotExist(err) {
t.Errorf("Data was recorded to disk")
}
}
func TestRoundTrip_Data(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Set-Cookie", "hello")
w.Write([]byte("hello")) // nolint: errcheck
}))
defer ts.Close()
rec := recorder.New("testdata/data")
cli := &http.Client{Transport: rec}
req, err := http.NewRequest(http.MethodPost, ts.URL, strings.NewReader(`{"hello": "world"}`))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Authorization", "abc")
resp, err := cli.Do(req)
if err != nil {
log.Fatal(err)
}
want := recorder.Entry{
Request: &recorder.Request{
Method: http.MethodPost,
URL: ts.URL,
Headers: map[string]string{
"Authorization": "abc",
},
Body: `{"hello": "world"}`,
},
Response: &recorder.Response{
StatusCode: 200,
Headers: map[string]string{
"Content-Length": "5",
"Set-Cookie": "hello",
"Content-Type": "text/plain; charset=utf-8", // Added by
"Date": "Tue, 30 Apr 2019 11:09:11 GMT", // go stdlib
},
Body: "hello",
},
}
// Check response
if resp.StatusCode != want.Response.StatusCode {
t.Errorf("Response status = %d, want = %d", resp.StatusCode, want.Response.StatusCode)
}
gotContent := resp.Header.Get("Content-Type")
wantContent := want.Response.Headers["Content-Type"]
if gotContent != wantContent {
t.Errorf("Response content-type header does not match\nGot %q\nWant %q", gotContent, wantContent)
}
gotBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Read body: %v", err)
}
if !bytes.Equal(gotBody, []byte(want.Response.Body)) {
t.Errorf("Response body does not match\nGot %s\nWant %s", string(gotBody), want.Response.Body)
}
// Check recorded entry
got, ok := rec.Lookup(http.MethodPost, ts.URL)
if !ok {
t.Fatalf("Entry was not recorded")
}
opts := []cmp.Option{
cmp.FilterPath(func(p cmp.Path) bool {
return p.String() == "Response.Headers"
}, cmp.Comparer(func(a, b map[string]string) bool {
return len(a) == len(b)
})),
}
if diff := cmp.Diff(got, want, opts...); diff != "" {
t.Errorf("Returned entry does not match (-got, +want)\n%s", diff)
}
// Nothing should be saved
_, err = os.Open("testdata/passthrough")
if !os.IsNotExist(err) {
t.Errorf("Data was recorded to disk")
}
}
func TestRemoveRequestHeader(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(202)
}))
defer ts.Close()
rec := recorder.New("testdata/req-header.yml", recorder.RemoveRequestHeader("Authorization"))
cli := &http.Client{Transport: rec}
req, _ := http.NewRequest(http.MethodPost, ts.URL, strings.NewReader(`{"hello": "world"}`))
req.Header.Add("Authorization", "abc")
_, err := cli.Do(req)
if err != nil {
log.Fatal(err)
}
saved, err := ioutil.ReadFile("testdata/req-header.yml")
if err != nil {
t.Fatal(err)
}
if bytes.Contains(saved, []byte("Authorization")) {
t.Errorf("Saved file contains auth header\n\n%s", string(saved))
}
}
func TestRemoveResponseHeader(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Set-Cookie", "hello")
}))
defer ts.Close()
rec := recorder.New("testdata/req-header.yml", recorder.RemoveResponseHeader("Set-Cookie"))
cli := &http.Client{Transport: rec}
_, err := cli.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
saved, err := ioutil.ReadFile("testdata/req-header.yml")
if err != nil {
t.Fatal(err)
}
if bytes.Contains(saved, []byte("Set-Cookie")) {
t.Errorf("Saved file contains cookie header\n\n%s", string(saved))
}
}
func TestFilterResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("oh, hello there!")) // nolint: errcheck
}))
defer ts.Close()
rec := recorder.New("testdata/req-response-filter", func(e *recorder.Entry) {
e.Response.Body = strings.Replace(e.Response.Body, "hello", "hi", -1)
})
cli := &http.Client{Transport: rec}
resp, err := cli.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
wantBody := "oh, hi there!"
if !bytes.Equal(body, []byte(wantBody)) {
t.Errorf("Returned body does not match\nGot %q\nWant %q", string(body), wantBody)
}
}
type SelectorFunc func(entries []recorder.Entry, req *http.Request) (recorder.Entry, bool)
func (f SelectorFunc) Select(entries []recorder.Entry, req *http.Request) (recorder.Entry, bool) {
return f(entries, req)
}
func TestSelect(t *testing.T) {
// This test verifies that the Select function is being used if provided. It
// sets up a recorder and test server and records 5 different calls to the
// server.
//
// It then switches to playback mode and configures a selector that will
// always choose the first entry, even if it doesn't otherwise match the
// incoming request. The test then issues the same 5 different calls to the
// recorder and verifies that it gets back the initial call repeated over and
// over.
serverCalls := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
serverCalls++
fmt.Fprintf(w, "call %d", serverCalls)
}))
defer ts.Close()
rec := recorder.New("testdata/select")
rec.Mode = recorder.Record
cli := &http.Client{Transport: rec}
for i := 1; i <= 5; i++ {
_, _ = cli.Get(fmt.Sprintf("%s/load/%d", ts.URL, i))
}
if serverCalls != 5 {
t.Fatalf("Expect to have made 5 calls to the server, but only made %d", serverCalls)
}
// Now switch to replay only, with our custom selector.
rec.Mode = recorder.ReplayOnly
selectCalls := 0
rec.Selector = SelectorFunc(func(entries []recorder.Entry, req *http.Request) (recorder.Entry, bool) {
selectCalls++
expectedPath := fmt.Sprintf("/load/%d", selectCalls)
if req.URL.Path != expectedPath {
t.Errorf("Selected select call %d to be requesting path %q but got %q",
selectCalls, expectedPath, req.URL.Path)
}
return entries[0], true
})
for i := 1; i <= 5; i++ {
resp, err := cli.Get(fmt.Sprintf("%s/load/%d", ts.URL, i))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
payload, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
// Our select always chooses the first call, so even though we're loading
// different URLs, we should always get back the recorded value for call 1.
if string(payload) != "call 1" {
t.Errorf("Expected replayed request to respond with `call 1` but got %#q", payload)
}
}
if selectCalls != 5 {
t.Errorf("Expected 5 select calls, but got %d", selectCalls)
}
}
func TestOncePerCall(t *testing.T) {
entries := []recorder.Entry{
{
Request: &recorder.Request{Method: "GET", URL: "http://foo.com/bar"},
Response: &recorder.Response{Body: "1"},
},
{
Request: &recorder.Request{Method: "GET", URL: "http://foo.com/bar"},
Response: &recorder.Response{Body: "2"},
},
{
Request: &recorder.Request{Method: "GET", URL: "http://foo.com/bar"},
Response: &recorder.Response{Body: "3"},
},
{
Request: &recorder.Request{Method: "GET", URL: "http://foo.com/baz"},
Response: &recorder.Response{Body: "baz!"},
},
{
Request: &recorder.Request{Method: "PUT", URL: "http://foo.com/baz"},
Response: &recorder.Response{Body: "putbaz!"},
},
}
testcases := []struct {
Method, URL, ExpectedBody string
}{
{"GET", "http://foo.com/bar", "1"},
{"GET", "http://foo.com/bar", "2"},
{"GET", "http://foo.com/bar", "3"},
{"GET", "http://foo.com/bar", ""}, // no matching request
{"GET", "http://foo.com/baz", "baz!"},
{"GET", "http://foo.com/baz", ""}, // no matching request
{"PUT", "http://foo.com/baz", "putbaz!"},
{"PUT", "http://foo.com/baz", ""}, // no matching request
}
var sel recorder.OncePerCall
for _, test := range testcases {
e, ok := sel.Select(entries, httptest.NewRequest(test.Method, test.URL, nil))
if test.ExpectedBody == "" { // nolint: gocritic
if ok {
t.Errorf("Expected no matching entry, but got %v", e)
}
} else if !ok {
t.Errorf("Expected a matching entry, but didn't get one")
} else if e.Response.Body != test.ExpectedBody {
t.Errorf("Entry mismatch. Expected body %q, but got %q",
test.ExpectedBody, e.Response.Body)
}
}
}
func TestNewWithExistingEmptyFileDoesntCrashIssue2(t *testing.T) {
const emptyFile = "testdata/empty.yml"
if err := os.MkdirAll(path.Dir(emptyFile), 0755); err != nil {
t.Fatal("mkdir:", err)
}
if f, err := os.OpenFile(emptyFile, os.O_CREATE|os.O_TRUNC, 0644); err != nil {
t.Fatal("creating the empty file:", err)
} else {
f.Close()
}
rec := recorder.New(emptyFile)
client := &http.Client{Transport: rec}
_, err := client.Get("https://jsonplaceholder.typicode.com/posts/1")
if err != nil {
t.Fatal("get:", err)
}
}