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: RequestInit doesn't allow explicit undefined #1242

Merged
merged 1 commit into from Feb 19, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions lib/fetch/request.js
Expand Up @@ -127,12 +127,12 @@ class Request {
}

// 10. If init["window"] exists and is non-null, then throw a TypeError.
if ('window' in init && window != null) {
if (init.window !== undefined && window != null) {
ronag marked this conversation as resolved.
Show resolved Hide resolved
throw new TypeError(`'window' option '${window}' must be null`)
}

// 11. If init["window"] exists, then set window to "no-window".
if ('window' in init) {
if (init.window !== undefined) {
window = 'no-window'
}

Expand Down Expand Up @@ -212,7 +212,7 @@ class Request {
}

// 14. If init["referrer"] exists, then:
if ('referrer' in init) {
if (init.referrer !== undefined) {
// 1. Let referrer be init["referrer"].
const referrer = init.referrer

Expand Down Expand Up @@ -248,7 +248,7 @@ class Request {

// 15. If init["referrerPolicy"] exists, then set request’s referrer policy
// to it.
if ('referrerPolicy' in init) {
if (init.referrerPolicy !== undefined) {
request.referrerPolicy = init.referrerPolicy
if (!referrerPolicy.includes(request.referrerPolicy)) {
throw new TypeError(
Expand All @@ -259,7 +259,7 @@ class Request {

// 16. Let mode be init["mode"] if it exists, and fallbackMode otherwise.
let mode
if ('mode' in init) {
if (init.mode !== undefined) {
mode = init.mode
if (!requestMode.includes(mode)) {
throw new TypeError(
Expand All @@ -282,7 +282,7 @@ class Request {

// 19. If init["credentials"] exists, then set request’s credentials mode
// to it.
if ('credentials' in init) {
if (init.credentials !== undefined) {
request.credentials = init.credentials
if (!requestCredentials.includes(request.credentials)) {
throw new TypeError(
Expand All @@ -292,7 +292,7 @@ class Request {
}

// 18. If init["cache"] exists, then set request’s cache mode to it.
if ('cache' in init) {
if (init.cache !== undefined) {
request.cache = init.cache
if (!requestCache.includes(request.cache)) {
throw new TypeError(
Expand All @@ -310,7 +310,7 @@ class Request {
}

// 22. If init["redirect"] exists, then set request’s redirect mode to it.
if ('redirect' in init) {
if (init.redirect !== undefined) {
request.redirect = init.redirect
if (!requestRedirect.includes(request.redirect)) {
throw new TypeError(
Expand All @@ -320,17 +320,17 @@ class Request {
}

// 23. If init["integrity"] exists, then set request’s integrity metadata to it.
if ('integrity' in init && init.integrity != null) {
if (init.integrity !== undefined && init.integrity != null) {
request.integrity = String(init.integrity)
}

// 24. If init["keepalive"] exists, then set request’s keepalive to it.
if ('keepalive' in init) {
if (init.keepalive !== undefined) {
request.keepalive = Boolean(init.keepalive)
}

// 25. If init["method"] exists, then:
if ('method' in init) {
if (init.method !== undefined) {
// 1. Let method be init["method"].
let method = init.method

Expand All @@ -353,7 +353,7 @@ class Request {
}

// 26. If init["signal"] exists, then set signal to it.
if ('signal' in init) {
if (init.signal !== undefined) {
signal = init.signal
}

Expand Down Expand Up @@ -416,7 +416,7 @@ class Request {
let headers = new Headers(this.headers)

// 2. If init["headers"] exists, then set headers to init["headers"].
if ('headers' in init) {
if (init.headers !== undefined) {
headers = init.headers
}

Expand All @@ -442,7 +442,7 @@ class Request {
// non-null, and request’s method is `GET` or `HEAD`, then throw a
// TypeError.
if (
(('body' in init && init.body != null) || inputBody != null) &&
((init.body !== undefined && init.body != null) || inputBody != null) &&
(request.method === 'GET' || request.method === 'HEAD')
) {
throw new TypeError('Request with GET/HEAD method cannot have body.')
Expand All @@ -452,7 +452,7 @@ class Request {
let initBody = null

// 36. If init["body"] exists and is non-null, then:
if ('body' in init && init.body != null) {
if (init.body !== undefined && init.body != null) {
// 1. Let Content-Type be null.
// 2. Set initBody and Content-Type to the result of extracting
// init["body"], with keepalive set to request’s keepalive.
Expand Down
66 changes: 66 additions & 0 deletions test/fetch/request.js
Expand Up @@ -6,6 +6,7 @@ const { test } = require('tap')
const {
Request
} = require('../../')
const { kState } = require('../../lib/fetch/symbols.js')

test('arg validation', (t) => {
// constructor
Expand Down Expand Up @@ -149,6 +150,71 @@ test('arg validation', (t) => {
t.end()
})

test('undefined window', t => {
t.doesNotThrow(() => new Request('http://asd', { window: undefined }))
t.end()
})

test('undefined body', t => {
const req = new Request('http://asd', { body: undefined })
t.equal(req[kState].body, null)
t.end()
})

test('undefined method', t => {
const req = new Request('http://asd', { method: undefined })
t.equal(req.method, 'GET')
t.end()
})

test('undefined headers', t => {
const req = new Request('http://asd', { headers: undefined })
t.equal([...req.headers.entries()].length, 0)
t.end()
})

test('undefined referrer', t => {
const req = new Request('http://asd', { referrer: undefined })
t.equal(req.referrer, 'about:client')
t.end()
})

test('undefined referrerPolicy', t => {
const req = new Request('http://asd', { referrerPolicy: undefined })
t.equal(req.referrerPolicy, '')
t.end()
})

test('undefined mode', t => {
const req = new Request('http://asd', { mode: undefined })
t.equal(req.mode, 'cors')
t.end()
})

test('undefined credentials', t => {
const req = new Request('http://asd', { credentials: undefined })
t.equal(req.credentials, 'same-origin')
t.end()
})

test('undefined cache', t => {
const req = new Request('http://asd', { cache: undefined })
t.equal(req.cache, 'default')
t.end()
})

test('undefined redirect', t => {
const req = new Request('http://asd', { redirect: undefined })
t.equal(req.redirect, 'follow')
t.end()
})

test('undefined keepalive', t => {
const req = new Request('http://asd', { keepalive: undefined })
t.equal(req.keepalive, false)
t.end()
})

test('undefined integrity', t => {
const req = new Request('http://asd', { integrity: undefined })
t.equal(req.integrity, '')
Expand Down