-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_client_mock.go
More file actions
386 lines (314 loc) · 12.3 KB
/
http_client_mock.go
File metadata and controls
386 lines (314 loc) · 12.3 KB
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
// Code generated by http://github.com/gojuno/minimock (v3.4.7). DO NOT EDIT.
package mock
//go:generate minimock -i github.com/deckhouse/module-sdk/pkg.HTTPClient -o http_client_mock.go -n HTTPClientMock -p mock
import (
"net/http"
"sync"
mm_atomic "sync/atomic"
mm_time "time"
"github.com/gojuno/minimock/v3"
)
// HTTPClientMock implements mm_pkg.HTTPClient
type HTTPClientMock struct {
t minimock.Tester
finishOnce sync.Once
funcDo func(req *http.Request) (rp1 *http.Response, err error)
funcDoOrigin string
inspectFuncDo func(req *http.Request)
afterDoCounter uint64
beforeDoCounter uint64
DoMock mHTTPClientMockDo
}
// NewHTTPClientMock returns a mock for mm_pkg.HTTPClient
func NewHTTPClientMock(t minimock.Tester) *HTTPClientMock {
m := &HTTPClientMock{t: t}
if controller, ok := t.(minimock.MockController); ok {
controller.RegisterMocker(m)
}
m.DoMock = mHTTPClientMockDo{mock: m}
m.DoMock.callArgs = []*HTTPClientMockDoParams{}
t.Cleanup(m.MinimockFinish)
return m
}
type mHTTPClientMockDo struct {
optional bool
mock *HTTPClientMock
defaultExpectation *HTTPClientMockDoExpectation
expectations []*HTTPClientMockDoExpectation
callArgs []*HTTPClientMockDoParams
mutex sync.RWMutex
expectedInvocations uint64
expectedInvocationsOrigin string
}
// HTTPClientMockDoExpectation specifies expectation struct of the HTTPClient.Do
type HTTPClientMockDoExpectation struct {
mock *HTTPClientMock
params *HTTPClientMockDoParams
paramPtrs *HTTPClientMockDoParamPtrs
expectationOrigins HTTPClientMockDoExpectationOrigins
results *HTTPClientMockDoResults
returnOrigin string
Counter uint64
}
// HTTPClientMockDoParams contains parameters of the HTTPClient.Do
type HTTPClientMockDoParams struct {
req *http.Request
}
// HTTPClientMockDoParamPtrs contains pointers to parameters of the HTTPClient.Do
type HTTPClientMockDoParamPtrs struct {
req **http.Request
}
// HTTPClientMockDoResults contains results of the HTTPClient.Do
type HTTPClientMockDoResults struct {
rp1 *http.Response
err error
}
// HTTPClientMockDoOrigins contains origins of expectations of the HTTPClient.Do
type HTTPClientMockDoExpectationOrigins struct {
origin string
originReq string
}
// Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning
// the test will fail minimock's automatic final call check if the mocked method was not called at least once.
// Optional() makes method check to work in '0 or more' mode.
// It is NOT RECOMMENDED to use this option unless you really need it, as default behaviour helps to
// catch the problems when the expected method call is totally skipped during test run.
func (mmDo *mHTTPClientMockDo) Optional() *mHTTPClientMockDo {
mmDo.optional = true
return mmDo
}
// Expect sets up expected params for HTTPClient.Do
func (mmDo *mHTTPClientMockDo) Expect(req *http.Request) *mHTTPClientMockDo {
if mmDo.mock.funcDo != nil {
mmDo.mock.t.Fatalf("HTTPClientMock.Do mock is already set by Set")
}
if mmDo.defaultExpectation == nil {
mmDo.defaultExpectation = &HTTPClientMockDoExpectation{}
}
if mmDo.defaultExpectation.paramPtrs != nil {
mmDo.mock.t.Fatalf("HTTPClientMock.Do mock is already set by ExpectParams functions")
}
mmDo.defaultExpectation.params = &HTTPClientMockDoParams{req}
mmDo.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1)
for _, e := range mmDo.expectations {
if minimock.Equal(e.params, mmDo.defaultExpectation.params) {
mmDo.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmDo.defaultExpectation.params)
}
}
return mmDo
}
// ExpectReqParam1 sets up expected param req for HTTPClient.Do
func (mmDo *mHTTPClientMockDo) ExpectReqParam1(req *http.Request) *mHTTPClientMockDo {
if mmDo.mock.funcDo != nil {
mmDo.mock.t.Fatalf("HTTPClientMock.Do mock is already set by Set")
}
if mmDo.defaultExpectation == nil {
mmDo.defaultExpectation = &HTTPClientMockDoExpectation{}
}
if mmDo.defaultExpectation.params != nil {
mmDo.mock.t.Fatalf("HTTPClientMock.Do mock is already set by Expect")
}
if mmDo.defaultExpectation.paramPtrs == nil {
mmDo.defaultExpectation.paramPtrs = &HTTPClientMockDoParamPtrs{}
}
mmDo.defaultExpectation.paramPtrs.req = &req
mmDo.defaultExpectation.expectationOrigins.originReq = minimock.CallerInfo(1)
return mmDo
}
// Inspect accepts an inspector function that has same arguments as the HTTPClient.Do
func (mmDo *mHTTPClientMockDo) Inspect(f func(req *http.Request)) *mHTTPClientMockDo {
if mmDo.mock.inspectFuncDo != nil {
mmDo.mock.t.Fatalf("Inspect function is already set for HTTPClientMock.Do")
}
mmDo.mock.inspectFuncDo = f
return mmDo
}
// Return sets up results that will be returned by HTTPClient.Do
func (mmDo *mHTTPClientMockDo) Return(rp1 *http.Response, err error) *HTTPClientMock {
if mmDo.mock.funcDo != nil {
mmDo.mock.t.Fatalf("HTTPClientMock.Do mock is already set by Set")
}
if mmDo.defaultExpectation == nil {
mmDo.defaultExpectation = &HTTPClientMockDoExpectation{mock: mmDo.mock}
}
mmDo.defaultExpectation.results = &HTTPClientMockDoResults{rp1, err}
mmDo.defaultExpectation.returnOrigin = minimock.CallerInfo(1)
return mmDo.mock
}
// Set uses given function f to mock the HTTPClient.Do method
func (mmDo *mHTTPClientMockDo) Set(f func(req *http.Request) (rp1 *http.Response, err error)) *HTTPClientMock {
if mmDo.defaultExpectation != nil {
mmDo.mock.t.Fatalf("Default expectation is already set for the HTTPClient.Do method")
}
if len(mmDo.expectations) > 0 {
mmDo.mock.t.Fatalf("Some expectations are already set for the HTTPClient.Do method")
}
mmDo.mock.funcDo = f
mmDo.mock.funcDoOrigin = minimock.CallerInfo(1)
return mmDo.mock
}
// When sets expectation for the HTTPClient.Do which will trigger the result defined by the following
// Then helper
func (mmDo *mHTTPClientMockDo) When(req *http.Request) *HTTPClientMockDoExpectation {
if mmDo.mock.funcDo != nil {
mmDo.mock.t.Fatalf("HTTPClientMock.Do mock is already set by Set")
}
expectation := &HTTPClientMockDoExpectation{
mock: mmDo.mock,
params: &HTTPClientMockDoParams{req},
expectationOrigins: HTTPClientMockDoExpectationOrigins{origin: minimock.CallerInfo(1)},
}
mmDo.expectations = append(mmDo.expectations, expectation)
return expectation
}
// Then sets up HTTPClient.Do return parameters for the expectation previously defined by the When method
func (e *HTTPClientMockDoExpectation) Then(rp1 *http.Response, err error) *HTTPClientMock {
e.results = &HTTPClientMockDoResults{rp1, err}
return e.mock
}
// Times sets number of times HTTPClient.Do should be invoked
func (mmDo *mHTTPClientMockDo) Times(n uint64) *mHTTPClientMockDo {
if n == 0 {
mmDo.mock.t.Fatalf("Times of HTTPClientMock.Do mock can not be zero")
}
mm_atomic.StoreUint64(&mmDo.expectedInvocations, n)
mmDo.expectedInvocationsOrigin = minimock.CallerInfo(1)
return mmDo
}
func (mmDo *mHTTPClientMockDo) invocationsDone() bool {
if len(mmDo.expectations) == 0 && mmDo.defaultExpectation == nil && mmDo.mock.funcDo == nil {
return true
}
totalInvocations := mm_atomic.LoadUint64(&mmDo.mock.afterDoCounter)
expectedInvocations := mm_atomic.LoadUint64(&mmDo.expectedInvocations)
return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations)
}
// Do implements mm_pkg.HTTPClient
func (mmDo *HTTPClientMock) Do(req *http.Request) (rp1 *http.Response, err error) {
mm_atomic.AddUint64(&mmDo.beforeDoCounter, 1)
defer mm_atomic.AddUint64(&mmDo.afterDoCounter, 1)
mmDo.t.Helper()
if mmDo.inspectFuncDo != nil {
mmDo.inspectFuncDo(req)
}
mm_params := HTTPClientMockDoParams{req}
// Record call args
mmDo.DoMock.mutex.Lock()
mmDo.DoMock.callArgs = append(mmDo.DoMock.callArgs, &mm_params)
mmDo.DoMock.mutex.Unlock()
for _, e := range mmDo.DoMock.expectations {
if minimock.Equal(*e.params, mm_params) {
mm_atomic.AddUint64(&e.Counter, 1)
return e.results.rp1, e.results.err
}
}
if mmDo.DoMock.defaultExpectation != nil {
mm_atomic.AddUint64(&mmDo.DoMock.defaultExpectation.Counter, 1)
mm_want := mmDo.DoMock.defaultExpectation.params
mm_want_ptrs := mmDo.DoMock.defaultExpectation.paramPtrs
mm_got := HTTPClientMockDoParams{req}
if mm_want_ptrs != nil {
if mm_want_ptrs.req != nil && !minimock.Equal(*mm_want_ptrs.req, mm_got.req) {
mmDo.t.Errorf("HTTPClientMock.Do got unexpected parameter req, expected at\n%s:\nwant: %#v\n got: %#v%s\n",
mmDo.DoMock.defaultExpectation.expectationOrigins.originReq, *mm_want_ptrs.req, mm_got.req, minimock.Diff(*mm_want_ptrs.req, mm_got.req))
}
} else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
mmDo.t.Errorf("HTTPClientMock.Do got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n",
mmDo.DoMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got))
}
mm_results := mmDo.DoMock.defaultExpectation.results
if mm_results == nil {
mmDo.t.Fatal("No results are set for the HTTPClientMock.Do")
}
return (*mm_results).rp1, (*mm_results).err
}
if mmDo.funcDo != nil {
return mmDo.funcDo(req)
}
mmDo.t.Fatalf("Unexpected call to HTTPClientMock.Do. %v", req)
return
}
// DoAfterCounter returns a count of finished HTTPClientMock.Do invocations
func (mmDo *HTTPClientMock) DoAfterCounter() uint64 {
return mm_atomic.LoadUint64(&mmDo.afterDoCounter)
}
// DoBeforeCounter returns a count of HTTPClientMock.Do invocations
func (mmDo *HTTPClientMock) DoBeforeCounter() uint64 {
return mm_atomic.LoadUint64(&mmDo.beforeDoCounter)
}
// Calls returns a list of arguments used in each call to HTTPClientMock.Do.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmDo *mHTTPClientMockDo) Calls() []*HTTPClientMockDoParams {
mmDo.mutex.RLock()
argCopy := make([]*HTTPClientMockDoParams, len(mmDo.callArgs))
copy(argCopy, mmDo.callArgs)
mmDo.mutex.RUnlock()
return argCopy
}
// MinimockDoDone returns true if the count of the Do invocations corresponds
// the number of defined expectations
func (m *HTTPClientMock) MinimockDoDone() bool {
if m.DoMock.optional {
// Optional methods provide '0 or more' call count restriction.
return true
}
for _, e := range m.DoMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
return false
}
}
return m.DoMock.invocationsDone()
}
// MinimockDoInspect logs each unmet expectation
func (m *HTTPClientMock) MinimockDoInspect() {
for _, e := range m.DoMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to HTTPClientMock.Do at\n%s with params: %#v", e.expectationOrigins.origin, *e.params)
}
}
afterDoCounter := mm_atomic.LoadUint64(&m.afterDoCounter)
// if default expectation was set then invocations count should be greater than zero
if m.DoMock.defaultExpectation != nil && afterDoCounter < 1 {
if m.DoMock.defaultExpectation.params == nil {
m.t.Errorf("Expected call to HTTPClientMock.Do at\n%s", m.DoMock.defaultExpectation.returnOrigin)
} else {
m.t.Errorf("Expected call to HTTPClientMock.Do at\n%s with params: %#v", m.DoMock.defaultExpectation.expectationOrigins.origin, *m.DoMock.defaultExpectation.params)
}
}
// if func was set then invocations count should be greater than zero
if m.funcDo != nil && afterDoCounter < 1 {
m.t.Errorf("Expected call to HTTPClientMock.Do at\n%s", m.funcDoOrigin)
}
if !m.DoMock.invocationsDone() && afterDoCounter > 0 {
m.t.Errorf("Expected %d calls to HTTPClientMock.Do at\n%s but found %d calls",
mm_atomic.LoadUint64(&m.DoMock.expectedInvocations), m.DoMock.expectedInvocationsOrigin, afterDoCounter)
}
}
// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *HTTPClientMock) MinimockFinish() {
m.finishOnce.Do(func() {
if !m.minimockDone() {
m.MinimockDoInspect()
}
})
}
// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *HTTPClientMock) MinimockWait(timeout mm_time.Duration) {
timeoutCh := mm_time.After(timeout)
for {
if m.minimockDone() {
return
}
select {
case <-timeoutCh:
m.MinimockFinish()
return
case <-mm_time.After(10 * mm_time.Millisecond):
}
}
}
func (m *HTTPClientMock) minimockDone() bool {
done := true
return done &&
m.MinimockDoDone()
}