I have an XHR request that responds normally with a status 200 and some data.
There is one case where the response is enriched with some header data. I want to simulate this case.
This is the mock setup:
cy.server().route({
method: 'POST',
url: /some-url/,
headers: { key: 'value' }
});
The resulting call is a non stubbed call but also the key: value headers are missing. They are not missing when I add a stubbed response to the route config.
The same happens with status.
Any progress on this stale issue? Should this at least be removed from the docs if it isnt a feature?
I was trying to stub a 500 status code without stubbing the response itself and it wasnt working. The XHR kept returning a 200 response.
Passing an empty array as response solved it.
I feel that the docs are rather clear on the point that the response determines a routes stubbing or not.
If you do not pass a response to a route, Cypress will pass the request through without stubbing it. We can still wait for the request to resolve later.
-- https://docs.cypress.io/api/commands/route.html#Without-Stubbing
If you pass a response to cy.route(), Cypress will stub the response in the request.
-- https://docs.cypress.io/api/commands/route.html#With-Stubbing
But, we are always looking for ways to improve our docs if there are some areas that were confusing you could point out.
Like @fvanwijk, I am trying to get cypress to add some headers to certain XHR requests. If his example isn't how you do it, then how do you do it?
You can't currently stub any options of a request unless you also stub the response. This is a feature that would have to be added to Cypress.
Oh, well maybe its worth explicitly putting that note in the docs? I seemed to think it was supported back when i was trying this 馃榾
I dont think its explicit enough as well :)
I would agree that it would be a very useful feature. For example on our project we rely on the Cloudfront cloudfront-viewer-country header to geolocate our users so I would like to add that header when mocking the page request without modifying anything else in the response.
Without it, I can't think of any other way to test our geolocation feature.
Any update on this feature?
This is part of the work planned within #4176 which is currently in progress.
Released in 5.1.0.
This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v5.1.0, please open a new issue.
The features requested in this issue are now possible as part of cy.route2().
cy.route2() is currently experimental and requires being enabled by passing "experimentalNetworkStubbing": true through your Cypress configuration. This will eventually be merged in as part of our standard API.
Please see the cy.route2() docs for full details: https://on.cypress.io/route2
If you encounter any issues or unexpected behavior while using cy.route2() we encourage you to open a new issue so that we can work out all the issues before public release. Thanks!
Most helpful comment
I would agree that it would be a very useful feature. For example on our project we rely on the Cloudfront
cloudfront-viewer-countryheader to geolocate our users so I would like to add that header when mocking the page request without modifying anything else in the response.Without it, I can't think of any other way to test our geolocation feature.