2

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}})
  })
})
  • I think that `cy.getCookie` only gets the cookie from the domain that you call `cy.visit` on. However, [this](https://github.com/cypress-io/cypress/issues/3221#issuecomment-498274788) workaround might sort of help you out. Alternatively, you could parse the response headers for the cookie if `cy.getCookie` doesn't work out – natn2323 Feb 22 '21 at 21:36

1 Answers1

1

This is not currently possible. See https://github.com/cypress-io/cypress/issues/8956

aryzing
  • 4,982
  • 7
  • 39
  • 42
  • Did the work around in the github issue worked for you? https://github.com/cypress-io/cypress/issues/8956#issuecomment-1028288886 – Gregoire Cattan Oct 14 '22 at 05:58