diff --git a/lib/fetch/request.js b/lib/fetch/request.js index 92ea5cad76a..62b3c69760f 100644 --- a/lib/fetch/request.js +++ b/lib/fetch/request.js @@ -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) { 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' } @@ -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 @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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 @@ -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 } @@ -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 } @@ -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.') @@ -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. diff --git a/test/fetch/request.js b/test/fetch/request.js index 4412d80aa6e..f4bb2c981a9 100644 --- a/test/fetch/request.js +++ b/test/fetch/request.js @@ -6,6 +6,7 @@ const { test } = require('tap') const { Request } = require('../../') +const { kState } = require('../../lib/fetch/symbols.js') test('arg validation', (t) => { // constructor @@ -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, '')