forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocks_test.go
361 lines (292 loc) · 6.9 KB
/
mocks_test.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
package httpexpect
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
// mock config
func newMockConfig(r Reporter) Config {
return Config{Reporter: r}.withDefaults()
}
// mock chain
func newMockChain(t *testing.T, flag ...chainFlags) *chain {
return newChainWithDefaults("test", newMockReporter(t), flag...)
}
// mock logger
type mockLogger struct {
testing *testing.T
logged bool
lastMessage string
}
func newMockLogger(t *testing.T) *mockLogger {
return &mockLogger{testing: t}
}
func (ml *mockLogger) Logf(message string, args ...interface{}) {
ml.testing.Logf(message, args...)
ml.lastMessage = fmt.Sprintf(message, args...)
ml.logged = true
}
// mock reporter
type mockReporter struct {
testing *testing.T
reported bool
reportCalled int
reportCb func()
}
func newMockReporter(t *testing.T) *mockReporter {
return &mockReporter{testing: t}
}
func (mr *mockReporter) Errorf(message string, args ...interface{}) {
mr.testing.Logf("Fail: "+message, args...)
mr.reported = true
mr.reportCalled++
if mr.reportCb != nil {
mr.reportCb()
}
}
// mock formatter
type mockFormatter struct {
testing *testing.T
formattedSuccess int
formattedFailure int
}
func newMockFormatter(t *testing.T) *mockFormatter {
return &mockFormatter{testing: t}
}
func (mf *mockFormatter) FormatSuccess(ctx *AssertionContext) string {
mf.formattedSuccess++
return ctx.TestName
}
func (mf *mockFormatter) FormatFailure(
ctx *AssertionContext, failure *AssertionFailure,
) string {
mf.formattedFailure++
return ctx.TestName
}
// mock assertion handler
type mockAssertionHandler struct {
ctx *AssertionContext
failure *AssertionFailure
successCalled int
failureCalled int
assertionCb func()
}
func (mh *mockAssertionHandler) Success(ctx *AssertionContext) {
mh.ctx = ctx
mh.successCalled++
if mh.assertionCb != nil {
mh.assertionCb()
}
}
func (mh *mockAssertionHandler) Failure(
ctx *AssertionContext, failure *AssertionFailure,
) {
mh.ctx = ctx
mh.failure = failure
mh.failureCalled++
if mh.assertionCb != nil {
mh.assertionCb()
}
}
// mock websocket printer
type mockWebsocketPrinter struct {
isWrittenTo bool
isReadFrom bool
}
func (mp *mockWebsocketPrinter) Request(*http.Request) {
}
func (mp *mockWebsocketPrinter) Response(*http.Response, time.Duration) {
}
func (mp *mockWebsocketPrinter) WebsocketWrite(typ int, content []byte, closeCode int) {
mp.isWrittenTo = true
}
func (mp *mockWebsocketPrinter) WebsocketRead(typ int, content []byte, closeCode int) {
mp.isReadFrom = true
}
// mock websocket connection
type mockWebsocketConn struct {
subprotocol string
closeError error
readMsgErr error
writeMsgErr error
readDlError error
writeDlError error
msgType int
msg []byte
}
func (mc *mockWebsocketConn) Subprotocol() string {
return mc.subprotocol
}
func (mc *mockWebsocketConn) Close() error {
return mc.closeError
}
func (mc *mockWebsocketConn) SetReadDeadline(t time.Time) error {
return mc.readDlError
}
func (mc *mockWebsocketConn) SetWriteDeadline(t time.Time) error {
return mc.writeDlError
}
func (mc *mockWebsocketConn) ReadMessage() (messageType int, p []byte, err error) {
return mc.msgType, []byte{}, mc.readMsgErr
}
func (mc *mockWebsocketConn) WriteMessage(messageType int, data []byte) error {
return mc.writeMsgErr
}
// mock http client
type mockClient struct {
req *http.Request
resp http.Response
err error
cb func(req *http.Request)
}
func (c *mockClient) Do(req *http.Request) (*http.Response, error) {
defer func() {
if c.cb != nil {
c.cb(req)
}
}()
c.req = req
if c.err == nil {
c.resp.Header = c.req.Header
c.resp.Body = c.req.Body
return &c.resp, nil
}
return nil, c.err
}
// mock http redirecting transport
type mockRedirectTransport struct {
// assertFn asserts the HTTP request
assertFn func(*http.Request)
// redirectHTTPStatusCode indicates the HTTP status code of redirection response
redirectHTTPStatusCode int
// tripCount tracks the number of trip that has been done
//
// When tripCount < maxRedirect,
// mockTransportRedirect responses with redirectHTTPStatusCode
//
// When tripCount = maxRedirect,
// mockTransportRedirect responses with HTTP 200 OK
tripCount int
// maxRedirects indicates the number of trip that can be done for redirection.
// -1 means always redirect.
maxRedirects int
}
func newMockRedirectTransport() *mockRedirectTransport {
return &mockRedirectTransport{
redirectHTTPStatusCode: http.StatusPermanentRedirect,
maxRedirects: -1,
}
}
func (mt *mockRedirectTransport) RoundTrip(origReq *http.Request) (
*http.Response, error,
) {
mt.tripCount++
if mt.assertFn != nil {
mt.assertFn(origReq)
}
res := httptest.NewRecorder()
if mt.maxRedirects == -1 || mt.tripCount <= mt.maxRedirects {
res.Result().StatusCode = mt.redirectHTTPStatusCode
res.Result().Header.Set("Location", "/redirect")
} else {
res.Result().StatusCode = http.StatusOK
}
return res.Result(), nil
}
// mock http request factory
type mockRequestFactory struct {
lastreq *http.Request
fail bool
}
func (mf *mockRequestFactory) NewRequest(
method, urlStr string, body io.Reader) (*http.Request, error) {
if mf.fail {
return nil, errors.New("testRequestFactory")
}
mf.lastreq = httptest.NewRequest(method, urlStr, body)
return mf.lastreq, nil
}
// mock http request or response body
type mockBody struct {
reader io.Reader
readCount int
readErr error
closeCount int
closeErr error
errCount int
eofCount int
}
func newMockBody(body string) *mockBody {
return &mockBody{
reader: bytes.NewBufferString(body),
}
}
func (mb *mockBody) Read(p []byte) (int, error) {
mb.readCount++
if mb.readErr != nil {
return 0, mb.readErr
}
n, err := mb.reader.Read(p)
if err == io.EOF {
mb.eofCount++
} else if err != nil {
mb.errCount++
}
return n, err
}
func (mb *mockBody) Close() error {
mb.closeCount++
if mb.closeErr != nil {
return mb.closeErr
}
return nil
}
// mock query string encoder (query.Encoder.EncodeValues)
type mockQueryEncoder string
func (mq mockQueryEncoder) EncodeValues(key string, v *url.Values) error {
if mq == "err" {
return errors.New("encoding error")
}
v.Set(key, string(mq))
return nil
}
// mock io.Writer
type mockWriter struct {
io.Writer
err error
}
func (mw *mockWriter) Write(p []byte) (n int, err error) {
if mw.err != nil {
return 0, err
}
return mw.Writer.Write(p)
}
// mock network error
type mockNetError struct {
isTimeout bool
isTemporary bool
}
func (me *mockNetError) Error() string {
return "mock net error"
}
func (me *mockNetError) Timeout() bool {
return me.isTimeout
}
func (me *mockNetError) Temporary() bool {
return me.isTemporary
}
// mock custom error
type mockError struct{}
func (me *mockError) Error() string {
return "mock error"
}
// mock sleep function
func mockSleep(time.Duration) <-chan time.Time {
return time.After(0)
}