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 expiry time not updating when cookie is updated #345

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 42 additions & 0 deletions lib/__tests__/cookieJar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@
url,
)
jest.advanceTimersByTime(2000) // so that 'h=8' expires
})

Check warning on line 501 in lib/__tests__/cookieJar.spec.ts

View workflow job for this annotation

GitHub Actions / build (latest)

File has too many lines (1526). Maximum allowed is 500

Check warning on line 501 in lib/__tests__/cookieJar.spec.ts

View workflow job for this annotation

GitHub Actions / build (lts/*)

File has too many lines (1526). Maximum allowed is 500

Check warning on line 501 in lib/__tests__/cookieJar.spec.ts

View workflow job for this annotation

GitHub Actions / build (lts/-1)

File has too many lines (1526). Maximum allowed is 500

it('should be able to get the cookies for http://nodejs.org', async () => {
const cookies = await cookieJar.getCookies('http://nodejs.org')
Expand Down Expand Up @@ -1203,6 +1203,48 @@
expect('/notauth' in pollutedObject).toBe(false)
})

it('should fix issue #154 - Expiry should not be affected by creation date', async () => {
// setting the time to zero here just to make it obvious that the expiry time is updated
jest.useFakeTimers({ now: 0 })
colincasey marked this conversation as resolved.
Show resolved Hide resolved

const jar = new CookieJar()

await jar.setCookie('foo=bar; Max-Age=60;', 'https://example.com')

const initialCookies = await jar.getCookies('https://example.com')
expect(initialCookies).toEqual([
expect.objectContaining({
key: 'foo',
value: 'bar',
path: '/',
domain: 'example.com',
maxAge: 60,
}),
])
// the expiry time should be 60s from now (0)
expect(initialCookies[0]?.expiryTime()).toBe(60 * 1000)

// advance the time by 1s, so now = 1000
jest.advanceTimersByTime(1000)

await jar.setCookie('foo=bar; Max-Age=60;', 'https://example.com')

const updatedCookies = await jar.getCookies('https://example.com')
expect(updatedCookies).toEqual([
expect.objectContaining({
key: 'foo',
value: 'bar',
path: '/',
domain: 'example.com',
maxAge: 60,
// the creation time should be unchanged as per the spec
creation: initialCookies[0]?.creation,
}),
])
// the expiry time should be 60s from now (1000)
expect(updatedCookies[0]?.expiryTime()).toBe(60 * 1000 + 1000)
})

// special use domains under a sub-domain
describe.each(['local', 'example', 'invalid', 'localhost', 'test'])(
'when special use domain is dev.%s',
Expand Down
2 changes: 1 addition & 1 deletion lib/cookie/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ export class Cookie {
// elsewhere)
expiryTime(now?: Date): number | undefined {
if (this.maxAge != null) {
const relativeTo = now || this.creation || new Date()
const relativeTo = now || this.lastAccessed || new Date()
const maxAge = typeof this.maxAge === 'number' ? this.maxAge : -Infinity
const age = maxAge <= 0 ? -Infinity : maxAge * 1000
if (relativeTo === 'Infinity') {
Expand Down
Loading