Msw: [v0.22.0] Not parsing JSON body

Created on 19 Nov 2020  路  3Comments  路  Source: mswjs/msw

Describe the bug

Hi, I am not sure if this is a bug or was a planned changed but after upgrading msw to the latest version, part of my tests started to fail. I fount this commit that changes the condition to parse the body.

Now when we have the following header:

Content-Type: application/json; charset=UTF-8

The body is not parsed.

Environment

  • msw: 0.22.0
  • nodejs: 12.19.0
  • npm: 6.14.8

To Reproduce

Steps to reproduce the behavior:

  1. Create a REST request handler
  2. Send a request with json body and add header Content-Type: application/json; charset=UTF-8
  3. req.body inside the request handler will be a string

Expected behavior

When the content-type header is application/json, the body is automatically parsed.

bug

All 3 comments

Hi, @otaciliolacerda. Thanks for reporting this.

That's a great catch, I believe that commit overlooks the charset as a part of the Content-Type request header. Reverting the logic to use .includes() should resolve the issue.

Would you be interested in shipping a pull request?

Yes, I will take a look on this. Thanks for the super fast reply.

.includes may be susceptible to errors if the Content-Type header includes an arbitrary content type, or a content type that includes the phrase "json" but does not represent an actual JSON content type. However, I still suggest we revert to .includes and address that only when such issue arises.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hauptrolle picture hauptrolle  路  4Comments

danielstreit picture danielstreit  路  3Comments

slowselfip picture slowselfip  路  3Comments

dashed picture dashed  路  4Comments

ff6347 picture ff6347  路  3Comments