From ed0086fb46e1e5b518dacd394c72f8a2b1b7a473 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Sun, 15 Jan 2017 12:20:24 -0800 Subject: [PATCH] url: check forEach callback is a function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Web IDL spec mandates such a check. Also make error messages consistent with rest of Node.js and add additional tests for forEach(). PR-URL: https://github.com/nodejs/node/pull/10905 Reviewed-By: Joyee Cheung Reviewed-By: Colin Ihrig Reviewed-By: Michaƫl Zasso --- lib/internal/url.js | 18 ++++++++---------- test/parallel/test-whatwg-url-searchparams.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index a4c31fb4266bb1..679e14d72a9c39 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -672,8 +672,7 @@ class URLSearchParams { throw new TypeError('Value of `this` is not a URLSearchParams'); } if (arguments.length < 2) { - throw new TypeError( - 'Both `name` and `value` arguments need to be specified'); + throw new TypeError('"name" and "value" arguments must be specified'); } name = String(name); @@ -687,7 +686,7 @@ class URLSearchParams { throw new TypeError('Value of `this` is not a URLSearchParams'); } if (arguments.length < 1) { - throw new TypeError('The `name` argument needs to be specified'); + throw new TypeError('"name" argument must be specified'); } const list = this[searchParams]; @@ -708,8 +707,7 @@ class URLSearchParams { throw new TypeError('Value of `this` is not a URLSearchParams'); } if (arguments.length < 2) { - throw new TypeError( - 'Both `name` and `value` arguments need to be specified'); + throw new TypeError('"name" and "value" arguments must be specified'); } const list = this[searchParams]; @@ -749,7 +747,7 @@ class URLSearchParams { throw new TypeError('Value of `this` is not a URLSearchParams'); } if (arguments.length < 1) { - throw new TypeError('The `name` argument needs to be specified'); + throw new TypeError('"name" argument must be specified'); } const list = this[searchParams]; @@ -767,7 +765,7 @@ class URLSearchParams { throw new TypeError('Value of `this` is not a URLSearchParams'); } if (arguments.length < 1) { - throw new TypeError('The `name` argument needs to be specified'); + throw new TypeError('"name" argument must be specified'); } const list = this[searchParams]; @@ -786,7 +784,7 @@ class URLSearchParams { throw new TypeError('Value of `this` is not a URLSearchParams'); } if (arguments.length < 1) { - throw new TypeError('The `name` argument needs to be specified'); + throw new TypeError('"name" argument must be specified'); } const list = this[searchParams]; @@ -814,8 +812,8 @@ class URLSearchParams { if (!this || !(this instanceof URLSearchParams)) { throw new TypeError('Value of `this` is not a URLSearchParams'); } - if (arguments.length < 1) { - throw new TypeError('The `callback` argument needs to be specified'); + if (typeof callback !== 'function') { + throw new TypeError('"callback" argument must be a function'); } let list = this[searchParams]; diff --git a/test/parallel/test-whatwg-url-searchparams.js b/test/parallel/test-whatwg-url-searchparams.js index ca8f4ff070bfeb..8b9e65123d9506 100644 --- a/test/parallel/test-whatwg-url-searchparams.js +++ b/test/parallel/test-whatwg-url-searchparams.js @@ -44,6 +44,20 @@ n = 0; for (val of sp.values()) { assert.strictEqual(val, String(values[n++])); } +n = 0; +sp.forEach(function(val, key, obj) { + assert.strictEqual(this, undefined); + assert.strictEqual(key, 'a'); + assert.strictEqual(val, String(values[n++])); + assert.strictEqual(obj, sp); +}); +sp.forEach(function() { + assert.strictEqual(this, m); +}, m); +assert.throws(() => sp.forEach(), + /^TypeError: "callback" argument must be a function$/); +assert.throws(() => sp.forEach(1), + /^TypeError: "callback" argument must be a function$/); m.search = '?a=a&b=b'; assert.strictEqual(sp.toString(), 'a=a&b=b');