mswjs / http-middleware

Spawn an HTTP server from your request handlers or apply them to an existing server using a middleware.
https://npm.im/@mswjs/http-middleware
109 stars 13 forks source link

fix: respect original req.body if it is still a buffer #22

Closed robbtraister closed 2 months ago

robbtraister commented 2 years ago

This PR coerces the MockedRequest body to be a buffer from a string (or stringified object). This works well if express.json() middleware has been mounted because we need to un-parse the work the middleware performed.

However, that in effect makes the express.json() middleware required, because otherwise we are undoing work that was never done and we end up stringifying the original Uint8Array buffer, which does not work as expected.

This change preserves the original body buffer if no express.json() (or similar) middleware has been mounted, allowing the subsequent req.json() call to work as expected, regardless.

ThijSlim commented 1 year ago

Would be great if this gets merged, running against this issue too 👍