http-middleware icon indicating copy to clipboard operation
http-middleware copied to clipboard

Body stream is already complete and cannot be read after using http-middleware

Open editedredx opened this issue 1 year ago • 5 comments

Hi,

This problem arises in v0.10.2, v0.10.1 worked without issue. Once a PUT or POST handler has been added the body has been read and cannot be read by any other middleware anymore. This is similar to what the express bodyParser middleware does but that would place the body inside the req object to be used but that's not the case here.

Simple testcase:

import express from 'express';
import { createMiddleware } from '@mswjs/http-middleware';
import * as msw from 'msw';

const app = express();

app.use(
  createMiddleware([
    msw.http.post('something', function () {
      return new Response(JSON.stringify({ foo: 123 }), {
        headers: {
          'Content-Type': 'application/json',
        },
      });
    }),
  ])
);

app.use((req, res) => {
  console.assert(req.readable === true);
  console.assert(req.readableDidRead === false);
  console.assert(req.readableEnded === false);
  res.sendStatus(200);
});

app.listen(9090, () => console.log('Ready at http://localhost:9090'));

Test by making a POST request to http://localhost:9090, path doesn't have to be /something

The assertions succeed on 0.10.1 and fail on 0.10.2. They do succeed if there are only GET handlers passed to createMiddleware.

editedredx avatar Oct 31 '24 12:10 editedredx