Skip to content

Commit

Permalink
feat: allow setCookie API to take a hostOnly option
Browse files Browse the repository at this point in the history
  • Loading branch information
AtofStryker committed Feb 16, 2023
1 parent ab60ed4 commit 19c0663
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3550,6 +3550,7 @@ declare namespace Cypress {
domain: string
secure: boolean
httpOnly: boolean
hostOnly: boolean
expiry: number
sameSite: SameSiteStatus
}
Expand Down Expand Up @@ -6080,6 +6081,7 @@ declare namespace Cypress {
value: string
path: string
domain: string
hostOnly?: boolean
httpOnly: boolean
secure: boolean
expiry?: number
Expand Down
8 changes: 8 additions & 0 deletions cli/types/tests/cypress-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,14 @@ namespace CypressSetCookieTests {
expiry: 12345,
sameSite: 'lax',
})
cy.setCookie('name', 'value', {
domain: 'www.foobar.com',
path: '/',
secure: false,
httpOnly: false,
hostOnly: true,
sameSite: 'lax',
})
cy.setCookie('name', 'value', { log: true, timeout: 10, domain: 'localhost' })

cy.setCookie('name') // $ExpectError
Expand Down
26 changes: 26 additions & 0 deletions packages/driver/cypress/e2e/commands/cookies.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,32 @@ describe('src/cy/commands/cookies - no stub', () => {
})
})
})

it('sets the cookie on the specified domain as hostOnly and validates hostOnly property persists through related commands that fetch cookies', () => {
cy.visit('http://www.barbaz.com:3500/fixtures/generic.html')
cy.setCookie('foo', 'bar', { hostOnly: true })

cy.getCookie('foo').its('domain').should('eq', 'www.barbaz.com')
cy.getCookie('foo').its('hostOnly').should('eq', true)

cy.getCookies().then((cookies) => {
expect(cookies).to.have.lengthOf(1)

const cookie = cookies[0]

expect(cookie).to.have.property('domain', 'www.barbaz.com')
expect(cookie).to.have.property('hostOnly', true)
})

cy.getAllCookies().then((cookies) => {
expect(cookies).to.have.lengthOf(1)

const cookie = cookies[0]

expect(cookie).to.have.property('domain', 'www.barbaz.com')
expect(cookie).to.have.property('hostOnly', true)
})
})
})

describe('src/cy/commands/cookies', () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/driver/src/cy/commands/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import Promise from 'bluebird'
import $utils from '../../cypress/utils'
import $errUtils from '../../cypress/error_utils'

// TODO: add hostOnly to COOKIE_PROPS
// https://github.com/cypress-io/cypress/issues/363
// https://github.com/cypress-io/cypress/issues/17527
const COOKIE_PROPS = 'name value path secure httpOnly expiry domain sameSite'.split(' ')
const COOKIE_PROPS = 'name value path secure hostOnly httpOnly expiry domain sameSite'.split(' ')

function pickCookieProps (cookie) {
if (!cookie) return cookie
Expand Down Expand Up @@ -359,6 +356,7 @@ export default function (Commands, Cypress: InternalCypress.Cypress, cy, state,
path: '/',
secure: false,
httpOnly: false,
hostOnly: false,
log: true,
expiry: $utils.addTwentyYears(),
})
Expand Down

0 comments on commit 19c0663

Please sign in to comment.