-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Stubbing not working for 3xx redirect situations due to "Location" header #3190
Comments
Reproducible code it('302 route', () => {
cy.visit('http://example.cypress.io/commands/network-requests')
cy.server()
cy.route({
method: 'GET',
url: 'comments/*',
status: 302,
response: '',
headers: {
'Location': 'https://example.cypress.io/'
}
}).as('getComments')
cy.get('.network-btn').click()
cy.wait('@getComments').its('status').should('eq', 302)
}) Edited with correct reproducible example |
should be But this would fail and redirect would never occur: it('302 route', () => {
cy.visit('http://example.cypress.io/commands/network-requests')
cy.server()
cy.route({
method: 'GET',
url: 'comments/*',
status: 302,
response: '',
headers: {
'Location': 'https://example.cypress.io/'
}
}).as('getComments')
cy.get('.network-btn').click()
cy.wait('@getComments').its('status').should('eq', 302)
}) |
Oops, my bad, copy pasted too quickly. Thanks for clarifying. I'll update my example. |
@jennifer-shehane please let me know if some additional info is required from me in order get past |
Any progress with this issue? |
No work has been done on this feature. We will update when work is done. |
There was some discussion that this may not be a bug, but reflecting the behavior of how 302 redirects work - by essentially returning a 200 after redirection. I haven't really found the evidence to support this from the spec or any other docs. @brian-mann |
This is apparently how it works in Cypress under the hood right now but IMHO stubbed endpoint should work as described in options and different URI from location header should be the one that should return 200 status code. Currently it is impossible to write tests that verify |
@jennifer-shehane any news about when this could be fixed? |
This issue still occurs in 9.5.3. |
Yes, true. |
I'm building an authentication app which relies on 302 redirects for api calls, and am running into a similar issue. Example: user enters credentials, I make a fetch POST request with creds which will return a 302 redirect if authentication was successful. Since it's a fetch api call that is returning a 302, the user won't automatically be navigated to the url in the Location header. To fix that I send the request with Unfortunately when trying to stub the Location header in cypress, cypress doesn't seem to pass this Location header url along when using I can't share my code directly but the cypress redirect looks something like this: cy.intercept("POST", "/login", { // login request is made with "redirect: manual"
statusCode: 302,
type: "opaqueredirect",
headers: {
Location: "http://location.the.user.is.supposed.to.go", // app navigates to /login instead
},
}); |
To be clear, I understand that opaqueredirect does not give you access to the headers at all for security reasons, so I'm not attempting to access the redirect via the Location header directly, but rather through the response.url property. When running in a real app/browser, the fetch response.url becomes the value that was in the Location header. In cypress the response.url remains the original url. |
This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided. |
This issue has been closed due to inactivity. |
Current behavior:
When "Location" header is stubbed (common case for 301 and 302 redirects), cypress actually does not set stubbed response status code and header.
While Chrome devtools network tab shows response status as it was stubbed, application under test and Cypress itself interprets response with status code 200 and without "Location" header.
Same works with any other header I tried. Wild guess is that "Location" header is internally messing with Cypress' own
cy.location()
method orwindow.location
.Desired behavior:
Response should have 302 status code and "Location" header.
Versions
Cypress 3.1.3,
MacOS 10.14.2
Chrome 71
The text was updated successfully, but these errors were encountered: