-
Notifications
You must be signed in to change notification settings - Fork 1
/
mocks.go
1338 lines (1165 loc) · 41.1 KB
/
mocks.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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package spectest
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/textproto"
"net/url"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"sync"
"time"
"github.com/davecgh/go-spew/spew"
difflib "github.com/nao1215/diff"
)
// Transport wraps components used to observe and manipulate the real request and response objects
type Transport struct {
// httpClient is the http client used when networking is enabled.
httpClient *http.Client
// mocks is the list of mocks to use when mocking the request
mocks Mocks
// mockResponseDelayEnabled will enable mock response delay
mockResponseDelayEnabled bool
// observers is the list of observers to use when observing the request and response
observers []Observe
// debug is used to enable/disable debug logging
debug *debug
// nativeTransport is the native http.RoundTripper
nativeTransport http.RoundTripper
// specTest is the spectest instance
specTest *SpecTest
}
// newTransport creates a new transport
// If you set httpClient to nil, http.DefaultClient will be used.
// If you set debug to nil, debug will be disabled.
func newTransport(
mocks Mocks,
httpClient *http.Client,
debug *debug,
mockResponseDelayEnabled bool,
observers []Observe,
specTest *SpecTest) *Transport {
t := &Transport{
mocks: mocks,
httpClient: httpClient,
debug: debug,
mockResponseDelayEnabled: mockResponseDelayEnabled,
observers: observers,
specTest: specTest,
}
if httpClient != nil {
t.nativeTransport = httpClient.Transport
} else {
t.nativeTransport = http.DefaultTransport
}
return t
}
// RoundTrip implementation intended to match a given expected mock request
// or throw an error with a list of reasons why no match was found.
func (r *Transport) RoundTrip(req *http.Request) (mockResponse *http.Response, err error) {
defer func() {
r.debug.mock(mockResponse, req)
}()
if r.observers != nil && len(r.observers) > 0 {
defer func() {
for _, observe := range r.observers {
observe(mockResponse, req, r.specTest)
}
}()
}
matchedResponse, err := matches(req, r.mocks)
if err != nil {
if r.debug.isEnable() {
fmt.Printf("failed to match mocks. Errors: %s\n", err)
}
return nil, err
}
res := buildResponseFromMock(matchedResponse)
res.Request = req
if matchedResponse.timeout {
return nil, ErrTimeout
}
if r.mockResponseDelayEnabled && matchedResponse.fixedDelayMillis > 0 {
time.Sleep(time.Duration(matchedResponse.fixedDelayMillis) * time.Millisecond)
}
return res, nil
}
// Hijack replace the transport implementation of the interaction under test in order to observe, mock and inject expectations
func (r *Transport) Hijack() {
if r.httpClient != nil {
r.httpClient.Transport = r
return
}
http.DefaultTransport = r
}
// Reset replace the hijacked transport implementation of the interaction under test to the original implementation
func (r *Transport) Reset() {
if r.httpClient != nil {
r.httpClient.Transport = r.nativeTransport
return
}
http.DefaultTransport = r.nativeTransport
}
// buildResponseFromMock builds a http.Response from a MockResponse
func buildResponseFromMock(mockResponse *MockResponse) *http.Response {
if mockResponse == nil {
return nil
}
// if the content type isn't set and the body contains json, set content type as json
contentTypeHeader := mockResponse.headers["Content-Type"]
var contentType string
if len(mockResponse.body) > 0 {
if len(contentTypeHeader) == 0 {
if json.Valid([]byte(mockResponse.body)) {
contentType = "application/json"
} else {
contentType = "text/plain"
}
} else {
contentType = contentTypeHeader[0]
}
}
res := &http.Response{
Body: io.NopCloser(strings.NewReader(mockResponse.body)),
Header: mockResponse.headers,
StatusCode: mockResponse.statusCode,
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: int64(len(mockResponse.body)),
}
for _, cookie := range mockResponse.cookies {
if v := cookie.ToHTTPCookie().String(); v != "" {
res.Header.Add("Set-Cookie", v)
}
}
if contentType != "" {
res.Header.Set("Content-Type", contentType)
}
return res
}
// Mock represents the entire interaction for a mock to be used for testing
type Mock struct {
m *sync.Mutex
// state is mock runnig state
state *state
// request is used to configure the request of the mock
request *MockRequest
// resopnse is used to configure the response of the mock
response *MockResponse
// httpClient is used to enable/disable networking for the test
httpClient *http.Client
// debugStandalone is used to enable/disable debug logging for standalone mocks
debugStandalone *debug
// execCount is used to track the number of times the mock has been executed
execCount *execCount
}
// Mocks is a slice of Mock
type Mocks []*Mock
// len returns the length of the mocks
func (mocks Mocks) len() int {
return len(mocks)
}
// findUnmatchedMocks returns a list of unmatched mocks.
// An unmatched mock is a mock that was not used, e.g. there was not a matching http Request for the mock
func (mocks Mocks) findUnmatchedMocks() []UnmatchedMock {
var unmatchedMocks []UnmatchedMock
for _, m := range mocks {
if !m.state.isRunning() {
unmatchedMocks = append(unmatchedMocks, UnmatchedMock{
URL: *m.request.url,
})
break
}
}
return unmatchedMocks
}
// Matches checks whether the given request matches the mock
func (m *Mock) Matches(req *http.Request) []error {
var errs []error
for _, matcher := range m.request.matchers {
if matcherError := matcher(req, m.request); matcherError != nil {
errs = append(errs, matcherError)
}
}
return errs
}
// deepCopy deepCopy Mock.
func (m *Mock) deepCopy() *Mock {
newMock := *m
newMock.m = &sync.Mutex{}
req := *m.request
newMock.request = &req
res := *m.response
newMock.response = &res
state := *m.state
newMock.state = &state
return &newMock
}
// MockRequest represents the http request side of a mock interaction
type MockRequest struct {
mock *Mock
url *url.URL
method string
headers map[string][]string
basicAuth basicAuth
headerPresent []string
headerNotPresent []string
formData map[string][]string
formDataPresent []string
formDataNotPresent []string
query map[string][]string
queryPresent []string
queryNotPresent []string
cookie []Cookie
cookiePresent []string
cookieNotPresent []string
body string
bodyRegexp string
matchers []Matcher
}
// newMockRequest return new MockRequest
func newMockRequest(m *Mock) *MockRequest {
return &MockRequest{
mock: m,
headers: map[string][]string{},
headerPresent: []string{},
headerNotPresent: []string{},
formData: map[string][]string{},
formDataPresent: []string{},
formDataNotPresent: []string{},
query: map[string][]string{},
queryPresent: []string{},
queryNotPresent: []string{},
cookie: []Cookie{},
cookiePresent: []string{},
cookieNotPresent: []string{},
matchers: defaultMatchers(),
}
}
// UnmatchedMock exposes some information about mocks that failed to match a request
type UnmatchedMock struct {
URL url.URL
}
// MockResponse represents the http response side of a mock interaction
type MockResponse struct {
mock *Mock
timeout bool
headers map[string][]string
cookies []*Cookie
body string
statusCode int
fixedDelayMillis int64
}
// newMockResponse return new MockResponse
func newMockResponse(m *Mock) *MockResponse {
return &MockResponse{
mock: m,
headers: map[string][]string{},
cookies: []*Cookie{},
}
}
// StandaloneMocks for using mocks outside of API tests context
type StandaloneMocks struct {
mocks Mocks
httpClient *http.Client
debug *debug
}
// NewStandaloneMocks create a series of StandaloneMocks
func NewStandaloneMocks(mocks ...*Mock) *StandaloneMocks {
return &StandaloneMocks{
mocks: mocks,
debug: newDebug(),
}
}
// HTTPClient use the given http client
func (r *StandaloneMocks) HTTPClient(cli *http.Client) *StandaloneMocks {
r.httpClient = cli
return r
}
// Debug switch on debugging mode
func (r *StandaloneMocks) Debug() *StandaloneMocks {
r.debug.enable()
return r
}
// End finalizes the mock, ready for use
func (r *StandaloneMocks) End() func() {
transport := newTransport(
r.mocks,
r.httpClient,
r.debug,
false,
nil,
nil,
)
resetFunc := func() { transport.Reset() }
transport.Hijack()
return resetFunc
}
// NewMock create a new mock, ready for configuration using the builder pattern
func NewMock() *Mock {
mock := &Mock{
debugStandalone: newDebug(),
m: &sync.Mutex{},
state: newState(),
execCount: newExecCount(1),
}
mock.request = newMockRequest(mock)
mock.response = newMockResponse(mock)
return mock
}
// Debug is used to set debug mode for mocks in standalone mode.
// This is overridden by the debug setting in the `SpecTest` struct
func (m *Mock) Debug() *Mock {
m.debugStandalone.enable()
return m
}
// HTTPClient allows the developer to provide a custom http client when using mocks
func (m *Mock) HTTPClient(cli *http.Client) *Mock {
m.httpClient = cli
return m
}
// Get configures the mock to match http method GET
func (m *Mock) Get(u string) *MockRequest {
m.parseURL(u)
m.request.method = http.MethodGet
return m.request
}
// Getf configures the mock to match http method GET and supports formatting
func (m *Mock) Getf(format string, args ...interface{}) *MockRequest {
return m.Get(fmt.Sprintf(format, args...))
}
// Put configures the mock to match http method PUT
func (m *Mock) Put(u string) *MockRequest {
m.parseURL(u)
m.request.method = http.MethodPut
return m.request
}
// Putf configures the mock to match http method PUT and supports formatting
func (m *Mock) Putf(format string, args ...interface{}) *MockRequest {
return m.Put(fmt.Sprintf(format, args...))
}
// Post configures the mock to match http method POST
func (m *Mock) Post(u string) *MockRequest {
m.parseURL(u)
m.request.method = http.MethodPost
return m.request
}
// Postf configures the mock to match http method POST and supports formatting
func (m *Mock) Postf(format string, args ...interface{}) *MockRequest {
return m.Post(fmt.Sprintf(format, args...))
}
// Delete configures the mock to match http method DELETE
func (m *Mock) Delete(u string) *MockRequest {
m.parseURL(u)
m.request.method = http.MethodDelete
return m.request
}
// Deletef configures the mock to match http method DELETE and supports formatting
func (m *Mock) Deletef(format string, args ...interface{}) *MockRequest {
return m.Delete(fmt.Sprintf(format, args...))
}
// Patch configures the mock to match http method PATCH
func (m *Mock) Patch(u string) *MockRequest {
m.parseURL(u)
m.request.method = http.MethodPatch
return m.request
}
// Patchf configures the mock to match http method PATCH and supports formatting
func (m *Mock) Patchf(format string, args ...interface{}) *MockRequest {
return m.Patch(fmt.Sprintf(format, args...))
}
// Head configures the mock to match http method HEAD
func (m *Mock) Head(u string) *MockRequest {
m.parseURL(u)
m.request.method = http.MethodHead
return m.request
}
// Headf configures the mock to match http method HEAD and supports formatting
func (m *Mock) Headf(format string, args ...interface{}) *MockRequest {
return m.Head(fmt.Sprintf(format, args...))
}
// Connect configures the mock to match http method CONNECT
func (m *Mock) Connect(u string) *MockRequest {
m.parseURL(u)
m.request.method = http.MethodConnect
return m.request
}
// Connectf configures the mock to match http method CONNECT and supports formatting
func (m *Mock) Connectf(format string, args ...interface{}) *MockRequest {
return m.Connect(fmt.Sprintf(format, args...))
}
// Options configures the mock to match http method OPTIONS
func (m *Mock) Options(u string) *MockRequest {
m.parseURL(u)
m.request.method = http.MethodOptions
return m.request
}
// Optionsf configures the mock to match http method OPTIONS and supports formatting
func (m *Mock) Optionsf(format string, args ...interface{}) *MockRequest {
return m.Options(fmt.Sprintf(format, args...))
}
// Trace configures the mock to match http method TRACE
func (m *Mock) Trace(u string) *MockRequest {
m.parseURL(u)
m.request.method = http.MethodTrace
return m.request
}
// Tracef configures the mock to match http method TRACE and supports formatting
func (m *Mock) Tracef(format string, args ...interface{}) *MockRequest {
return m.Trace(fmt.Sprintf(format, args...))
}
// Method configures mock to match given http method
func (m *Mock) Method(method string) *MockRequest {
m.request.method = method
return m.request
}
// parseURL parses the given url and sets it on the mock request
func (m *Mock) parseURL(u string) {
parsed, err := url.Parse(u)
if err != nil {
panic(err)
}
m.request.url = parsed
}
// matches checks whether the given request matches any of the given mocks
func matches(req *http.Request, mocks Mocks) (*MockResponse, error) {
mockError := newUnmatchedMockError()
for mockNumber, mock := range mocks {
mock.m.Lock() // lock is for isUsed when matches is called concurrently by RoundTripper
if mock.state.isRunning() {
mock.m.Unlock()
continue
}
errs := mock.Matches(req)
if len(errs) == 0 {
mock.state.Start()
mock.m.Unlock()
return mock.response, nil
}
mockError = mockError.append(mockNumber+1, errs...)
mock.m.Unlock()
}
return nil, mockError
}
// Body configures the mock request to match the given body
func (r *MockRequest) Body(b string) *MockRequest {
r.body = b
return r
}
// BodyRegexp configures the mock request to match the given body using the regexp matcher
func (r *MockRequest) BodyRegexp(b string) *MockRequest {
r.body = b
return r
}
// Bodyf configures the mock request to match the given body. Supports formatting the body
func (r *MockRequest) Bodyf(format string, args ...interface{}) *MockRequest {
return r.Body(fmt.Sprintf(format, args...))
}
// BodyFromFile configures the mock request to match the given body from a file
func (r *MockRequest) BodyFromFile(f string) *MockRequest {
b, err := os.ReadFile(filepath.Clean(f))
if err != nil {
panic(err)
}
r.body = string(b)
return r
}
// JSON is a convenience method for setting the mock request body
func (r *MockRequest) JSON(v interface{}) *MockRequest {
switch x := v.(type) {
case string:
r.body = x
case []byte:
r.body = string(x)
default:
asJSON, _ := json.Marshal(x)
r.body = string(asJSON)
}
return r
}
// Header configures the mock request to match the given header
func (r *MockRequest) Header(key, value string) *MockRequest {
normalizedKey := textproto.CanonicalMIMEHeaderKey(key)
r.headers[normalizedKey] = append(r.headers[normalizedKey], value)
return r
}
// Headers configures the mock request to match the given headers
func (r *MockRequest) Headers(headers map[string]string) *MockRequest {
for k, v := range headers {
normalizedKey := textproto.CanonicalMIMEHeaderKey(k)
r.headers[normalizedKey] = append(r.headers[normalizedKey], v)
}
return r
}
// HeaderPresent configures the mock request to match when this header is present, regardless of value
func (r *MockRequest) HeaderPresent(key string) *MockRequest {
r.headerPresent = append(r.headerPresent, key)
return r
}
// HeaderNotPresent configures the mock request to match when the header is not present
func (r *MockRequest) HeaderNotPresent(key string) *MockRequest {
r.headerNotPresent = append(r.headerNotPresent, key)
return r
}
// BasicAuth configures the mock request to match the given basic auth parameters
func (r *MockRequest) BasicAuth(userName, password string) *MockRequest {
r.basicAuth = newBasicAuth(userName, password)
return r
}
// FormData configures the mock request to math the given form data
func (r *MockRequest) FormData(key string, values ...string) *MockRequest {
r.formData[key] = append(r.formData[key], values...)
return r
}
// FormDataPresent configures the mock request to match when the form data is present, regardless of values
func (r *MockRequest) FormDataPresent(key string) *MockRequest {
r.formDataPresent = append(r.formDataPresent, key)
return r
}
// FormDataNotPresent configures the mock request to match when the form data is not present
func (r *MockRequest) FormDataNotPresent(key string) *MockRequest {
r.formDataNotPresent = append(r.formDataNotPresent, key)
return r
}
// Query configures the mock request to match a query param
func (r *MockRequest) Query(key, value string) *MockRequest {
r.query[key] = append(r.query[key], value)
return r
}
// QueryParams configures the mock request to match a number of query params
func (r *MockRequest) QueryParams(queryParams map[string]string) *MockRequest {
for k, v := range queryParams {
r.query[k] = append(r.query[k], v)
}
return r
}
// QueryCollection configures the mock request to match a number of repeating query params, e.g. ?a=1&a=2&a=3
func (r *MockRequest) QueryCollection(queryParams map[string][]string) *MockRequest {
for k, v := range queryParams {
r.query[k] = append(r.query[k], v...)
}
return r
}
// QueryPresent configures the mock request to match when a query param is present, regardless of value
func (r *MockRequest) QueryPresent(key string) *MockRequest {
r.queryPresent = append(r.queryPresent, key)
return r
}
// QueryNotPresent configures the mock request to match when the query param is not present
func (r *MockRequest) QueryNotPresent(key string) *MockRequest {
r.queryNotPresent = append(r.queryNotPresent, key)
return r
}
// Cookie configures the mock request to match a cookie
func (r *MockRequest) Cookie(name, value string) *MockRequest {
r.cookie = append(r.cookie, Cookie{name: &name, value: &value})
return r
}
// CookiePresent configures the mock request to match when a cookie is present, regardless of value
func (r *MockRequest) CookiePresent(name string) *MockRequest {
r.cookiePresent = append(r.cookiePresent, name)
return r
}
// CookieNotPresent configures the mock request to match when a cookie is not present
func (r *MockRequest) CookieNotPresent(name string) *MockRequest {
r.cookieNotPresent = append(r.cookieNotPresent, name)
return r
}
// AddMatcher configures the mock request to match using a custom matcher
func (r *MockRequest) AddMatcher(matcher Matcher) *MockRequest {
r.matchers = append(r.matchers, matcher)
return r
}
// RespondWith finalizes the mock request phase of set up and allowing the definition of response attributes to be defined
func (r *MockRequest) RespondWith() *MockResponse {
return r.mock.response
}
// Timeout forces the mock to return a http timeout
func (r *MockResponse) Timeout() *MockResponse {
r.timeout = true
return r
}
// Header respond with the given header
func (r *MockResponse) Header(key string, value string) *MockResponse {
normalizedKey := textproto.CanonicalMIMEHeaderKey(key)
r.headers[normalizedKey] = append(r.headers[normalizedKey], value)
return r
}
// Headers respond with the given headers
func (r *MockResponse) Headers(headers map[string]string) *MockResponse {
for k, v := range headers {
normalizedKey := textproto.CanonicalMIMEHeaderKey(k)
r.headers[normalizedKey] = append(r.headers[normalizedKey], v)
}
return r
}
// Cookies respond with the given cookies
func (r *MockResponse) Cookies(cookie ...*Cookie) *MockResponse {
r.cookies = append(r.cookies, cookie...)
return r
}
// Cookie respond with the given cookie
func (r *MockResponse) Cookie(name, value string) *MockResponse {
r.cookies = append(r.cookies, NewCookie(name).Value(value))
return r
}
// Body sets the mock response body
func (r *MockResponse) Body(body string) *MockResponse {
r.body = body
return r
}
// Bodyf sets the mock response body. Supports formatting
func (r *MockResponse) Bodyf(format string, args ...interface{}) *MockResponse {
return r.Body(fmt.Sprintf(format, args...))
}
// BodyFromFile defines the mock response body from a file
func (r *MockResponse) BodyFromFile(f string) *MockResponse {
b, err := os.ReadFile(filepath.Clean(f))
if err != nil {
panic(err)
}
r.body = string(b)
return r
}
// JSON is a convenience method for setting the mock response body
func (r *MockResponse) JSON(v interface{}) *MockResponse {
switch x := v.(type) {
case string:
r.body = x
case []byte:
r.body = string(x)
default:
asJSON, _ := json.Marshal(x)
r.body = string(asJSON)
}
return r
}
// Status respond with the given status
func (r *MockResponse) Status(statusCode int) *MockResponse {
r.statusCode = statusCode
return r
}
// FixedDelay will return the response after the given number of milliseconds.
// SpecTest::EnableMockResponseDelay must be set for this to take effect.
// If Timeout is set this has no effect.
func (r *MockResponse) FixedDelay(delay int64) *MockResponse {
r.fixedDelayMillis = delay
return r
}
// Times respond the given number of times
func (r *MockResponse) Times(times uint) *MockResponse {
r.mock.execCount.updateExpectCount(times)
return r
}
// End finalizes the response definition phase in order for the mock to be used
func (r *MockResponse) End() *Mock {
return r.mock
}
// EndStandalone finalizes the response definition of standalone mocks
func (r *MockResponse) EndStandalone(other ...*Mock) func() {
transport := newTransport(
append(Mocks{r.mock}, other...),
r.mock.httpClient,
r.mock.debugStandalone,
false,
nil,
nil,
)
resetFunc := func() { transport.Reset() }
transport.Hijack()
return resetFunc
}
// Matcher type accepts the actual request and a mock request to match against.
// Will return an error that describes why there was a mismatch if the inputs do not match or nil if they do.
type Matcher func(*http.Request, *MockRequest) error
// defaultMatchers returns the default list of matchers used by the mock server.
func defaultMatchers() []Matcher {
return []Matcher{
pathMatcher,
hostMatcher,
schemeMatcher,
methodMatcher,
headerMatcher,
basicAuthMatcher,
headerPresentMatcher,
headerNotPresentMatcher,
queryParamMatcher,
queryPresentMatcher,
queryNotPresentMatcher,
formDataMatcher,
formDataPresentMatcher,
formDataNotPresentMatcher,
bodyMatcher,
bodyRegexpMatcher,
cookieMatcher,
cookiePresentMatcher,
cookieNotPresentMatcher,
}
}
// pathMatcher compares the path of the received HTTP request with the path specified in the mock request.
// If the paths match, it returns nil, indicating a successful match. If the paths do not match,
// it attempts to match using regular expressions. If a match is found, it returns an error describing
// the mismatch. If no match is found, it returns nil. The error message includes details about
// the received path and the expected mock path that did not match.
func pathMatcher(r *http.Request, spec *MockRequest) error {
receivedPath := r.URL.Path
mockPath := spec.url.Path
if receivedPath == mockPath {
return nil
}
matched, err := regexp.MatchString(mockPath, receivedPath)
return errorOrNil(matched && err == nil, func() string {
return fmt.Sprintf("received path %s did not match mock path %s", receivedPath, mockPath)
})
}
// hostMatcher compares the host of the received HTTP request with the host specified in the mock request.
// If the hosts match, it returns nil, indicating a successful match. If the hosts do not match,
// it attempts to match using regular expressions. If a match is found, it returns an error describing
// the mismatch. If no match is found, it returns nil. The error message includes details about
// the received host and the expected mock host that did not match.
func hostMatcher(r *http.Request, spec *MockRequest) error {
receivedHost := r.Host
if receivedHost == "" {
receivedHost = r.URL.Host
}
mockHost := spec.url.Host
if mockHost == "" {
return nil
}
if receivedHost == mockHost {
return nil
}
matched, err := regexp.MatchString(mockHost, r.URL.Path)
return errorOrNil(matched && err != nil, func() string {
return fmt.Sprintf("received host %s did not match mock host %s", receivedHost, mockHost)
})
}
// methodMatcher compares the HTTP request method (GET, POST, PUT, etc.) with the method specified in the mock request.
// If the methods match, it returns nil, indicating a successful match. If the methods do not match,
// it returns an error describing the mismatch. If the mock method is an empty string, it matches any request method.
// The error message includes details about the received method and the expected mock method that did not match.
func methodMatcher(r *http.Request, spec *MockRequest) error {
receivedMethod := r.Method
mockMethod := spec.method
if receivedMethod == mockMethod {
return nil
}
if mockMethod == "" {
return nil
}
return fmt.Errorf("received method %s did not match mock method %s", receivedMethod, mockMethod)
}
// schemeMatcher compares the scheme (http, https, etc.) of the received HTTP request URL
// with the scheme specified in the mock request URL.
// If the schemes match, it returns nil, indicating a successful match. If the schemes do not match,
// it returns an error describing the mismatch. If either the received or mock scheme is an empty string,
// it matches any scheme. The error message includes details about the received scheme and the expected
// mock scheme that did not match.
func schemeMatcher(r *http.Request, spec *MockRequest) error {
receivedScheme := r.URL.Scheme
mockScheme := spec.url.Scheme
if receivedScheme == "" {
return nil
}
if mockScheme == "" {
return nil
}
return errorOrNil(receivedScheme == mockScheme, func() string {
return fmt.Sprintf("received scheme %s did not match mock scheme %s", receivedScheme, mockScheme)
})
}
// headerMatcher compares the headers of the received HTTP request with the headers specified in the mock request.
// It checks each header key-value pair in the mock request against the corresponding header values in the received request.
// If all the headers in the mock request match the received request headers (based on regular expressions),
// it returns nil, indicating a successful match. If any header does not match, it returns an error describing the mismatch.
// The error message includes details about the specific headers that did not match between the received and expected requests.
func headerMatcher(req *http.Request, spec *MockRequest) error {
mockHeaders := spec.headers
for key, values := range mockHeaders {
var match bool
var err error
receivedHeaders := req.Header
for _, field := range receivedHeaders[key] {
for _, value := range values {
match, err = regexp.MatchString(value, field)
if err != nil {
return fmt.Errorf("failed to parse regexp for header %s with value %s", key, value)
}
}
if match {
break
}
}
if !match {
return fmt.Errorf("not all of received headers %s matched expected mock headers %s", receivedHeaders, mockHeaders)
}
}
return nil
}
// basicAuthMatcher compares the basic auth credentials of the received HTTP request with the credentials specified in the mock request.
func basicAuthMatcher(req *http.Request, spec *MockRequest) error {
if spec.basicAuth.isUserNameEmpty() || spec.basicAuth.isPasswordEmpty() {
return nil
}
username, password, ok := req.BasicAuth()
if !ok {
return errors.New("request did not contain valid HTTP Basic Authentication string")
}
return spec.basicAuth.auth(username, password)
}
// headerPresentMatcher compares the headers of the received HTTP request with the headers specified in the mock request.
func headerPresentMatcher(req *http.Request, spec *MockRequest) error {
for _, header := range spec.headerPresent {
if req.Header.Get(header) == "" {
return fmt.Errorf("expected header '%s' was not present", header)
}
}
return nil
}
// headerNotPresentMatcher checks that specific headers are not present in the received HTTP request.
// It compares the list of headers specified in the mock request with the headers in the received request.
// If any of the specified headers are found in the received request, it returns an error indicating
// that an unexpected header was present. If all specified headers are not present, it returns nil,
// indicating a successful match.
func headerNotPresentMatcher(req *http.Request, spec *MockRequest) error {
for _, header := range spec.headerNotPresent {
if req.Header.Get(header) != "" {
return fmt.Errorf("unexpected header '%s' was present", header)
}
}
return nil
}
// queryParamMatcher compares the query parameters of the received HTTP request with the query parameters specified in the mock request.
// It checks each query parameter key-value pair in the mock request against the corresponding query parameter values in the received request.
// If all the query parameters in the mock request match the received request query parameters (based on regular expressions),
// it returns nil, indicating a successful match. If any query parameter does not match, it returns an error describing the mismatch.
// The error message includes details about the specific query parameters that did not match between the received and expected requests.
func queryParamMatcher(req *http.Request, spec *MockRequest) error {
mockQueryParams := spec.query
for key, values := range mockQueryParams {
receivedQueryParams := req.URL.Query()
if _, ok := receivedQueryParams[key]; !ok {
return fmt.Errorf("not all of received query params %s matched expected mock query params %s", receivedQueryParams, mockQueryParams)
}
found := 0
for _, field := range receivedQueryParams[key] {
for _, value := range values {
match, err := regexp.MatchString(value, field)
if err != nil {
return fmt.Errorf("failed to parse regexp for query param %s with value %s", key, value)
}
if match {
found++
}
}
}
if found != len(values) {
return fmt.Errorf("not all of received query params %s matched expected mock query params %s", receivedQueryParams, mockQueryParams)
}
}
return nil
}
// queryPresentMatcher checks if specific query parameters specified in the mock request are present in the received HTTP request.
// It compares each expected query parameter with the corresponding query parameter in the received request's URL.
// If any expected query parameter is not found in the received request, it returns an error indicating
// that the expected query parameter was not received. If all expected query parameters are present,
// it returns nil, indicating a successful match.
func queryPresentMatcher(req *http.Request, spec *MockRequest) error {
for _, query := range spec.queryPresent {
if req.URL.Query().Get(query) == "" {
return fmt.Errorf("expected query param %s not received", query)
}
}
return nil
}
// queryNotPresentMatcher checks if specific query parameters specified in the mock request are not present in the received HTTP request.
// It compares each query parameter specified in the `queryNotPresent` list with the corresponding query parameter in the received request's URL.