Closed aethr closed 3 years ago
Sounds like 'cypress/packages/driver/src/cy/net-stubbing/events/utils.ts' just needs to be updated to detect more valid JSON MIME types.
I don't think we should proactively JSON.parse all incoming request and response bodies, sounds like it could do more harm than good. For example, if you have an endpoint that returns plain text, the type of the response could be object
or string
depending on if the response happens to be JSON-parseable or not. Keep in mind that true
, false
, 1
, and "foo"
are all valid JSON bodies.
Good points. I did some testing and route
in Cypress 5 and 6 are both consistent in not converting the response body to objects in this scenario, so my suggestion would probably be a breaking change for some.
A brief search on iana turned up a few likely JSON-compatible media types:
Most seem to follow the same pattern:
application/SOMETHING+json
application/vnd.SOMETHING+json
Perhaps the check could be expanded to:
return contentType && /^application\/.*json/i.test(contentType)
@aethr Sounds good. If it gets done by March we can squeeze it into the 7.0.0 release.
Internal Jira issue: TR-681
The code for this is done in cypress-io/cypress#15129, but has yet to be released. We'll update this issue and reference the changelog when it's released.
Released in 7.0.0
.
This comment thread has been locked. If you are still experiencing this issue after upgrading to Cypress v7.0.0, please open a new issue.
Possibly a duplicate or related to https://github.com/cypress-io/cypress/issues/14273.
Our back-end services use JSON:API spec (https://jsonapi.org/) which recommends the media type application/vnd.api+json. They deliver valid JSON in the response body, which should be converted to an object by
cy.wait
to allow for deep assertions.Current behavior
If request is intercepted with
cy.intercept()
, thency.wait()
yields a string for the response body (related issue: #14273.)From this comment: https://github.com/cypress-io/cypress/issues/14273#issuecomment-765625317, I believe this is due to the check in https://github.com/cypress-io/cypress/blob/6ee7a9c27e9ea8b7dceee6bae140ce86d816a5e1/packages/driver/src/cy/net-stubbing/events/utils.ts#L4-L20
Due to our back-end sending
content-type: application/vnd.api+json
this check fails, and so the response body is not converted usingJSON.parse
.Desired behavior
I believe that if the response to a request is valid JSON, it should be converted to a JS object regardless of the headers. This approach is taken elsewhere in Cypress, ie: https://github.com/cypress-io/cypress/blob/dae76a8ec0a149d5f4ca4fbe4d15bea1b5f7449d/packages/server/lib/controllers/xhrs.js#L9-L23
Effectively if
JSON.parse(text)
succeeds without throwing, we can assume thattext
is valid JSON and return the resulting parsed object.Test code to reproduce
Versions
6.3.0