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

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

Open
editedredx opened this issue Oct 31, 2024 · 0 comments

Comments

@editedredx
Copy link

editedredx commented Oct 31, 2024

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 makeing 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 editedredx changed the title Body stream is alredy complete and cannot be read after using http-middleware Body stream is already complete and cannot be read after using http-middleware Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant