-
Notifications
You must be signed in to change notification settings - Fork 15
/
matchers_test.go
345 lines (332 loc) · 8.45 KB
/
matchers_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
package govcr_test
import (
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/seborama/govcr/v15"
"github.com/seborama/govcr/v15/cassette/track"
)
func Test_DefaultHeaderMatcher(t *testing.T) {
tt := []*struct {
name string
reqHeaders http.Header
trackHeaders http.Header
want bool
}{
{
name: "matches nil headers",
reqHeaders: nil,
trackHeaders: nil,
want: true,
},
{
name: "matches nil request header with empty track header",
reqHeaders: nil,
trackHeaders: http.Header{},
want: true,
},
{
name: "matches empty request header with nil track header",
reqHeaders: http.Header{},
trackHeaders: nil,
want: true,
},
{
name: "does not match nil request header with non-empty track header",
reqHeaders: nil,
trackHeaders: http.Header{"header": {"value"}},
want: false,
},
{
name: "does not match non-empty request header with nil track header",
reqHeaders: http.Header{"header": {"value"}},
trackHeaders: nil,
want: false,
},
{
name: "matches two complex unordered equivalent non-empty headers",
reqHeaders: http.Header{"header1": {"value1"}, "header2": {"value2b", "value2a"}},
trackHeaders: http.Header{"header2": {"value2a", "value2b"}, "header1": {"value1"}},
want: true,
},
{
name: "does not match two non-identical non-empty headers",
reqHeaders: http.Header{"header": {"value"}},
trackHeaders: http.Header{"other": {"something"}},
want: false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
httpReq := track.Request{Header: tc.reqHeaders}
trackReq := track.Request{Header: tc.trackHeaders}
actualMatch := govcr.DefaultHeaderMatcher(&httpReq, &trackReq)
assert.Equal(t, tc.want, actualMatch)
})
}
}
func Test_DefaultMethodMatcher(t *testing.T) {
tt := []*struct {
name string
reqMethod string
trackMethod string
want bool
}{
{
name: "matches nil methods",
reqMethod: string([]byte(nil)),
trackMethod: string([]byte(nil)),
want: true,
},
{
name: "matches nil request method with empty track method",
reqMethod: string([]byte(nil)),
trackMethod: "",
want: true,
},
{
name: "matches empty request method with nil track method",
reqMethod: "",
trackMethod: string([]byte(nil)),
want: true,
},
{
name: "does not match nil request method with non-empty track method",
reqMethod: string([]byte(nil)),
trackMethod: http.MethodGet,
want: false,
},
{
name: "does not match non-empty request method with nil track method",
reqMethod: http.MethodGet,
trackMethod: string([]byte(nil)),
want: false,
},
{
name: "matches two identical methods",
reqMethod: http.MethodGet,
trackMethod: http.MethodGet,
want: true,
},
{
name: "does not match differing methods",
reqMethod: http.MethodGet,
trackMethod: http.MethodPost,
want: false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
httpReq := track.Request{Method: tc.reqMethod}
trackReq := track.Request{Method: tc.trackMethod}
actualMatch := govcr.DefaultMethodMatcher(&httpReq, &trackReq)
assert.Equal(t, tc.want, actualMatch)
})
}
}
func Test_DefaultURLMatcher(t *testing.T) {
tt := []*struct {
name string
reqURL *url.URL
trackURL *url.URL
want bool
}{
{
name: "matches nil URLs",
reqURL: nil,
trackURL: nil,
want: true,
},
{
name: "matches empty request URL with nil track URL",
reqURL: &url.URL{},
trackURL: nil,
want: true,
},
{
name: "matches nil request URL with empty track URL",
reqURL: &url.URL{},
trackURL: nil,
want: true,
},
{
name: "does not match non-empty request URL with nil track URL",
reqURL: &url.URL{
User: url.UserPassword("a", "b"),
},
trackURL: nil,
want: false,
},
{
name: "does not match nil request URL with non-empty track URL",
reqURL: &url.URL{
User: url.UserPassword("a", "b"),
},
trackURL: nil,
want: false,
},
{
name: "matches two identical URLs",
reqURL: &url.URL{
Scheme: "scheme",
Opaque: "opaque",
User: url.UserPassword("a", "b"),
Host: "host",
Path: "path/",
RawPath: "/path/raw",
ForceQuery: false,
RawQuery: "rawq",
Fragment: "frag",
},
trackURL: &url.URL{
Scheme: "scheme",
Opaque: "opaque",
User: url.UserPassword("a", "b"),
Host: "host",
Path: "path/",
RawPath: "/path/raw",
ForceQuery: false,
RawQuery: "rawq",
Fragment: "frag",
},
want: true,
},
{
name: "does not match differing URLs",
reqURL: &url.URL{
User: url.UserPassword("1", "2"),
},
trackURL: &url.URL{
User: url.UserPassword("a", "b"),
},
want: false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
httpReq := track.Request{URL: tc.reqURL}
trackReq := track.Request{URL: tc.trackURL}
actualMatch := govcr.DefaultURLMatcher(&httpReq, &trackReq)
assert.Equal(t, tc.want, actualMatch)
})
}
}
func Test_DefaultBodyMatcher(t *testing.T) {
tt := []*struct {
name string
reqBody []byte
trackBody []byte
want bool
}{
{
name: "matches nil bodies",
reqBody: nil,
trackBody: nil,
want: true,
},
{
name: "matches nil request bodies with empty track bodies",
reqBody: nil,
trackBody: []byte{},
want: true,
},
{
name: "matches empty request bodies with nil track bodies",
reqBody: []byte{},
trackBody: nil,
want: true,
},
{
name: "does not match nil request bodies with non-empty track bodies",
reqBody: nil,
trackBody: []byte("something"),
want: false,
},
{
name: "does not match non-empty request bodies with nil track bodies",
reqBody: []byte("something"),
trackBody: nil,
want: false,
},
{
name: "matches two identical bodies",
reqBody: []byte("something"),
trackBody: []byte("something"),
want: true,
},
{
name: "does not match differing bodies",
reqBody: []byte("something"),
trackBody: []byte("another thing"),
want: false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
httpReq := track.Request{Body: tc.reqBody}
trackReq := track.Request{Body: tc.trackBody}
actualMatch := govcr.DefaultBodyMatcher(&httpReq, &trackReq)
assert.Equal(t, tc.want, actualMatch)
})
}
}
func Test_DefaultTrailerMatcher(t *testing.T) {
tt := []*struct {
name string
reqHeaders http.Header
trackHeaders http.Header
want bool
}{
{
name: "matches nil trailers",
reqHeaders: nil,
trackHeaders: nil,
want: true,
},
{
name: "matches nil request trailer with empty track trailer",
reqHeaders: nil,
trackHeaders: http.Header{},
want: true,
},
{
name: "matches empty request trailer with nil track trailer",
reqHeaders: http.Header{},
trackHeaders: nil,
want: true,
},
{
name: "does not match nil request trailer with non-empty track trailer",
reqHeaders: nil,
trackHeaders: http.Header{"trailer": {"value"}},
want: false,
},
{
name: "does not match non-empty request trailer with nil track trailer",
reqHeaders: http.Header{"trailer": {"value"}},
trackHeaders: nil,
want: false,
},
{
name: "matches two complex unordered equivalent non-empty trailers",
reqHeaders: http.Header{"trailer1": {"value1"}, "trailer2": {"value2b", "value2a"}},
trackHeaders: http.Header{"trailer2": {"value2a", "value2b"}, "trailer1": {"value1"}},
want: true,
},
{
name: "does not match two non-identical non-empty trailers",
reqHeaders: http.Header{"trailer": {"value"}},
trackHeaders: http.Header{"other": {"something"}},
want: false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
httpReq := track.Request{Header: tc.reqHeaders}
trackReq := track.Request{Header: tc.trackHeaders}
actualMatch := govcr.DefaultHeaderMatcher(&httpReq, &trackReq)
assert.Equal(t, tc.want, actualMatch)
})
}
}