Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing writeToFile if the request body is used #202

Open
3 of 5 tasks
nagytom opened this issue Apr 4, 2024 · 2 comments
Open
3 of 5 tasks

Failing writeToFile if the request body is used #202

nagytom opened this issue Apr 4, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@nagytom
Copy link

nagytom commented Apr 4, 2024

Software versions

  • OS: macOS 14.4.1
  • Pact Node version: "@pactflow/pact-msw-adapter": "3.0.0"
  • Node Version: 20.12.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
  • 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

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

-

@nagytom nagytom added the bug Something isn't working label Apr 4, 2024
@daniel-meneses
Copy link

Temporary workaround:

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

@YOU54F
Copy link
Member

YOU54F commented Jun 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants