pactflow / pact-msw-adapter

Create MSW (mock-service-worker) mocks, and generate pact contracts from the recorded interactions.
MIT License
43 stars 13 forks source link

Failing `writeToFile` if the request body is used #202

Open nagytom opened 3 months ago

nagytom commented 3 months ago

Software versions

Issue Checklist

Please confirm the following:

Expected behavior

The request body should be consumable in the MSW handler without causing writeToFile to fail.

Actual behavior

writeToFile fails because it tries to clone the request and read the body which is already consumed (src).

This is not allowed by the specs (MDN):

clone() throws a TypeError if the request body has already been used.

Steps to reproduce

test('echo', async () => {
  server.use(
    http.post('*/echo', async ({ request }) => {
      const body = await request.json();
      return HttpResponse.json(body);
    })
  );

  let response = await fetch('http://localhost/echo', {
    body: JSON.stringify({ test: 'data' }),
    method: 'POST',
    headers: { 'content-type': 'application/json' },
  });
  let body = await response.json();

  expect(body).toEqual({ test: 'data' });
});

Relevant log files

-

daniel-meneses commented 1 week ago

Temporary workaround:

http.post('*/echo', async ({ request }) => {
    const body = await request.clone().json();
    return HttpResponse.json(body);
})
YOU54F commented 1 week ago

Happy to accept pull requests to sort, sorry for late reply