From 100f087e73cfdff5e7df3ad1236b6cf34f4bdd11 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Wed, 11 Jan 2017 11:06:12 +0100 Subject: [PATCH 1/3] Test new URLSearchParams constructor features MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The main thing here is new tests for https://github.com/whatwg/url/pull/175/files but I also found issues with some existing tests that I’m attempting to fix at the same time. The current tests assumed the constructor argument was mandatory, but it’s in fact optional. The other issue was that the current test assume stringifying DOMException.prototype throws, but I don’t think that’s the case. --- url/urlsearchparams-constructor.html | 37 ++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/url/urlsearchparams-constructor.html b/url/urlsearchparams-constructor.html index 4e4d5dac35cfd81..2977c3bf7bd2f81 100644 --- a/url/urlsearchparams-constructor.html +++ b/url/urlsearchparams-constructor.html @@ -18,15 +18,18 @@ }, 'Basic URLSearchParams construction'); test(function() { - assert_throws(new TypeError(), function () { URLSearchParams(); }, - 'Calling \'URLSearchParams\' without \'new\' should throw.'); - assert_throws(new TypeError(), function () { new URLSearchParams(DOMException.prototype); }); - var params = new URLSearchParams(''); + var params = new URLSearchParams() + assert_equals(params.toString(), "") + + params = new URLSearchParams(DOMException.prototype); + assert_equals(params.toString(), "Error=") + + params = new URLSearchParams(''); assert_true(params != null, 'constructor returned non-null value.'); assert_equals(params.__proto__, URLSearchParams.prototype, 'expected URLSearchParams.prototype as prototype.'); params = new URLSearchParams({}); assert_equals(params + '', '%5Bobject+Object%5D='); -}, 'URLSearchParams constructor, empty.'); +}, 'URLSearchParams constructor, empty and unusual input.'); test(function() { var params = new URLSearchParams('a=b'); @@ -124,6 +127,30 @@ params = new URLSearchParams('a%f0%9f%92%a9b=c'); assert_equals(params.get('a\uD83D\uDCA9b'), 'c'); }, 'Parse %f0%9f%92%a9'); // Unicode Character 'PILE OF POO' (U+1F4A9) + +;[ + { "input": {"+": "%C2"}, "output": [[" ", "\uFFFD"]], "name": "object with +" }, + { "input": {c: "x", a: "?"}, "output": [["c", "x"], ["a", "?"]], "name": "object with two keys" }, + { "input": [["c", "x"], ["a", "?"]], "output": [["c", "x"], ["a", "?"]], "name": "array with two keys" } +].forEach((val) => { + test(() => { + let params = new URLSearchParams(val.input), + i = 0 + for (let param of params) { + assert_array_equals(param, val.output[i]) + i++ + } + }, "Construct with " + val.name) +}) + +test(() => { + let params1 = new URLSearchParams("a=b"), + params2 = new URLSearchParams() + params2[Symbol.iterator] = params1[Symbol.iterator] + let params3 = new URLSearchParams(params2) + assert_equals(params3.get("a"), params1.get("a")) + assert_equals(params3.get("a"), "b") +}, "Evil constructor test") From 76ac826a8a34bab1a6bbdb6af5ee6cc1970f750e Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Wed, 11 Jan 2017 15:38:30 +0100 Subject: [PATCH 2/3] address review comments --- url/urlsearchparams-constructor.html | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/url/urlsearchparams-constructor.html b/url/urlsearchparams-constructor.html index 2977c3bf7bd2f81..3da00213ee34c1a 100644 --- a/url/urlsearchparams-constructor.html +++ b/url/urlsearchparams-constructor.html @@ -20,16 +20,23 @@ test(function() { var params = new URLSearchParams() assert_equals(params.toString(), "") +}, "URLSearchParams constructor, no arguments") +test(() => { params = new URLSearchParams(DOMException.prototype); assert_equals(params.toString(), "Error=") +}, "URLSearchParams constructor, DOMException.prototype as argument") +test(() => { params = new URLSearchParams(''); assert_true(params != null, 'constructor returned non-null value.'); assert_equals(params.__proto__, URLSearchParams.prototype, 'expected URLSearchParams.prototype as prototype.'); +}, "URLSearchParams constructor, empty string as argument") + +test(() => { params = new URLSearchParams({}); assert_equals(params + '', '%5Bobject+Object%5D='); -}, 'URLSearchParams constructor, empty and unusual input.'); +}, 'URLSearchParams constructor, {} as argument'); test(function() { var params = new URLSearchParams('a=b'); @@ -144,12 +151,12 @@ }) test(() => { - let params1 = new URLSearchParams("a=b"), - params2 = new URLSearchParams() - params2[Symbol.iterator] = params1[Symbol.iterator] - let params3 = new URLSearchParams(params2) - assert_equals(params3.get("a"), params1.get("a")) - assert_equals(params3.get("a"), "b") + params = new URLSearchParams() + params[Symbol.iterator] = function *() { + yield ["a", "b"] + } + let params2 = new URLSearchParams(params) + assert_equals(params2.get("a"), "b") }, "Evil constructor test") From 697198fa79122f2f85ebdb423841489725f4d85e Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Thu, 12 Jan 2017 07:35:02 +0100 Subject: [PATCH 3/3] more excellent review feedback --- url/urlsearchparams-constructor.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/url/urlsearchparams-constructor.html b/url/urlsearchparams-constructor.html index 3da00213ee34c1a..ef1f0485ba18a53 100644 --- a/url/urlsearchparams-constructor.html +++ b/url/urlsearchparams-constructor.html @@ -35,7 +35,7 @@ test(() => { params = new URLSearchParams({}); - assert_equals(params + '', '%5Bobject+Object%5D='); + assert_equals(params + '', ""); }, 'URLSearchParams constructor, {} as argument'); test(function() { @@ -157,7 +157,7 @@ } let params2 = new URLSearchParams(params) assert_equals(params2.get("a"), "b") -}, "Evil constructor test") +}, "Custom [Symbol.iterator]")