Skip to content

Commit

Permalink
fix: fix the cookie_behavior tests by syncing cookies immediately if …
Browse files Browse the repository at this point in the history
…the application is already stable
  • Loading branch information
AtofStryker committed Feb 16, 2023
1 parent 4101f91 commit 63260bd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 106 deletions.
143 changes: 40 additions & 103 deletions packages/driver/cypress/e2e/e2e/origin/cookies/request_behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,8 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(window.makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/set-cookie-credentials?cookie=foo1=bar1; Domain=foobar.com`, 'xmlHttpRequest', true))
})

// though request is cross origin, site should have access directly to cookie because it is same site
// assert cookie value is actually set in the browser
// current expected assertion. NOTE: This SHOULD be consistent
if (Cypress.isBrowser('firefox')) {
// firefox actually sets the cookie correctly
cy.getCookie('foo1').its('value').should('equal', 'bar1')
} else {
cy.getCookie('foo1').should('equal', null)
}

// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
// future expected assertion
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
// but send the cookies in the request
Expand Down Expand Up @@ -294,19 +283,8 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(window.makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/set-cookie-credentials?cookie=foo1=bar1; Domain=foobar.com`, 'xmlHttpRequest', true))
})

// though request is cross origin, site should have access directly to cookie because it is same site
// assert cookie value is actually set in the browser
// current expected assertion. NOTE: This SHOULD be consistent
if (Cypress.isBrowser('firefox')) {
// firefox actually sets the cookie correctly
cy.getCookie('foo1').its('value').should('equal', 'bar1')
} else {
cy.getCookie('foo1').should('equal', null)
}

// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
// future expected assertion
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
// but send the cookies in the request
Expand Down Expand Up @@ -374,17 +352,7 @@ export default function (scheme: 'http' | 'https' = 'http') {
})

// assert cookie value is actually set in the browser
// current expected assertion.
if (Cypress.isBrowser('firefox')) {
// firefox actually sets the cookie correctly
cy.getCookie('foo1').its('value').should('equal', 'bar1')
} else {
cy.getCookie('foo1').should('equal', null)
}

// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
// future expected assertion
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
return cy.wrap(window.makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/test-request-credentials`, 'fetch', 'include'))
Expand Down Expand Up @@ -416,17 +384,7 @@ export default function (scheme: 'http' | 'https' = 'http') {
})

// assert cookie value is actually set in the browser
// current expected assertion. NOTE: This SHOULD be consistent
if (Cypress.isBrowser('firefox')) {
// firefox actually sets the cookie correctly
cy.getCookie('foo1').its('value').should('equal', 'bar1')
} else {
cy.getCookie('foo1').should('equal', null)
}

// FIXME: Ideally, browser should have access to this cookie. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
// future expected assertion
// cy.getCookie('foo1').its('value').should('equal', 'bar1')
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
return cy.wrap(window.makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/test-request-credentials`, 'fetch'))
Expand Down Expand Up @@ -527,14 +485,11 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'xmlHttpRequest', true))
})

// assert cookie value is actually set in the browser
if (scheme === 'https') {
// FIXME: cy.getCookie does not believe this cookie exists. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')
} else {
cy.getCookie('bar1').should('equal', null)
}
Expand Down Expand Up @@ -570,12 +525,10 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'xmlHttpRequest', true))
})

// FIXME: cy.getCookie does not believe this cookie exists. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')

cy.window().then((win) => {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/test-request-credentials`, 'xmlHttpRequest', true))
Expand Down Expand Up @@ -647,14 +600,11 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'fetch', 'include'))
})

// assert cookie value is actually set in the browser
if (scheme === 'https') {
// FIXME: cy.getCookie does not believe this cookie exists. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')
} else {
cy.getCookie('bar1').should('equal', null)
}
Expand Down Expand Up @@ -693,14 +643,10 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'fetch', 'include'))
})

// assert cookie value is actually set in the browser

// FIXME: cy.getCookie does not believe this cookie exists, though it is set in the browser. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')

cy.window().then((win) => {
return cy.wrap(window.makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/test-request-credentials`, 'fetch', 'include'))
Expand Down Expand Up @@ -1022,7 +968,7 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/set-cookie-credentials?cookie=foo1=bar1; Domain=foobar.com`, 'xmlHttpRequest', true))
})

// firefox actually sets the cookie correctly
// assert cookie value is actually set in the browser
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
Expand All @@ -1048,6 +994,7 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/set-cookie-credentials?cookie=foo1=bar1; Domain=foobar.com`, 'xmlHttpRequest', true))
})

// assert cookie value is actually set in the browser
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
Expand Down Expand Up @@ -1095,6 +1042,7 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/set-cookie-credentials?cookie=foo1=bar1; Domain=foobar.com`, 'fetch', 'include'))
})

// assert cookie value is actually set in the browser
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
Expand All @@ -1118,6 +1066,7 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(makeRequest(win, `${scheme}://app.foobar.com:${crossOriginPort}/set-cookie-credentials?cookie=foo1=bar1; Domain=foobar.com`, 'fetch', 'include'))
})

// assert cookie value is actually set in the browser
cy.getCookie('foo1').its('value').should('equal', 'bar1')

cy.window().then((win) => {
Expand Down Expand Up @@ -1190,14 +1139,11 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'xmlHttpRequest', true))
})

// assert cookie value is actually set in the browser
if (scheme === 'https') {
// FIXME: cy.getCookie does not believe this cookie exists, though it is set in the browser. Should be fixed in https://github.com/cypress-io/cypress/pull/23643.
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')
} else {
cy.getCookie('bar1').should('equal', null)
}
Expand All @@ -1223,12 +1169,10 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'xmlHttpRequest', true))
})

// FIXME: cy.getCookie does not believe this cookie exists, though it is set in the browser. Should be fixed in https://github.com/cypress-io/cypress/pull/23643
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')

cy.window().then((win) => {
return cy.wrap(makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/test-request-credentials`, 'xmlHttpRequest', true))
Expand Down Expand Up @@ -1278,14 +1222,11 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'fetch', 'include'))
})

// assert cookie value is actually set in the browser
if (scheme === 'https') {
// FIXME: cy.getCookie does not believe this cookie exists, though it is set in the browser. Should be fixed in https://github.com/cypress-io/cypress/pull/23643
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')
} else {
cy.getCookie('bar1').should('equal', null)
}
Expand Down Expand Up @@ -1314,14 +1255,10 @@ export default function (scheme: 'http' | 'https' = 'http') {
return cy.wrap(makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/set-cookie-credentials?cookie=bar1=baz1; Domain=barbaz.com; SameSite=None; Secure`, 'fetch', 'include'))
})

// assert cookie value is actually set in the browser

// FIXME: cy.getCookie does not believe this cookie exists, though it is set in the browser. Should be fixed in https://github.com/cypress-io/cypress/pull/23643
cy.getCookie('bar1').should('equal', null)
// can only set third-party SameSite=None with Secure attribute, which is only possibly over https

//expected future assertion
// cy.getCookie('bar1').its('value').should('equal', 'baz1')
// assert cookie value is actually set in the browser, even if in a different domain
cy.getCookie('bar1', {
domain: 'barbaz.com',
}).its('value').should('equal', 'baz1')

cy.window().then((win) => {
return cy.wrap(makeRequest(win, `${scheme}://www.barbaz.com:${sameOriginPort}/test-request-credentials`, 'fetch', 'include'))
Expand Down
18 changes: 15 additions & 3 deletions packages/driver/src/cross-origin/events/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const handleCrossOriginCookies = (Cypress: ICypress) => {
// this event allows running a handler before stability is released.
// this prevents subsequent commands from running until the cookies
// are set via automation
// @ts-ignore
Cypress.once('before:stability:release', () => {

const addCookiesCallback = () => {
const cookies = cookiesToSend

cookiesToSend = []
Expand All @@ -37,6 +37,18 @@ export const handleCrossOriginCookies = (Cypress: ICypress) => {
.catch(() => {
// errors here can be ignored as they're not user-actionable
})
})
}

// if the application is already stable, sync the cookies to the automation client immediately
if (cy.state('isStable')) {
addCookiesCallback()
} else {
// otherwise, wait until stability is achieved
// this event allows running a handler before stability is released.
// this prevents subsequent commands from running until the cookies
// are set via automation
// @ts-ignore
Cypress.once('before:stability:release', addCookiesCallback)
}
})
}

0 comments on commit 63260bd

Please sign in to comment.