-
Notifications
You must be signed in to change notification settings - Fork 14
/
govcr_wb_test.go
364 lines (287 loc) · 12 KB
/
govcr_wb_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
package govcr
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/seborama/govcr/v15/cassette/track"
"github.com/seborama/govcr/v15/stats"
)
type GoVCRWBTestSuite struct {
suite.Suite
testServer *httptest.Server
}
func TestGoVCRWBTestSuite(t *testing.T) {
suite.Run(t, new(GoVCRWBTestSuite))
}
func (ts *GoVCRWBTestSuite) SetupTest() {
func() {
// note to the wiser: adding a trailer causes the content to be chunked and
// content-length will be -1 (i.e. unknown)
counter := 0
ts.testServer = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Trailer", "trailer_1")
w.Header().Set("header_1", "header_1_value")
w.WriteHeader(http.StatusOK)
counter++
iQuery := r.URL.Query().Get("i")
_, _ = fmt.Fprintf(w, "Hello, server responds '%d' to query '%s'", counter, iQuery)
w.Header().Set("trailer_1", "trailer_1_value")
}))
}()
testServerClient := ts.testServer.Client()
testServerClient.Timeout = 3 * time.Second
}
func (ts *GoVCRWBTestSuite) TearDownTest() {
ts.testServer.Close()
}
type action int
const (
actionKeepCassette = iota
actionDeleteCassette
)
func (ts *GoVCRWBTestSuite) newVCR(cassetteName string, a action) *ControlPanel {
if a == actionDeleteCassette {
_ = os.Remove(cassetteName)
}
testServerClient := ts.testServer.Client()
testServerClient.Timeout = 3 * time.Second
return NewVCR(
NewCassetteLoader(cassetteName),
WithClient(testServerClient),
// WithTrackRecordingMutators(trackMutator),
)
}
func (ts *GoVCRWBTestSuite) TestRoundTrip_RequestMatcherDoesNotMutateState() {
const cassetteName = "temp-fixtures/TestRoundTrip_RequestMatcherDoesNotMutateState.cassette.json"
requestMatcherCount := 0
reqMatchers := func(outcome bool) RequestMatchers {
return RequestMatchers{
// attempt to mutate state
func(httpRequest, trackRequest *track.Request) bool {
requestMatcherCount++
httpRequest.Method = "test"
httpRequest.URL = &url.URL{}
httpRequest.Body = nil
trackRequest.Method = "test"
trackRequest.URL = &url.URL{}
trackRequest.Body = nil
httpRequest = &track.Request{} //nolint:staticcheck
trackRequest = &track.Request{} //nolint:staticcheck
return outcome
},
}
}
// 1st call - live
vcr := ts.newVCR(cassetteName, actionDeleteCassette)
vcr.SetLiveOnlyMode() // ensure we record one track so we can have a request matcher execution later (no track on cassette = no request matching)
vcr.SetRequestMatchers(reqMatchers(false)...) // false: we want to attempt but not satisfy request matching so to check if the live request was altered
req, err := http.NewRequest(http.MethodGet, ts.testServer.URL, nil)
ts.Require().NoError(err)
preRoundTripReq := track.CloneHTTPRequest(req)
resp, err := vcr.HTTPClient().Do(req)
ts.Require().NoError(err)
defer func() { _ = resp.Body.Close() }()
expectedStats := &stats.Stats{
TotalTracks: 1,
TracksLoaded: 0,
TracksRecorded: 1,
TracksPlayed: 0,
}
ts.Require().EqualValues(expectedStats, vcr.Stats())
ts.Require().Equal(0, requestMatcherCount) // no track on cassette so no matching attempted by govcr
postRoundTripReq := track.CloneHTTPRequest(req)
ts.Require().EqualValues(preRoundTripReq, postRoundTripReq)
// for simplification, we're using our own track.Response
// we'll make the assumption that if that's well, the rest ought to be too.
vcrResp := track.ToResponse(resp)
ts.Assert().Equal("Hello, server responds '1' to query ''", string(vcrResp.Body))
vcrResp.Body = nil
// 2nd call - live
vcr = ts.newVCR(cassetteName, actionKeepCassette)
vcr.SetNormalMode() // ensure we attempt request matching
vcr.SetRequestMatchers(reqMatchers(false)...) // false: we want to attempt but not satisfy request matching so to check if the live request was altered
req, err = http.NewRequest(http.MethodGet, ts.testServer.URL, nil)
ts.Require().NoError(err)
resp2, err := vcr.HTTPClient().Do(req) //nolint: bodyclose
ts.Require().NoError(err)
defer func() { _ = resp.Body.Close() }()
expectedStats = &stats.Stats{
TotalTracks: 2,
TracksLoaded: 1,
TracksRecorded: 1,
TracksPlayed: 0,
}
ts.Require().EqualValues(expectedStats, vcr.Stats())
ts.Require().Equal(1, requestMatcherCount) // an attempt to match the request should be made (albeit unsuccessful)
postRoundTripReq = track.CloneHTTPRequest(req)
ts.Require().EqualValues(preRoundTripReq, postRoundTripReq)
vcrResp2 := track.ToResponse(resp2)
ts.Assert().Equal("Hello, server responds '2' to query ''", string(vcrResp2.Body))
vcrResp2.Body = nil
ts.Require().EqualValues(vcrResp, vcrResp2)
// 3rd call - replayed
vcr = ts.newVCR(cassetteName, actionKeepCassette)
vcr.SetOfflineMode()
requestMatcherCount = 0
vcr.SetRequestMatchers(reqMatchers(true)...) // true: satisfy request matching and force replay from track to ensure no mutation
req, err = http.NewRequest(http.MethodGet, ts.testServer.URL, nil)
ts.Require().NoError(err)
resp3, err := vcr.HTTPClient().Do(req)
ts.Require().NoError(err)
defer func() { _ = resp3.Body.Close() }()
expectedStats = &stats.Stats{
TotalTracks: 2,
TracksLoaded: 2,
TracksRecorded: 0,
TracksPlayed: 1,
}
ts.Require().EqualValues(expectedStats, vcr.Stats())
ts.Require().Equal(1, requestMatcherCount)
postRoundTripReq = track.CloneHTTPRequest(req)
ts.Require().EqualValues(preRoundTripReq, postRoundTripReq)
// for simplification, we're using our own track.Response
// we'll make the assumption that if that's well, the rest ought to be too.
vcrResp3 := track.ToResponse(resp3)
ts.Assert().Equal("Hello, server responds '1' to query ''", string(vcrResp3.Body))
vcrResp3.Body = nil
vcrResp.TLS, vcrResp3.TLS = nil, nil // TLS will not match fully
ts.Require().EqualValues(vcrResp, vcrResp3)
}
func (ts *GoVCRWBTestSuite) TestRoundTrip_WithRecordingAndReplayingMutations() {
const cassetteName = "temp-fixtures/TestRoundTrip_WithRecordingAndReplayingMutations.cassette.json"
// 1st execution of set of calls
vcr := ts.newVCR(cassetteName, actionDeleteCassette)
vcr.SetRecordingMutators(trackMutator)
ts.makeHTTPCalls_WithSuccess(vcr.HTTPClient())
expectedStats := &stats.Stats{
TotalTracks: 2,
TracksLoaded: 0,
TracksRecorded: 2,
TracksPlayed: 0,
}
ts.Require().EqualValues(expectedStats, vcr.Stats())
// load the cassette and verify contents has been mutated.
vcr = ts.newVCR(cassetteName, actionKeepCassette)
vcr.SetRecordingMutators(trackMutator)
// note: remember that it usually doesn't make sense to modify the request in the replaying track mutator
trackMutatorAgain := track.Mutator(
func(trk *track.Track) {
trk.Response.Header.Set("TrackRecordingMutatorHeader", "headers have been mutated AGAIN AT PLAYBACK")
})
vcr.AddReplayingMutators(trackMutatorAgain)
tracks := vcr.vcrTransport().cassette.Tracks
for n := range tracks {
ts.Require().EqualValues("this_query_key_has_been_mutated", tracks[n].Request.URL.Query().Get("mutated_query_key"), "track #%d", n)
ts.Require().EqualValues("headers have been mutated", tracks[n].Response.Header.Get("TrackRecordingMutatorHeader"), "track #%d", n)
}
// 2nd execution of set of calls (replayed)
ts.replayHTTPCalls_WithMutations_WithSuccess(vcr.HTTPClient(), "headers have been mutated AGAIN AT PLAYBACK")
expectedStats = &stats.Stats{
TotalTracks: 2,
TracksLoaded: 2,
TracksRecorded: 0,
TracksPlayed: 2,
}
ts.EqualValues(expectedStats, vcr.Stats())
}
func (ts *GoVCRWBTestSuite) TestRoundTrip_SavesAndReplaysMutatedTracksToCassette() {
const cassetteName = "temp-fixtures/TestRoundTrip_SavesAndReplaysMutatedTracksToCassette.cassette.json"
// 1st execution of set of calls
vcr := ts.newVCR(cassetteName, actionDeleteCassette)
vcr.SetRecordingMutators(trackMutator)
ts.makeHTTPCalls_WithSuccess(vcr.HTTPClient())
expectedStats := &stats.Stats{
TotalTracks: 2,
TracksLoaded: 0,
TracksRecorded: 2,
TracksPlayed: 0,
}
ts.Require().EqualValues(expectedStats, vcr.Stats())
// load the cassette and verify contents has been mutated.
vcr = ts.newVCR(cassetteName, actionKeepCassette)
vcr.SetRecordingMutators(trackMutator)
tracks := vcr.vcrTransport().cassette.Tracks
for n := range tracks {
ts.Require().EqualValues("this_query_key_has_been_mutated", tracks[n].Request.URL.Query().Get("mutated_query_key"), "track #%d", n)
ts.Require().EqualValues("headers have been mutated", tracks[n].Response.Header.Get("TrackRecordingMutatorHeader"), "track #%d", n)
}
// 2nd execution of set of calls (replayed)
ts.replayHTTPCalls_WithMutations_WithSuccess(vcr.HTTPClient(), "headers have been mutated")
expectedStats = &stats.Stats{
TotalTracks: 2,
TracksLoaded: 2,
TracksRecorded: 0,
TracksPlayed: 2,
}
ts.EqualValues(expectedStats, vcr.Stats())
}
func (ts *GoVCRWBTestSuite) makeHTTPCalls_WithSuccess(httpClient *http.Client) {
for i := 1; i <= 2; i++ {
req, err := http.NewRequest(http.MethodGet, ts.testServer.URL+fmt.Sprintf("?i=%d", i), nil)
ts.Require().NoError(err)
req.Header.Add("header", "value")
req.SetBasicAuth("not_a_username", "not_a_password")
resp, err := httpClient.Do(req)
ts.Require().NoError(err)
// read body first because the server is passing Trailers in http.Response.
bodyBytes, err := io.ReadAll(resp.Body)
ts.Require().NoError(err)
_ = resp.Body.Close()
ts.Assert().Equal(fmt.Sprintf("Hello, server responds '%d' to query '%d'", i, i), string(bodyBytes))
ts.Assert().Equal("200 OK", resp.Status)
ts.Assert().Equal(http.StatusOK, resp.StatusCode)
ts.Assert().EqualValues("", resp.Header.Get("Content-Length"))
ts.Assert().EqualValues(-1, resp.ContentLength)
ts.Assert().EqualValues("text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
ts.Assert().EqualValues("header_1_value", resp.Header.Get("header_1"))
ts.Require().EqualValues("", resp.Header.Get("TrackRecordingMutatorHeader")) // the header is injected, not present in the live traffic
ts.Assert().Len(resp.Trailer, 1)
ts.Assert().EqualValues("trailer_1_value", resp.Trailer.Get("trailer_1"))
ts.Assert().NotNil(resp.Request)
ts.Assert().NotNil(resp.TLS)
}
}
func (ts *GoVCRWBTestSuite) replayHTTPCalls_WithMutations_WithSuccess(httpClient *http.Client, trackRecordingMutatorHeaderValue string) {
for i := 1; i <= 2; i++ {
req, err := http.NewRequest(http.MethodGet, ts.testServer.URL+fmt.Sprintf("?i=%d", i), nil)
ts.Require().NoError(err)
// update our request in line with the previous recording mutations that took place.
// not doing so would prevent matching our request against the (mutated) cassette.
q := req.URL.Query()
q.Set("mutated_query_key", "this_query_key_has_been_mutated")
req.URL.RawQuery = q.Encode()
req.Header.Add("header", "value")
req.SetBasicAuth("not_a_username", "not_a_password")
resp, err := httpClient.Do(req)
ts.Require().NoError(err)
ts.Require().Equal("200 OK", resp.Status)
ts.Require().Equal(http.StatusOK, resp.StatusCode)
ts.Assert().EqualValues("", resp.Header.Get("Content-Length"))
ts.Assert().EqualValues(-1, resp.ContentLength)
ts.Assert().EqualValues("text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
ts.Assert().EqualValues("header_1_value", resp.Header.Get("header_1"))
ts.Require().EqualValues(trackRecordingMutatorHeaderValue, resp.Header.Get("TrackRecordingMutatorHeader"))
ts.Assert().Len(resp.Trailer, 1)
ts.Assert().EqualValues("trailer_1_value", resp.Trailer.Get("trailer_1"))
bodyBytes, err := io.ReadAll(resp.Body)
ts.Require().NoError(err)
_ = resp.Body.Close()
ts.Require().Equal(fmt.Sprintf("Hello, server responds '%d' to query '%d'", i, i), string(bodyBytes))
ts.Require().NotNil(resp.Request)
ts.Require().NotNil(resp.TLS)
}
}
var trackMutator = track.Mutator(
func(trk *track.Track) {
q := trk.Request.URL.Query()
q.Set("mutated_query_key", "this_query_key_has_been_mutated")
trk.Request.URL.RawQuery = q.Encode()
trk.Response.Header.Add("TrackRecordingMutatorHeader", "headers have been mutated")
trk.Response.Header.Del("Date") // to avoid matching issues
})