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

cy.intercept request.body is ArrayBuffer for JSON requests with long Cyrillic strings #16292

Closed
KarKarbI4 opened this issue Apr 30, 2021 · 8 comments · Fixed by #16862
Closed
Labels
topic: cy.intercept() type: unexpected behavior User expected result, but got another

Comments

@KarKarbI4
Copy link

KarKarbI4 commented Apr 30, 2021

Current behavior

When sending request with cyrillic strings in json data to url, stubbed by cy.intercept, request.body object is ArrayBuffer, not plain object.

// intercept some url with any data
cy.intercept(URL, {
  body: { result: 'ok' },
}).as('testRequest');

// send json request with cyrillic string in data (length must be more than 6)
sendRequest({name: "Фрауиванович"})
cy.wait('@testRequest')
    .its('request')
    .then((req) => {
      console.log(req.body); // is ArrayBuffer object, not plain object
});

// send json request without cyrillic in data
sendRequest({name: "FrausIvanovich"})
cy.wait('@testRequest')
    .its('request')
    .then((req) => {
      console.log(req.body); // is plain object, as expected
});

Desired behavior

When sending request with cyrillic strings in json data to url, stubbed by cy.intercept, request.body is plain object

// send json request with cyrillic string in data (length must be more than 6)
sendRequest({name: "Фрауиванович"})
cy.wait('@testRequest')
    .its('request')
    .then((req) => {
      console.log(req.body); // is plain object
});

Test code to reproduce

Test code to reproduce: https://github.com/KarKarbI4/cypress-cyrillic-intercept-issue/blob/main/cypress/integration/spec.js
Test repo: https://github.com/KarKarbI4/cypress-cyrillic-intercept-issue

Versions

Cypress: cypress@7.1.0, cypress@7.2.0
Browser: Google Chrome 90
OS: MacOS Big Sur 11.2.3

@jennifer-shehane
Copy link
Member

The example you gave would 'sometimes fail' the other assertions because the requests were being made synchronously, so sometimes the 3rd definition had already run by the time the first wait ran.

Regardless, the last test below will always reliably fail, as the response is an ArrayBuffer when the response data is Фраусиванович. Seems unexpected to me.

it('cy.intercept cyrillyc issue test', () => {
  cy.intercept('https://example.com/test', {
    body: { result: 'ok' },
  }).as('testRequest');

  cy.window().then(() => {
    let xhr = new XMLHttpRequest();
    xhr.open('POST', 'https://example.com/test');
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(JSON.stringify({ name: 'Frausivanovich' }))
  })
  cy.wait('@testRequest')
    .its('request')
    .then((req) => {
      expect(req.body).to.not.be.an('ArrayBuffer'); // ✅ passes
      expect(req.body).to.be.an('object'); // ✅ passes
    });

  cy.window().then(() => {
    let xhr = new XMLHttpRequest();
    xhr.open('POST', 'https://example.com/test');
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(JSON.stringify({ name: 'Фраус' }))
  })

  cy.wait('@testRequest')
    .its('request')
    .then((req) => {
      expect(req.body).to.not.be.an('ArrayBuffer'); // ✅ passes
      expect(req.body).to.be.an('object');  // ✅ passes
    });

  cy.window().then(() => {
    let xhr = new XMLHttpRequest();
    xhr.open('POST', 'https://example.com/test');
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(JSON.stringify({ name: 'Фраусиванович' }))
  })

  cy.wait('@testRequest')
    .its('request')
    .then((req) => {
      expect(req.body).to.be.an('ArrayBuffer'); // ✅ passes
      expect(req.body).to.be.an('object'); // ❗️ fails
    });
});

@jennifer-shehane jennifer-shehane added type: unexpected behavior User expected result, but got another stage: ready for work The issue is reproducible and in scope labels May 3, 2021
@alexneo2003
Copy link

I have the same problem since version 7.0.0, but I couldn't manage to write a test to reproduce this error. In my live tests, I was getting an error but in the isolated test, nothing was reproducible. And now, thanks to @jennifer-shehane , I also managed to reproduce it.

@jk4235
Copy link

jk4235 commented May 14, 2021

same issue.
use cypress.intercept stub graphql request

cy.intercept('POST', '/graphql', (req) => {
  if (req.body.operationName.includes('ListPosts')) {
    req.alias = 'gqlListPostsQuery'
  }
console.log(req.body)
// any graphql request contains 'mutation' req.body will be arraybuffer
})

cypress 7.3.0

@sainthkh
Copy link
Contributor

It seems that the problem happens with non-ascii characters. I tested with Korean and it fails, too.

But interestingly, it passes with less characters, if I change 안녕녕 below with 안녕, it passes.

cy.window().then(() => {
  let xhr = new XMLHttpRequest()

  xhr.open('POST', 'https://example.com/test')
  xhr.setRequestHeader('Content-Type', 'application/json')
  xhr.send(JSON.stringify({ name: '안녕녕', value: 'sss' }))
})

cy.wait('@testRequest')
.its('request')
.then((req) => {
  console.log(req.body)

  expect(req.body).to.be.an('ArrayBuffer') // ✅ passes
  expect(req.body).to.be.an('object') // ❗️ fails
})

@sainthkh
Copy link
Contributor

The cause of the problem is bevry/istextorbinary#13.

@sainthkh
Copy link
Contributor

sainthkh commented Jun 2, 2021

I submited PR at bevry/istextorbinary#214. It's now awaiting external fix.

@sainthkh sainthkh added stage: awaiting external fix A 3rd party bug in Cypress - awaiting release and removed stage: ready for work The issue is reproducible and in scope labels Jun 2, 2021
@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: awaiting external fix A 3rd party bug in Cypress - awaiting release stage: work in progress labels Jun 9, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jun 11, 2021

The code for this is done in cypress-io/cypress#16862, 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 Jun 23, 2021

Released in 7.6.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v7.6.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Jun 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
topic: cy.intercept() type: unexpected behavior User expected result, but got another
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants