cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.52k stars 3.2k forks source link

cy.intercept unable to handle binary/streaming resource #24278

Open damian-nrj opened 2 years ago

damian-nrj commented 2 years ago

Current behavior

Hi. I have the following problem:

I want to cy.intercept a call that fetches music stream in order to confirm that music is actually played. Therefore, I decided to spy on a particular http call. Unfortunately, creating such interceptor hangs a call and it never fulfills. If no cy.intercept created, it would go well and return 200 OK.

After 30s cy.wait time-outs and test case fails.

image

I reached help on Cypress Discord but without success. I was suggested that it might be due to binary/streaming resource and Cypress proxy might be unable to handle it correctly.

Desired behavior

cy.intercept does not hang call to an external resource.

cy.wait fulfills.

Tests are passed.

Test code to reproduce

describe('E2E: Radio stream & Playouts', () => {
  it('Can play radio station from player', () => {
    cy.intercept({
      url: 'https://energyzuerich.mp3.energy.ch/energyzuerich-high.mp3?ua=energy+website+desktop',
    }).as('audioStream');

    cy.visit('https://energy.ch');
    // eslint-disable-next-line cypress/no-unnecessary-waiting
    cy.wait(5000);
    cy.visit('https://energy.ch');
    // eslint-disable-next-line cypress/no-unnecessary-waiting
    cy.wait(5000);

    // Play music
    cy.get('button.flex-shrink-0.w-12.h-12.p-2.cursor-pointer')
      .filter(':visible')
      .click();

    // This makes request hang and never fulfill. Later time-outs after 30s. Why?
    cy.wait('@audioStream').its('response.statusCode').should('eq', 200);

    // eslint-disable-next-line cypress/no-unnecessary-waiting
    cy.wait(3000);
  });
});

Cypress Version

10.10.0

Node version

16.13.1

Operating System

macOS 12.2.1

Debug Logs

No response

Other

No response

AtofStryker commented 2 years ago

Hi @damian-nrj . Thank you for opening an issue. I was able to reproduce the behavior you were describing in this reproduction repository. Like your comment describes, the fetch request just seems to hang and the intercept never seems to get called.

Screen Shot 2022-10-19 at 6 00 02 PM

lipingguo0 commented 1 year ago

i am having similar issue, the only difference is that mine is text/event-stream type. i can await successfully in my localhost server but failed in my real server env.

also wondering if there is way to intercept stream response with fixtures?

zalqudah-intrado commented 8 months ago

i had this issue before and to resolve it i created a command func with all the needed cy.intercept for my test cases and and run it in the beginning of my test cases and when needed i called them cy.wait(@alias).then(xhr:any) =>{}