-
-
Notifications
You must be signed in to change notification settings - Fork 127
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
fix(xhr): clone the request before calculating its body size #632
Conversation
Hey all, can we get this merged and msw released? Thx, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Golden 🥇 Thank you for adding this fix, @robbtraister!
What we are missing is an automated test for this. Would you have some time to add one? You can add it to XHR regression tests.
If not, let me know, I will add it once I have a moment.
@kettanaito I tried adding a test, but I can't seem to duplicate the behavior. In the test environment, reading the request buffer does not seem to lock it from future attempts to read/clone. This is a simplification of what I tried: interceptor.once('request', async ({ controller, request }) => {
await request.arrayBuffer() // <-- this is the addition to the existing test, along with changing to POST
controller.respondWith(
new UndiciResponse('Hello world') as unknown as Response
)
}) |
I think the issue is that the vite environment has its own Request implementation that does not block cloning after consumption. I was able to figure out a hacky workaround by importing |
@robbtraister I'm impacted by this one and I'm using Vite + Vitest. Please see mswjs/msw#2273 (comment) |
Hmm, this seems a bit too much. We are using the Undici's |
0fd1ac6
to
2810a96
Compare
I've added an isolated test and confirm it failing with the following error without the proposed fix:
With the fix, the test is passing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great. Waiting for the tests before merging.
It looks like the key difference is using the |
Released: v0.35.3 🎉This has been released in v0.35.3! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
The root cause of the issue is that cloning the Request to calculate the
Content-Length
header occurs too late. At the point in which clone() is called, the body buffer has already been consumed, so cloning fails.Since this
kFetchRequest
value is only used for this specific purpose of calculating content-length, there is no harm in creating and storing a clone immediately upon creation of the Request object.