mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
15.97k stars 519 forks source link

Bypassed POST request yields invalid on-the-wire HTTP request (missing starting-line) with form-data file attachment #2309

Closed thecopy closed 2 weeks ago

thecopy commented 1 month ago

Prerequisites

Environment check

Node.js version

v20.18.0

Reproduction repository

https://github.com/thecopy/msw-bug

Reproduction steps

  1. npm install
  2. node index.cjs

If you comment line mwsServer.listen(...), it works. OR If you remove the attached file, it works.

If i downgrade to msw@2.4.4 it works.

Current behavior

Invalid HTTP request is generated

HTTP request:

Host: localhost:51244
Accept-Encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------506525977784982220171228
Content-Length: 3693
Connection: close

Resulting in error from node:http:

[Error: Parse Error: Invalid method encountered] {
  bytesParsed: 3,
  code: 'HPE_INVALID_METHOD',
  reason: 'Invalid method encountered',
  rawPacket: <Buffer 0d 0a 48 6f 73 74 3a 20 6c 6f 63 61 6c 68 6f 73 74 3a 35 31 32 34 34 0d 0a 41 63 63 65 70 74 2d 45 6e 63 6f 64 69 6e 67 3a 20 67 7a 69 70 2c 20 64 65 ... 143 more bytes>
}

Expected behavior

HTTP request:

POST /test HTTP/1.1 <------------- THIS IS MISSING
Host: localhost:51244
Accept-Encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------506525977784982220171228
Content-Length: 3693
Connection: close
thecopy commented 1 month ago
Without file attachment With file attachment
image image
kettanaito commented 2 weeks ago

@thecopy, thanks for reporting this. I've translated your reproduction repo to a test case and fixed the issue in @mswjs/interceptors. Will propagate to MSW soon.