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

response.body isn't converted to JSON if content-type is application/vnd.api+json #5165

Closed
jazwiecki opened this issue Sep 19, 2019 · 2 comments · Fixed by #5166
Closed

response.body isn't converted to JSON if content-type is application/vnd.api+json #5165

jazwiecki opened this issue Sep 19, 2019 · 2 comments · Fixed by #5166
Labels
pkg/server This is due to an issue in the packages/server directory

Comments

@jazwiecki
Copy link
Contributor

jazwiecki commented Sep 19, 2019

Current behavior:

If a response comes back with a content-type header set to application/vnd.api+json, response.body does not contain JSON:

Object
body: Blob {size: 4370, type: "application/vnd.api+json"}
headers: {content-length: "1877", content-type: "application/vnd.api+json"}
__proto__: Object

I think this is because request.coffee only returns JSON if content-type exactly matches application/json, based on these lines in the develop branch:

contentTypeIsJson: (response) ->
      ## TODO: use https://github.com/jshttp/type-is for this
      response?.headers?["content-type"]?.includes("application/json")

Those lines include a TODO which suggests using type-is to improve on the potential JSON content-type pattern-matching.

Desired behavior:

Cypress XHR responses with a content-type of application/vnd.api+json also have their bodies parsed as JSON. Happy to write a PR to use type-is to handle application\/[\S]+?\+?json$ if no one objects.

Steps to reproduce: (app code and test code)

This spec will write a fixture file without the bodies of the responses, they will all be undefined:

describe('Cypress does not parse JSON API', () => {
  // We declare an empty array to gather XHR responses
  const xhrData = [];
  after(() => {
    // In record mode, save gathered XHR data to local JSON file
    if (Cypress.env('RECORD')) {
      const path = './cypress/fixtures/fixture.json';
      cy.writeFile(path, xhrData);
    }
  });
  it('Works', () => {
    cy.server({
      // Here we handle all requests passing through Cypress' server
      onResponse: (response) => {
        if (Cypress.env('RECORD')) {
          const url = response.url;
          const method = response.method;
          const data = response.body;
          // We push a new entry into the xhrData array
          xhrData.push({ url, method, data });
        }
      },
    });
    // This tells Cypress to hook into any GET request
    if (Cypress.env('RECORD')) {
      cy.route({
        method: 'GET',
        url: '*',
      });
    }
    if (!Cypress.env('RECORD')) {
      cy.fixture('fixture').then((data) => {
        for (let i = 0, length = data.length; i < length; i++) {
          cy.route(data[i].method, data[i].url, data[i].data);
        }
      });
    }


    cy.visit('/');
  });
});

Versions

$ npx cypress --version
Cypress package version: 3.1.5
Cypress binary version: 3.1.5
@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels Sep 19, 2019
@jennifer-shehane jennifer-shehane added the pkg/server This is due to an issue in the packages/server directory label Sep 19, 2019
@cypress-bot cypress-bot bot added stage: work in progress and removed stage: needs review The PR code is done & tested, needs review labels Sep 20, 2019
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Sep 20, 2019

The code for this is done in cypress-io/cypress#5166, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 23, 2019

Released in 3.5.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pkg/server This is due to an issue in the packages/server directory
Projects
None yet
2 participants