Skip to content
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

JSON response with special chars truncated since 2.2.0 #2045

Closed
4 tasks done
LeBenLeBen opened this issue Feb 20, 2024 · 3 comments · Fixed by #2046
Closed
4 tasks done

JSON response with special chars truncated since 2.2.0 #2045

LeBenLeBen opened this issue Feb 20, 2024 · 3 comments · Fixed by #2046
Labels
bug Something isn't working good first issue Good for newcomers scope:browser Related to MSW running in a browser scope:node Related to MSW running in Node

Comments

@LeBenLeBen
Copy link

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

20.9.0

Reproduction repository

https://github.com/LeBenLeBen/msw-truncated

Reproduction steps

Clone the reproduction repo, then:

  1. npm install
  2. npm start
  3. Visit http://localhost:7051/user

Current behavior

The JSON response is truncated a few characters before the end and cannot be parsed by the browser. It’s caused by the special char é in the response. The problem does not occur before MSW 2.2.0, so I’m guessing it’s a regression caused by #1658

Expected behavior

The JSON response should be complete.

@LeBenLeBen LeBenLeBen added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Feb 20, 2024
@ZeroCho
Copy link
Contributor

ZeroCho commented Feb 20, 2024

I left a comment in #1658 but as this is the same problem with mine, I'll copy it here.

I have an issue with 2.2.0+ version of MSW, when I put CJK(Chinese, Japanese, Korean character) in MSW response.
Until msw@2.1.0, it was fine. I checked out what was changed in 2.2.0, and I found this commit could be the problem.

For example, the length of string '조현영' (which is Korean) is 9 bytes in network, whereas 'abc' is 3 bytes. But both are 3 lengths in JavaScript. I think current implementation which calculate length by just string.length can be problem.
I think using new TextEncoder().encode(str).length would be better to calculate Content-Length. Thank you

image

@kettanaito kettanaito added scope:browser Related to MSW running in a browser and removed needs:triage Issues that have not been investigated yet. labels Feb 20, 2024
@kettanaito
Copy link
Member

kettanaito commented Feb 20, 2024

Root cause

Right now, we are relying on the string length to determine the value of the automatic Content-Length response header.

Instead, we should convert the string to buffer and measure its byte length. There are a couple of ways to do that:

  1. new TextEncoder().encode(text).byteLength
  2. new Blob([text]).size

Pull requests are welcome! I'd start with a failing test for special characters right here:

describe('HttpResponse.text()', () => {

@kettanaito
Copy link
Member

Released: v2.2.2 🎉

This has been released in v2.2.2!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers scope:browser Related to MSW running in a browser scope:node Related to MSW running in Node
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants