Msw: Cannot mock 204 response

Created on 8 Jan 2021  路  3Comments  路  Source: mswjs/msw

Describe the bug

It seems to be impossible to construct a 204 (No content) response. When I just invoke
return res(ctx.status(204))
I get the following error:
_Failed to construct 'Response': Response with null body status cannot have body_
It does not help trying to add ctx.json({}) or ctx.body(null) - the body seems to end up as the empty string regardless. This fails when msw is trying to invoke const response = new Response(payload.body, payload) in the function createResponseListener as responses with status 204 are not allowed to have any body.

Environment

  • msw: 0.25.0
  • Chrome: 87.0.4280.141
bug

Most helpful comment

Hi @abrudin thanks for rasing this :).

I have opened a PR with the fix. The problem is that in the service worker response.text() has been called. But the return value of that method is an empty string if the body is null.

All 3 comments

Hi @abrudin thanks for rasing this :).

I have opened a PR with the fix. The problem is that in the service worker response.text() has been called. But the return value of that method is an empty string if the body is null.

@marcosvega91 It makes me really comfortable using this library with this incredibly low response time on issues - just don't push yourself too hard :)

We are trying to make our best :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hauptrolle picture hauptrolle  路  4Comments

derekr picture derekr  路  3Comments

otaciliolacerda picture otaciliolacerda  路  3Comments

tomalexhughes picture tomalexhughes  路  3Comments

danielstreit picture danielstreit  路  3Comments