Search code examples
node.jspact

Pact-js: POST body is garbled by VerifyProvider



I'm running into an odd issue with Pact-js and POST bodies.

The background:
Consumer Side
- I have a NodeJs app which I'm trying to test
- I configured Pact and set up the appropriate framework
- All test run successfully and generate contract

Provider Side:
- Again, I have a NodeJs app which I'm trying to test
- Pact has been set up and framework in place
- When i run the test, all GET requests run successfully, however all POSTs report a fail.

The Issue:
- When I echo out the POST body being passed to the service from Pact (veryifyProvider), i can see that its wrapped the body (JSON) inside another 'Key: value' pairing, where the body i want to parse is the Key and the value is empty. Its also added escape chars ( \ ) to all the double quotes inside the Body. EX:
{"{\"Account\":\"123\",\"Orbit\":\"5.00\",\"Date\":\"2016-06-22\",\"Client\":\"1234\",\"Spring\":\"1234\"}":""}

When i look in my Pact contract json, everything looks correct. Somewhere between VerifyProvider reading in the JSON and passing it to the REST endpoint, the body is mangled. This only seam to happen during tests, normal invocation works appropriately.

Here is the code I'm using Provider side to execute the Verify:

const verifier = require('pact').Verifier;
const path = require('path');

let contract = path.join(__dirname, 'pactContract.json');

let opts = {
 providerBaseUrl: "http://localhost:3001",
 pactUrls: [contract],
};

verifier.verifyProvider(opts)
 .then((res) => {
 console.log('pact veryify complete, !!!');
 console.log(res);
 process.exit(0);
 }).catch((error) => {
 console.log(error);
 process.exit(1);
});

I'm unable to pull down my exact consumer codebase, but its nearly identical in structure shown here Pact-js.

Any help is VERY much appreciated!


Solution

  • Please check the Content-Type header and ensure it is application/json. This can happen if the service thinks it's matching text messages.