Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 33 additions & 44 deletions lib/__tests__/cookieJar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

/* eslint vitest/expect-expect: [error, {assertFunctionNames: [expect, assertions]}] */
/* eslint vitest/no-standalone-expect: [error, {additionalTestBlockFunctions: [apiVariants, beforeEach]}]*/

import {
afterAll,
Expand Down Expand Up @@ -1539,19 +1540,16 @@ describe.each(['local', 'example', 'invalid', 'localhost', 'test'])(
rejectPublicSuffixes: true,
allowSpecialUseDomain: false,
})
try {
await cookieJar.setCookie(
await expect(
cookieJar.setCookie(
`settingThisShouldPass=true; Domain=dev.${specialUseDomain}; Path=/;`,
`http://dev.${specialUseDomain}`,
)
} catch (e) {
if (!(e instanceof Error)) {
throw new Error('This should be an error instance')
}
expect(e.message).toBe(
`Cookie has domain set to the public suffix "${specialUseDomain}" which is a special use domain. To allow this, configure your CookieJar with {allowSpecialUseDomain: true, rejectPublicSuffixes: false}.`,
)
}
),
).rejects.toThrow(
`Cookie has domain set to the public suffix "${
specialUseDomain
}" which is a special use domain. To allow this, configure your CookieJar with {allowSpecialUseDomain: true, rejectPublicSuffixes: false}.`,
)
})
},
)
Expand Down Expand Up @@ -1590,19 +1588,16 @@ describe.each(['local', 'example', 'invalid', 'localhost', 'test'])(
it('should reject special domain cookies if allowSpecialUseDomain is set to the default value', async () => {
expect.assertions(1)
const cookieJar = new CookieJar()
try {
await cookieJar.setCookie(
await expect(
cookieJar.setCookie(
`settingThisShouldPass=true; Domain=${specialUseDomain}; Path=/;`,
`http://${specialUseDomain}`,
)
} catch (e) {
if (!(e instanceof Error)) {
throw new Error('This should be an error instance')
}
expect(e.message).toBe(
`Cookie has domain set to the public suffix "${specialUseDomain}" which is a special use domain. To allow this, configure your CookieJar with {allowSpecialUseDomain: true, rejectPublicSuffixes: false}.`,
)
}
),
).rejects.toThrow(
`Cookie has domain set to the public suffix "${
specialUseDomain
}" which is a special use domain. To allow this, configure your CookieJar with {allowSpecialUseDomain: true, rejectPublicSuffixes: false}.`,
)
})
}

Expand Down Expand Up @@ -1638,19 +1633,16 @@ describe.each(['local', 'example', 'invalid', 'localhost', 'test'])(
rejectPublicSuffixes: true,
allowSpecialUseDomain: true,
})
try {
await cookieJar.setCookie(
await expect(
cookieJar.setCookie(
`settingThisShouldPass=true; Domain=${specialUseDomain}; Path=/;`,
`http://${specialUseDomain}`,
)
} catch (e) {
if (!(e instanceof Error)) {
throw new Error('This should be an error instance')
}
expect(e.message).toBe(
`Cookie has domain set to the public suffix "${specialUseDomain}" which is a special use domain. To allow this, configure your CookieJar with {allowSpecialUseDomain: true, rejectPublicSuffixes: false}.`,
)
}
),
).rejects.toThrow(
`Cookie has domain set to the public suffix "${
specialUseDomain
}" which is a special use domain. To allow this, configure your CookieJar with {allowSpecialUseDomain: true, rejectPublicSuffixes: false}.`,
)
})
}

Expand All @@ -1660,19 +1652,16 @@ describe.each(['local', 'example', 'invalid', 'localhost', 'test'])(
rejectPublicSuffixes: true,
allowSpecialUseDomain: false,
})
try {
await cookieJar.setCookie(
await expect(
cookieJar.setCookie(
`settingThisShouldPass=true; Domain=${specialUseDomain}; Path=/;`,
`http://${specialUseDomain}`,
)
} catch (e) {
if (!(e instanceof Error)) {
throw new Error('This should be an error instance')
}
expect(e.message).toBe(
`Cookie has domain set to the public suffix "${specialUseDomain}" which is a special use domain. To allow this, configure your CookieJar with {allowSpecialUseDomain: true, rejectPublicSuffixes: false}.`,
)
}
),
).rejects.toThrow(
`Cookie has domain set to the public suffix "${
specialUseDomain
}" which is a special use domain. To allow this, configure your CookieJar with {allowSpecialUseDomain: true, rejectPublicSuffixes: false}.`,
)
})
},
)
Expand Down
2 changes: 2 additions & 0 deletions lib/__tests__/cookiePrefixes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ let cookieJar: CookieJar
const insecureUrl = 'http://www.example.com'
const secureUrl = 'https://www.example.com'

/* eslint vitest/no-standalone-expect: [error, {additionalTestBlockFunctions: [beforeEach]}]*/

describe('When `prefixSecurity` is enabled for `CookieJar`', () => {
describe('silent', () => {
beforeEach(() => {
Expand Down
20 changes: 2 additions & 18 deletions lib/__tests__/ietf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,14 @@ describe('IETF http state tests', () => {
it.each(exampleDates)(
`ietf_data/dates/examples: $test`,
({ test, expected }) => {
if (expected) {
const date = parseDate(test)
if (!date) {
throw new Error('This should have parsed')
}
expect(date.toUTCString()).toBe(expected)
} else {
expect(parseDate(test)).toBeUndefined()
}
expect(parseDate(test)?.toUTCString()).toBe(expected || undefined)
},
)

it.each(bsdExampleDates)(
`ietf_data/dates/bsd_examples: $test`,
({ test, expected }) => {
if (expected) {
const date = parseDate(test)
if (!date) {
throw new Error('This should have parsed')
}
expect(date.toUTCString()).toBe(expected)
} else {
expect(parseDate(test)).toBeUndefined()
}
expect(parseDate(test)?.toUTCString()).toBe(expected || undefined)
},
)
})
Expand Down
36 changes: 14 additions & 22 deletions lib/__tests__/jarSerialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,32 +253,24 @@ describe('cookieJar serialization', () => {
domain?: string
path?: string
}
expect(typeof parsedValue.domain).toBe('string')
expect(typeof parsedValue.path).toBe('string')
// eslint-disable-next-line vitest/no-conditional-expect
expect(parsedValue.domain).toBeTypeOf('string')
// eslint-disable-next-line vitest/no-conditional-expect
expect(parsedValue.path).toBeTypeOf('string')
}

if (
serializedCookie.key === 'infExp' ||
serializedCookie.key === 'max'
) {
expect(serializedCookie.expires).toBeFalsy()
} else {
expect(serializedCookie.expires).toBe(expires.toISOString())
}
const expiry =
serializedCookie.key === 'infExp' || serializedCookie.key === 'max'
? undefined
: expires.toISOString()
expect(serializedCookie.expires).toBe(expiry)

if (serializedCookie.key === 'max') {
expect(serializedCookie.maxAge).toBe(3600)
} else {
expect(serializedCookie.maxAge).toBeUndefined()
}
const maxAge = serializedCookie.key === 'max' ? 3600 : undefined
expect(serializedCookie.maxAge).toBe(maxAge)

if (serializedCookie.key === 'flags') {
expect(serializedCookie.secure).toBe(true)
expect(serializedCookie.httpOnly).toBe(true)
} else {
expect(serializedCookie.secure).toBeUndefined()
expect(serializedCookie.httpOnly).toBeUndefined()
}
const flag = serializedCookie.key === 'flags' ? true : undefined
expect(serializedCookie.secure).toBe(flag)
expect(serializedCookie.httpOnly).toBe(flag)

expect(serializedCookie.hostOnly).toBe(serializedCookie.key === 'honly')

Expand Down
2 changes: 2 additions & 0 deletions lib/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,11 @@ describe('Cookie.parse', () => {

const value = input === undefined ? undefined : input.valueOf()
const cookie = Cookie.parse(value as string, parseOptions)
// eslint-disable-next-line vitest/no-conditional-expect
expect(cookie).toEqual(output && expect.objectContaining(output))

if (cookie && typeof assertValidateReturns === 'boolean') {
// eslint-disable-next-line vitest/no-conditional-expect
expect(cookie.validate()).toBe(assertValidateReturns)
}
})
Expand Down
6 changes: 3 additions & 3 deletions lib/cookie/cookieJar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ function isHostPrefixConditionMet(cookie: Cookie): boolean {
!startsWithHostPrefix ||
Boolean(
cookie.secure &&
cookie.hostOnly &&
cookie.path != null &&
cookie.path === '/',
cookie.hostOnly &&
cookie.path != null &&
cookie.path === '/',
)
)
}
Expand Down
Loading