msw icon indicating copy to clipboard operation
msw copied to clipboard

Axios FormData upload broken in v2

Open pschyma opened this issue 3 years ago • 4 comments

Prerequisites

Environment check

  • [X] I'm using the latest msw version
  • [X] I'm using Node.js version 18 or higher

Node.js version

v18.18.2

Reproduction repository

https://github.com/pschyma/msw-examples/tree/axios-formdata-reproducer

Reproduction steps

cd examples/with-vitest
npm install
npm test

Current behavior

I'm not sure where the issue is, but as msw v2 triggered this issue for me, I'll start here.

After updating to msw 2 FormData upload via Axios stops working due to missing boundary in Content-Type header.

Headers observed when posting data with fetch:

{
  'content-type' => {
    name: 'content-type',
    value: 'multipart/form-data; boundary=----formdata-undici-088515513855'
  }
}

Headers observed when posting data with Axios:

{
  'content-type' => {
    name: 'content-type',
    value: 'multipart/form-data'
  }
}

Expected behavior

Axios requests behave like when msw < 2 is used and provide the boundary in headers.

In previous msw version, I used parse-multipart-data to extract the boundary from request headers and for parsing the text content of that request.

pschyma avatar Nov 04 '23 17:11 pschyma

I believe there is a CVE based on why you are no longer seeing this data: https://github.com/axios/axios/issues/6006

axios 1.5.0 was the last working version with msw, and the axios 1.5.1 update breaks our Testing.

bignamehere avatar Nov 06 '23 19:11 bignamehere

Hi, @pschyma. Thanks for reporting this.

I looked at your reproduction example and it looks okay. More to that, if it works with fetch and breaks with Axios, then I assume this is an Axios' issue.

MSW does not control/polyfill/affect/modify the FormData behavior. It's implemented by undici in Node.js and you can see that the same Undici (global fetch) appends the boundary to the Content-Type header correctly.

Our integration test for FormData is passing so it's unlikely there's an issue with MSW.

I recommend you create an issue in the Axios repo and share the team's response with us here so others can learn from it too. Thanks.

kettanaito avatar Nov 07 '23 09:11 kettanaito

Hi, thanks for the quick reaction.

I recommend you create an issue in the Axios repo and share the team's response with us here so others can learn from it too. Thanks.

Done. The Axios team adapted their code, so everything should be working again when the change gets released.

pschyma avatar Nov 08 '23 10:11 pschyma

Incredible collaboration! Thank you, @pschyma!

kettanaito avatar Nov 08 '23 11:11 kettanaito