<#1210 payload changed by pact between consumer te...
# pact-js-development
g
#1210 payload changed by pact between consumer test and provider test Issue created by ajoanny Thank you for reporting a bug! We appreciate it very much. Issues are a big input into the priorities for Pact-JS development All italic text in this template is safe to remove before submitting Thanks again! Software versions Please provide at least OS and version of pact-js • OS: MacOS Sonoma v14.4.1 • Consumer Pact library: v14.3.3 • Provider Pact library: v14.3.3 • Node Version: v20.11.1 Issue Checklist Please confirm the following: ☑︎ I have upgraded to the latest ☑︎ I have the read the FAQs in the Readme ☑︎ I have triple checked, that there are no unhandled promises in my code and have read the section on intermittent test failures ☑︎ I have set my log level to debug and attached a log file showing the complete request/response cycle ☑︎ For bonus points and virtual high fives, I have created a reproduceable git repository (see below) to illustrate the problem Hello ! Expected behaviour When this request is defined in the consumer test
Copy code
.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
Copy code
{ data: { attributes: { name: 'name' } } }
Actual behaviour The body used in the request for the provider is
Copy code
{"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 :
Copy code
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:
Copy code
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 :
Copy code
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-js