Cannot mock 204 response
See original GitHub issueDescribe 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
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How do I unit test an Angular HttpClient 204 response?
So, the simple answer is, pass '' to flush(): request.flush('', { status: 204, statusText: 'No Data' });.
Read more >Issue with REST Mockservice Response, when respons...
When the HTTP status code in the mock response is set to "204 - No Content", then no response is sent back from...
Read more >HTTP Status 204 (No Content) - REST
By default, 204 (No Content) the response is cacheable. If caching needs to be overridden then the response must include cache respective cache ......
Read more >Null API Responses and HTTP 204 Results in ASP.NET Core
ASP.NET Core 3.x has a behavior that results in API results that return null from the controller action returning a HTTP 204 -...
Read more >204 No Content - HTTP - MDN Web Docs - Mozilla
The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kettanaito
You are right. It was my mistake. The 204 response contained an empty ( {} ) body payload in my case. As you suggested
res(ctx.status(204))
is the right way here.Thank you for your support and time.
Hi, @SerkanSipahi. The change you’re referencing has been successfully merged:
https://github.com/mswjs/msw/blob/9a43bc9cba49f3c18b1ab1ed700d3f41f9fe86f5/src/mockServiceWorker.js#L129-L130
Could you please ensure you’re running
msw@0.29.0
?Perhaps the response you’re composing is not a valid HTTP response? A response with a 204 status code cannot have a response body, that’d be a violation of the spec. Can you please try composing a mocked 204 response without any response body?