-
-
Notifications
You must be signed in to change notification settings - Fork 469
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
Unable to determine whether req.body was sent as valid JSON #433
Comments
Hey, @tomalexhughes. Thanks for reporting this, it's an interesting one. Strangely, this isn't the behavior I observe in the latest Above you can see that un-stringified request body is indeed available as The same behavior is on the version Can you please provide a reproduction scenario for this issue? You can use this sandbox to get started, or push a minimal reproduction repository on GitHub. Thanks! |
Note that the request's body is already implicitly validated for a JSON validity as a part of the request transformation logic internally: msw/src/utils/request/parseBody.ts Lines 7 to 22 in 327b384
Thus, if you may reason about the request's body validity by checking its rest.post('/json', (req, res, ctx) => {
const isJson = req.headers.get('content-type')?.endsWith('json')
const isValidJson = isJson && req.body?.constructor.name === 'Object'
}) |
Hey, thanks for the reply :) I added a console.log statement inside my request resolver and noticed that the response was different in my tests compared to the browser. In the browser However, I am using the same request resolver for my tests in Node and getting a different result. My testing environment is as follows:
I'll try and create a reproduction repo over the weekend. |
Reproduction can be found in this repo. @kettanaito I've also added the |
Hi @tomalexhughes to send JSON with
You should parse your object as |
@marcosvega91 Hey Marco! Thanks for the reply. I'm aware how to send valid JSON but my issue is that I want to be able to test in my resolver when the request payload is invalid so I can return an error response rather than a success. Currently when invalid JSON is sent I’m aware that Node doesn’t have a native fetch and therefore react-scripts will be polyfilling it for tests (although my assumption would be that the polyfill would be the same in the browser as Node) but I would expect |
Ah, I didn't read your first question. So, your example is using
Looking inside the code of the library I see that when send is called a I'm not sure if it's right that |
I think @marcosvega91 provided a valid observation:
send(data) {
if (requestContentTypeIsJson && typeof data !== 'string') {
coerceData(data) // produce a "window.fetch"-like behavior
}
} |
The maintainers of |
Unfortunately, per |
I see little value in keeping this issue open: there is nothing to be addressed on the MSW side. The issue is in the You can still track the resolution progress with |
The fix on the |
For anyone who comes across this issue, although the issue is now closed, this still seems to be an issue even after the Testing environment is:
@kettanaito I will try and debug this further and create a follow-up on the whatwg-fetch repo or here depending on where the issue resides. I've updated my reproduction repo if this is something you want to look into yourself or if you wish to re-open this issue. |
Thanks for reporting back, @tomalexhughes. Just to be safe: could you please ensure that both |
Hey @kettanaito, my versions are as follows:
I'll attempt to force the resolution to whatwg-fetch 3.6.0 as it looks like 3.6.1 changed some of the code your PR introduced and let you know if 3.6.0 works. EDIT: Appears that 3.6.2 of whatwg-fetch reverts your original code change with JakeChampion/fetch@e42f201 :/ I'll continue the discussion on the github/fetch repo |
Thanks for figuring that out, @tomalexhughes. I hope the whatwg-fetch maintainers share some insights as to why that was reverted and we can learn from that to come up with a better solution on their side. |
Hi, big fan of
msw
. But I’m experiencing the following issue:Describe the bug
I want to return a different response in my resolver depending on whether
req.body
is valid JSON or not. This is to allow closer imitation of our backend API within our tests.Previously my test passed despite sending invalid JSON as I always returned a success response, therefore I would like to return an error response from my mock API if the JSON sent is invalid. I believe this is the correct approach as the Request assertions documentation states that we should not be asserting on the body directly and instead include failure scenarios.
I'm currently unable to validate whether the JSON sent is valid as
parseBody
automatically parses JSON for us or will return the raw object if not parsable, therefore, the following two requests will have the same body in the request resolver.Valid JSON Request
Invalid JSON Request
Resolver
In this scenario isValidJSON would always return as false because
req.body
is always the raw object, I understand returning the raw object is a convenience helper and is a desired behaviour but it makes it impossible to imitate our backend API.Environment
msw: ^0.21.3
nodejs: 12.18.3
npm: 6.14.8
To Reproduce
Use the above resolver with the above payloads.
Expected behavior
As mentioned above I recognise the automatic parsing of
req.body
is intentional and would be a breaking change if removed.My initial thoughts should be that
req.body
should be '[object Object]' if sent as an object that is not valid JSON. If the body was sent as valid JSON then it should remain as the parsed object.The text was updated successfully, but these errors were encountered: