-
Notifications
You must be signed in to change notification settings - Fork 0
/
expectation.go
549 lines (459 loc) · 14.3 KB
/
expectation.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
package httpmock
import (
"encoding/json"
"fmt"
"net/http"
"net/textproto"
"os"
"path/filepath"
"regexp"
"sync"
"time"
"go.nhat.io/wait"
"go.nhat.io/httpmock/matcher"
"go.nhat.io/httpmock/must"
"go.nhat.io/httpmock/planner"
"go.nhat.io/httpmock/value"
)
// Expectation sets the expectations for a http request.
//
// nolint: interfacebloat
type Expectation interface {
// WithHeader sets an expected header of the given request.
//
// Server.Expect(httpmock.MethodGet, "/path").
// WithHeader("foo", "bar")
WithHeader(header string, value any) Expectation
// WithHeaders sets a list of expected headers of the given request.
//
// Server.Expect(httpmock.MethodGet, "/path").
// WithHeaders(map[string]any{"foo": "bar"})
WithHeaders(headers map[string]any) Expectation
// WithBody sets the expected body of the given request. It could be []byte, string, fmt.Stringer, or a Matcher.
//
// Server.Expect(httpmock.MethodGet, "/path").
// WithBody("hello world!")
WithBody(body any) Expectation
// WithBodyf formats according to a format specifier and use it as the expected body of the given request.
//
// Server.Expect(httpmock.MethodGet, "/path").
// WithBodyf("hello %s", "john)
WithBodyf(format string, args ...any) Expectation
// WithBodyJSON marshals the object and use it as the expected body of the given request.
//
// Server.Expect(httpmock.MethodGet, "/path").
// WithBodyJSON(map[string]string{"foo": "bar"})
//
WithBodyJSON(v any) Expectation
// ReturnCode sets the response code.
//
// Server.Expect(httpmock.MethodGet, "/path").
// ReturnCode(httpmock.StatusBadRequest)
ReturnCode(code int) Expectation
// ReturnHeader sets a response header.
//
// Server.Expect(httpmock.MethodGet, "/path").
// ReturnHeader("foo", "bar")
ReturnHeader(header, value string) Expectation
// ReturnHeaders sets a list of response headers.
//
// Server.Expect(httpmock.MethodGet, "/path").
// ReturnHeaders(httpmock.Header{"foo": "bar"})
ReturnHeaders(headers Header) Expectation
// Return sets the result to return to client.
//
// Server.Expect(httpmock.MethodGet, "/path").
// Return("hello world!")
Return(v any) Expectation
// Returnf formats according to a format specifier and use it as the result to return to client.
//
// Server.Expect(httpmock.MethodGet, "/path").
// Returnf("hello %s", "john")
Returnf(format string, args ...any) Expectation
// ReturnJSON marshals the object using json.Marshal and uses it as the result to return to client.
//
// Server.Expect(httpmock.MethodGet, "/path").
// ReturnJSON(map[string]string{"foo": "bar"})
ReturnJSON(body any) Expectation
// ReturnFile reads the file using ioutil.ReadFile and uses it as the result to return to client.
//
// Server.Expect(httpmock.MethodGet, "/path").
// ReturnFile("resources/fixtures/response.txt")
ReturnFile(filePath string) Expectation
// Run sets the handler to handle a given request.
//
// Server.Expect(httpmock.MethodGet, "/path").
// Run(func(*http.Request) ([]byte, error) {
// return []byte("hello world!"), nil
// })
Run(handle func(r *http.Request) ([]byte, error)) Expectation
// Once indicates that the mock should only return the value once.
//
// Server.Expect(http.MethodGet, "/path").
// Return("hello world!").
// Once()
Once() Expectation
// Twice indicates that the mock should only return the value twice.
//
// Server.Expect(http.MethodGet, "/path").
// Return("hello world!").
// Twice()
Twice() Expectation
// UnlimitedTimes indicates that the mock should return the value at least once and there is no max limit in the
// number of return.
//
// Server.Expect(http.MethodGet, "/path").
// Return("hello world!").
// UnlimitedTimes()
UnlimitedTimes() Expectation
// Times indicates that the mock should only return the indicated number of times.
//
// Server.Expect(http.MethodGet, "/path").
// Return("hello world!").
// Times(5)
Times(i uint) Expectation
// WaitUntil sets the channel that will block the mocked return until its closed
// or a message is received.
//
// Server.Expect(http.MethodGet, "/path").
// WaitUntil(time.After(time.Second)).
// Return("hello world!")
WaitUntil(w <-chan time.Time) Expectation
// After sets how long to block until the call returns.
//
// Server.Expect(http.MethodGet, "/path").
// After(time.Second).
// Return("hello world!")
After(d time.Duration) Expectation
}
// ExpectationHandler handles the expectation.
type ExpectationHandler interface {
Handle(w http.ResponseWriter, r *http.Request, defaultHeaders map[string]string) error
}
var (
_ Expectation = (*requestExpectation)(nil)
_ planner.Expectation = (*requestExpectation)(nil)
)
// requestExpectation is an expectation.
type requestExpectation struct {
locker sync.Locker
waiter wait.Waiter
// requestMethod is the expected HTTP requestMethod of the given request.
requestMethod string
// requestURIMatcher is the expected HTTP request URI of the given request.
// The uri does not need to be exactly same but satisfies the matcher.
requestURIMatcher matcher.Matcher
// requestHeaderMatcher is a list of expected headers of the given request.
requestHeaderMatcher matcher.HeaderMatcher
// requestBodyMatcher is the expected body of the given request.
requestBodyMatcher *matcher.BodyMatcher
// responseCode is the response code when the request is handled.
responseCode int
// responseHeader is a list of response headers to be sent to client when the request is handled.
responseHeader Header
handle func(r *http.Request) ([]byte, error)
fulfilledTimes uint
repeatTimes uint
}
func (e *requestExpectation) lock() {
e.locker.Lock()
}
func (e *requestExpectation) unlock() {
e.locker.Unlock()
}
func (e *requestExpectation) Method() string {
e.lock()
defer e.unlock()
return e.requestMethod
}
func (e *requestExpectation) URIMatcher() matcher.Matcher {
e.lock()
defer e.unlock()
return e.requestURIMatcher
}
func (e *requestExpectation) HeaderMatcher() matcher.HeaderMatcher {
e.lock()
defer e.unlock()
return e.requestHeaderMatcher
}
func (e *requestExpectation) BodyMatcher() *matcher.BodyMatcher {
e.lock()
defer e.unlock()
return e.requestBodyMatcher
}
func (e *requestExpectation) RemainTimes() uint {
e.lock()
defer e.unlock()
return e.repeatTimes
}
func (e *requestExpectation) Fulfilled() {
e.lock()
defer e.unlock()
if e.repeatTimes > 0 {
e.repeatTimes--
}
e.fulfilledTimes++
}
func (e *requestExpectation) FulfilledTimes() uint {
e.lock()
defer e.unlock()
return e.fulfilledTimes
}
// WithHeader sets an expected header of the given request.
//
// Server.Expect(httpmock.MethodGet, "/path").
// WithHeader("foo", "bar")
//
//nolint:unparam
func (e *requestExpectation) WithHeader(header string, val any) Expectation {
e.lock()
defer e.unlock()
if e.requestHeaderMatcher == nil {
e.requestHeaderMatcher = matcher.HeaderMatcher{}
}
e.requestHeaderMatcher[header] = matcher.Match(val)
return e
}
// WithHeaders sets a list of expected headers of the given request.
//
// Server.Expect(httpmock.MethodGet, "/path").
// WithHeaders(map[string]any{"foo": "bar"})
func (e *requestExpectation) WithHeaders(headers map[string]any) Expectation {
for header, val := range headers {
e.WithHeader(header, val)
}
return e
}
// WithBody sets the expected body of the given request. It could be []byte, string, fmt.Stringer, or a Matcher.
//
// Server.Expect(httpmock.MethodGet, "/path").
// WithBody("hello world!")
func (e *requestExpectation) WithBody(body any) Expectation {
e.lock()
defer e.unlock()
e.requestBodyMatcher = matchBody(body)
return e
}
// WithBodyf formats according to a format specifier and use it as the expected body of the given request.
//
// Server.Expect(httpmock.MethodGet, "/path").
// WithBodyf("hello %s", "john)
func (e *requestExpectation) WithBodyf(format string, args ...any) Expectation {
return e.WithBody(fmt.Sprintf(format, args...))
}
// WithBodyJSON marshals the object and use it as the expected body of the given request.
//
// Server.Expect(httpmock.MethodGet, "/path").
// WithBodyJSON(map[string]string{"foo": "bar"})
//
// nolint:unparam
func (e *requestExpectation) WithBodyJSON(v any) Expectation {
body, err := json.Marshal(v)
if err != nil {
panic(err)
}
return e.WithBody(matcher.JSON(string(body)))
}
// ReturnCode sets the response code.
//
// Server.Expect(httpmock.MethodGet, "/path").
// ReturnCode(httpmock.StatusBadRequest)
func (e *requestExpectation) ReturnCode(code int) Expectation {
e.lock()
defer e.unlock()
e.responseCode = code
return e
}
// ReturnHeader sets a response header.
//
// Server.Expect(httpmock.MethodGet, "/path").
// ReturnHeader("foo", "bar")
func (e *requestExpectation) ReturnHeader(header, value string) Expectation {
e.lock()
defer e.unlock()
if e.responseHeader == nil {
e.responseHeader = map[string]string{}
}
e.responseHeader[header] = value
return e
}
// ReturnHeaders sets a list of response headers.
//
// Server.Expect(httpmock.MethodGet, "/path").
// ReturnHeaders(httpmock.Header{"foo": "bar"})
func (e *requestExpectation) ReturnHeaders(headers Header) Expectation {
e.lock()
defer e.unlock()
e.responseHeader = headers
return e
}
// Return sets the result to return to client.
//
// Server.Expect(httpmock.MethodGet, "/path").
// Return("hello world!")
func (e *requestExpectation) Return(v any) Expectation {
body := []byte(value.String(v))
return e.Run(func(*http.Request) ([]byte, error) {
return body, nil
})
}
// Returnf formats according to a format specifier and use it as the result to return to client.
//
// Server.Expect(httpmock.MethodGet, "/path").
// Returnf("hello %s", "john")
func (e *requestExpectation) Returnf(format string, args ...any) Expectation {
return e.Return(fmt.Sprintf(format, args...))
}
// ReturnJSON marshals the object using json.Marshal and uses it as the result to return to client.
//
// Server.Expect(httpmock.MethodGet, "/path").
// ReturnJSON(map[string]string{"foo": "bar"})
func (e *requestExpectation) ReturnJSON(body any) Expectation {
return e.Run(func(*http.Request) ([]byte, error) {
return json.Marshal(body)
})
}
// ReturnFile reads the file using ioutil.ReadFile and uses it as the result to return to client.
//
// Server.Expect(httpmock.MethodGet, "/path").
// ReturnFile("resources/fixtures/response.txt")
//
// nolint:unparam
func (e *requestExpectation) ReturnFile(filePath string) Expectation {
filePath = filepath.Join(".", filepath.Clean(filePath))
_, err := os.Stat(filePath)
must.NotFail(err)
return e.Run(func(*http.Request) ([]byte, error) {
// nolint:gosec // filePath is cleaned above.
return os.ReadFile(filePath)
})
}
// Run sets the handler to handle a given request.
//
// Server.Expect(httpmock.MethodGet, "/path").
// Run(func(*http.Request) ([]byte, error) {
// return []byte("hello world!"), nil
// })
func (e *requestExpectation) Run(handle func(r *http.Request) ([]byte, error)) Expectation {
e.lock()
defer e.unlock()
e.handle = handle
return e
}
// Once indicates that the mock should only return the value once.
//
// Server.Expect(http.MethodGet, "/path").
// Return("hello world!").
// Once()
func (e *requestExpectation) Once() Expectation {
return e.Times(1)
}
// Twice indicates that the mock should only return the value twice.
//
// Server.Expect(http.MethodGet, "/path").
// Return("hello world!").
// Twice()
func (e *requestExpectation) Twice() Expectation {
return e.Times(2)
}
// UnlimitedTimes indicates that the mock should return the value at least once and there is no max limit in the number
// of return.
//
// Server.Expect(http.MethodGet, "/path").
// Return("hello world!").
// UnlimitedTimes()
func (e *requestExpectation) UnlimitedTimes() Expectation {
return e.Times(0)
}
// Times indicates that the mock should only return the indicated number of times.
//
// Server.Expect(http.MethodGet, "/path").
// Return("hello world!").
// Times(5)
func (e *requestExpectation) Times(i uint) Expectation {
e.lock()
defer e.unlock()
e.repeatTimes = i
return e
}
// WaitUntil sets the channel that will block the mocked return until its closed
// or a message is received.
//
// Server.Expect(http.MethodGet, "/path").
// WaitUntil(time.After(time.Second)).
// Return("hello world!")
//
// nolint: unparam
func (e *requestExpectation) WaitUntil(w <-chan time.Time) Expectation {
e.lock()
defer e.unlock()
e.waiter = wait.ForSignal(w)
return e
}
// After sets how long to block until the call returns.
//
// Server.Expect(http.MethodGet, "/path").
// After(time.Second).
// Return("hello world!")
//
// nolint: unparam
func (e *requestExpectation) After(d time.Duration) Expectation {
e.lock()
defer e.unlock()
e.waiter = wait.ForDuration(d)
return e
}
// Handle handles the HTTP request.
func (e *requestExpectation) Handle(w http.ResponseWriter, req *http.Request, defaultHeaders map[string]string) error {
e.lock()
defer e.unlock()
if err := e.waiter.Wait(req.Context()); err != nil {
return err
}
body, err := e.handle(req)
if err != nil {
_ = FailResponse(w, err.Error()) // nolint: errcheck
return err
}
for key, val := range mergeHeaders(e.responseHeader, defaultHeaders) {
w.Header().Set(key, val)
}
w.WriteHeader(e.responseCode)
_, err = w.Write(body)
return err
}
// newRequestExpectation creates a new request expectation.
func newRequestExpectation(method string, requestURI any) *requestExpectation {
return &requestExpectation{
locker: &sync.Mutex{},
requestMethod: method,
responseCode: http.StatusOK,
requestURIMatcher: matcher.Match(requestURI),
repeatTimes: 0,
waiter: wait.NoWait,
handle: func(r *http.Request) ([]byte, error) {
return nil, nil
},
}
}
func matchBody(v any) *matcher.BodyMatcher {
switch v := v.(type) {
case matcher.Matcher,
func() matcher.Matcher,
*regexp.Regexp:
return matcher.Body(v)
}
return matcher.Body(value.String(v))
}
// mergeHeaders merges a list of headers with some defaults. If a default header appears in the given headers, it
// will not be merged, no matter what the value is.
func mergeHeaders(headers, defaultHeaders Header) Header {
result := make(Header, len(headers)+len(defaultHeaders))
for header, val := range defaultHeaders {
result[textproto.CanonicalMIMEHeaderKey(header)] = val
}
for header, val := range headers {
result[textproto.CanonicalMIMEHeaderKey(header)] = val
}
return result
}