Skip to content

Commit

Permalink
test: use globalThis.Headers and skip if is missing (nodejs#3684)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored and flakey5 committed Oct 8, 2024
1 parent a658ab3 commit a99f32a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
12 changes: 8 additions & 4 deletions lib/web/cookies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const { stringify } = require('./util')
const { webidl } = require('../fetch/webidl')
const { Headers } = require('../fetch/headers')

const headersToBrandCheck = globalThis.Headers
? [Headers, globalThis.Headers]
: [Headers]

/**
* @typedef {Object} Cookie
* @property {string} name
Expand All @@ -26,7 +30,7 @@ const { Headers } = require('../fetch/headers')
function getCookies (headers) {
webidl.argumentLengthCheck(arguments, 1, 'getCookies')

webidl.brandCheckMultiple(headers, [Headers, globalThis.Headers])
webidl.brandCheckMultiple(headers, headersToBrandCheck)

const cookie = headers.get('cookie')

Expand All @@ -53,7 +57,7 @@ function getCookies (headers) {
* @returns {void}
*/
function deleteCookie (headers, name, attributes) {
webidl.brandCheckMultiple(headers, [Headers, globalThis.Headers])
webidl.brandCheckMultiple(headers, headersToBrandCheck)

const prefix = 'deleteCookie'
webidl.argumentLengthCheck(arguments, 2, prefix)
Expand All @@ -78,7 +82,7 @@ function deleteCookie (headers, name, attributes) {
function getSetCookies (headers) {
webidl.argumentLengthCheck(arguments, 1, 'getSetCookies')

webidl.brandCheckMultiple(headers, [Headers, globalThis.Headers])
webidl.brandCheckMultiple(headers, headersToBrandCheck)

const cookies = headers.getSetCookie()

Expand All @@ -97,7 +101,7 @@ function getSetCookies (headers) {
function setCookie (headers, cookie) {
webidl.argumentLengthCheck(arguments, 2, 'setCookie')

webidl.brandCheckMultiple(headers, [Headers, globalThis.Headers])
webidl.brandCheckMultiple(headers, headersToBrandCheck)

cookie = webidl.converters.Cookie(cookie)

Expand Down
8 changes: 4 additions & 4 deletions test/cookie/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ test('Cookie setCookie does not throw if headers is an instance of undici owns H
})
})

test('Cookie setCookie does not throw if headers is an instance of the global Headers class', () => {
test('Cookie setCookie does not throw if headers is an instance of the global Headers class', { skip: !globalThis.Headers }, () => {
const headers = new globalThis.Headers()
setCookie(headers, {
name: 'key',
Expand Down Expand Up @@ -659,7 +659,7 @@ test('Cookie getCookies does not throw if headers is an instance of undici owns
getCookies(headers)
})

test('Cookie getCookie does not throw if headers is an instance of the global Headers class', () => {
test('Cookie getCookie does not throw if headers is an instance of the global Headers class', { skip: !globalThis.Headers }, () => {
const headers = new globalThis.Headers()
getCookies(headers)
})
Expand All @@ -682,7 +682,7 @@ test('Cookie getSetCookies does not throw if headers is an instance of undici ow
getSetCookies(headers)
})

test('Cookie setCookie does not throw if headers is an instance of the global Headers class', () => {
test('Cookie setCookie does not throw if headers is an instance of the global Headers class', { skip: !globalThis.Headers }, () => {
const headers = new globalThis.Headers({ 'set-cookie': 'Space=Cat' })
getSetCookies(headers)
})
Expand All @@ -705,7 +705,7 @@ test('Cookie deleteCookie does not throw if headers is an instance of undici own
deleteCookie(headers, 'deno')
})

test('Cookie getCookie does not throw if headers is an instance of the global Headers class', () => {
test('Cookie getCookie does not throw if headers is an instance of the global Headers class', { skip: !globalThis.Headers }, () => {
const headers = new globalThis.Headers()
deleteCookie(headers, 'deno')
})
22 changes: 11 additions & 11 deletions test/cookie/global-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ const {
} = require('../..')

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

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

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

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

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

Expand All @@ -46,17 +46,17 @@ describe('Using global Headers', async () => {
}
})

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

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

describe('Headers check is not too lax', () => {
class Headers {}
Object.defineProperty(Headers.prototype, Symbol.toStringTag, {
describe('Headers check is not too lax', { skip: !globalThis.Headers }, () => {
class Headers { }
Object.defineProperty(globalThis.Headers.prototype, Symbol.toStringTag, {
value: 'Headers',
configurable: true
})
Expand Down
14 changes: 11 additions & 3 deletions test/node-test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ test('debug#websocket', { skip: !process.versions.icu || isCITGM }, async t => {
const assert = tspl(t, { plan: 6 })
const child = spawn(
process.execPath,
[join(__dirname, '../fixtures/websocket.js')],
[
'--no-experimental-fetch',
join(__dirname, '../fixtures/websocket.js')],
{
env: {
NODE_DEBUG: 'websocket'
Expand Down Expand Up @@ -50,7 +52,10 @@ test('debug#fetch', { skip: isCITGM }, async t => {
const assert = tspl(t, { plan: 7 })
const child = spawn(
process.execPath,
[join(__dirname, '../fixtures/fetch.js')],
[
'--no-experimental-fetch',
join(__dirname, '../fixtures/fetch.js')
],
{
env: Object.assign({}, process.env, { NODE_DEBUG: 'fetch' })
}
Expand Down Expand Up @@ -85,7 +90,10 @@ test('debug#undici', { skip: isCITGM }, async t => {
const assert = tspl(t, { plan: 7 })
const child = spawn(
process.execPath,
[join(__dirname, '../fixtures/undici.js')],
[
'--no-experimental-fetch',
join(__dirname, '../fixtures/undici.js')
],
{
env: {
NODE_DEBUG: 'undici'
Expand Down
4 changes: 2 additions & 2 deletions test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ describe('DispatchOptions#reset', () => {
})

describe('Should include headers from iterable objects', scope => {
test('Should include headers built with Headers global object', async t => {
test('Should include headers built with Headers global object', { skip: !globalThis.Headers }, async t => {
t = tspl(t, { plan: 3 })

const server = createServer((req, res) => {
Expand All @@ -286,7 +286,7 @@ describe('Should include headers from iterable objects', scope => {
res.end('hello')
})

const headers = new Headers()
const headers = new globalThis.Headers()
headers.set('hello', 'world')

after(() => server.close())
Expand Down

0 comments on commit a99f32a

Please sign in to comment.