-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
388 lines (332 loc) · 10.9 KB
/
mock.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
package httpmock
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// tHelper is a minimal interface that expects a type to satisfy the
// [testing.TB] Helper method.
type tHelper interface {
Helper()
}
// Mock is the workhorse used to track activity of a server's requesst.
// For an example of its usage, refer to the README.
type Mock struct {
// Represents the requests that are expected to be received.
ExpectedRequests []*Request
// Holds the requests that were made to a mocked handler or server.
Requests []Request
// test is an optional variable that holds the test struct, to be used when
// an invalid mock request was made.
test mock.TestingT
mutex sync.Mutex
}
// On starts a description of an expectation of the specified [Request] being
// received.
//
// Mock.On(http.MethodDelete, "/some/path/1234")
func (m *Mock) On(method string, URL string, body []byte) *Request {
parsedURL, err := url.Parse(URL)
if err != nil {
m.fail("failed to parse url. Error: %v\n", err)
}
expected := newRequest(
m,
method,
parsedURL,
body,
)
m.mutex.Lock()
defer m.mutex.Unlock()
m.ExpectedRequests = append(m.ExpectedRequests, expected)
return expected
}
// Test sets the test struct variable of the [Mock] object.
func (m *Mock) Test(t mock.TestingT) *Mock {
m.mutex.Lock()
defer m.mutex.Unlock()
m.test = t
return m
}
// fail the current test with the given formatted format and args. In the case
// that a testing object was defined, it uses the test APIs for failing a test;
// otherwise, it uses panic.
func (m *Mock) fail(format string, args ...interface{}) {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.test == nil {
panic(fmt.Sprintf(format, args...))
}
m.test.Errorf(format, args...)
m.test.FailNow()
}
// expectedRequests provides a safe mechanism for viewing and modifying the list
// of expected [Request]'s.
func (m *Mock) expectedRequests() []*Request {
return append([]*Request{}, m.ExpectedRequests...)
}
// expectedRequests provides a safe mechanism for viewing and modifying the list
// of received [Request]'s.
func (m *Mock) requests() []Request {
return append([]Request{}, m.Requests...)
}
// findExpectedRequest finds the first [Request] that exactly matches a received
// request and does not have its repeatability disabled.
func (m *Mock) findExpectedRequest(actual *http.Request) (int, *Request) {
var expected *Request
for i, er := range m.ExpectedRequests {
if _, d := er.diff(actual); d != 0 {
continue
}
expected = er
if er.repeatability > -1 {
return i, er
}
}
return -1, expected
}
// findClosestRequest finds the first [Request] that most closely matches a
// received [http.Request].
//
// This method should only be used if there is no exact match of a received
// request to the list of expected [Request]'s. If a closest match is found,
// it is returned, along with a formatted string of the differences.
func (m *Mock) findClosestRequest(received *http.Request) (*Request, string) {
var bestMatch matchCandidate
for _, expected := range m.expectedRequests() {
errInfo, diffCount := expected.diff(received)
tempCandidate := matchCandidate{
request: expected,
mismatch: errInfo,
diffCount: diffCount,
}
if tempCandidate.isBetterMatchThan(bestMatch) {
bestMatch = tempCandidate
}
}
return bestMatch.request, bestMatch.mismatch
}
// Requested tells the mock that a [http.Request] has been received and gets a
// response to return. Panics if the request is unexpected (i.e. not preceded
// by appropriate [Mock.On] calls).
func (m *Mock) Requested(received *http.Request) *Response {
m.mutex.Lock()
receivedBody, err := SafeReadBody(received)
if err != nil {
m.mutex.Unlock()
m.fail("\nassert: httpmock: Failed to read requested body. Error: %v", err)
}
found, expected := m.findExpectedRequest(received)
if found < 0 {
// Expected request found, but has already been requested with repeatable times
if expected != nil {
m.mutex.Unlock()
m.fail("\nassert: httpmock: The request has been called over %d times.\n\tEither do one more Mock.On(%q, %q), or remove extra request.", expected.totalRequests, received.Method, received.URL.String())
}
// We have to fail here - because we don't know what to do for the
// response. This is becuase:
//
// a) This is a totally unexpected request
// b) The arguments are not what was expected, or
// c) The deveoper has forgotten to add an accompanying On...Respond pair
closest, mismatch := m.findClosestRequest(received)
m.mutex.Unlock()
if closest != nil {
tempRequest := &Request{
parent: m,
method: received.Method,
url: received.URL,
body: receivedBody,
}
tempStr := "\t" + strings.Join(strings.Split(tempRequest.String(), "\n"), "\n\t")
closestStr := "\t" + strings.Join(strings.Split(closest.String(), "\n"), "\n\t")
m.fail("\n\nhttpmock: Unexpected Request\n-----------------------------\n\n%s\n\nThe closest request I have is: \n\n%s\nDiff: %s\n",
tempStr,
closestStr,
strings.TrimSpace(mismatch),
)
} else {
m.fail("\nassert: httpmock: I don't know what to return because the request was unexpected.\n\tEither do Mock.On(%q, %q), or remove the request.\n", received.Method, received.URL.String())
}
}
if expected.repeatability == 1 {
expected.repeatability = -1
} else if expected.repeatability > 1 {
expected.repeatability--
}
expected.totalRequests++
// Add a clean request to received request list
newRequest := newRequest(m, received.Method, received.URL, receivedBody)
if expected.response != nil {
newResponse := *expected.response
newRequest.response = &newResponse
}
m.Requests = append(m.Requests, *newRequest)
m.mutex.Unlock()
return expected.response
}
// matchCandidate holds details about possible [Request] matches for a received
// [http.Request].
type matchCandidate struct {
// Matched [*Request]
request *Request
// Formatted string showing differences
mismatch string
// Number of differences between matchCandidate and received http.Request.
diffCount int
}
// isBetterMatchThan compares two matchCandidate's to determine whether the
// referenced candidate is better than the other candidate.
func (mc matchCandidate) isBetterMatchThan(other matchCandidate) bool {
if mc.request == nil {
return false
} else if other.request == nil {
return true
}
if mc.diffCount > other.diffCount {
return false
} else if mc.diffCount < other.diffCount {
return true
}
if mc.request.repeatability > 0 && other.request.repeatability <= 0 {
return true
}
return false
}
// AssertExpectations assert that everything specified with [Mock.On] and
// [Request.Respond] was in fact requested as expected. [Request]'s may have
// occurred in any order.
func (m *Mock) AssertExpectations(t mock.TestingT) bool {
if th, ok := t.(tHelper); ok {
th.Helper()
}
m.mutex.Lock()
defer m.mutex.Unlock()
var failedExpectations int
// Iterate through each expectation
expectedRequests := m.expectedRequests()
for _, er := range expectedRequests {
if satisfied, reason := m.checkExpectation(er); !satisfied {
failedExpectations++
t.Logf(reason)
}
}
if failedExpectations != 0 {
t.Errorf("FAIL: %d out of %d expectation(s) were met.\n\tThe code you are testing needs to make %d more requests(s).", len(expectedRequests)-failedExpectations, len(expectedRequests), failedExpectations)
}
return failedExpectations == 0
}
// AssertNumberOfRequests asserts that the request was made expectedRequests times.
//
// This assertion behaves a bit differently than other assertions. There are a few
// parts of the request that are ignored when calculating, including:
// - URL username/password information
// - URL query parameters
// - URL fragment
func (m *Mock) AssertNumberOfRequests(t mock.TestingT, method string, path string, expectedRequests int) bool {
if th, ok := t.(tHelper); ok {
th.Helper()
}
// Remove parts of the URL for the purposes of general comparison
u, err := url.Parse(path)
if err != nil {
t.Errorf("FAIL: unable to parse path %q into URL: %v", path, err)
t.FailNow()
}
u.User = nil
u.RawQuery = ""
u.Fragment = ""
u.RawFragment = ""
path = u.String()
m.mutex.Lock()
defer m.mutex.Unlock()
var actualRequests int
for _, actual := range m.requests() {
if actual.method != method {
continue
}
u := *actual.url
u.User = nil
u.RawQuery = ""
u.Fragment = ""
if u.String() != path {
continue
}
actualRequests++
}
return assert.Equal(t, expectedRequests, actualRequests)
}
// AssertRequested asserts that the request was received.
func (m *Mock) AssertRequested(t mock.TestingT, method string, path string, body []byte) bool {
if th, ok := t.(tHelper); ok {
th.Helper()
}
u, err := url.Parse(path)
if err != nil {
t.Errorf("FAIL: unable to parse path %q into URL: %v", path, err)
t.FailNow()
}
m.mutex.Lock()
defer m.mutex.Unlock()
if !m.checkWasRequested(method, u, body) {
tempRequest := newRequest(m, method, u, body)
v := "\t" + strings.Join(strings.Split(tempRequest.String(), "\n"), "\n\t")
return assert.Fail(
t,
"Should have requested with the given constraints",
fmt.Sprintf("Expected to have been requested with\n%v\nbut no actual requests happened", v),
)
}
return true
}
// AssertRequested asserts that the request was not received.
func (m *Mock) AssertNotRequested(t mock.TestingT, method string, path string, body []byte) bool {
if th, ok := t.(tHelper); ok {
th.Helper()
}
u, err := url.Parse(path)
if err != nil {
t.Errorf("FAIL: unable to parse path %q into URL: %v", path, err)
t.FailNow()
}
m.mutex.Lock()
defer m.mutex.Unlock()
if m.checkWasRequested(method, u, body) {
tempRequest := newRequest(m, method, u, body)
v := "\t" + strings.Join(strings.Split(tempRequest.String(), "\n"), "\n\t")
return assert.Fail(
t,
"Should not have been requested with the given constraints",
fmt.Sprintf("Expected not to have been requested with\n%v\nbut actually it was.", v),
)
}
return true
}
// checkExpectation checks whether an expected [Request] was received,
// whether it received the expected number of times.
func (m *Mock) checkExpectation(expected *Request) (bool, string) {
if (!m.checkWasRequested(expected.method, expected.url, expected.body) && expected.totalRequests == 0) || (expected.repeatability > 0) {
return false, fmt.Sprintf("FAIL:\t%s %s\n\t(%d) %s", expected.method, expected.url, len(expected.body), trimBody(expected.body))
}
return true, fmt.Sprintf("PASS:\t%s %s\n\t(%d) %s", expected.method, expected.url, len(expected.body), trimBody(expected.body))
}
// checkWasRequested checks whether a set of [Request] parameters was received.
func (m *Mock) checkWasRequested(method string, URL *url.URL, body []byte) bool {
tempReceived := &http.Request{
Method: method,
URL: URL,
Body: io.NopCloser(bytes.NewReader(body)),
}
for _, actual := range m.requests() {
if _, d := actual.diff(tempReceived); d == 0 {
return true
}
}
return false
}