forked from jarcoal/httpmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
385 lines (356 loc) · 12.6 KB
/
response.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
package httpmock
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/jarcoal/httpmock/internal"
)
// Responder is a callback that receives an http request and returns
// a mocked response.
type Responder func(*http.Request) (*http.Response, error)
func (r Responder) times(name string, n int, fn ...func(...interface{})) Responder {
count := 0
return func(req *http.Request) (*http.Response, error) {
count++
if count > n {
err := internal.StackTracer{
Err: fmt.Errorf("Responder not found for %s %s (coz %s and already called %d times)", req.Method, req.URL, name, count),
}
if len(fn) > 0 {
err.CustomFn = fn[0]
}
return nil, err
}
return r(req)
}
}
// Times returns a Responder callable n times before returning an
// error. If the Responder is called more than n times and fn is
// passed and non-nil, it acts as the fn parameter of
// NewNotFoundResponder, allowing to dump the stack trace to localize
// the origin of the call.
// import (
// "testing"
// "github.com/jarcoal/httpmock"
// )
// ...
// func TestMyApp(t *testing.T) {
// ...
// // This responder is callable 3 times, then an error is returned and
// // the stacktrace of the call logged using t.Log()
// httpmock.RegisterResponder("GET", "/foo/bar",
// httpmock.NewStringResponder(200, "{}").Times(3, t.Log),
// )
func (r Responder) Times(n int, fn ...func(...interface{})) Responder {
return r.times("Times", n, fn...)
}
// Once returns a new Responder callable once before returning an
// error. If the Responder is called 2 or more times and fn is passed
// and non-nil, it acts as the fn parameter of NewNotFoundResponder,
// allowing to dump the stack trace to localize the origin of the
// call.
// import (
// "testing"
// "github.com/jarcoal/httpmock"
// )
// ...
// func TestMyApp(t *testing.T) {
// ...
// // This responder is callable only once, then an error is returned and
// // the stacktrace of the call logged using t.Log()
// httpmock.RegisterResponder("GET", "/foo/bar",
// httpmock.NewStringResponder(200, "{}").Once(t.Log),
// )
func (r Responder) Once(fn ...func(...interface{})) Responder {
return r.times("Once", 1, fn...)
}
// Trace returns a new Responder that allows to easily trace the calls
// of the original Responder using fn. It can be used in conjunction
// with the testing package as in the example below with the help of
// (*testing.T).Log method:
// import (
// "testing"
// "github.com/jarcoal/httpmock"
// )
// ...
// func TestMyApp(t *testing.T) {
// ...
// httpmock.RegisterResponder("GET", "/foo/bar",
// httpmock.NewStringResponder(200, "{}").Trace(t.Log),
// )
func (r Responder) Trace(fn func(...interface{})) Responder {
return func(req *http.Request) (*http.Response, error) {
resp, err := r(req)
return resp, internal.StackTracer{
CustomFn: fn,
Err: err,
}
}
}
// ResponderFromResponse wraps an *http.Response in a Responder.
//
// Be careful, except for responses generated by httpmock
// (NewStringResponse and NewBytesResponse functions) for which there
// is no problems, it is the caller responsibility to ensure the
// response body can be read several times and concurrently if needed,
// as it is shared among all Responder returned responses.
//
// For home-made responses, NewRespBodyFromString and
// NewRespBodyFromBytes functions can be used to produce response
// bodies that can be read several times and concurrently.
func ResponderFromResponse(resp *http.Response) Responder {
return func(req *http.Request) (*http.Response, error) {
res := *resp
// Our stuff: generate a new io.ReadCloser instance sharing the same buffer
if body, ok := resp.Body.(*dummyReadCloser); ok {
res.Body = body.copy()
}
res.Request = req
return &res, nil
}
}
// NewErrorResponder creates a Responder that returns an empty request and the
// given error. This can be used to e.g. imitate more deep http errors for the
// client.
func NewErrorResponder(err error) Responder {
return func(req *http.Request) (*http.Response, error) {
return nil, err
}
}
// NewNotFoundResponder creates a Responder typically used in
// conjunction with RegisterNoResponder() function and testing
// package, to be proactive when a Responder is not found. fn is
// called with a unique string parameter containing the name of the
// missing route and the stack trace to localize the origin of the
// call. If fn returns (= if it does not panic), the responder returns
// an error of the form: "Responder not found for GET http://foo.bar/path".
// Note that fn can be nil.
//
// It is useful when writing tests to ensure that all routes have been
// mocked.
//
// Example of use:
// import (
// "testing"
// "github.com/jarcoal/httpmock"
// )
// ...
// func TestMyApp(t *testing.T) {
// ...
// // Calls testing.Fatal with the name of Responder-less route and
// // the stack trace of the call.
// httpmock.RegisterNoResponder(httpmock.NewNotFoundResponder(t.Fatal))
//
// Will abort the current test and print something like:
// transport_test.go:735: Called from net/http.Get()
// at /go/src/github.com/jarcoal/httpmock/transport_test.go:714
// github.com/jarcoal/httpmock.TestCheckStackTracer()
// at /go/src/testing/testing.go:865
// testing.tRunner()
// at /go/src/runtime/asm_amd64.s:1337
func NewNotFoundResponder(fn func(...interface{})) Responder {
return func(req *http.Request) (*http.Response, error) {
return nil, internal.StackTracer{
CustomFn: fn,
Err: fmt.Errorf("Responder not found for %s %s", req.Method, req.URL),
}
}
}
// NewStringResponse creates an *http.Response with a body based on
// the given string. Also accepts an http status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewStringResponse(200, httpmock.File("body.txt").String())
func NewStringResponse(status int, body string) *http.Response {
return &http.Response{
Status: strconv.Itoa(status),
StatusCode: status,
Body: NewRespBodyFromString(body),
Header: http.Header{},
ContentLength: -1,
}
}
// NewStringResponder creates a Responder from a given body (as a string) and status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewStringResponder(200, httpmock.File("body.txt").String())
func NewStringResponder(status int, body string) Responder {
return ResponderFromResponse(NewStringResponse(status, body))
}
// NewBytesResponse creates an *http.Response with a body based on the
// given bytes. Also accepts an http status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewBytesResponse(200, httpmock.File("body.raw").Bytes())
func NewBytesResponse(status int, body []byte) *http.Response {
return &http.Response{
Status: strconv.Itoa(status),
StatusCode: status,
Body: NewRespBodyFromBytes(body),
Header: http.Header{},
ContentLength: -1,
}
}
// NewBytesResponder creates a Responder from a given body (as a byte
// slice) and status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewBytesResponder(200, httpmock.File("body.raw").Bytes())
func NewBytesResponder(status int, body []byte) Responder {
return ResponderFromResponse(NewBytesResponse(status, body))
}
// NewJsonResponse creates an *http.Response with a body that is a
// json encoded representation of the given interface{}. Also accepts
// an http status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewJsonResponse(200, httpmock.File("body.json"))
func NewJsonResponse(status int, body interface{}) (*http.Response, error) { // nolint: golint
encoded, err := json.Marshal(body)
if err != nil {
return nil, err
}
response := NewBytesResponse(status, encoded)
response.Header.Set("Content-Type", "application/json")
return response, nil
}
// NewJsonResponder creates a Responder from a given body (as an
// interface{} that is encoded to json) and status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewJsonResponder(200, httpmock.File("body.json"))
func NewJsonResponder(status int, body interface{}) (Responder, error) { // nolint: golint
resp, err := NewJsonResponse(status, body)
if err != nil {
return nil, err
}
return ResponderFromResponse(resp), nil
}
// NewJsonResponderOrPanic is like NewJsonResponder but panics in case of error.
//
// It simplifies the call of RegisterResponder, avoiding the use of a
// temporary variable and an error check, and so can be used as
// NewStringResponder or NewBytesResponder in such context:
// httpmock.RegisterResponder(
// "GET",
// "/test/path",
// httpmock.NewJSONResponderOrPanic(200, &MyBody),
// )
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewJsonResponderOrPanic(200, httpmock.File("body.json"))
func NewJsonResponderOrPanic(status int, body interface{}) Responder { // nolint: golint
responder, err := NewJsonResponder(status, body)
if err != nil {
panic(err)
}
return responder
}
// NewXmlResponse creates an *http.Response with a body that is an xml
// encoded representation of the given interface{}. Also accepts an
// http status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewXmlResponse(200, httpmock.File("body.xml"))
func NewXmlResponse(status int, body interface{}) (*http.Response, error) { // nolint: golint
var (
encoded []byte
err error
)
if f, ok := body.(File); ok {
encoded, err = f.bytes()
} else {
encoded, err = xml.Marshal(body)
}
if err != nil {
return nil, err
}
response := NewBytesResponse(status, encoded)
response.Header.Set("Content-Type", "application/xml")
return response, nil
}
// NewXmlResponder creates a Responder from a given body (as an
// interface{} that is encoded to xml) and status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewXmlResponder(200, httpmock.File("body.xml"))
func NewXmlResponder(status int, body interface{}) (Responder, error) { // nolint: golint
resp, err := NewXmlResponse(status, body)
if err != nil {
return nil, err
}
return ResponderFromResponse(resp), nil
}
// NewXmlResponderOrPanic is like NewXmlResponder but panics in case of error.
//
// It simplifies the call of RegisterResponder, avoiding the use of a
// temporary variable and an error check, and so can be used as
// NewStringResponder or NewBytesResponder in such context:
// httpmock.RegisterResponder(
// "GET",
// "/test/path",
// httpmock.NewXmlResponderOrPanic(200, &MyBody),
// )
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewXmlResponderOrPanic(200, httpmock.File("body.xml"))
func NewXmlResponderOrPanic(status int, body interface{}) Responder { // nolint: golint
responder, err := NewXmlResponder(status, body)
if err != nil {
panic(err)
}
return responder
}
// NewRespBodyFromString creates an io.ReadCloser from a string that
// is suitable for use as an http response body.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewRespBodyFromString(httpmock.File("body.txt").String())
func NewRespBodyFromString(body string) io.ReadCloser {
return &dummyReadCloser{orig: body}
}
// NewRespBodyFromBytes creates an io.ReadCloser from a byte slice
// that is suitable for use as an http response body.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewRespBodyFromBytes(httpmock.File("body.txt").Bytes())
func NewRespBodyFromBytes(body []byte) io.ReadCloser {
return &dummyReadCloser{orig: body}
}
type dummyReadCloser struct {
orig interface{} // string or []byte
body io.ReadSeeker // instanciated on demand from orig
}
// copy returns a new instance resetting d.body to nil.
func (d *dummyReadCloser) copy() *dummyReadCloser {
return &dummyReadCloser{orig: d.orig}
}
// setup ensures d.body is correctly initialized.
func (d *dummyReadCloser) setup() {
if d.body == nil {
switch body := d.orig.(type) {
case string:
d.body = strings.NewReader(body)
case []byte:
d.body = bytes.NewReader(body)
}
}
}
func (d *dummyReadCloser) Read(p []byte) (n int, err error) {
d.setup()
n, err = d.body.Read(p)
if err == io.EOF {
d.body.Seek(0, 0) // nolint: errcheck
}
return n, err
}
func (d *dummyReadCloser) Close() error {
d.setup()
d.body.Seek(0, 0) // nolint: errcheck
return nil
}