cypress: cy.request doesn't set cookie with specific domain
Current behavior:
baseURLis set to: http://localhost:3000
cy.request() is not setting cookies that comes with a domain.

Desired behavior:
cy.request() should set cookies with specific domain.
Steps to reproduce: (app code and test code)
describe('Cookie', () => {
Cypress.Cookies.debug(true);
it('SHOULD set the cookie if it have domain', () => {
cy.request({
method: 'POST',
url: 'http://localhost:3000/cookie-with-domain'
})
.then(()=> {
cy.getCookie('domain-cookie')
.should('have.property', 'value', '1');
});
});
it('Does set the cookie', () => {
cy.request({
method: 'POST',
url: 'http://localhost:3000/cookie-without-domain'
})
.then(()=> {
cy.getCookie('cookie')
.should('have.property', 'value', '1');
});
});
it('Works fine on both', () => {
cy.visit('http://localhost:3000/cookie-with-domain');
cy.visit('http://localhost:3000/cookie-without-domain');
cy.getCookie('domain-cookie')
.should('have.property', 'value', '1');
cy.getCookie('cookie')
.should('have.property', 'value', '1');
});
});
Using expressjs on the server and this:
app.all('/cookie-with-domain', (req, res)=>{
res.cookie('domain-cookie',1, { maxAge: 900000, httpOnly: true, domain: 'localhost'});
res.send('Hello!');
});
app.all('/cookie-without-domain', (req, res)=>{
res.cookie('cookie',1, { maxAge: 900000, httpOnly: true});
res.send('Hello!');
});
Versions
Cypress: 3.1.1 OS: Ubuntu 18.04 Browser: Chrome 71
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 21 (7 by maintainers)
Hello team,
Do you have any news about this regression? Unfortunately, the workarounds are not the final solution.
Thanks for information.
I just checked the reproducible example provided in the OP and it appears to be fixed starting with Cypress 3.5.0. Please upgrade your version of Cypress to the latest to get the fix for this issue.
My workaround, which is similar to @rewop 's one:
After that, I use the regular page navigation with a click() function and navigation works correctly.
In my opinion, there are two problems here:
cy.request()does not set cookies automatically (login endpoint responds with HTTP 302 and cookie with domain name set)cy.visit()does not attach cookies automaticallyI hope I saved someone hours of experimenting.
It looks like I am facing the same problem. However to me it seems that the error only happen if
Domain="localhost"is set. If theDomain=is present and set to something else cookies are set as expected.Our work around is setting the cookie without domain
For information, this issue appears to be complete killing my ability to use Cypress against our application (and appreciate that this could be a limitation due to how our app works).
During the tests we need to interact with our API as an Administrator to create specific organizations to isolate tests. Once that’s done we shift to a specific tenant domain which responds correctly with a domain cookie in the response header. The tenant domain cookie isn’t set, and we end up with unauthenticated errors.
I’ve tried working around this by parsing the set-cookie header and then setting a cookie with the following code (without any luck so far):
I figured the above would work…but no luck yet.