-
-
Notifications
You must be signed in to change notification settings - Fork 515
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
Binary body of a request gets corrupted in the handler #929
Comments
Found the solution. In the file mockServiceWorker.js replace
with
Now the body is an encoded utf-8 string. The following call to the File constructor in RequestHandler.ts at function parseMultipartData uses the utf-8 encoded string |
Hey, @Ovaron1994. Thank you for reporting this. I'd like to clarify a few things before we can proceed with discussing a solution to this:
Ideally, if you could put your usage scenario into an integration test in our repository that'd speed up the work on this issue tremendously. Please, if you have some time, consider doing so. You can learn how to contribute with a test, as well as browser the existing tests for practical reference. Thank you. |
Hi kettanaito,
|
Hi @kettanaito, need help here, Added a test but I don't get the test to run as expected. Michael |
Hello! Im suffering with the same issue. I tried the proposed solution, but due to the size of the images im trying to upload I hit a Maximum call stack exceeded for String::fromCharCode. |
Hi PupoSDC, have you tried to use the fromCharCode for single characters in a for loop like this: let body = ""; This is not an effective way but it should work. Another way should be: const body = new TextDecoder().decode(new Uint8Array(await request.arrayBuffer())); Did it work with this? Greetings Michael |
I had tried the one using In the end i decided to go a different route. In my scenario i was trying to mock a image upload / download flow. I opted to just ignore the blob that is sent on the upload, and just returning a mock image on the download. It looks a bit awkward, but its acceptable for a mock. |
Released: v0.43.0 🎉This has been released in v0.43.0! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
@kettanaito I don't think that this issue is resolved in v0.43.0 I am still getting it with 0.44.2 when trying to make PATCH request with binary request bodies and as @Ovaron1994 stated, the Maybe you can re-open this issue. |
It seems works in "fallback mode" btw. It's a bit difficult to say, because the only way to get into fallback mode was to set a breakpoint here and set "useFallbackMode" to "true" in the debugger. Maybe a quick solution to this problem is to allow to set fallback mode manually. |
Hey. I'm still experiencing this issue with latest version of the MSW. Code for the request:
|
Hi, @theishinz-maersk. I'm sorry to hear you're experiencing issues regarding this. You're right, we shouldn't read the request body as text all the time. We've already migrated away from it and now reading request body as Line 256 in ccc576d
That being said, I'm not going to backport this fix to the current version of MSW due to time considerations. I highly recommend you migrate your app to the |
Environment
Request handlers
Mocking setup via storybook add-on:
In the req.body I expect a multipart content which owns an jpeg image.
In preview.js I have:
Actual request
The request is a multipart content which contains a jpeg image sent by fetch.
Current behavior
The data received in the request handler is a damaged jpeg file. After a lot debugging I found out that the image data gets damaged at two locations:
This damages the binary data because the data is converted to utf-8.
msw/src/mockServiceWorker.js
Line 175 in 11bb244
msw/src/utils/internal/parseMultipartData.ts
Line 87 in 11bb244
Expected behavior
Expected is that the data from the post request will not get damaged.
To prevent a damage of the image for 1. the content should be get as unicode string by using something like this:
For 2. we need transform the extracted data of the multipart data so the image correctly used for the creation of the File.
To transform it back from unicode string to ArrayBuffer we need soething like this:
The text was updated successfully, but these errors were encountered: