-
Notifications
You must be signed in to change notification settings - Fork 55
/
mocks.go
1251 lines (1066 loc) · 32.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 apitest
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/textproto"
"net/url"
"reflect"
"regexp"
"sort"
"strings"
"sync"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/steinfletcher/apitest/difflib"
)
// Transport wraps components used to observe and manipulate the real request and response objects
type Transport struct {
debugEnabled bool
mockResponseDelayEnabled bool
mocks []*Mock
nativeTransport http.RoundTripper
httpClient *http.Client
observers []Observe
apiTest *APITest
}
func newTransport(
mocks []*Mock,
httpClient *http.Client,
debugEnabled bool,
mockResponseDelayEnabled bool,
observers []Observe,
apiTest *APITest) *Transport {
t := &Transport{
mocks: mocks,
httpClient: httpClient,
debugEnabled: debugEnabled,
mockResponseDelayEnabled: mockResponseDelayEnabled,
observers: observers,
apiTest: apiTest,
}
if httpClient != nil {
t.nativeTransport = httpClient.Transport
} else {
t.nativeTransport = http.DefaultTransport
}
return t
}
type unmatchedMockError struct {
errors map[int][]error
}
func newUnmatchedMockError() *unmatchedMockError {
return &unmatchedMockError{
errors: map[int][]error{},
}
}
func (u *unmatchedMockError) addErrors(mockNumber int, errors ...error) *unmatchedMockError {
u.errors[mockNumber] = append(u.errors[mockNumber], errors...)
return u
}
// Error implementation of in-built error human readable string function
func (u *unmatchedMockError) Error() string {
var strBuilder strings.Builder
strBuilder.WriteString("received request did not match any mocks\n\n")
for _, mockNumber := range u.orderedMockKeys() {
strBuilder.WriteString(fmt.Sprintf("Mock %d mismatches:\n", mockNumber))
for _, err := range u.errors[mockNumber] {
strBuilder.WriteString("• ")
strBuilder.WriteString(err.Error())
strBuilder.WriteString("\n")
}
strBuilder.WriteString("\n")
}
return strBuilder.String()
}
func (u *unmatchedMockError) orderedMockKeys() []int {
var mockKeys []int
for mockKey := range u.errors {
mockKeys = append(mockKeys, mockKey)
}
sort.Ints(mockKeys)
return mockKeys
}
// 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, matchErrors error) {
if r.debugEnabled {
defer func() {
debugMock(mockResponse, req)
}()
}
if r.observers != nil && len(r.observers) > 0 {
defer func() {
for _, observe := range r.observers {
observe(mockResponse, req, r.apiTest)
}
}()
}
matchedResponse, matchErrors := matches(req, r.mocks)
if matchErrors == nil {
res := buildResponseFromMock(matchedResponse)
res.Request = req
if matchedResponse.timeout {
return nil, timeoutError{}
}
if r.mockResponseDelayEnabled && matchedResponse.fixedDelayMillis > 0 {
time.Sleep(time.Duration(matchedResponse.fixedDelayMillis) * time.Millisecond)
}
return res, nil
}
if r.debugEnabled {
fmt.Printf("failed to match mocks. Errors: %s\n", matchErrors)
}
return nil, matchErrors
}
func debugMock(res *http.Response, req *http.Request) {
requestDump, err := httputil.DumpRequestOut(req, true)
if err == nil {
debugLog(requestDebugPrefix, "request to mock", string(requestDump))
}
if res != nil {
responseDump, err := httputil.DumpResponse(res, true)
if err == nil {
debugLog(responseDebugPrefix, "response from mock", string(responseDump))
}
} else {
debugLog(responseDebugPrefix, "response from mock", "")
}
}
// 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
}
func buildResponseFromMock(mockResponse *MockResponse) *http.Response {
if mockResponse == nil {
return nil
}
mockResponse.mu.RLock() // Lock for reading
contentTypeHeader := mockResponse.headers["Content-Type"]
var contentType string
// if the content type isn't set and the body contains json, set content type as json
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]
}
}
mockResponse.mu.RUnlock() // Unlock after reading
res := &http.Response{
Body: ioutil.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 != "" {
mockResponse.mu.Lock() // Lock for writing
res.Header.Set("Content-Type", contentType)
mockResponse.mu.Unlock() // Unlock after writing
}
return res
}
// Mock represents the entire interaction for a mock to be used for testing
type Mock struct {
m *sync.Mutex
isUsed bool
request *MockRequest
response *MockResponse
httpClient *http.Client
debugStandalone bool
times int
timesSet bool
anyTimesSet bool
}
// 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
}
func (m *Mock) copy() *Mock {
newMock := *m
newMock.m = &sync.Mutex{}
req := *m.request
newMock.request = &req
newMock.response = m.response.deepCopy()
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
basicAuthUsername string
basicAuthPassword 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
body string
bodyRegexp string
matchers []Matcher
}
// 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
mu sync.RWMutex // Add a mutex for thread-safe access
}
func (r *MockResponse) deepCopy() *MockResponse {
newResponse := &MockResponse{
timeout: r.timeout,
headers: make(map[string][]string),
cookies: make([]*Cookie, len(r.cookies)),
body: r.body,
statusCode: r.statusCode,
fixedDelayMillis: r.fixedDelayMillis,
mu: sync.RWMutex{},
}
for k, v := range r.headers {
newHeader := make([]string, len(v))
copy(newHeader, v)
newResponse.headers[k] = newHeader
}
for i, cookie := range r.cookies {
newCookie := *cookie
newResponse.cookies[i] = &newCookie
}
return newResponse
}
// StandaloneMocks for using mocks outside of API tests context
type StandaloneMocks struct {
mocks []*Mock
httpClient *http.Client
debug bool
}
// NewStandaloneMocks create a series of StandaloneMocks
func NewStandaloneMocks(mocks ...*Mock) *StandaloneMocks {
return &StandaloneMocks{
mocks: mocks,
}
}
// 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 = true
return r
}
// End finalises 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{
m: &sync.Mutex{},
times: 1,
}
mock.request = &MockRequest{
mock: mock,
headers: map[string][]string{},
formData: map[string][]string{},
query: map[string][]string{},
matchers: defaultMatchers,
}
mock.response = &MockResponse{
mock: mock,
headers: map[string][]string{},
}
return mock
}
// Debug is used to set debug mode for mocks in standalone mode.
// This is overridden by the debug setting in the `APITest` struct
func (m *Mock) Debug() *Mock {
m.debugStandalone = true
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...))
}
// 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
}
// 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...))
}
func (m *Mock) parseUrl(u string) {
parsed, err := url.Parse(u)
if err != nil {
panic(err)
}
m.request.url = parsed
}
// Method configures mock to match given http method
func (m *Mock) Method(method string) *MockRequest {
m.request.method = method
return m.request
}
func matches(req *http.Request, mocks []*Mock) (*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.isUsed && mock.anyTimesSet == false {
mock.m.Unlock()
continue
}
errs := mock.Matches(req)
if len(errs) == 0 {
mock.isUsed = true
mock.m.Unlock()
return mock.response, nil
}
mockError = mockError.addErrors(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.bodyRegexp = 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 := ioutil.ReadFile(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.basicAuthUsername = username
r.basicAuthPassword = 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 {
for _, val := range v {
r.query[k] = append(r.query[k], val)
}
}
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 finalises 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 {
r.mu.Lock()
defer r.mu.Unlock()
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 {
r.mu.Lock()
defer r.mu.Unlock()
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.mu.Lock()
defer r.mu.Unlock()
r.cookies = append(r.cookies, cookie...)
return r
}
// Cookie respond with the given cookie
func (r *MockResponse) Cookie(name, value string) *MockResponse {
r.mu.Lock()
defer r.mu.Unlock()
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.mu.Lock()
defer r.mu.Unlock()
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 {
r.mu.Lock()
defer r.mu.Unlock()
b, err := ioutil.ReadFile(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.mu.Lock()
defer r.mu.Unlock()
r.statusCode = statusCode
return r
}
// FixedDelay will return the response after the given number of milliseconds.
// APITest::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, if AnyTimes is set this has no effect
func (r *MockResponse) Times(times int) *MockResponse {
r.mu.Lock()
defer r.mu.Unlock()
r.mock.times = times
r.mock.timesSet = true
return r
}
// AnyTimes respond any number of times
func (r *MockResponse) AnyTimes() *MockResponse {
r.mock.anyTimesSet = true
return r
}
// End finalise the response definition phase in order for the mock to be used
func (r *MockResponse) End() *Mock {
return r.mock
}
// EndStandalone finalises the response definition of standalone mocks
func (r *MockResponse) EndStandalone(other ...*Mock) func() {
transport := newTransport(
append([]*Mock{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
var pathMatcher Matcher = func(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)
})
}
var hostMatcher Matcher = func(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)
})
}
var methodMatcher Matcher = func(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)
}
var schemeMatcher Matcher = func(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)
})
}
var headerMatcher = func(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
}
var basicAuthMatcher = func(req *http.Request, spec *MockRequest) error {
if spec.basicAuthUsername == "" {
return nil
}
username, password, ok := req.BasicAuth()
if !ok {
return errors.New("request did not contain valid HTTP Basic Authentication string")
}
if spec.basicAuthUsername != username {
return fmt.Errorf("basic auth request username '%s' did not match mock username '%s'",
username, spec.basicAuthUsername)
}
if spec.basicAuthPassword != password {
return fmt.Errorf("basic auth request password '%s' did not match mock password '%s'",
password, spec.basicAuthPassword)
}
return nil
}
var headerPresentMatcher = func(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
}
var headerNotPresentMatcher = func(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
}
var queryParamMatcher = func(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
}
var queryPresentMatcher = func(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
}
var queryNotPresentMatcher = func(req *http.Request, spec *MockRequest) error {
for _, query := range spec.queryNotPresent {
if req.URL.Query().Get(query) != "" {
return fmt.Errorf("unexpected query param '%s' present", query)
}
}
return nil
}
var formDataMatcher = func(req *http.Request, spec *MockRequest) error {
mockFormData := spec.formData
for key, values := range mockFormData {
r := copyHttpRequest(req)
err := r.ParseForm()
if err != nil {
return errors.New("unable to parse form data")
}
receivedFormData := r.PostForm
if _, ok := receivedFormData[key]; !ok {
return fmt.Errorf("not all of received form data values %s matched expected mock form data values %s",
receivedFormData, mockFormData)
}
found := 0
for _, field := range receivedFormData[key] {
for _, value := range values {
match, err := regexp.MatchString(value, field)
if err != nil {
return fmt.Errorf("failed to parse regexp for form data %s with value %s", key, value)
}
if match {