@@ -173,6 +173,7 @@ var simpleSliceOfStringsRequest = `{
173
173
]
174
174
}
175
175
`
176
+
176
177
var simpleSliceOfStringsCassette = `{
177
178
"strings": [
178
179
"3",
@@ -182,6 +183,162 @@ var simpleSliceOfStringsCassette = `{
182
183
}
183
184
`
184
185
186
+ var ludacrisBodyRequest = `{
187
+ "payload": {
188
+ "artists": [
189
+ {
190
+ "name": "Ludacris",
191
+ "age": 45,
192
+ "songs": ["Ludacris", "Ludacris", "Ludacris"]
193
+ }
194
+ }
195
+ }
196
+ }
197
+ `
198
+
199
+ var jdillaBodyCassette = `{
200
+ "payload": {
201
+ "artists": [
202
+ {
203
+ "name": "Jdilla",
204
+ "age": 54,
205
+ "songs": ["this", "is", "jdilla"]
206
+ }
207
+ ]
208
+ }
209
+ }
210
+ `
211
+
212
+ var requestInstanceSettings = `{
213
+ "settings": [
214
+ {
215
+ "name": "max_connections",
216
+ "value": "200"
217
+ },
218
+ {
219
+ "name": "max_parallel_workers",
220
+ "value": "2"
221
+ },
222
+ {
223
+ "name": "effective_cache_size",
224
+ "value": "1300"
225
+ },
226
+ {
227
+ "name": "maintenance_work_mem",
228
+ "value": "150"
229
+ },
230
+ {
231
+ "name": "max_parallel_workers_per_gather",
232
+ "value": "2"
233
+ },
234
+ {
235
+ "name": "work_mem",
236
+ "value": "4"
237
+ }
238
+ ]
239
+ }
240
+ `
241
+
242
+ var cassetteInstanceSettings = `{
243
+ "settings": [
244
+ {
245
+ "name": "maintenance_work_mem",
246
+ "value": "150"
247
+ },
248
+ {
249
+ "name": "effective_cache_size",
250
+ "value": "1300"
251
+ },
252
+ {
253
+ "name": "work_mem",
254
+ "value": "4"
255
+ },
256
+ {
257
+ "name": "max_parallel_workers",
258
+ "value": "2"
259
+ },
260
+ {
261
+ "name": "max_parallel_workers_per_gather",
262
+ "value": "2"
263
+ },
264
+ {
265
+ "name": "max_connections",
266
+ "value": "200"
267
+ }
268
+ ]
269
+ }
270
+ `
271
+
272
+ var objectBodyRequest = `{
273
+ "Id": "MyPolicy",
274
+ "Statement": [
275
+ {
276
+ "Action": [
277
+ "s3:ListBucket",
278
+ "s3:GetObject"
279
+ ],
280
+ "Effect": "Allow",
281
+ "Principal": {
282
+ "SCW": "*"
283
+ },
284
+ "Resource": [
285
+ "tf-tests-scw-obp-basic-4713290580220176511",
286
+ "tf-tests-scw-obp-basic-4713290580220176511/*"
287
+ ],
288
+ "Sid": "GrantToEveryone"
289
+ },
290
+ {
291
+ "Action": [
292
+ "s3:ListBucket",
293
+ "s3:GetObject"
294
+ ],
295
+ "Effect": "Allow",
296
+ "Principal": {
297
+ "SCW": "*"
298
+ },
299
+ "Sid": "GrantToEveryone",
300
+ "project_id": "1234567890"
301
+ }
302
+ ],
303
+ "Version": "2012-10-17"
304
+ }
305
+ `
306
+
307
+ var objectBodyCassette = `{
308
+ "Id": "MyPolicy",
309
+ "Statement": [
310
+ {
311
+ "Action": [
312
+ "s3:ListBucket",
313
+ "s3:GetObject"
314
+ ],
315
+ "Effect": "Allow",
316
+ "Principal": {
317
+ "SCW": "*"
318
+ },
319
+ "Sid": "GrantToEveryone",
320
+ "Resource": [
321
+ "tf-tests-scw-obp-basic-1234567890",
322
+ "tf-tests-scw-obp-basic-1234567890/*"
323
+ ]
324
+ },
325
+ {
326
+ "Action": [
327
+ "s3:ListBucket",
328
+ "s3:GetObject"
329
+ ],
330
+ "Effect": "Allow",
331
+ "Principal": {
332
+ "SCW": "*"
333
+ },
334
+ "Sid": "GrantToEveryone",
335
+ "project_id": "9876543210"
336
+ }
337
+ ],
338
+ "Version": "2012-10-17"
339
+ }
340
+ `
341
+
185
342
// we don't use httptest.NewRequest because it does not set the GetBody func
186
343
func newRequest (method , url string , body io.Reader ) * http.Request {
187
344
req , err := http .NewRequestWithContext (context .Background (), method , url , body )
@@ -197,7 +354,7 @@ var testBodyMatcherCases = []struct {
197
354
cassetteBody * cassette.Request
198
355
shouldMatch bool
199
356
}{
200
- // create bar compare with foo
357
+ // bar does not match foo
201
358
{
202
359
requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (barMemberCreationBody )),
203
360
cassetteBody : & cassette.Request {
@@ -208,7 +365,7 @@ var testBodyMatcherCases = []struct {
208
365
},
209
366
shouldMatch : false ,
210
367
},
211
- // create bar compare with bar
368
+ // bar matches bar
212
369
{
213
370
requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (barMemberCreationBody )),
214
371
cassetteBody : & cassette.Request {
@@ -231,8 +388,7 @@ var testBodyMatcherCases = []struct {
231
388
shouldMatch : true ,
232
389
},
233
390
// patch secret with nested slices of map[string]interface{} in different order
234
- // we cannot user deep equal because the order of the slices is different although the values are the same
235
- // it is not possible to sort them because they are not comparable (map[string]interface{})
391
+ // in such simple cases we flatten the maps and compare them in same order
236
392
{
237
393
requestBody : newRequest (http .MethodPatch , "https://api.scaleway.com/secrets/v1/secrets/123" , strings .NewReader (secretPatchBodyRequest )),
238
394
cassetteBody : & cassette.Request {
@@ -243,7 +399,29 @@ var testBodyMatcherCases = []struct {
243
399
},
244
400
shouldMatch : true ,
245
401
},
246
- // compare nested slices of different integers
402
+ // flatten the maps and compare them in same order
403
+ {
404
+ requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (requestInstanceSettings )),
405
+ cassetteBody : & cassette.Request {
406
+ URL : "https://api.scaleway.com/iam/v1alpha1/users" ,
407
+ Method : http .MethodPost ,
408
+ Body : cassetteInstanceSettings ,
409
+ ContentLength : int64 (len (cassetteInstanceSettings )),
410
+ },
411
+ shouldMatch : true ,
412
+ },
413
+ // complex slice of maps case
414
+ {
415
+ requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/policies" , strings .NewReader (objectBodyRequest )),
416
+ cassetteBody : & cassette.Request {
417
+ URL : "https://api.scaleway.com/iam/v1alpha1/policies" ,
418
+ Method : http .MethodPost ,
419
+ Body : objectBodyCassette ,
420
+ ContentLength : int64 (len (objectBodyCassette )),
421
+ },
422
+ shouldMatch : true ,
423
+ },
424
+ // compare slices of different integers
247
425
{
248
426
requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (integertestBodyRequest )),
249
427
cassetteBody : & cassette.Request {
@@ -254,7 +432,7 @@ var testBodyMatcherCases = []struct {
254
432
},
255
433
shouldMatch : false ,
256
434
},
257
- // compare nested slices of same integers in different order
435
+ // compare slices of same integers in different order
258
436
{
259
437
requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (integerBodyRequestOutOfOrder )),
260
438
cassetteBody : & cassette.Request {
@@ -265,7 +443,7 @@ var testBodyMatcherCases = []struct {
265
443
},
266
444
shouldMatch : true ,
267
445
},
268
- // compare nested slices of slices of strings
446
+ // compare slices of slices of strings in different order
269
447
{
270
448
requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (nestedSliceOfSlicesRequest )),
271
449
cassetteBody : & cassette.Request {
@@ -286,7 +464,7 @@ var testBodyMatcherCases = []struct {
286
464
},
287
465
shouldMatch : true ,
288
466
},
289
- // compare simple slice of strings
467
+ // compare slices of strings in different order
290
468
{
291
469
requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (simpleSliceOfStringsRequest )),
292
470
cassetteBody : & cassette.Request {
@@ -297,13 +475,26 @@ var testBodyMatcherCases = []struct {
297
475
},
298
476
shouldMatch : true ,
299
477
},
478
+ // ludacris does not match jdilla
479
+ {
480
+ requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (ludacrisBodyRequest )),
481
+ cassetteBody : & cassette.Request {
482
+ URL : "https://api.scaleway.com/iam/v1alpha1/users" ,
483
+ Method : http .MethodPost ,
484
+ Body : jdillaBodyCassette ,
485
+ ContentLength : int64 (len (jdillaBodyCassette )),
486
+ },
487
+ shouldMatch : false ,
488
+ },
300
489
}
301
490
302
491
func TestCassetteMatcher (t * testing.T ) {
303
492
for i , test := range testBodyMatcherCases {
304
493
shouldMatch := acctest .CassetteMatcher (test .requestBody , * test .cassetteBody )
305
494
if shouldMatch != test .shouldMatch {
306
495
t .Errorf ("test %d: expected %v, got %v" , i , test .shouldMatch , shouldMatch )
496
+ t .Errorf ("requestBody: %s" , test .requestBody .Body )
497
+ t .Errorf ("cassetteBody: %s" , test .cassetteBody .Body )
307
498
}
308
499
}
309
500
}
0 commit comments