-
Notifications
You must be signed in to change notification settings - Fork 0
/
verification.go
399 lines (363 loc) · 11.3 KB
/
verification.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
package mock_server_client
import (
"encoding/json"
"fmt"
"regexp"
"strings"
)
type bodyDecoder func(interface{}) error
type assertion struct {
bodyDecoder bodyDecoder
requireBodyAssertion bool
headers map[string]string
headersRegexp map[string]*regexp.Regexp
noHeaders map[string]struct{}
queryParams map[string]string
queryParamRegexp map[string]*regexp.Regexp
noQueryParams map[string]struct{}
path string
requirePathAssertion bool
}
// NewAssertion creates new assertion to be checked on MockServer.Verify...
func NewAssertion() *assertion {
return &assertion{}
}
// WithJsonBody setup HTTP request JSON body unmarshaler. The expectedBody will be prefilled on MockServer.Verify...
// and can be checked during testing.
// For example:
// b := SomeBody{}
// expectation := serverMock.On(http.SomeMethod, "/some/endpoint").
// AssertionAtCall(0, msc.NewAssertion().WithJsonBody(&b)
// someService.Process(...)
// ...
// serverMock.Verify(ctx, t)
// assert.Equal(t, "some-value", b.SomeField)
func (a *assertion) WithJsonBody(expectedBody interface{}) *assertion {
a.bodyDecoder = func(actualBody interface{}) error {
switch body := actualBody.(type) {
case []byte:
if !json.Valid(body) {
return fmt.Errorf("request %s was invalid json", string(body))
}
err := json.Unmarshal(body, expectedBody)
if err != nil {
return err
}
case string:
if !json.Valid([]byte(body)) {
return fmt.Errorf("request %s was invalid json", body)
}
err := json.Unmarshal([]byte(body), expectedBody)
if err != nil {
return err
}
default:
m := map[string]interface{}{}
bytes, err := json.Marshal(actualBody)
if err != nil {
return fmt.Errorf("unable to marshal request %v: %w", actualBody, err)
}
err = json.Unmarshal(bytes, &m)
if err != nil {
return fmt.Errorf("unable to unmarshal request %v: %w", actualBody, err)
}
switch m["type"] {
case "JSON":
detectedBody, ok := m["json"]
if !ok {
return fmt.Errorf("no string parameters in json [%v]", actualBody)
}
bytes, err := json.Marshal(detectedBody)
if err != nil {
return fmt.Errorf("unable to marshal request %v: %w", detectedBody, err)
}
err = json.Unmarshal(bytes, expectedBody)
if err != nil {
return fmt.Errorf("unable to unmarshal request %v: %w", detectedBody, err)
}
default:
return fmt.Errorf("unknown json type, expected one of [%v]; got %v", []string{"JSON"}, m["type"])
}
}
return nil
}
a.requireBodyAssertion = true
return a
}
// WithFormURLEncodedBody setup HTTP request form body unmarshaler. The expectedBody will be prefilled on MockServer.Verify...
// and can be checked during testing.
func (a *assertion) WithFormURLEncodedBody(expectedBody map[string][]string) *assertion {
a.bodyDecoder = func(actualBody interface{}) error {
m := map[string]interface{}{}
bytes, err := json.Marshal(actualBody)
if err != nil {
return fmt.Errorf("unable to marshal request %v: %w", actualBody, err)
}
err = json.Unmarshal(bytes, &m)
if err != nil {
return fmt.Errorf("unable to unmarshal request %v: %w", actualBody, err)
}
switch m["type"] {
case "STRING":
line, ok := m["string"]
if !ok {
return fmt.Errorf("no string parameters in form [%v]", actualBody)
}
pairs := strings.Split(line.(string), "&")
for _, pair := range pairs {
kv := strings.Split(pair, "=")
if len(kv) != 2 {
return fmt.Errorf("form parameter violates requirements %v", kv)
}
params, ok := expectedBody[kv[0]]
if !ok {
params = []string{}
}
params = append(params, kv[1])
expectedBody[kv[0]] = params
}
default:
return fmt.Errorf("unknown form type, expected one of [%v]; got %v", []string{"STRING"}, m["type"])
}
return nil
}
a.requireBodyAssertion = true
return a
}
// WithPlainTextBody setup HTTP request plain text body unmarshaler. The expectedBody will be prefilled on MockServer.Verify...
// and can be checked during testing.
func (a *assertion) WithPlainTextBody(expectedBody *string) *assertion {
a.bodyDecoder = func(actualBody interface{}) error {
switch body := actualBody.(type) {
case []byte:
*expectedBody = string(body)
case string:
*expectedBody = body
default:
return fmt.Errorf("expected body can not be interpretade as string %v", actualBody)
}
return nil
}
a.requireBodyAssertion = true
return a
}
// WithNoBody setup HTTP requests with no body. The expectation will fail on MockServer.Verify...
// if system sends request with body.
func (a *assertion) WithNoBody() *assertion {
a.bodyDecoder = func(actualBody interface{}) error {
if actualBody != nil {
return fmt.Errorf("expected no body, but got %v", actualBody)
}
return nil
}
a.requireBodyAssertion = true
return a
}
// WithHeaders sets required headers which sent within HTTP request to mock server.
// Sets exactly those headers which were passed to the method.
// Verifies exact match of header value.
func (a *assertion) WithHeaders(h map[string]string) *assertion {
a.headers = h
return a
}
// AddHeaders adds required headers which sent within HTTP request to mock server.
// Verifies exact match of header value.
func (a *assertion) AddHeaders(h map[string]string) *assertion {
if a.headers == nil {
return a.WithHeaders(h)
}
for k, v := range h {
a.headers[k] = v
}
return a
}
// AddHeader adds a single header which sent within HTTP request to mock server.
// Verifies exact match of header value.
func (a *assertion) AddHeader(key, value string) *assertion {
if a.headers == nil {
return a.WithHeaders(map[string]string{key: value})
}
a.headers[key] = value
return a
}
// AddHeaderRegexp adds a single header which sent within HTTP request to mock server.
// Verifies header by regexp.
func (a *assertion) AddHeaderRegexp(key, value string) *assertion {
if a.headersRegexp == nil {
a.headersRegexp = map[string]*regexp.Regexp{}
}
a.headersRegexp[key] = regexp.MustCompile(value)
return a
}
// WithNoHeader verifies that the particular header was not sent to mock server app withing particular request.
func (a *assertion) WithNoHeader(key string) *assertion {
if a.noHeaders == nil {
a.noHeaders = map[string]struct{}{}
}
a.noHeaders[key] = struct{}{}
return a
}
// WithQueryParameters sets required query parameters which sent within HTTP request to mock server.
// Sets exactly those query parameters that have to be validated.
// Verifies exact match of query parameters.
func (a *assertion) WithQueryParameters(p map[string]string) *assertion {
a.queryParams = p
return a
}
// AddQueryParameters adds required query parameters which sent within HTTP request to mock server.
// Verifies exact match of query parameters.
func (a *assertion) AddQueryParameters(p map[string]string) *assertion {
if a.queryParams == nil {
return a.WithQueryParameters(p)
}
for k, v := range p {
a.queryParams[k] = v
}
return a
}
// AddQueryParameter adds a single query parameter which sent within HTTP request to mock server.
// Verifies exact match of query parameters.
func (a *assertion) AddQueryParameter(key, value string) *assertion {
if a.queryParams == nil {
return a.WithQueryParameters(map[string]string{key: value})
}
a.queryParams[key] = value
return a
}
// AddQueryParamRegexp adds a single query parameter which sent within HTTP request to mock server.
// Verifies query parameter by regexp.
func (a *assertion) AddQueryParamRegexp(key, value string) *assertion {
if a.queryParamRegexp == nil {
a.queryParamRegexp = map[string]*regexp.Regexp{}
}
a.queryParamRegexp[key] = regexp.MustCompile(value)
return a
}
// WithNoQueryParameter verifies that the particular query parameter was not sent to mock server app withing particular request.
func (a *assertion) WithNoQueryParameter(key string) *assertion {
if a.noQueryParams == nil {
a.noQueryParams = map[string]struct{}{}
}
a.noQueryParams[key] = struct{}{}
return a
}
// WithPath verifies exact endpoint path which was called on mock server app withing particular request.
func (a *assertion) WithPath(path string) *assertion {
a.path = path
a.requirePathAssertion = true
return a
}
type verification struct {
path string
queryParams map[string][]string
headers map[string]interface{}
body interface{}
}
func (v *verification) assertBody(decoder bodyDecoder) error {
return decoder(v.body)
}
func (v *verification) assertHeader(key string, value string) error {
actualValue, ok := v.headers[key]
if !ok {
return fmt.Errorf("no expected header: %s", key)
}
switch av := actualValue.(type) {
case []interface{}:
for _, s := range av {
if s == value {
return nil
}
}
return fmt.Errorf("for header %s expected value %s; actual values %v", key, value, av)
case interface{}:
if av == value {
return nil
}
return fmt.Errorf("for header %s expected value %s; actual value %s", key, value, av)
default:
return fmt.Errorf("expected header %s is not a string %+v", key, av)
}
}
func (v *verification) assertHeaderRegexp(key string, r *regexp.Regexp) error {
actualValue, ok := v.headers[key]
if !ok {
return fmt.Errorf("no expected query parameter: %s", key)
}
switch av := actualValue.(type) {
case string:
if r.MatchString(av) {
return nil
}
return fmt.Errorf("header parameter %s : %s does not mathc pattern: %s", key, av, r.String())
case []string:
for _, s := range av {
if r.MatchString(s) {
return nil
}
}
return fmt.Errorf("for header %s : %v no one mathces pattern: %s", key, actualValue, r.String())
case []interface{}:
for _, h := range av {
header, ok := h.(string)
if !ok {
continue
}
if r.MatchString(header) {
return nil
}
}
return fmt.Errorf("for header %s : %v no one mathces pattern: %s", key, actualValue, r.String())
case interface{}:
header, ok := av.(string)
if ok && r.MatchString(header) {
return nil
}
return fmt.Errorf("for header %s : %v no one mathces pattern: %s", key, actualValue, r.String())
default:
return fmt.Errorf("unable validate header %s by regexp %s as header values are not string %v", key, r.String(), actualValue)
}
}
func (v *verification) assertNoHeader(key string) error {
val, ok := v.headers[key]
if ok {
return fmt.Errorf("unexpected header found %v for key '%s'", val, key)
}
return nil
}
func (v *verification) assertQueryParameter(key string, value string) error {
actualValue, ok := v.queryParams[key]
if !ok {
return fmt.Errorf("no expected query parameter: %s", key)
}
for _, s := range actualValue {
if s == value {
return nil
}
}
return fmt.Errorf("for query parameter %s expected value %s; actual values %v", key, value, actualValue)
}
func (v *verification) assertQueryParameterRegexp(key string, r *regexp.Regexp) error {
actualValue, ok := v.queryParams[key]
if !ok {
return fmt.Errorf("no expected query parameter: %s", key)
}
for _, s := range actualValue {
if r.MatchString(s) {
return nil
}
}
return fmt.Errorf("for query parameter %s : %v no one mathces pattern: %s", key, actualValue, r.String())
}
func (v *verification) assertNoQueryParameter(key string) error {
val, ok := v.queryParams[key]
if ok {
return fmt.Errorf("unexpected query parameter found %v for key '%s'", val, key)
}
return nil
}
func (v *verification) assertPath(path string) error {
if v.path == path {
return nil
}
return fmt.Errorf("expected path %s; actual path %s", path, v.path)
}