pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.08k stars 479 forks source link

Error Parsing String in POST body. #791

Open dikshaarora opened 5 years ago

dikshaarora commented 5 years ago

I am trying to create a PACT consumer test that passes String as a body in the POST request with Content-Type = application/json Version: "au.com.dius:pact-jvm-consumer-java8_2.11:3.5.21"

I am declaring my pact as: `
Map<String, String> requestHeaders = new HashMap<>(); requestHeaders.put(ACCEPT, APPLICATION_JSON); requestHeaders.put(CONTENT_TYPE, APPLICATION_JSON);

.given("Description") .uponReceiving("Description") .headers(requestHeaders) .method(POST) .query(query) .body("s1="val1" and s2="val2") .path("/path") .willRespondWith() .body(ResponseString) .status(OK_200) .toPact();`

But I am getting the following error when I run consumer test: `11:48:00.318 [Thread-2] ERROR a.c.d.pact.consumer.BaseMockServer - Failed to generate response groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

The current character read is 's' with an int value of 115 Unable to determine the current character, it is not a string, number, array, or object line number 1 index number 0 "s1="val1" and s2="val2"" `

It fails with java.lang.AssertionError: Pact Test function failed with an exception, possibly due to ExpectedButNotReceived(expectedRequests=[ method: POST.

If I change the Content-Type to text/plain I get a partial mismatch with header "content-type" not matching.

Is this a bug another way to test this scenario? Or because the body is of type string, it's not a valid scenario and header at client side need to be changed?

Thanks!

uglyog commented 5 years ago

If you set the content type to application/json, the body needs to be formatted as JSON. Looks like you are trying to do a FORM POST with body("s1="val1" and s2="val2")