Giter Site home page Giter Site logo

Comments (11)

kentcdodds avatar kentcdodds commented on April 29, 2024 1

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.

from msw.

kentcdodds avatar kentcdodds commented on April 29, 2024 1

Confirmed. This worked. Thanks!

from msw.

kettanaito avatar kettanaito commented on April 29, 2024 1

@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.

from msw.

kettanaito avatar kettanaito commented on April 29, 2024

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.

from msw.

kentcdodds avatar kentcdodds commented on April 29, 2024

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

from msw.

kettanaito avatar kettanaito commented on April 29, 2024

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.

from msw.

kettanaito avatar kettanaito commented on April 29, 2024

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.

from msw.

pandrodo avatar pandrodo commented on April 29, 2024

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.

from msw.

kettanaito avatar kettanaito commented on April 29, 2024

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)

from msw.

pandrodo avatar pandrodo commented on April 29, 2024
  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

from msw.

pandrodo avatar pandrodo commented on April 29, 2024

@kettanaito, thanks for so fast fix.

from msw.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.