Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the cookie_behavior tests by syncing cookies immediately if … #25855

Merged
merged 7 commits into from
Feb 20, 2023
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ _Released 03/1/2023 (PENDING)_
**Bugfixes:**

- Fixed an issue where cookies were being duplicated with the same hostname, but a prepended dot. Fixed an issue where cookies may not be expiring correctly. Fixes [#25174](https://github.com/cypress-io/cypress/issues/25174), [#25205](https://github.com/cypress-io/cypress/issues/25205) and [#25495](https://github.com/cypress-io/cypress/issues/25495).
- Fixed an issue where cookies weren't being synced when the application was stable. Fixed in [#25855](https://github.com/cypress-io/cypress/pull/25855). Fixes [#25835](https://github.com/cypress-io/cypress/issues/25835).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a user-facing fix or just a test fix? if its just for tests, this does't need an entry

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also user facing since the cookies will now sync immediately if Cypress is stable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does that mean to users though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in certain cases cookies weren't synced back to the automation client when using cy.origin (or if a cross origin injection occurs) if the application is already stable. This is mainly problematic for setting cookies inside cy.origin after the page has loaded / is stable that the app couldnt otherwise set


## 12.6.0

Expand Down
92 changes: 20 additions & 72 deletions packages/driver/cypress/e2e/e2e/origin/cookie_behavior.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,7 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {

// 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 @@ -298,17 +288,7 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {

// 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 @@ -376,17 +356,7 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
})

// 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 @@ -418,17 +388,7 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
})

// 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 @@ -529,14 +489,11 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
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 @@ -572,12 +529,10 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
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 @@ -649,14 +604,11 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
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 @@ -695,14 +647,10 @@ describe('Cookie Behavior', { browser: '!webkit' }, () => {
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
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
AtofStryker marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore
Cypress.once('before:stability:release', () => {

const addCookiesCallback = () => {
AtofStryker marked this conversation as resolved.
Show resolved Hide resolved
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)
}
})
}