@@ -121,7 +121,7 @@ func TestLoadInteractionPlainTextConstraints(t *testing.T) {
121
121
}
122
122
123
123
func TestLoadInteractionJSONConstraints (t * testing.T ) {
124
- arrMatchersNotPresent := `{
124
+ nestedArrMatchersNotPresent := `{
125
125
"description": "A request to create an address",
126
126
"request": {
127
127
"method": "POST",
@@ -143,7 +143,7 @@ func TestLoadInteractionJSONConstraints(t *testing.T) {
143
143
}
144
144
}
145
145
}`
146
- arrMatcherPresent :=
146
+ nestedArrMatcherPresent :=
147
147
`{
148
148
"description": "A request to create an address",
149
149
"request": {
@@ -171,14 +171,50 @@ func TestLoadInteractionJSONConstraints(t *testing.T) {
171
171
}
172
172
}
173
173
}`
174
+ arrayOfStrings := `{
175
+ "description": "A request to create an address",
176
+ "request": {
177
+ "method": "POST",
178
+ "path": "/addresses",
179
+ "headers": {
180
+ "Content-Type": "application/json"
181
+ },
182
+ "body": ["line 1", "line 2"]
183
+ },
184
+ "response": {
185
+ "status": 200,
186
+ "headers": {
187
+ "Content-Type": "application/json"
188
+ },
189
+ "body": ["line 1", "line 2"]
190
+ }
191
+ }`
192
+ arrayOfObjects := `{
193
+ "description": "A request to create an address",
194
+ "request": {
195
+ "method": "POST",
196
+ "path": "/addresses",
197
+ "headers": {
198
+ "Content-Type": "application/json"
199
+ },
200
+ "body": [ {"key": "val"}, {"key": "val"} ]
201
+ },
202
+ "response": {
203
+ "status": 200,
204
+ "headers": {
205
+ "Content-Type": "application/json"
206
+ },
207
+ "body": [ {"key": "val"}, {"key": "val"} ]
208
+ }
209
+ }`
174
210
tests := []struct {
175
211
name string
176
212
interaction []byte
177
213
wantConstraints []interactionConstraint
178
214
}{
179
215
{
180
- name : "array and matcher not present - interactions are created per element" ,
181
- interaction : []byte (arrMatchersNotPresent ),
216
+ name : "nested array and matcher not present - interactions are created per element" ,
217
+ interaction : []byte (nestedArrMatchersNotPresent ),
182
218
wantConstraints : []interactionConstraint {
183
219
{
184
220
Path : "$.body.addressLines[0]" ,
@@ -198,8 +234,8 @@ func TestLoadInteractionJSONConstraints(t *testing.T) {
198
234
},
199
235
},
200
236
{
201
- name : "array and matcher present - interaction is not created for matched element" ,
202
- interaction : []byte (arrMatcherPresent ),
237
+ name : "nested array and matcher present - interaction is not created for matched element" ,
238
+ interaction : []byte (nestedArrMatcherPresent ),
203
239
wantConstraints : []interactionConstraint {
204
240
{
205
241
Path : "$.body.addressLines[1]" ,
@@ -213,6 +249,48 @@ func TestLoadInteractionJSONConstraints(t *testing.T) {
213
249
},
214
250
},
215
251
},
252
+ {
253
+ name : "body array and matcher not present - interactions are created per element" ,
254
+ interaction : []byte (arrayOfStrings ),
255
+ wantConstraints : []interactionConstraint {
256
+ {
257
+ Path : "$.body[0]" ,
258
+ Format : "%v" ,
259
+ Values : []interface {}{"line 1" },
260
+ },
261
+ {
262
+ Path : "$.body[1]" ,
263
+ Format : "%v" ,
264
+ Values : []interface {}{"line 2" },
265
+ },
266
+ {
267
+ Path : "$.body" ,
268
+ Format : fmtLen ,
269
+ Values : []interface {}{2 },
270
+ },
271
+ },
272
+ },
273
+ {
274
+ name : "body array of objects - interactions are created per element" ,
275
+ interaction : []byte (arrayOfObjects ),
276
+ wantConstraints : []interactionConstraint {
277
+ {
278
+ Path : "$.body[0].key" ,
279
+ Format : "%v" ,
280
+ Values : []interface {}{"val" },
281
+ },
282
+ {
283
+ Path : "$.body[1].key" ,
284
+ Format : "%v" ,
285
+ Values : []interface {}{"val" },
286
+ },
287
+ {
288
+ Path : "$.body" ,
289
+ Format : fmtLen ,
290
+ Values : []interface {}{2 },
291
+ },
292
+ },
293
+ },
216
294
}
217
295
for _ , tt := range tests {
218
296
t .Run (tt .name , func (t * testing.T ) {
@@ -362,140 +440,6 @@ func TestV3MatchingRulesLeadToCorrectConstraints(t *testing.T) {
362
440
}
363
441
}
364
442
365
- func TestLoadArrayRequestBodyInteractions (t * testing.T ) {
366
- arrayOfStrings := `{
367
- "description": "A request to create an address",
368
- "request": {
369
- "method": "POST",
370
- "path": "/addresses",
371
- "headers": {
372
- "Content-Type": "application/json"
373
- },
374
- "body": ["a", "b", "c"]
375
- },
376
- "response": {
377
- "status": 200,
378
- "headers": {
379
- "Content-Type": "application/json"
380
- },
381
- "body": ["a", "b", "c"]
382
- }
383
- }`
384
- arrayOfInts := `{
385
- "description": "A request to create an address",
386
- "request": {
387
- "method": "POST",
388
- "path": "/addresses",
389
- "headers": {
390
- "Content-Type": "application/json"
391
- },
392
- "body": [1, 2, 3]
393
- },
394
- "response": {
395
- "status": 200,
396
- "headers": {
397
- "Content-Type": "application/json"
398
- },
399
- "body": [1, 2, 3]
400
- }
401
- }`
402
- arrayOfBools := `{
403
- "description": "A request to create an address",
404
- "request": {
405
- "method": "POST",
406
- "path": "/addresses",
407
- "headers": {
408
- "Content-Type": "application/json"
409
- },
410
- "body": [true, false, true]
411
- },
412
- "response": {
413
- "status": 200,
414
- "headers": {
415
- "Content-Type": "application/json"
416
- },
417
- "body": [true, false, true]
418
- }
419
- }`
420
- arrayOfObjects := `{
421
- "description": "A request to create an address",
422
- "request": {
423
- "method": "POST",
424
- "path": "/addresses",
425
- "headers": {
426
- "Content-Type": "application/json"
427
- },
428
- "body": [ {"key": "val"}, {"key": "val"} ]
429
- },
430
- "response": {
431
- "status": 200,
432
- "headers": {
433
- "Content-Type": "application/json"
434
- },
435
- "body": [ {"key": "val"}, {"key": "val"} ]
436
- }
437
- }`
438
- arrayOfStringsWithMatcher :=
439
- `{
440
- "description": "A request to create an address",
441
- "request": {
442
- "method": "POST",
443
- "path": "/addresses",
444
- "headers": {
445
- "Content-Type": "application/json"
446
- },
447
- "body": ["a", "b", "c"],
448
- "matchingRules": {
449
- "$.body": {
450
- "match": "type"
451
- }
452
- }
453
- },
454
- "response": {
455
- "status": 200,
456
- "headers": {
457
- "Content-Type": "application/json"
458
- },
459
- "body": ["a", "b", "c"]
460
- }
461
- }`
462
-
463
- tests := []struct {
464
- name string
465
- interaction []byte
466
- }{
467
- {
468
- name : "array of strings" ,
469
- interaction : []byte (arrayOfStrings ),
470
- },
471
- {
472
- name : "array of ints" ,
473
- interaction : []byte (arrayOfInts ),
474
- },
475
- {
476
- name : "array of bools" ,
477
- interaction : []byte (arrayOfBools ),
478
- },
479
- {
480
- name : "array of objects" ,
481
- interaction : []byte (arrayOfObjects ),
482
- },
483
- {
484
- name : "array of strings with matcher" ,
485
- interaction : []byte (arrayOfStringsWithMatcher ),
486
- },
487
- }
488
-
489
- for _ , tt := range tests {
490
- t .Run (tt .name , func (t * testing.T ) {
491
- interaction , err := LoadInteraction (tt .interaction , "alias" )
492
- require .NoError (t , err , "unexpected error %v" , err )
493
-
494
- require .Empty (t , interaction .constraints , "No constraint should be added for the interaction" )
495
- })
496
- }
497
- }
498
-
499
443
func Test_parseMediaType (t * testing.T ) {
500
444
tests := []struct {
501
445
name string
0 commit comments