-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransport.go
397 lines (318 loc) · 10.4 KB
/
transport.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
package httpmitm
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"testing"
)
// MitmTransport implements http.RoundTripper, which hijacks http request issued by an http.Client with mitm scheme.
// It defferrs to the registered responders instead of making a real http request.
type MitmTransport struct {
mux sync.Mutex
testing *testing.T
stubs map[string]*Responser // responders registered for MITM request
stubbed bool // indicate whether http.DefaultTransport stubbed?
paused bool // indicate whether current mocked transport paused?
mocked bool // indicate whether current chain finished?
lastMockedMethod string
lastMockedURL string
lastMockedMatcher RequestMatcher
lastMockedTimes int
}
// NewMitmTransport creates MitmTransport for stubs && mocks.
func NewMitmTransport() *MitmTransport {
return &MitmTransport{
stubs: make(map[string]*Responser),
stubbed: false,
paused: false,
mocked: false,
lastMockedMethod: "",
lastMockedURL: "",
lastMockedMatcher: DefaultMatcher,
lastMockedTimes: MockDefaultTimes,
}
}
// StubDefaultTransport stubs http.DefaultTransport with MitmTransport.
func (mitm *MitmTransport) StubDefaultTransport(t *testing.T) *MitmTransport {
mitm.mux.Lock()
defer mitm.mux.Unlock()
mitm.testing = t
if !mitm.stubbed {
mitm.stubbed = true
http.DefaultTransport = mitm
}
return mitm
}
// UnstubDefaultTransport restores http.DefaultTransport
func (mitm *MitmTransport) UnstubDefaultTransport() {
mitm.mux.Lock()
defer mitm.mux.Unlock()
if mitm.stubbed {
mitm.stubbed = false
http.DefaultTransport = httpDefaultResponder
}
// is times missing match?
if !mitm.paused {
errlogs := []string{}
for key, stubs := range mitm.stubs {
for path, mocker := range stubs.Mocks() {
if mocker.IsTimesExceed() {
key = strings.Replace(key, MockScheme, mocker.Scheme(), 1)
expected, invoked := mocker.Times()
errlogs = append(errlogs, DefaultLeaddingSpace+"Error Trace: %s:%d\n"+DefaultLeaddingSpace+"Error: Expected "+key+path+" with "+fmt.Sprintf("%d", expected)+" times, but got "+fmt.Sprintf("%d", invoked)+" times\n")
}
}
}
if len(errlogs) > 0 {
pcs := make([]uintptr, 20)
max := runtime.Callers(2, pcs)
frames := runtime.CallersFrames(pcs[:max])
var (
frame runtime.Frame
more bool
)
for {
tmpframe, tmpmore := frames.Next()
if strings.HasPrefix(tmpframe.Function, "testing.") {
if !tmpmore {
frame, more = tmpframe, tmpmore
}
break
}
frame, more = tmpframe, tmpmore
if !more {
break
}
}
// format errlogs
for i, errlog := range errlogs {
errlogs[i] = fmt.Sprintf(errlog, filepath.Base(frame.File), frame.Line)
}
fmt.Printf("--- FAIL: %s\n%s\n", filepath.Base(frame.Function), strings.Join(errlogs, "\n"))
mitm.testing.Fail()
}
}
mitm.stubs = make(map[string]*Responser)
mitm.testing = nil
}
// MockRequest stubs resource with request method
func (mitm *MitmTransport) MockRequest(method, rawurl string) *MitmTransport {
mitm.mux.Lock()
defer mitm.mux.Unlock()
key, err := mitm.calcRequestKey(method, rawurl)
if err != nil {
panic(err.Error())
}
// adjust empty responder with RefusedResponser for prev un-finished mocks
if mitm.mocked == false && mitm.lastMockedMethod != "" && mitm.lastMockedURL != "" {
lastKey, _ := mitm.calcRequestKey(mitm.lastMockedMethod, mitm.lastMockedURL)
if lastKey == key {
return mitm
}
if mitm.stubs[lastKey] == nil {
mitm.stubs[lastKey] = RefusedResponser
}
}
mitm.mocked = false
mitm.lastMockedMethod = method
mitm.lastMockedURL = rawurl
mitm.lastMockedMatcher = DefaultMatcher
mitm.lastMockedTimes = MockDefaultTimes
return mitm
}
// ByMatcher apply custom matcher for current stub
func (mitm *MitmTransport) ByMatcher(matcher func(r *http.Request, urlobj *url.URL) bool) *MitmTransport {
mitm.mux.Lock()
defer mitm.mux.Unlock()
mitm.ensureChained()
// modify mocked matcher
lastKey, _ := mitm.calcRequestKey(mitm.lastMockedMethod, mitm.lastMockedURL)
responser, ok := mitm.stubs[lastKey]
if ok {
responser.SetMatcherByRawURL(mitm.lastMockedURL, matcher)
} else {
mitm.lastMockedMatcher = matcher
}
return mitm
}
// Times apply custom match times for current stub
func (mitm *MitmTransport) Times(i int) *MitmTransport {
mitm.mux.Lock()
defer mitm.mux.Unlock()
mitm.ensureChained()
if i < 0 && i != MockUnlimitedTimes {
panic(ErrTimes.Error())
}
// modify mocked times
lastKey, _ := mitm.calcRequestKey(mitm.lastMockedMethod, mitm.lastMockedURL)
responser, ok := mitm.stubs[lastKey]
if ok {
responser.SetExpectedTimesByRawURL(mitm.lastMockedURL, i)
} else {
mitm.lastMockedTimes = i
}
return mitm
}
// AnyTimes apply ulimited times for current stub
func (mitm *MitmTransport) AnyTimes() *MitmTransport {
return mitm.Times(MockUnlimitedTimes)
}
// WithResponser apply http.RoundTripper for current stub
func (mitm *MitmTransport) WithResponser(responder http.RoundTripper) *MitmTransport {
mitm.mux.Lock()
defer mitm.mux.Unlock()
mitm.ensureChained()
key, _ := mitm.calcRequestKey(mitm.lastMockedMethod, mitm.lastMockedURL)
if mitm.stubs[key] == nil || mitm.stubs[key] == RefusedResponser {
mitm.stubs[key] = NewResponser(responder, mitm.lastMockedURL, mitm.lastMockedTimes)
} else {
mitm.stubs[key].New(responder, mitm.lastMockedURL, mitm.lastMockedTimes)
}
mitm.stubs[key].SetMatcherByRawURL(mitm.lastMockedURL, mitm.lastMockedMatcher)
mitm.mocked = true
return mitm
}
// WithResponse apply http text/palin response for current stub
func (mitm *MitmTransport) WithResponse(code int, header http.Header, body interface{}) *MitmTransport {
return mitm.WithResponser(NewResponder(code, header, body))
}
// WithJsonResponse apply http application/json response for current stub
func (mitm *MitmTransport) WithJsonResponse(code int, header http.Header, body interface{}) *MitmTransport {
return mitm.WithResponser(NewJsonResponder(code, header, body))
}
// WithXmlResponse apply http text/xml response for current stub
func (mitm *MitmTransport) WithXmlResponse(code int, header http.Header, body interface{}) *MitmTransport {
return mitm.WithResponser(NewXmlResponder(code, header, body))
}
// WithCalleeResponse apply custom func for current stub
func (mitm *MitmTransport) WithCalleeResponse(callee func(r *http.Request) (code int, header http.Header, body io.Reader, err error)) *MitmTransport {
return mitm.WithResponser(NewCalleeResponder(callee))
}
// RoundTrip implments http.RoundTripper
func (mitm *MitmTransport) RoundTrip(r *http.Request) (*http.Response, error) {
// direct connection for none mitm scheme
if strings.ToLower(r.URL.Scheme) != MockScheme {
return httpDefaultResponder.RoundTrip(r)
}
response, ok := mitm.stubs[mitm.normalizeKey(r.Method, MockScheme, r.URL.Host)]
if !ok {
return RefusedResponser.RoundTrip(r)
}
mocker := response.Find(r.URL.Path)
if mocker == nil {
return NotFoundResponser.RoundTrip(r)
}
// direct connection for paused
if mitm.paused {
// adjust request url scheme
r.URL.Scheme = mocker.Scheme()
resp, err := httpDefaultResponder.RoundTrip(r)
if err != nil {
return resp, err
}
// try to write back response data if response code is 2xx or equal to expected
responder, ok := mocker.responder.(*Responder)
if !ok || (resp.StatusCode/100 != 2 && resp.StatusCode != responder.code) {
return resp, err
}
var (
data []byte
)
switch resp.Header.Get("Content-Encoding") {
case "gzip":
gzipReader, gzipErr := gzip.NewReader(resp.Body)
if gzipErr != nil {
return resp, gzipErr
}
data, err = ioutil.ReadAll(gzipReader)
if err != nil {
return resp, err
}
gzipReader.Close()
// reset response header with new data
resp.Header.Del("Content-Encoding")
resp.Header.Set("Content-Length", strconv.FormatInt(int64(len(data)), 10))
default:
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return resp, err
}
resp.Body.Close()
}
// rewrite response body for client
resp.Body = ioutil.NopCloser(bytes.NewReader(data))
// invoke testdata writer
if werr := responder.Write(r.Method, r.URL, data); werr != nil {
mitm.testing.Logf("Response writes %s %s with: %v", r.Method, r.URL.String(), werr)
} else {
mitm.testing.Logf("Response write %s %s OK!", r.Method, r.URL.String())
}
return resp, err
}
return mocker.RoundTrip(r)
}
// TODO: what's behavior of request timeout?
func (mitm *MitmTransport) CancelRequest(r *http.Request) {
}
// Pause pauses all stubs of all requests
func (mitm *MitmTransport) Pause() {
mitm.mux.Lock()
if mitm.stubbed {
mitm.paused = true
}
mitm.mux.Unlock()
}
// Resume resumes all paused stubs of all requests
func (mitm *MitmTransport) Resume() {
mitm.mux.Lock()
if mitm.stubbed {
mitm.paused = false
}
mitm.mux.Unlock()
}
// PrettyPrint dumps MitmTransport in well foramt.
func (mitm *MitmTransport) PrettyPrint() {
buf := bytes.NewBuffer(nil)
buf.WriteString("stubs<map[string]&httpmitm.Responder>{\n")
for key, stub := range mitm.stubs {
buf.WriteString(` "` + key + `": &httpmitm.Responder{` + "\n")
buf.WriteString(` mocks<map[string]&httpmitm.Mocker>{` + "\n")
for subkey, mock := range stub.mocks {
tmp := fmt.Sprintf("%#v", mock)
tmp = strings.Replace(tmp, `sync.Mutex{state:0, sema:0x0}`, "sync.Mutex()", -1)
tmp = strings.Replace(tmp, "{", "{\n ", -1)
tmp = strings.Replace(tmp, ", ", ",\n ", -1)
tmp = strings.Replace(tmp, "}", "\n }", -1)
tmp = strings.Replace(tmp, "sync.Mutex()", `sync.Mutex{state:0, sema:0x0}`, -1)
buf.WriteString(` "` + subkey + `": ` + tmp + "\n")
}
buf.WriteString(` }` + "\n")
buf.WriteString(` }` + "\n")
}
buf.WriteString("\n}\n")
println(buf.String())
}
func (mitm *MitmTransport) ensureChained() {
if mitm.lastMockedMethod == "" || mitm.lastMockedURL == "" {
panic(ErrInvocation.Error())
}
}
func (mitm *MitmTransport) calcRequestKey(method, rawurl string) (string, error) {
urlobj, err := url.Parse(rawurl)
if err != nil {
return "", err
}
return mitm.normalizeKey(method, MockScheme, urlobj.Host), nil
}
func (mitm *MitmTransport) normalizeKey(method, scheme, host string) string {
return strings.ToUpper(method) + " " + strings.TrimRight(strings.ToLower(scheme+"://"+host), "/")
}