cypress-io / cypress

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

Redirect in XHR not handled properly #2702

Closed eroteev closed 3 years ago

eroteev commented 6 years ago

Redirects in XHR do not work in the test runner started by Cypress.io (both inside the test itself or when I open the application in a new tab). The functionality works as expected in regular chrome.

Steps to reproduce:

  1. Open web application.
  2. Click button which sends POST XHR Request to the server.
  3. The server returns Redirect response with X-Redirect header (https://www.yiiframework.com/doc/api/2.0/yii-web-controller#redirect()-detail)
  4. The application has set callback for $(document).ajaxComplete() which is supposed to assign the new url, but this callback is never triggered.

Current behavior:

Response is returned with Location: undefined

Desired behavior:

The browser should be redirected to the new url. (Works in Chrome/Firefox/Opera/Safari)

Versions

Cypress.io version: 3.1.0 Chrome version: 69.0.3497.100 (Official Build) (64-bit)

Response headers in Chrome browser

HTTP/1.1 302 Found
Server: openresty
Date: Thu, 01 Nov 2018 14:42:27 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
X-Redirect: http://app.local/post/123/edit

Response headers in a Chrome browser controlled by Cypress.io Test Runner

HTTP/1.1 302 Found
server: openresty
date: Thu, 01 Nov 2018 14:40:07 GMT
Content-Type: text/plain; charset=utf-8
transfer-encoding: chunked
connection: close
expires: Thu, 19 Nov 1981 08:52:00 GMT
cache-control: no-store, no-cache, must-revalidate
pragma: no-cache
x-redirect: http://app.local/post/123/edit
Location: undefined
Vary: Accept, Accept-Encoding
Content-Length: 31

Additional notes

The application is based on Yii2 Framework and in the link that I have pointed out above is explained that redirects in AJAX mode normally won't work as expected. Therefore the framework provides Javascript code that reads the X-Redirect header and switches the location of the browser.

In the tests I performed it looks like this code is never executed in Cypress.io during the redirect response:

$(document).ajaxComplete(function (event, xhr) {
        var url = xhr && xhr.getResponseHeader('X-Redirect');
        if (url) {
           window.location.assign(url);
        }
    });

Also maybe this has something to do with the additional "Location: undefined" header.

TomaszG commented 3 years ago

I had the same issue some time ago, checked it now on 6.2.0 and it seems to be fixed.

jennifer-shehane commented 3 years ago

Since this issue hasn't had activity in a while, we'll close the issue until we can confirm this is still happening. Please comment if there is new information to provide concerning the original issue and we'd be happy to reopen.