-
Notifications
You must be signed in to change notification settings - Fork 103
/
match.go
519 lines (463 loc) · 12.9 KB
/
match.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
package httpmock
import (
"bytes"
"fmt"
"io"
"io/ioutil" //nolint: staticcheck
"net/http"
"runtime"
"strings"
"sync/atomic"
"github.com/jarcoal/httpmock/internal"
)
var ignorePackages = map[string]bool{}
func init() {
IgnoreMatcherHelper()
}
// IgnoreMatcherHelper should be called by external helpers building
// [Matcher], typically in an init() function, to avoid they appear in
// the autogenerated [Matcher] names.
func IgnoreMatcherHelper(skip ...int) {
sk := 2
if len(skip) > 0 {
sk += skip[0]
}
if pkg := getPackage(sk); pkg != "" {
ignorePackages[pkg] = true
}
}
// Copied from github.com/maxatome/go-testdeep/internal/trace.getPackage.
func getPackage(skip int) string {
if pc, _, _, ok := runtime.Caller(skip); ok {
if fn := runtime.FuncForPC(pc); fn != nil {
return extractPackage(fn.Name())
}
}
return ""
}
// extractPackage extracts package part from a fully qualified function name:
//
// "foo/bar/test.fn" → "foo/bar/test"
// "foo/bar/test.X.fn" → "foo/bar/test"
// "foo/bar/test.(*X).fn" → "foo/bar/test"
// "foo/bar/test.(*X).fn.func1" → "foo/bar/test"
// "weird" → ""
//
// Derived from github.com/maxatome/go-testdeep/internal/trace.SplitPackageFunc.
func extractPackage(fn string) string {
sp := strings.LastIndexByte(fn, '/')
if sp < 0 {
sp = 0 // std package
}
dp := strings.IndexByte(fn[sp:], '.')
if dp < 0 {
return ""
}
return fn[:sp+dp]
}
// calledFrom returns a string like "@PKG.FUNC() FILE:LINE".
func calledFrom(skip int) string {
pc := make([]uintptr, 128)
npc := runtime.Callers(skip+1, pc)
pc = pc[:npc]
frames := runtime.CallersFrames(pc)
var lastFrame runtime.Frame
for {
frame, more := frames.Next()
// If testing package is encountered, it is too late
if strings.HasPrefix(frame.Function, "testing.") {
break
}
lastFrame = frame
// Stop if httpmock is not the caller
if !ignorePackages[extractPackage(frame.Function)] || !more {
break
}
}
if lastFrame.Line == 0 {
return ""
}
return fmt.Sprintf(" @%s() %s:%d",
lastFrame.Function, lastFrame.File, lastFrame.Line)
}
// MatcherFunc type is the function to use to check a [Matcher]
// matches an incoming request. When httpmock calls a function of this
// type, it is guaranteed req.Body is never nil. If req.Body is nil in
// the original request, it is temporarily replaced by an instance
// returning always [io.EOF] for each Read() call, during the call.
type MatcherFunc func(req *http.Request) bool
func matcherFuncOr(mfs []MatcherFunc) MatcherFunc {
return func(req *http.Request) bool {
for _, mf := range mfs {
rearmBody(req)
if mf(req) {
return true
}
}
return false
}
}
func matcherFuncAnd(mfs []MatcherFunc) MatcherFunc {
if len(mfs) == 0 {
return nil
}
return func(req *http.Request) bool {
for _, mf := range mfs {
rearmBody(req)
if !mf(req) {
return false
}
}
return true
}
}
// Check returns true if mf is nil, otherwise it returns mf(req).
func (mf MatcherFunc) Check(req *http.Request) bool {
return mf == nil || mf(req)
}
// Or combines mf and all mfs in a new [MatcherFunc]. This new
// [MatcherFunc] succeeds if one of mf or mfs succeeds. Note that as
// a nil [MatcherFunc] is considered succeeding, if mf or one of mfs
// items is nil, nil is returned.
func (mf MatcherFunc) Or(mfs ...MatcherFunc) MatcherFunc {
if len(mfs) == 0 || mf == nil {
return mf
}
cmfs := make([]MatcherFunc, len(mfs)+1)
cmfs[0] = mf
for i, cur := range mfs {
if cur == nil {
return nil
}
cmfs[i+1] = cur
}
return matcherFuncOr(cmfs)
}
// And combines mf and all mfs in a new [MatcherFunc]. This new
// [MatcherFunc] succeeds if all of mf and mfs succeed. Note that a
// [MatcherFunc] also succeeds if it is nil, so if mf and all mfs
// items are nil, nil is returned.
func (mf MatcherFunc) And(mfs ...MatcherFunc) MatcherFunc {
if len(mfs) == 0 {
return mf
}
cmfs := make([]MatcherFunc, 0, len(mfs)+1)
if mf != nil {
cmfs = append(cmfs, mf)
}
for _, cur := range mfs {
if cur != nil {
cmfs = append(cmfs, cur)
}
}
return matcherFuncAnd(cmfs)
}
// Matcher type defines a match case. The zero Matcher{} corresponds
// to the default case. Otherwise, use [NewMatcher] or any helper
// building a [Matcher] like [BodyContainsBytes], [BodyContainsBytes],
// [HeaderExists], [HeaderIs], [HeaderContains] or any of
// [github.com/maxatome/tdhttpmock] functions.
type Matcher struct {
name string
fn MatcherFunc // can be nil → means always true
}
var matcherID int64
// NewMatcher returns a [Matcher]. If name is empty and fn is non-nil,
// a name is automatically generated. When fn is nil, it is a default
// [Matcher]: its name can be empty.
//
// Automatically generated names have the form:
//
// [email protected]() FILE:LINE
//
// Legend:
// - HEXANUMBER is a unique 10 digit hexadecimal number, always increasing;
// - PKG is the NewMatcher caller package (except if
// [IgnoreMatcherHelper] has been previously called, in this case it
// is the caller of the caller package and so on);
// - FUNC is the function name of the caller in the previous PKG package;
// - FILE and LINE are the location of the call in FUNC function.
func NewMatcher(name string, fn MatcherFunc) Matcher {
if name == "" && fn != nil {
// Auto-name the matcher
name = fmt.Sprintf("~%010x%s", atomic.AddInt64(&matcherID, 1), calledFrom(1))
}
return Matcher{
name: name,
fn: fn,
}
}
// BodyContainsBytes returns a [Matcher] checking that request body
// contains subslice.
//
// The name of the returned [Matcher] is auto-generated (see [NewMatcher]).
// To name it explicitly, use [Matcher.WithName] as in:
//
// BodyContainsBytes([]byte("foo")).WithName("10-body-contains-foo")
//
// See also [github.com/maxatome/tdhttpmock.Body],
// [github.com/maxatome/tdhttpmock.JSONBody] and
// [github.com/maxatome/tdhttpmock.XMLBody] for powerful body testing.
func BodyContainsBytes(subslice []byte) Matcher {
return NewMatcher("",
func(req *http.Request) bool {
rearmBody(req)
b, err := ioutil.ReadAll(req.Body)
return err == nil && bytes.Contains(b, subslice)
})
}
// BodyContainsString returns a [Matcher] checking that request body
// contains substr.
//
// The name of the returned [Matcher] is auto-generated (see [NewMatcher]).
// To name it explicitly, use [Matcher.WithName] as in:
//
// BodyContainsString("foo").WithName("10-body-contains-foo")
//
// See also [github.com/maxatome/tdhttpmock.Body],
// [github.com/maxatome/tdhttpmock.JSONBody] and
// [github.com/maxatome/tdhttpmock.XMLBody] for powerful body testing.
func BodyContainsString(substr string) Matcher {
return NewMatcher("",
func(req *http.Request) bool {
rearmBody(req)
b, err := ioutil.ReadAll(req.Body)
return err == nil && bytes.Contains(b, []byte(substr))
})
}
// HeaderExists returns a [Matcher] checking that request contains
// key header.
//
// The name of the returned [Matcher] is auto-generated (see [NewMatcher]).
// To name it explicitly, use [Matcher.WithName] as in:
//
// HeaderExists("X-Custom").WithName("10-custom-exists")
//
// See also [github.com/maxatome/tdhttpmock.Header] for powerful
// header testing.
func HeaderExists(key string) Matcher {
return NewMatcher("",
func(req *http.Request) bool {
_, ok := req.Header[key]
return ok
})
}
// HeaderIs returns a [Matcher] checking that request contains
// key header set to value.
//
// The name of the returned [Matcher] is auto-generated (see [NewMatcher]).
// To name it explicitly, use [Matcher.WithName] as in:
//
// HeaderIs("X-Custom", "VALUE").WithName("10-custom-is-value")
//
// See also [github.com/maxatome/tdhttpmock.Header] for powerful
// header testing.
func HeaderIs(key, value string) Matcher {
return NewMatcher("",
func(req *http.Request) bool {
return req.Header.Get(key) == value
})
}
// HeaderContains returns a [Matcher] checking that request contains key
// header itself containing substr.
//
// The name of the returned [Matcher] is auto-generated (see [NewMatcher]).
// To name it explicitly, use [Matcher.WithName] as in:
//
// HeaderContains("X-Custom", "VALUE").WithName("10-custom-contains-value")
//
// See also [github.com/maxatome/tdhttpmock.Header] for powerful
// header testing.
func HeaderContains(key, substr string) Matcher {
return NewMatcher("",
func(req *http.Request) bool {
return strings.Contains(req.Header.Get(key), substr)
})
}
// Name returns the m's name.
func (m Matcher) Name() string {
return m.name
}
// WithName returns a new [Matcher] based on m with name name.
func (m Matcher) WithName(name string) Matcher {
return NewMatcher(name, m.fn)
}
// Check returns true if req is matched by m.
func (m Matcher) Check(req *http.Request) bool {
return m.fn.Check(req)
}
// Or combines m and all ms in a new [Matcher]. This new [Matcher]
// succeeds if one of m or ms succeeds. Note that as a [Matcher]
// succeeds if internal fn is nil, if m's internal fn or any of ms
// item's internal fn is nil, the returned [Matcher] always
// succeeds. The name of returned [Matcher] is m's one.
func (m Matcher) Or(ms ...Matcher) Matcher {
if len(ms) == 0 || m.fn == nil {
return m
}
mfs := make([]MatcherFunc, 1, len(ms)+1)
mfs[0] = m.fn
for _, cur := range ms {
if cur.fn == nil {
return Matcher{}
}
mfs = append(mfs, cur.fn)
}
m.fn = matcherFuncOr(mfs)
return m
}
// And combines m and all ms in a new [Matcher]. This new [Matcher]
// succeeds if all of m and ms succeed. Note that a [Matcher] also
// succeeds if [Matcher] [MatcherFunc] is nil. The name of returned
// [Matcher] is m's one if the empty/default [Matcher] is returned.
func (m Matcher) And(ms ...Matcher) Matcher {
if len(ms) == 0 {
return m
}
mfs := make([]MatcherFunc, 0, len(ms)+1)
if m.fn != nil {
mfs = append(mfs, m.fn)
}
for _, cur := range ms {
if cur.fn != nil {
mfs = append(mfs, cur.fn)
}
}
m.fn = matcherFuncAnd(mfs)
if m.fn != nil {
return m
}
return Matcher{}
}
type matchResponder struct {
matcher Matcher
responder Responder
}
type matchResponders []matchResponder
// add adds or replaces a matchResponder.
func (mrs matchResponders) add(mr matchResponder) matchResponders {
// default is always at end
if mr.matcher.fn == nil {
if len(mrs) > 0 && (mrs)[len(mrs)-1].matcher.fn == nil {
mrs[len(mrs)-1] = mr
return mrs
}
return append(mrs, mr)
}
for i, cur := range mrs {
if cur.matcher.name == mr.matcher.name {
mrs[i] = mr
return mrs
}
}
for i, cur := range mrs {
if cur.matcher.fn == nil || cur.matcher.name > mr.matcher.name {
mrs = append(mrs, matchResponder{})
copy(mrs[i+1:], mrs[i:len(mrs)-1])
mrs[i] = mr
return mrs
}
}
return append(mrs, mr)
}
func (mrs matchResponders) checkEmptiness() matchResponders {
if len(mrs) == 0 {
return nil
}
return mrs
}
func (mrs matchResponders) shrink() matchResponders {
mrs[len(mrs)-1] = matchResponder{}
mrs = mrs[:len(mrs)-1]
return mrs.checkEmptiness()
}
func (mrs matchResponders) remove(name string) matchResponders {
// Special case, even if default has been renamed, we consider ""
// matching this default
if name == "" {
// default is always at end
if len(mrs) > 0 && mrs[len(mrs)-1].matcher.fn == nil {
return mrs.shrink()
}
return mrs.checkEmptiness()
}
for i, cur := range mrs {
if cur.matcher.name == name {
copy(mrs[i:], mrs[i+1:])
return mrs.shrink()
}
}
return mrs.checkEmptiness()
}
func (mrs matchResponders) findMatchResponder(req *http.Request) *matchResponder {
if len(mrs) == 0 {
return nil
}
if mrs[0].matcher.fn == nil { // nil match is always the last
return &mrs[0]
}
copyBody := &bodyCopyOnRead{body: req.Body}
req.Body = copyBody
defer func() {
copyBody.rearm()
req.Body = copyBody.body
}()
for _, mr := range mrs {
copyBody.rearm()
if mr.matcher.Check(req) {
return &mr
}
}
return nil
}
type matchRouteKey struct {
internal.RouteKey
name string
}
func (m matchRouteKey) String() string {
if m.name == "" {
return m.RouteKey.String()
}
return m.RouteKey.String() + " <" + m.name + ">"
}
func rearmBody(req *http.Request) {
if req != nil {
if body, ok := req.Body.(interface{ rearm() }); ok {
body.rearm()
}
}
}
type buffer struct {
*bytes.Reader
}
func (b buffer) Close() error {
return nil
}
// bodyCopyOnRead mutates body into a buffer on first Read(), except
// if body is nil or http.NoBody. In this case, EOF is returned for
// each Read() and body stays untouched.
type bodyCopyOnRead struct {
body io.ReadCloser
}
func (b *bodyCopyOnRead) rearm() {
if buf, ok := b.body.(buffer); ok {
buf.Seek(0, io.SeekStart) //nolint:errcheck
} // else b.body contains the original body, so don't touch
}
func (b *bodyCopyOnRead) copy() {
if _, ok := b.body.(buffer); !ok && b.body != nil && b.body != http.NoBody {
buf, _ := ioutil.ReadAll(b.body)
b.body.Close()
b.body = buffer{bytes.NewReader(buf)}
}
}
func (b *bodyCopyOnRead) Read(p []byte) (n int, err error) {
b.copy()
if b.body == nil {
return 0, io.EOF
}
return b.body.Read(p)
}
func (b *bodyCopyOnRead) Close() error {
return nil
}