@@ -246,100 +246,152 @@ func TestModifiedBodyWithFirstAndLastName_ForNRequests(t *testing.T) {
246
246
the_nth_response_body_has_ (3 , "last_name" , "any" )
247
247
}
248
248
249
- func TestTextPlainContentType (t * testing.T ) {
250
- given , when , then := NewProxyStage (t )
251
-
252
- given .
253
- a_pact_that_expects_plain_text ()
254
-
255
- when .
256
- a_plain_text_request_is_sent ()
249
+ type nonJsonTestCase struct {
250
+ reqContentType string
251
+ reqBody string
252
+ respContentType string
253
+ respBody string
254
+ }
257
255
258
- then .
259
- pact_verification_is_successful ().and ().
260
- the_response_is_ (http .StatusOK ).and ().
261
- the_response_body_to_plain_text_request_is_correct ()
256
+ func createNonJsonTestCases () map [string ]nonJsonTestCase {
257
+ return map [string ]nonJsonTestCase {
258
+ // text/plain
259
+ "text/plain request and text/plain response" : {
260
+ reqContentType : "text/plain" ,
261
+ reqBody : "req text" ,
262
+ respContentType : "text/plain" ,
263
+ respBody : "resp text" ,
264
+ },
265
+ "text/plain request and application/json response" : {
266
+ reqContentType : "text/plain" ,
267
+ reqBody : "req text" ,
268
+ respContentType : "application/json" ,
269
+ respBody : `{"status":"ok"}` ,
270
+ },
271
+ // csv
272
+ "text/csv request and text/csv response" : {
273
+ reqContentType : "text/csv" ,
274
+ reqBody : "firstname,lastname\n foo,bar" ,
275
+ respContentType : "text/csv" ,
276
+ respBody : "status,name\n 200,ok" ,
277
+ },
278
+ "text/csv request and application/json response" : {
279
+ reqContentType : "text/csv" ,
280
+ reqBody : "firstname,lastname\n foo,bar" ,
281
+ respContentType : "application/json" ,
282
+ respBody : `{"status":"ok"}` ,
283
+ },
284
+ // xml
285
+ "application/xml request and text/csv response" : {
286
+ reqContentType : "application/xml" ,
287
+ reqBody : "<root><firstname>foo</firstname></root>" ,
288
+ respContentType : "application/xml" ,
289
+ respBody : "<root><status>200</status></root>" ,
290
+ },
291
+ "application/xml request and application/json response" : {
292
+ reqContentType : "application/xml" ,
293
+ reqBody : "<root><firstname>foo</firstname></root>" ,
294
+ respContentType : "application/json" ,
295
+ respBody : `{"status":"ok"}` ,
296
+ },
297
+ }
262
298
}
263
299
264
- func TestModifiedStatusCodeWithPlainTextBody (t * testing.T ) {
265
- given , when , then := NewProxyStage (t )
300
+ func TestNonJsonContentType (t * testing.T ) {
301
+ for testName , tc := range createNonJsonTestCases () {
302
+ t .Run (testName , func (t * testing.T ) {
303
+ given , when , then := NewProxyStage (t )
266
304
267
- given .
268
- a_pact_that_expects_plain_text ().and ().
269
- a_modified_response_status_of_ (http .StatusInternalServerError )
305
+ given .
306
+ a_pact_that_expects (tc .reqContentType , tc .reqBody , tc .respContentType , tc .respBody )
270
307
271
- when .
272
- a_plain_text_request_is_sent ()
308
+ when .
309
+ a_request_is_sent_with (tc .reqContentType , tc .reqBody )
310
+
311
+ then .
312
+ pact_verification_is_successful ().and ().
313
+ the_response_is_ (http .StatusOK ).and ().
314
+ the_response_body_is (tc .respBody )
315
+ })
316
+ }
273
317
274
- then .
275
- pact_verification_is_successful ().and ().
276
- the_response_is_ (http .StatusInternalServerError ).and ().
277
- the_response_body_to_plain_text_request_is_correct ()
278
318
}
279
319
280
- func TestPlainTextConstraintMatches (t * testing.T ) {
281
- given , when , then := NewProxyStage (t )
320
+ func TestNonJsonWithModifiedStatusCode (t * testing.T ) {
321
+ for testName , tc := range createNonJsonTestCases () {
322
+ t .Run (testName , func (t * testing.T ) {
323
+ given , when , then := NewProxyStage (t )
282
324
283
- given .
284
- a_pact_that_expects_plain_text ()
325
+ given .
326
+ a_pact_that_expects (tc .reqContentType , tc .reqBody , tc .respContentType , tc .respBody ).and ().
327
+ a_modified_response_status_of_ (http .StatusInternalServerError )
285
328
286
- when .
287
- a_body_constraint_is_added ("text" ).and ().
288
- a_plain_text_request_is_sent ()
329
+ when .
330
+ a_request_is_sent_with (tc .reqContentType , tc .reqBody )
289
331
290
- then .
291
- pact_verification_is_successful ().and ().
292
- the_response_is_ (http .StatusOK ).and ().
293
- the_response_body_to_plain_text_request_is_correct ()
332
+ then .
333
+ pact_verification_is_successful ().and ().
334
+ the_response_is_ (http .StatusInternalServerError ).and ().
335
+ the_response_body_is (tc .respBody )
336
+ })
337
+ }
294
338
}
295
339
296
- func TestPlainTextDefaultConstraintAdded (t * testing.T ) {
297
- given , when , then := NewProxyStage (t )
340
+ func TestNonJsonConstraintMatches (t * testing.T ) {
341
+ for testName , tc := range createNonJsonTestCases () {
342
+ t .Run (testName , func (t * testing.T ) {
343
+ given , when , then := NewProxyStage (t )
298
344
299
- given .
300
- a_pact_that_expects_plain_text ( )
345
+ given .
346
+ a_pact_that_expects ( tc . reqContentType , tc . reqBody , tc . respContentType , tc . respBody )
301
347
302
- when .
303
- a_plain_text_request_is_sent_with_body ("request with doesn't match constraint" )
348
+ when .
349
+ a_body_constraint_is_added (tc .reqBody ).and ().
350
+ a_request_is_sent_with (tc .reqContentType , tc .reqBody )
304
351
305
- then .
306
- pact_verification_is_not_successful ().and ().
307
- the_response_is_ (http .StatusBadRequest )
352
+ then .
353
+ pact_verification_is_successful ().and ().
354
+ the_response_is_ (http .StatusOK ).and ().
355
+ the_response_body_is (tc .respBody )
356
+ })
357
+ }
308
358
}
309
359
310
- func TestPlainTextConstraintDoesNotMatch (t * testing.T ) {
311
- given , when , then := NewProxyStage (t )
360
+ func TestNonJsonDefaultConstraintAdded (t * testing.T ) {
361
+ for testName , tc := range createNonJsonTestCases () {
362
+ t .Run (testName , func (t * testing.T ) {
363
+ given , when , then := NewProxyStage (t )
312
364
313
- given .
314
- a_pact_that_expects_plain_text ( )
365
+ given .
366
+ a_pact_that_expects ( tc . reqContentType , tc . reqBody , tc . respContentType , tc . respBody )
315
367
316
- when .
317
- a_body_constraint_is_added ("incorrect file content" ).and ().
318
- a_plain_text_request_is_sent ()
368
+ when .
369
+ a_request_is_sent_with ("text/plain" , "request with doesn't match constraint" )
319
370
320
- then .
321
- pact_verification_is_not_successful ().and ().
322
- the_response_is_ (http .StatusBadRequest )
371
+ then .
372
+ pact_verification_is_not_successful ().and ().
373
+ the_response_is_ (http .StatusBadRequest )
374
+ })
375
+ }
323
376
}
324
377
325
- func TestPlainTextDifferentRequestAndResponseBodies (t * testing.T ) {
326
- given , when , then := NewProxyStage (t )
327
-
328
- reqBody := "request body"
329
- respBody := "response body"
330
- requestConstraint := "request body"
378
+ func TestNonJsonConstraintDoesNotMatch (t * testing.T ) {
379
+ for testName , tc := range createNonJsonTestCases () {
380
+ t .Run (testName , func (t * testing.T ) {
381
+ given , when , then := NewProxyStage (t )
331
382
332
- given .
333
- a_pact_that_expects_plain_text_with_request_response ( reqBody , respBody )
383
+ given .
384
+ a_pact_that_expects ( tc . reqContentType , tc . reqBody , tc . respContentType , tc . respBody )
334
385
335
- when .
336
- a_body_constraint_is_added (requestConstraint ).and ().
337
- a_plain_text_request_is_sent_with_body ( "request body" )
386
+ when .
387
+ a_body_constraint_is_added ("incorrect file content" ).and ().
388
+ a_request_is_sent_with ( tc . reqContentType , tc . reqBody )
338
389
339
- then .
340
- pact_verification_is_successful ().and ().
341
- the_response_is_ (http .StatusOK ).and ().
342
- the_response_body_is ([]byte (respBody ))
390
+ then .
391
+ pact_verification_is_not_successful ().and ().
392
+ the_response_is_ (http .StatusBadRequest )
393
+ })
394
+ }
343
395
}
344
396
345
397
func TestIncorrectContentTypes (t * testing.T ) {
0 commit comments