GitHub
04/22/2024, 8:25 AM.withRequest('POST', '/api/test', (builder) => {
builder.headers({
'Content-Type': 'application/vnd.api+json',
});
builder.jsonBody({ data: { attributes: { name: 'name' } } });
})
Then the request body used in the provider test should be
{ data: { attributes: { name: 'name' } } }
Actual behaviour
The body used in the request for the provider is
{"body":{"type":"Buffer","data":[123,34,100,97,116,97,34,58,123,34,97,116,116,114,105,98,117,116,101,115,34,58,123,34,110,97,109,101,34,58,34,110,97,109,101,34,125,125,125]}
Steps to reproduce
Consumer test :
const provider = new PactV4({
provider: 'provider',
consumer: 'consumer',
dir: path.resolve(process.cwd(), 'pacts'),
spec: 4,
logLevel: 'debug',
});
describe('POST /api/test', () => {
it('send a request', () => {
const body = { data: { attributes: { name: 'name' } } };
return provider
.addInteraction()
.uponReceiving('json api content type')
.withRequest('POST', '/api/test', (builder) => {
builder.headers({
'Content-Type': 'application/vnd.api+json',
});
builder.jsonBody(body);
})
.willRespondWith(200, () => {})
.executeTest(async (mockserver) => {
const response = await axios.request({
baseURL: mockserver.url,
headers: {
'Content-Type': 'application/vnd.api+json',
},
method: 'POST',
url: `/api/test`,
data: body,
});
expect(response.status).toEqual(200);
});
});
});
Provider test:
describe('Order-API contracts in order that', () => {
it('comply with consumers expectations', () => {
return new Verifier({
provider: 'provider',
providerVersion: '1.0',
providerBaseUrl: '<http://localhost:8080>',
pactBrokerUrl: getPactBrokerURL(),
consumerVersionSelectors: [{ branch: 'fix/pact' }],
publishVerificationResult: true,
failIfNoPactsFound: false,
enablePending: true,
logLevel: 'debug',
})
.verifyProvider()
.then(() => {
console.log('Successfully verified Pact contracts!');
});
});
});
The request handler :
const schema = z.object({
data: z.object({
attributes: z.object({
name: z.string(),
}),
}),
});
<http://routes.post|routes.post>('/api/test', async (req, res) => {
const result = schema.safeParse(req.body);
if (result.success) {
res.status(200).send();
} else {
res.status(400).send();
}
});
I have a workaround which is to use the content-type 'application/json' to keep the body as it was defined in the consumer test. I dont know if it should be flagged as a bug, but it's really surprising that the request was not exactly the same on both side of participants.
If I have enough time today I'll use the template to create a repository to reproduce the behavior.
Have a nice day 👋🏽 !
Provide a Minimal, Reproducible Example. You can create your own repository, or use this template that's already hooked up to CI and everything.
Relevant log files
consumer-provider.json
provider.log
consumer.log
Please ensure you set logging to DEBUG and attach any relevant log files here (or link to a gist).
pact-foundation/pact-jsGitHub
04/23/2024, 11:57 AM