I'm trying to authenticate at a different domain as part of a Cypress test using cy.request
. The authentication request needs to contain the value of the XSRF-TOKEN cookie as a header. This is easily solved when on the same baseURL as the authentication domain: visit the domain, read the cookie via cy.getCookie
, then make the request. Since cypress does not allow visiting multiple domains in a single test a different workflow is needed.
My solution right now is to replace the initial cy.visit
with a cy.reqeuest
(this sets the cookie as verified by looking at the request headers of the second request). However, I can't figure out how to read this cookie before I make the second authentication request. cy.getCookies()
is empty, and document.cookie
is empty. The response of the cy.request
only contains a "set-cookie" header the first time, and I can't figure out how to read the default headers of the cy.request
since cy.intercept
does not work with cy.request
.
Sketch of my attempt (where c.value
is null
):
cy.request({url: "https://notbaseurl/login"}).then(res => {
cy.getCookie("XSRF-TOKEN").then(c => {
cy.request({url: "https://notbaseurl/auth", method: "POST", headers: {"X-XSRF-TOKEN": c.value}})
})
})