Skip to content

Commit

Permalink
Merge branch 'master' into avoid-void
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhsf authored Jan 16, 2024
2 parents 0945bfe + ed834af commit decbc1d
Show file tree
Hide file tree
Showing 12 changed files with 494 additions and 688 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
interval: "monthly"
labels:
- "dependencies"
- "npm"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ Base class for CookieJar stores. Available as `tough.Store`.

### Store API

The storage model for each `CookieJar` instance can be replaced with a custom implementation. The default is `MemoryCookieStore` which can be found in [`lib/memstore.js`](https://github.com/salesforce/tough-cookie/blob/master/lib/memstore.js). The API uses continuation-passing-style to allow for asynchronous stores.
The storage model for each `CookieJar` instance can be replaced with a custom implementation. The default is `MemoryCookieStore` which can be found in [`lib/memstore.ts`](https://github.com/salesforce/tough-cookie/blob/master/lib/memstore.ts). The API uses continuation-passing-style to allow for asynchronous stores.

Stores should inherit from the base `Store` class, which is available as a top-level export.

Expand Down
3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const config: JestConfigWithTsJest = {
testEnvironment: 'node',
rootDir: './lib/',
testPathIgnorePatterns: ['./lib/__tests__/data/'],
fakeTimers: {
enableGlobally: true
}
}

export default config
2 changes: 0 additions & 2 deletions lib/__tests__/cookie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

import { Cookie } from '../cookie/cookie'

jest.useFakeTimers()

// ported from test/api_test.js (cookie tests)
describe('Cookie', () => {
let cookie: Cookie
Expand Down
42 changes: 40 additions & 2 deletions lib/__tests__/cookieJar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import { MemoryCookieStore } from '../memstore'
import { Store } from '../store'
import { ParameterError } from '../validators'

jest.useFakeTimers()

// ported from:
// - test/api_test.js (cookie jar tests)
// - test/cookie_jar_test.js
Expand Down Expand Up @@ -1203,6 +1201,46 @@ it('should fix issue #282 - Prototype pollution when setting a cookie with the d
expect('/notauth' in pollutedObject).toBe(false)
})

it('should fix issue #154 - Expiry should not be affected by creation date', async () => {
const now = Date.now()
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(now + 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(now + 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: 0 additions & 2 deletions lib/__tests__/cookieSorting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { Cookie } from '../cookie/cookie'
import { cookieCompare } from '../cookie/cookieCompare'
import { CookieJar } from '../cookie/cookieJar'

jest.useFakeTimers()

describe('Cookie sorting', () => {
describe('assumptions', () => {
it('should set the creation index during construction', () => {
Expand Down
2 changes: 0 additions & 2 deletions lib/__tests__/cookieToAndFromJson.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Cookie } from '../cookie/cookie'

jest.useFakeTimers()

describe('Cookie.toJSON()', () => {
it('should serialize a cookie to JSON', () => {
const cookie = Cookie.parse(
Expand Down
2 changes: 0 additions & 2 deletions lib/__tests__/jarSerialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import { MemoryCookieStore } from '../memstore'
import { Store } from '../store'
import { version } from '../version'

jest.useFakeTimers()

describe('cookieJar serialization', () => {
it('should use the expected version', () => {
expect(version).toBe('5.0.0-rc.0')
Expand Down
2 changes: 0 additions & 2 deletions lib/__tests__/lifetime.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Cookie } from '../cookie/cookie'

jest.useFakeTimers()

describe('Lifetime', () => {
it('should be able to set a TTL using max-age', () => {
const cookie = new Cookie()
Expand Down
4 changes: 1 addition & 3 deletions lib/__tests__/nodeUtilFallback.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import util from 'util'
import { Cookie } from '../cookie/cookie'
import { CookieJar } from '../cookie/cookieJar'
import { MemoryCookieStore, inspectFallback } from '../memstore'
import { inspectFallback, MemoryCookieStore } from '../memstore'
import { getCustomInspectSymbol, getUtilInspect } from '../utilHelper'

jest.useFakeTimers()

describe('Node util module fallback for non-node environments', () => {
describe('getCustomInspectSymbol', () => {
it('should not be null in a node environment', () => {
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

0 comments on commit decbc1d

Please sign in to comment.