Msw: Status 400 (OK) displaying when it should display (Bad Request)

Created on 20 Mar 2020  路  11Comments  路  Source: mswjs/msw

Here's what a request looks like when I set the status to 400 with msw vs a real server sending a 400:

image

I'm not sure what could be causing this.

tests

All 11 comments

My guess is that since MSW constructs a new Response it has statusText: 'OK' by default. I presume you set the status code alone (i.e. via ctx.status(400)), so it doesn't map it to any status text associated with that status code.

One way to solve it for now could be to provide an explicit status text alongside the status code:

ctx.status(400, 'Bad request')

I value the validity of requests and responses, but I wonder whether it's the library's responsibility to map status codes to texts implicitly. I'd expect this to be done by Request constructor. Would have to look into this in more detail.

That's fine. Thank you for helping me know what to do there.

I think that having a default statusText that's the same as what the statusText would be for a given status code would be useful. I could always override it if I want to, but having a default to what the browser does would be sensible IMO.

I was thinking of keeping a status code-status text map internally in MSW and mapping it by default, as you described. I guess there's little harm in that, but a big gain in developer experience.

I went with the solution that reuses the statuses package thats's used in Express to map status codes to status texts.

Starting from 0.9.3 MSW now implicitly maps given status codes to the associated status text, if such is present, and if no custom status text is provided.

@kentcdodds, could you please update to 0.9.3 and let me know it produced correct status texts for you? Thanks.

Confirmed. This worked. Thanks!

Sorry for bumping that old issue, but when I using setup server with such handler:
rest.post('http://127.0.0.1:3000/getUserPoints', (req, res, ctx) => res(ctx.status(401))),
I receiving response with status text OK. Its not only for 401, but for other statuses too.

Hey, @pandrodo. That's perfectly fine.

Can you clarify a few things:

  1. Which version of msw are you using?
  2. What version of NodeJS are you on?
  3. How do you make an actual request? (include the code)
  1. Version of msv is 0.21.3.
  2. Version of NodeJS is 14.15.0. Running server in my jest tests (version 24.9.0 shipped with react-scripts 3.4.3).
  3. Code is slightly simplified, but response still same with wrong statusText

@pandrodo, thanks for including that. The issue confirmed as reproducible, the fix pull request is issued. Regression test was added, so we don't miss it occurring again. Will be released in the next minor version of msw.

@kettanaito, thanks for so fast fix.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

otaciliolacerda picture otaciliolacerda  路  3Comments

otaciliolacerda picture otaciliolacerda  路  3Comments

veronesecoms picture veronesecoms  路  3Comments

danielstreit picture danielstreit  路  3Comments

slowselfip picture slowselfip  路  3Comments