On a site, I have a caching infrastructure like this:
- Redis cache, to cache database-queries.
- Varnish cache, to cache application responses (HTML, CSS, JS, etc.).
- Browser cache (Cypress clears this between each test - so far, so good!).
After a deploy, I would like to receive a 'fresh' and uncached version of the site for my Cypress-tests. I don't want to clear the cache for all visitors. I just want my Cypress-tests to see an uncached version, after a deploy.
My idea was add a header to all my cy.visit and cy.request, and then setting up my Varnish-cache settings, to look for that. And handling it in Redis as well.
It appears to be quite cumbersome, but doable, to actually do this 'bypass-cache'-setup in Varnish and Redis. If anyone has a better/simpler way of doing this, I'm all ears.
What the core of this question is: 'How do I send a header with all my cy.visit and cy.request, without having to type it out like this, every time (see below)?
cy.request({
method: 'GET',
url: 'https://example.org',
headers: {
'x-my-custom-header': 'myValue'
}
}).then((response) => {
// ...
});
cy.visit('/', {
headers: {
'x-my-custom-header': 'myValue'
}});
My solution considerations
I considered building my own custom command on top of this. But it gets long-tailed and clumbsy quickly.
I also considered, if I could intercept all requests and add the header to it, in beforeEach in my e2e.js. I couldn't get this to work, though. This was my attempt:
beforeEach(() => {
cy.intercept('*', (req) => {
req.headers['x-foo'] = 'bar';
});
});
Requests issued from the test (handled with intercept)
You haven't mentioned the problem with the intercept, just that there is one. If there are other intercepts in the pipeline, the
middlewareoption ensures the headers are added before those other intercepts are called.Passing a request to the next request handler
Example
Requests issued from the test (not handled by intercept)
Using a custom command to add header to all
cy.request()