Skip to content
Open
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
151 changes: 75 additions & 76 deletions test/cookie/cookies.js

Large diffs are not rendered by default.

45 changes: 22 additions & 23 deletions test/cookie/global-headers.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
'use strict'

const { describe, test } = require('node:test')
const assert = require('node:assert')
const {
deleteCookie,
getCookies,
getSetCookies,
setCookie
} = require('../..')

describe('Using global Headers', async () => {
test('deleteCookies', { skip: !globalThis.Headers }, () => {
describe('Using global Headers', () => {
test('deleteCookies', { skip: !globalThis.Headers }, (t) => {
const headers = new globalThis.Headers()

assert.equal(headers.get('set-cookie'), null)
t.assert.strictEqual(headers.get('set-cookie'), null)
deleteCookie(headers, 'undici')
assert.equal(headers.get('set-cookie'), 'undici=; Expires=Thu, 01 Jan 1970 00:00:00 GMT')
t.assert.strictEqual(headers.get('set-cookie'), 'undici=; Expires=Thu, 01 Jan 1970 00:00:00 GMT')
})

test('getCookies', { skip: !globalThis.Headers }, () => {
test('getCookies', { skip: !globalThis.Headers }, (t) => {
const headers = new globalThis.Headers({
cookie: 'get=cookies; and=attributes'
})

assert.deepEqual(getCookies(headers), { get: 'cookies', and: 'attributes' })
t.assert.deepEqual(getCookies(headers), { get: 'cookies', and: 'attributes' })
})

test('getSetCookies', { skip: !globalThis.Headers }, () => {
test('getSetCookies', { skip: !globalThis.Headers }, (t) => {
const headers = new globalThis.Headers({
'set-cookie': 'undici=getSetCookies; Secure'
})

const supportsCookies = headers.getSetCookie()

if (!supportsCookies) {
assert.deepEqual(getSetCookies(headers), [])
t.assert.deepEqual(getSetCookies(headers), [])
} else {
assert.deepEqual(getSetCookies(headers), [
t.assert.deepEqual(getSetCookies(headers), [
{
name: 'undici',
value: 'getSetCookies',
Expand All @@ -46,23 +45,23 @@ describe('Using global Headers', async () => {
}
})

test('setCookie', { skip: !globalThis.Headers }, () => {
test('setCookie', { skip: !globalThis.Headers }, (t) => {
const headers = new globalThis.Headers()

setCookie(headers, { name: 'undici', value: 'setCookie' })
assert.equal(headers.get('Set-Cookie'), 'undici=setCookie')
t.assert.strictEqual(headers.get('Set-Cookie'), 'undici=setCookie')
})
})

describe('Headers check is not too lax', { skip: !globalThis.Headers }, () => {
class Headers { }
Object.defineProperty(globalThis.Headers.prototype, Symbol.toStringTag, {
value: 'Headers',
configurable: true
})
test('Headers check is not too lax', { skip: !globalThis.Headers }, (t) => {
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

[nitpick] This should be wrapped in a separate describe block or use test instead of being nested directly within the existing describe block. The original structure had this as a separate describe block.

Copilot uses AI. Check for mistakes.

class Headers { }
Object.defineProperty(globalThis.Headers.prototype, Symbol.toStringTag, {
value: 'Headers',
configurable: true
})

assert.throws(() => getCookies(new Headers()), { code: 'ERR_INVALID_THIS' })
assert.throws(() => getSetCookies(new Headers()), { code: 'ERR_INVALID_THIS' })
assert.throws(() => setCookie(new Headers(), { name: 'a', value: 'b' }), { code: 'ERR_INVALID_THIS' })
assert.throws(() => deleteCookie(new Headers(), 'name'), { code: 'ERR_INVALID_THIS' })
t.assert.throws(() => getCookies(new Headers()), { code: 'ERR_INVALID_THIS' })
t.assert.throws(() => getSetCookies(new Headers()), { code: 'ERR_INVALID_THIS' })
t.assert.throws(() => setCookie(new Headers(), { name: 'a', value: 'b' }), { code: 'ERR_INVALID_THIS' })
t.assert.throws(() => deleteCookie(new Headers(), 'name'), { code: 'ERR_INVALID_THIS' })
})
})
101 changes: 50 additions & 51 deletions test/cookie/is-ctl-excluding-htab.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,84 @@
'use strict'

const { test, describe } = require('node:test')
const { strictEqual } = require('node:assert')

const {
isCTLExcludingHtab
} = require('../../lib/web/cookies/util')

describe('isCTLExcludingHtab', () => {
test('should return false for 0x00 - 0x08 characters', () => {
strictEqual(isCTLExcludingHtab('\x00'), true)
strictEqual(isCTLExcludingHtab('\x01'), true)
strictEqual(isCTLExcludingHtab('\x02'), true)
strictEqual(isCTLExcludingHtab('\x03'), true)
strictEqual(isCTLExcludingHtab('\x04'), true)
strictEqual(isCTLExcludingHtab('\x05'), true)
strictEqual(isCTLExcludingHtab('\x06'), true)
strictEqual(isCTLExcludingHtab('\x07'), true)
strictEqual(isCTLExcludingHtab('\x08'), true)
test('should return false for 0x00 - 0x08 characters', (t) => {
t.assert.strictEqual(isCTLExcludingHtab('\x00'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x01'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x02'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x03'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x04'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x05'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x06'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x07'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x08'), true)
})

test('should return false for 0x09 HTAB character', () => {
strictEqual(isCTLExcludingHtab('\x09'), false)
test('should return false for 0x09 HTAB character', (t) => {
t.assert.strictEqual(isCTLExcludingHtab('\x09'), false)
})

test('should return false for 0x0A - 0x1F characters', () => {
strictEqual(isCTLExcludingHtab('\x0A'), true)
strictEqual(isCTLExcludingHtab('\x0B'), true)
strictEqual(isCTLExcludingHtab('\x0C'), true)
strictEqual(isCTLExcludingHtab('\x0D'), true)
strictEqual(isCTLExcludingHtab('\x0E'), true)
strictEqual(isCTLExcludingHtab('\x0F'), true)
strictEqual(isCTLExcludingHtab('\x10'), true)
strictEqual(isCTLExcludingHtab('\x11'), true)
strictEqual(isCTLExcludingHtab('\x12'), true)
strictEqual(isCTLExcludingHtab('\x13'), true)
strictEqual(isCTLExcludingHtab('\x14'), true)
strictEqual(isCTLExcludingHtab('\x15'), true)
strictEqual(isCTLExcludingHtab('\x16'), true)
strictEqual(isCTLExcludingHtab('\x17'), true)
strictEqual(isCTLExcludingHtab('\x18'), true)
strictEqual(isCTLExcludingHtab('\x19'), true)
strictEqual(isCTLExcludingHtab('\x1A'), true)
strictEqual(isCTLExcludingHtab('\x1B'), true)
strictEqual(isCTLExcludingHtab('\x1C'), true)
strictEqual(isCTLExcludingHtab('\x1D'), true)
strictEqual(isCTLExcludingHtab('\x1E'), true)
strictEqual(isCTLExcludingHtab('\x1F'), true)
test('should return false for 0x0A - 0x1F characters', (t) => {
t.assert.strictEqual(isCTLExcludingHtab('\x0A'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x0B'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x0C'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x0D'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x0E'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x0F'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x10'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x11'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x12'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x13'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x14'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x15'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x16'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x17'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x18'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x19'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x1A'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x1B'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x1C'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x1D'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x1E'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x1F'), true)
})

test('should return false for a 0x7F character', t => {
strictEqual(isCTLExcludingHtab('\x7F'), true)
t.assert.strictEqual(isCTLExcludingHtab('\x7F'), true)
})

test('should return false for a 0x20 / space character', t => {
strictEqual(isCTLExcludingHtab(' '), false)
t.assert.strictEqual(isCTLExcludingHtab(' '), false)
})

test('should return false for a printable character', t => {
strictEqual(isCTLExcludingHtab('A'), false)
strictEqual(isCTLExcludingHtab('Z'), false)
strictEqual(isCTLExcludingHtab('a'), false)
strictEqual(isCTLExcludingHtab('z'), false)
strictEqual(isCTLExcludingHtab('!'), false)
t.assert.strictEqual(isCTLExcludingHtab('A'), false)
t.assert.strictEqual(isCTLExcludingHtab('Z'), false)
t.assert.strictEqual(isCTLExcludingHtab('a'), false)
t.assert.strictEqual(isCTLExcludingHtab('z'), false)
t.assert.strictEqual(isCTLExcludingHtab('!'), false)
})

test('should return false for an empty string', () => {
strictEqual(isCTLExcludingHtab(''), false)
test('should return false for an empty string', (t) => {
t.assert.strictEqual(isCTLExcludingHtab(''), false)
})

test('all printable characters (0x20 - 0x7E)', () => {
test('all printable characters (0x20 - 0x7E)', (t) => {
for (let i = 0x20; i < 0x7F; i++) {
strictEqual(isCTLExcludingHtab(String.fromCharCode(i)), false)
t.assert.strictEqual(isCTLExcludingHtab(String.fromCharCode(i)), false)
}
})

test('valid case', () => {
strictEqual(isCTLExcludingHtab('Space=Cat; Secure; HttpOnly; Max-Age=2'), false)
test('valid case', (t) => {
t.assert.strictEqual(isCTLExcludingHtab('Space=Cat; Secure; HttpOnly; Max-Age=2'), false)
})

test('invalid case', () => {
strictEqual(isCTLExcludingHtab('Space=Cat; Secure; HttpOnly; Max-Age=2\x7F'), true)
test('invalid case', (t) => {
t.assert.strictEqual(isCTLExcludingHtab('Space=Cat; Secure; HttpOnly; Max-Age=2\x7F'), true)
})
})
67 changes: 33 additions & 34 deletions test/cookie/npm-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,84 +25,83 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const { describe, it } = require('node:test')
const assert = require('node:assert')
const { parseCookie } = require('../..')

describe('parseCookie(str)', function () {
it('should parse cookie string to object', function () {
assert.deepStrictEqual(parseCookie('foo=bar'), { name: 'foo', value: 'bar' })
assert.deepStrictEqual(parseCookie('foo=123'), { name: 'foo', value: '123' })
describe('parseCookie(str)', () => {
it('should parse cookie string to object', (t) => {
t.assert.deepStrictEqual(parseCookie('foo=bar'), { name: 'foo', value: 'bar' })
t.assert.deepStrictEqual(parseCookie('foo=123'), { name: 'foo', value: '123' })
})

it('should ignore OWS', function () {
assert.deepStrictEqual(parseCookie('FOO = bar; baz = raz'), {
it('should ignore OWS', (t) => {
t.assert.deepStrictEqual(parseCookie('FOO = bar; baz = raz'), {
name: 'FOO',
value: 'bar',
unparsed: ['baz=raz']
})
})

it('should parse cookie with empty value', function () {
assert.deepStrictEqual(parseCookie('foo=; bar='), { name: 'foo', value: '', unparsed: ['bar='] })
it('should parse cookie with empty value', (t) => {
t.assert.deepStrictEqual(parseCookie('foo=; bar='), { name: 'foo', value: '', unparsed: ['bar='] })
})

it('should parse cookie with minimum length', function () {
assert.deepStrictEqual(parseCookie('f='), { name: 'f', value: '' })
assert.deepStrictEqual(parseCookie('f=;b='), { name: 'f', value: '', unparsed: ['b='] })
it('should parse cookie with minimum length', (t) => {
t.assert.deepStrictEqual(parseCookie('f='), { name: 'f', value: '' })
t.assert.deepStrictEqual(parseCookie('f=;b='), { name: 'f', value: '', unparsed: ['b='] })
})

it('should URL-decode values', function () {
assert.deepStrictEqual(parseCookie('foo="bar=123456789&name=Magic+Mouse"'), {
it('should URL-decode values', (t) => {
t.assert.deepStrictEqual(parseCookie('foo="bar=123456789&name=Magic+Mouse"'), {
name: 'foo',
value: '"bar=123456789&name=Magic+Mouse"'
})

assert.deepStrictEqual(parseCookie('email=%20%22%2c%3b%2f'), { name: 'email', value: ' ",;/' })
t.assert.deepStrictEqual(parseCookie('email=%20%22%2c%3b%2f'), { name: 'email', value: ' ",;/' })
})

it('should trim whitespace around key and value', function () {
assert.deepStrictEqual(parseCookie(' foo = "bar" '), { name: 'foo', value: '"bar"' })
assert.deepStrictEqual(parseCookie(' foo = bar ; fizz = buzz '), {
it('should trim whitespace around key and value', (t) => {
t.assert.deepStrictEqual(parseCookie(' foo = "bar" '), { name: 'foo', value: '"bar"' })
t.assert.deepStrictEqual(parseCookie(' foo = bar ; fizz = buzz '), {
name: 'foo',
value: 'bar',
unparsed: ['fizz=buzz']
})
assert.deepStrictEqual(parseCookie(' foo = " a b c " '), { name: 'foo', value: '" a b c "' })
assert.deepStrictEqual(parseCookie(' = bar '), { name: '', value: 'bar' })
assert.deepStrictEqual(parseCookie(' foo = '), { name: 'foo', value: '' })
assert.deepStrictEqual(parseCookie(' = '), { name: '', value: '' })
assert.deepStrictEqual(parseCookie('\tfoo\t=\tbar\t'), { name: 'foo', value: 'bar' })
t.assert.deepStrictEqual(parseCookie(' foo = " a b c " '), { name: 'foo', value: '" a b c "' })
t.assert.deepStrictEqual(parseCookie(' = bar '), { name: '', value: 'bar' })
t.assert.deepStrictEqual(parseCookie(' foo = '), { name: 'foo', value: '' })
t.assert.deepStrictEqual(parseCookie(' = '), { name: '', value: '' })
t.assert.deepStrictEqual(parseCookie('\tfoo\t=\tbar\t'), { name: 'foo', value: 'bar' })
})

it('should return original value on escape error', function () {
assert.deepStrictEqual(parseCookie('foo=%1;bar=bar'), { name: 'foo', value: '%1', unparsed: ['bar=bar'] })
it('should return original value on escape error', (t) => {
t.assert.deepStrictEqual(parseCookie('foo=%1;bar=bar'), { name: 'foo', value: '%1', unparsed: ['bar=bar'] })
})

it('should ignore cookies without value', function () {
assert.deepStrictEqual(parseCookie('foo=bar;fizz ; buzz'), { name: 'foo', value: 'bar', unparsed: ['fizz=', 'buzz='] })
assert.deepStrictEqual(parseCookie(' fizz; foo= bar'), { name: '', value: 'fizz', unparsed: ['foo=bar'] })
it('should ignore cookies without value', (t) => {
t.assert.deepStrictEqual(parseCookie('foo=bar;fizz ; buzz'), { name: 'foo', value: 'bar', unparsed: ['fizz=', 'buzz='] })
t.assert.deepStrictEqual(parseCookie(' fizz; foo= bar'), { name: '', value: 'fizz', unparsed: ['foo=bar'] })
})

it('should ignore duplicate cookies', function () {
assert.deepStrictEqual(parseCookie('foo=%1;bar=bar;foo=boo'), {
it('should ignore duplicate cookies', (t) => {
t.assert.deepStrictEqual(parseCookie('foo=%1;bar=bar;foo=boo'), {
name: 'foo',
value: '%1',
unparsed: ['bar=bar', 'foo=boo']
})
assert.deepStrictEqual(parseCookie('foo=false;bar=bar;foo=true'), {
t.assert.deepStrictEqual(parseCookie('foo=false;bar=bar;foo=true'), {
name: 'foo',
value: 'false',
unparsed: ['bar=bar', 'foo=true']
})
assert.deepStrictEqual(parseCookie('foo=;bar=bar;foo=boo'), {
t.assert.deepStrictEqual(parseCookie('foo=;bar=bar;foo=boo'), {
name: 'foo',
value: '',
unparsed: ['bar=bar', 'foo=boo']
})
})

it('should parse native properties', function () {
assert.deepStrictEqual(parseCookie('toString=foo;valueOf=bar'), {
it('should parse native properties', (t) => {
t.assert.deepStrictEqual(parseCookie('toString=foo;valueOf=bar'), {
name: 'toString',
unparsed: [
'valueOf=bar'
Expand Down
7 changes: 3 additions & 4 deletions test/cookie/to-imf-date.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
'use strict'

const { test, describe } = require('node:test')
const { strictEqual } = require('node:assert')

const {
toIMFDate
} = require('../../lib/web/cookies/util')

describe('toIMFDate', () => {
test('should return the same as Date.prototype.toGMTString()', () => {
test('should return the same as Date.prototype.toGMTString()', (t) => {
for (let i = 1; i <= 1e6; i *= 2) {
const date = new Date(i)
strictEqual(toIMFDate(date), date.toGMTString())
t.assert.strictEqual(toIMFDate(date), date.toGMTString())
}
for (let i = 0; i <= 1e6; i++) {
const date = new Date(Math.trunc(Math.random() * 8640000000000000))
strictEqual(toIMFDate(date), date.toGMTString())
t.assert.strictEqual(toIMFDate(date), date.toGMTString())
}
})
})
Loading
Loading