From 97b974cf2dfc4fb262684456c2c4a2e583a6e2e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sat, 12 Feb 2022 12:35:29 +0100 Subject: [PATCH] fix: make fetch's signature spec-compliant (#1226) --- index.js | 4 +-- test/fetch/client-fetch.js | 53 ++++++-------------------------------- test/fetch/file.js | 51 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 47 deletions(-) diff --git a/index.js b/index.js index 0c789aebf5d..888eb6a5a4f 100644 --- a/index.js +++ b/index.js @@ -94,12 +94,12 @@ module.exports.getGlobalDispatcher = getGlobalDispatcher if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 5)) { let fetchImpl = null - module.exports.fetch = async function fetch (resource, init) { + module.exports.fetch = async function fetch (resource) { if (!fetchImpl) { fetchImpl = require('./lib/fetch') } const dispatcher = getGlobalDispatcher() - return fetchImpl.call(dispatcher, resource, init) + return fetchImpl.apply(dispatcher, arguments) } module.exports.Headers = require('./lib/fetch/headers').Headers module.exports.Response = require('./lib/fetch/response').Response diff --git a/test/fetch/client-fetch.js b/test/fetch/client-fetch.js index 5879d0054b6..ef6942f6bea 100644 --- a/test/fetch/client-fetch.js +++ b/test/fetch/client-fetch.js @@ -8,55 +8,18 @@ const { ReadableStream } = require('stream/web') const { Blob } = require('buffer') const { fetch, Response, Request, FormData, File, FileLike } = require('../..') -test('args validation', (t) => { - t.plan(12) - - t.throws(() => { - File.prototype.name.call(null) - }, TypeError) - t.throws(() => { - File.prototype.lastModified.call(null) - }, TypeError) - t.throws(() => { - File.prototype[Symbol.toStringTag].call(null) - }, TypeError) - - t.throws(() => { - FileLike.prototype.stream.call(null) - }, TypeError) - t.throws(() => { - FileLike.prototype.arrayBuffer.call(null) - }, TypeError) - t.throws(() => { - FileLike.prototype.slice.call(null) - }, TypeError) - t.throws(() => { - FileLike.prototype.text.call(null) - }, TypeError) - t.throws(() => { - FileLike.prototype.size.call(null) - }, TypeError) - t.throws(() => { - FileLike.prototype.type.call(null) - }, TypeError) - t.throws(() => { - FileLike.prototype.name.call(null) - }, TypeError) - t.throws(() => { - FileLike.prototype.lastModified.call(null) - }, TypeError) - t.throws(() => { - FileLike.prototype[Symbol.toStringTag].call(null) - }, TypeError) +test('function signature', (t) => { + t.plan(2) + + t.equal(fetch.name, 'fetch') + t.equal(fetch.length, 1) }) -test('return value of File.lastModified', (t) => { +test('args validation', async (t) => { t.plan(2) - const f = new File(['asd1'], 'filename123') - const lastModified = f.lastModified - t.ok(typeof lastModified === typeof Date.now()) - t.ok(lastModified >= 0 && lastModified <= Date.now()) + await t.rejects(fetch(), TypeError) + await t.rejects(fetch('ftp://unsupported'), TypeError) }) test('request json', (t) => { diff --git a/test/fetch/file.js b/test/fetch/file.js index 1e65c9325a3..38cdcc7b837 100644 --- a/test/fetch/file.js +++ b/test/fetch/file.js @@ -3,6 +3,57 @@ const { test } = require('tap') const { File, FileLike } = require('../../lib/fetch/file') +test('args validation', (t) => { + t.plan(12) + + t.throws(() => { + File.prototype.name.call(null) + }, TypeError) + t.throws(() => { + File.prototype.lastModified.call(null) + }, TypeError) + t.throws(() => { + File.prototype[Symbol.toStringTag].call(null) + }, TypeError) + + t.throws(() => { + FileLike.prototype.stream.call(null) + }, TypeError) + t.throws(() => { + FileLike.prototype.arrayBuffer.call(null) + }, TypeError) + t.throws(() => { + FileLike.prototype.slice.call(null) + }, TypeError) + t.throws(() => { + FileLike.prototype.text.call(null) + }, TypeError) + t.throws(() => { + FileLike.prototype.size.call(null) + }, TypeError) + t.throws(() => { + FileLike.prototype.type.call(null) + }, TypeError) + t.throws(() => { + FileLike.prototype.name.call(null) + }, TypeError) + t.throws(() => { + FileLike.prototype.lastModified.call(null) + }, TypeError) + t.throws(() => { + FileLike.prototype[Symbol.toStringTag].call(null) + }, TypeError) +}) + +test('return value of File.lastModified', (t) => { + t.plan(2) + + const f = new File(['asd1'], 'filename123') + const lastModified = f.lastModified + t.ok(typeof lastModified === typeof Date.now()) + t.ok(lastModified >= 0 && lastModified <= Date.now()) +}) + test('Symbol.toStringTag', (t) => { t.plan(2) t.equal(new File()[Symbol.toStringTag], 'File')