Browse Source

test, url: synchronize WPT url tests

* attributon of WPT in url-setter-tests
* add WPT test utilities
* synchronize WPT URLSearchParams tests
* synchronize WPT url tests
* split whatwg-url-inspect test
* port historical url tests from WPT
* protocol setter and special URLs

Refs: https://github.com/w3c/web-platform-tests/pull/4413
Refs: https://github.com/whatwg/url/issues/104
Backport-of: https://github.com/nodejs/node/pull/11079
v7.x
Joyee Cheung 8 years ago
committed by Italo A. Casas
parent
commit
7aaa960f4c
No known key found for this signature in database GPG Key ID: 23EFEFE93C4CFFFE
  1. 25
      test/common.js
  2. 48
      test/fixtures/url-setter-tests.json
  3. 87
      test/fixtures/url-tests-additional.js
  4. 142
      test/parallel/test-whatwg-url-constructor.js
  5. 46
      test/parallel/test-whatwg-url-historical.js
  6. 83
      test/parallel/test-whatwg-url-inspect.js
  7. 52
      test/parallel/test-whatwg-url-origin.js
  8. 192
      test/parallel/test-whatwg-url-parsing.js
  9. 2
      test/parallel/test-whatwg-url-properties.js
  10. 103
      test/parallel/test-whatwg-url-searchparams-append.js
  11. 316
      test/parallel/test-whatwg-url-searchparams-constructor.js
  12. 91
      test/parallel/test-whatwg-url-searchparams-delete.js
  13. 1
      test/parallel/test-whatwg-url-searchparams-entries.js
  14. 79
      test/parallel/test-whatwg-url-searchparams-foreach.js
  15. 69
      test/parallel/test-whatwg-url-searchparams-get.js
  16. 80
      test/parallel/test-whatwg-url-searchparams-getall.js
  17. 76
      test/parallel/test-whatwg-url-searchparams-has.js
  18. 7
      test/parallel/test-whatwg-url-searchparams-inspect.js
  19. 1
      test/parallel/test-whatwg-url-searchparams-keys.js
  20. 73
      test/parallel/test-whatwg-url-searchparams-set.js
  21. 238
      test/parallel/test-whatwg-url-searchparams-stringifier.js
  22. 1
      test/parallel/test-whatwg-url-searchparams-values.js
  23. 1
      test/parallel/test-whatwg-url-searchparams.js
  24. 82
      test/parallel/test-whatwg-url-setters.js
  25. 2
      test/parallel/test-whatwg-url-tostringtag.js

25
test/common.js

@ -588,3 +588,28 @@ Object.defineProperty(exports, 'hasIntl', {
return process.binding('config').hasIntl; return process.binding('config').hasIntl;
} }
}); });
// https://github.com/w3c/testharness.js/blob/master/testharness.js
exports.WPT = {
test: (fn, desc) => {
try {
fn();
} catch (err) {
if (err instanceof Error)
err.message = `In ${desc}:\n ${err.message}`;
throw err;
}
},
assert_equals: assert.strictEqual,
assert_true: (value, message) => assert.strictEqual(value, true, message),
assert_false: (value, message) => assert.strictEqual(value, false, message),
assert_throws: (code, func, desc) => {
assert.throws(func, (err) => {
return typeof err === 'object' && 'name' in err && err.name === code.name;
}, desc);
},
assert_array_equals: assert.deepStrictEqual,
assert_unreached(desc) {
assert.fail(undefined, undefined, `Reached unreachable code: ${desc}`);
}
};

48
test/fixtures/url-setter-tests.json

@ -1,5 +1,6 @@
{ {
"comment": [ "comment": [
"License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html",
"## Tests for setters of https://url.spec.whatwg.org/#urlutils-members", "## Tests for setters of https://url.spec.whatwg.org/#urlutils-members",
"", "",
"This file contains a JSON object.", "This file contains a JSON object.",
@ -19,8 +20,7 @@
" get the attribute `key` (invoke its getter).", " get the attribute `key` (invoke its getter).",
" The returned string must be equal to `value`.", " The returned string must be equal to `value`.",
"", "",
"Note: the 'href' setter is already covered by urltestdata.json.", "Note: the 'href' setter is already covered by urltestdata.json."
"Source: https://github.com/w3c/web-platform-tests/tree/master/url"
], ],
"protocol": [ "protocol": [
{ {
@ -103,7 +103,7 @@
} }
}, },
{ {
"comment": "Can’t switch from special scheme to non-special. Note: this may change, see https://github.com/whatwg/url/issues/104", "comment": "Can’t switch from special scheme to non-special",
"href": "http://example.net", "href": "http://example.net",
"new_value": "b", "new_value": "b",
"expected": { "expected": {
@ -111,6 +111,22 @@
"protocol": "http:" "protocol": "http:"
} }
}, },
{
"href": "https://example.net",
"new_value": "s",
"expected": {
"href": "https://example.net/",
"protocol": "https:"
}
},
{
"href": "ftp://example.net",
"new_value": "test",
"expected": {
"href": "ftp://example.net/",
"protocol": "ftp:"
}
},
{ {
"comment": "Cannot-be-a-base URL doesn’t have a host, but URL in a special scheme must.", "comment": "Cannot-be-a-base URL doesn’t have a host, but URL in a special scheme must.",
"href": "mailto:me@example.net", "href": "mailto:me@example.net",
@ -121,7 +137,7 @@
} }
}, },
{ {
"comment": "Can’t switch from non-special scheme to special. Note: this may change, see https://github.com/whatwg/url/issues/104", "comment": "Can’t switch from non-special scheme to special",
"href": "ssh://me@example.net", "href": "ssh://me@example.net",
"new_value": "http", "new_value": "http",
"expected": { "expected": {
@ -129,6 +145,30 @@
"protocol": "ssh:" "protocol": "ssh:"
} }
}, },
{
"href": "ssh://me@example.net",
"new_value": "gopher",
"expected": {
"href": "ssh://me@example.net/",
"protocol": "ssh:"
}
},
{
"href": "ssh://me@example.net",
"new_value": "file",
"expected": {
"href": "ssh://me@example.net/",
"protocol": "ssh:"
}
},
{
"href": "nonsense:///test",
"new_value": "https",
"expected": {
"href": "nonsense:///test",
"protocol": "nonsense:"
}
},
{ {
"comment": "Stuff after the first ':' is ignored", "comment": "Stuff after the first ':' is ignored",
"href": "http://example.net", "href": "http://example.net",

87
test/fixtures/url-tests-additional.js

@ -0,0 +1,87 @@
module.exports = [
{
'url': 'tftp://foobar.com/someconfig;mode=netascii',
'protocol': 'tftp:',
'hostname': 'foobar.com',
'pathname': '/someconfig;mode=netascii'
},
{
'url': 'telnet://user:pass@foobar.com:23/',
'protocol': 'telnet:',
'username': 'user',
'password': 'pass',
'hostname': 'foobar.com',
'port': '23',
'pathname': '/'
},
{
'url': 'ut2004://10.10.10.10:7777/Index.ut2',
'protocol': 'ut2004:',
'hostname': '10.10.10.10',
'port': '7777',
'pathname': '/Index.ut2'
},
{
'url': 'redis://foo:bar@somehost:6379/0?baz=bam&qux=baz',
'protocol': 'redis:',
'username': 'foo',
'password': 'bar',
'hostname': 'somehost',
'port': '6379',
'pathname': '/0',
'search': '?baz=bam&qux=baz'
},
{
'url': 'rsync://foo@host:911/sup',
'protocol': 'rsync:',
'username': 'foo',
'hostname': 'host',
'port': '911',
'pathname': '/sup'
},
{
'url': 'git://github.com/foo/bar.git',
'protocol': 'git:',
'hostname': 'github.com',
'pathname': '/foo/bar.git'
},
{
'url': 'irc://myserver.com:6999/channel?passwd',
'protocol': 'irc:',
'hostname': 'myserver.com',
'port': '6999',
'pathname': '/channel',
'search': '?passwd'
},
{
'url': 'dns://fw.example.org:9999/foo.bar.org?type=TXT',
'protocol': 'dns:',
'hostname': 'fw.example.org',
'port': '9999',
'pathname': '/foo.bar.org',
'search': '?type=TXT'
},
{
'url': 'ldap://localhost:389/ou=People,o=JNDITutorial',
'protocol': 'ldap:',
'hostname': 'localhost',
'port': '389',
'pathname': '/ou=People,o=JNDITutorial'
},
{
'url': 'git+https://github.com/foo/bar',
'protocol': 'git+https:',
'hostname': 'github.com',
'pathname': '/foo/bar'
},
{
'url': 'urn:ietf:rfc:2648',
'protocol': 'urn:',
'pathname': 'ietf:rfc:2648'
},
{
'url': 'tag:joe@example.org,2001:foo/bar',
'protocol': 'tag:',
'pathname': 'joe@example.org,2001:foo/bar'
}
];

142
test/parallel/test-whatwg-url-constructor.js

@ -0,0 +1,142 @@
'use strict';
const common = require('../common');
const path = require('path');
const { URL, URLSearchParams } = require('url');
const { test, assert_equals, assert_true, assert_throws } = common.WPT;
if (!common.hasIntl) {
// A handful of the tests fail when ICU is not included.
common.skip('missing Intl');
return;
}
const request = {
response: require(path.join(common.fixturesDir, 'url-tests.json'))
};
/* eslint-disable */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/8791bed/url/url-constructor.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
function runURLConstructorTests() {
// var setup = async_test("Loading data…")
// setup.step(function() {
// var request = new XMLHttpRequest()
// request.open("GET", "urltestdata.json")
// request.send()
// request.responseType = "json"
// request.onload = setup.step_func(function() {
runURLTests(request.response)
// setup.done()
// })
// })
}
function bURL(url, base) {
return new URL(url, base || "about:blank")
}
function runURLTests(urltests) {
for(var i = 0, l = urltests.length; i < l; i++) {
var expected = urltests[i]
if (typeof expected === "string") continue // skip comments
test(function() {
if (expected.failure) {
assert_throws(new TypeError(), function() {
bURL(expected.input, expected.base)
})
return
}
var url = bURL(expected.input, expected.base)
assert_equals(url.href, expected.href, "href")
assert_equals(url.protocol, expected.protocol, "protocol")
assert_equals(url.username, expected.username, "username")
assert_equals(url.password, expected.password, "password")
assert_equals(url.host, expected.host, "host")
assert_equals(url.hostname, expected.hostname, "hostname")
assert_equals(url.port, expected.port, "port")
assert_equals(url.pathname, expected.pathname, "pathname")
assert_equals(url.search, expected.search, "search")
if ("searchParams" in expected) {
assert_true("searchParams" in url)
// assert_equals(url.searchParams.toString(), expected.searchParams, "searchParams")
}
assert_equals(url.hash, expected.hash, "hash")
}, "Parsing: <" + expected.input + "> against <" + expected.base + ">")
}
}
function runURLSearchParamTests() {
test(function() {
var url = bURL('http://example.org/?a=b')
assert_true("searchParams" in url)
var searchParams = url.searchParams
assert_true(url.searchParams === searchParams, 'Object identity should hold.')
}, 'URL.searchParams getter')
test(function() {
var url = bURL('http://example.org/?a=b')
assert_true("searchParams" in url)
var searchParams = url.searchParams
assert_equals(searchParams.toString(), 'a=b')
searchParams.set('a', 'b')
assert_equals(url.searchParams.toString(), 'a=b')
assert_equals(url.search, '?a=b')
url.search = ''
assert_equals(url.searchParams.toString(), '')
assert_equals(url.search, '')
assert_equals(searchParams.toString(), '')
}, 'URL.searchParams updating, clearing')
test(function() {
'use strict'
var urlString = 'http://example.org'
var url = bURL(urlString)
assert_throws(TypeError(), function() { url.searchParams = new URLSearchParams(urlString) })
}, 'URL.searchParams setter, invalid values')
test(function() {
var url = bURL('http://example.org/file?a=b&c=d')
assert_true("searchParams" in url)
var searchParams = url.searchParams
assert_equals(url.search, '?a=b&c=d')
assert_equals(searchParams.toString(), 'a=b&c=d')
// Test that setting 'search' propagates to the URL object's query object.
url.search = 'e=f&g=h'
assert_equals(url.search, '?e=f&g=h')
assert_equals(searchParams.toString(), 'e=f&g=h')
// ..and same but with a leading '?'.
url.search = '?e=f&g=h'
assert_equals(url.search, '?e=f&g=h')
assert_equals(searchParams.toString(), 'e=f&g=h')
// And in the other direction, altering searchParams propagates
// back to 'search'.
// searchParams.append('i', ' j ')
// assert_equals(url.search, '?e=f&g=h&i=+j+')
// assert_equals(url.searchParams.toString(), 'e=f&g=h&i=+j+')
// assert_equals(searchParams.get('i'), ' j ')
// searchParams.set('e', 'updated')
// assert_equals(url.search, '?e=updated&g=h&i=+j+')
// assert_equals(searchParams.get('e'), 'updated')
// var url2 = bURL('http://example.org/file??a=b&c=d')
// assert_equals(url2.search, '??a=b&c=d')
// assert_equals(url2.searchParams.toString(), '%3Fa=b&c=d')
// url2.href = 'http://example.org/file??a=b'
// assert_equals(url2.search, '??a=b')
// assert_equals(url2.searchParams.toString(), '%3Fa=b')
}, 'URL.searchParams and URL.search setters, update propagation')
}
runURLSearchParamTests()
runURLConstructorTests()
/* eslint-enable */

46
test/parallel/test-whatwg-url-historical.js

@ -0,0 +1,46 @@
'use strict';
const common = require('../common');
const URL = require('url').URL;
const { test, assert_equals, assert_throws } = common.WPT;
if (!common.hasIntl) {
// A handful of the tests fail when ICU is not included.
common.skip('missing Intl');
return;
}
/* eslint-disable */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/8791bed/url/historical.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
// var objects = [
// [function() { return window.location }, "location object"],
// [function() { return document.createElement("a") }, "a element"],
// [function() { return document.createElement("area") }, "area element"],
// ];
// objects.forEach(function(o) {
// test(function() {
// var object = o[0]();
// assert_false("searchParams" in object,
// o[1] + " should not have a searchParams attribute");
// }, "searchParams on " + o[1]);
// });
test(function() {
var url = new URL("./foo", "http://www.example.org");
assert_equals(url.href, "http://www.example.org/foo");
assert_throws(new TypeError(), function() {
url.href = "./bar";
});
}, "Setting URL's href attribute and base URLs");
test(function() {
assert_equals(URL.domainToASCII, undefined);
}, "URL.domainToASCII should be undefined");
test(function() {
assert_equals(URL.domainToUnicode, undefined);
}, "URL.domainToUnicode should be undefined");
/* eslint-enable */

83
test/parallel/test-whatwg-url-inspect.js

@ -0,0 +1,83 @@
'use strict';
const common = require('../common');
const util = require('util');
const URL = require('url').URL;
const path = require('path');
const assert = require('assert');
if (!common.hasIntl) {
// A handful of the tests fail when ICU is not included.
common.skip('missing Intl');
return;
}
// Tests below are not from WPT.
const tests = require(path.join(common.fixturesDir, 'url-tests.json'));
const additional_tests = require(
path.join(common.fixturesDir, 'url-tests-additional.js'));
const allTests = additional_tests.slice();
for (const test of tests) {
if (test.failure || typeof test === 'string') continue;
allTests.push(test);
}
for (const test of allTests) {
const url = test.url ? new URL(test.url) : new URL(test.input, test.base);
for (const showHidden of [true, false]) {
const res = util.inspect(url, {
showHidden
});
const lines = res.split('\n');
const firstLine = lines[0];
assert.strictEqual(firstLine, 'URL {');
const lastLine = lines[lines.length - 1];
assert.strictEqual(lastLine, '}');
const innerLines = lines.slice(1, lines.length - 1);
const keys = new Set();
for (const line of innerLines) {
const i = line.indexOf(': ');
const k = line.slice(0, i).trim();
const v = line.slice(i + 2);
assert.strictEqual(keys.has(k), false, 'duplicate key found: ' + k);
keys.add(k);
const hidden = new Set([
'password',
'cannot-be-base',
'special'
]);
if (showHidden) {
if (!hidden.has(k)) {
assert.strictEqual(v, url[k], k);
continue;
}
if (k === 'password') {
assert.strictEqual(v, url[k], k);
}
if (k === 'cannot-be-base') {
assert.ok(v.match(/^true$|^false$/), k + ' is Boolean');
}
if (k === 'special') {
assert.ok(v.match(/^true$|^false$/), k + ' is Boolean');
}
continue;
}
// showHidden is false
if (k === 'password') {
assert.strictEqual(v, '--------', k);
continue;
}
assert.strictEqual(hidden.has(k), false, 'no hidden keys: ' + k);
assert.strictEqual(v, url[k], k);
}
}
}

52
test/parallel/test-whatwg-url-origin.js

@ -0,0 +1,52 @@
'use strict';
const common = require('../common');
const path = require('path');
const URL = require('url').URL;
const { test, assert_equals } = common.WPT;
if (!common.hasIntl) {
// A handful of the tests fail when ICU is not included.
common.skip('missing Intl');
return;
}
const request = {
response: require(path.join(common.fixturesDir, 'url-tests.json'))
};
/* eslint-disable */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/8791bed/url/url-origin.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
function runURLOriginTests() {
// var setup = async_test("Loading data…")
// setup.step(function() {
// var request = new XMLHttpRequest()
// request.open("GET", "urltestdata.json")
// request.send()
// request.responseType = "json"
// request.onload = setup.step_func(function() {
runURLTests(request.response)
// setup.done()
// })
// })
}
function bURL(url, base) {
return new URL(url, base || "about:blank")
}
function runURLTests(urltests) {
for(var i = 0, l = urltests.length; i < l; i++) {
var expected = urltests[i]
if (typeof expected === "string" || !("origin" in expected)) continue
test(function() {
var url = bURL(expected.input, expected.base)
assert_equals(url.origin, expected.origin, "origin")
}, "Origin parsing: <" + expected.input + "> against <" + expected.base + ">")
}
}
runURLOriginTests()
/* eslint-enable */

192
test/parallel/test-whatwg-url-parsing.js

@ -1,7 +1,9 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const util = require('util'); const URL = require('url').URL;
const path = require('path');
const assert = require('assert');
if (!common.hasIntl) { if (!common.hasIntl) {
// A handful of the tests fail when ICU is not included. // A handful of the tests fail when ICU is not included.
@ -9,25 +11,9 @@ if (!common.hasIntl) {
return; return;
} }
const URL = require('url').URL; // Tests below are not from WPT.
const path = require('path');
const assert = require('assert');
const tests = require(path.join(common.fixturesDir, 'url-tests.json')); const tests = require(path.join(common.fixturesDir, 'url-tests.json'));
function verifyURL(url, test) {
if (test.href) assert.strictEqual(url.href, test.href);
if (test.origin) assert.strictEqual(url.origin, test.origin);
if (test.protocol) assert.strictEqual(url.protocol, test.protocol);
if (test.username) assert.strictEqual(url.username, test.username);
if (test.password) assert.strictEqual(url.password, test.password);
if (test.hostname) assert.strictEqual(url.hostname, test.hostname);
if (test.host) assert.strictEqual(url.host, test.host);
if (test.port !== undefined) assert.strictEqual(url.port, test.port);
if (test.pathname) assert.strictEqual(url.pathname, test.pathname);
if (test.search) assert.strictEqual(url.search, test.search);
if (test.hash) assert.strictEqual(url.hash, test.hash);
}
for (const test of tests) { for (const test of tests) {
if (typeof test === 'string') if (typeof test === 'string')
continue; continue;
@ -35,167 +21,23 @@ for (const test of tests) {
if (test.failure) { if (test.failure) {
assert.throws(() => new URL(test.input, test.base), assert.throws(() => new URL(test.input, test.base),
/^TypeError: Invalid URL$/); /^TypeError: Invalid URL$/);
} else {
const url = new URL(test.input, test.base);
verifyURL(url, test);
} }
} }
const additional_tests = [ const additional_tests = require(
{ path.join(common.fixturesDir, 'url-tests-additional.js'));
'url': 'tftp://foobar.com/someconfig;mode=netascii',
'protocol': 'tftp:',
'hostname': 'foobar.com',
'pathname': '/someconfig;mode=netascii'
},
{
'url': 'telnet://user:pass@foobar.com:23/',
'protocol': 'telnet:',
'username': 'user',
'password': 'pass',
'hostname': 'foobar.com',
'port': '23',
'pathname': '/'
},
{
'url': 'ut2004://10.10.10.10:7777/Index.ut2',
'protocol': 'ut2004:',
'hostname': '10.10.10.10',
'port': '7777',
'pathname': '/Index.ut2'
},
{
'url': 'redis://foo:bar@somehost:6379/0?baz=bam&qux=baz',
'protocol': 'redis:',
'username': 'foo',
'password': 'bar',
'hostname': 'somehost',
'port': '6379',
'pathname': '/0',
'search': '?baz=bam&qux=baz'
},
{
'url': 'rsync://foo@host:911/sup',
'protocol': 'rsync:',
'username': 'foo',
'hostname': 'host',
'port': '911',
'pathname': '/sup'
},
{
'url': 'git://github.com/foo/bar.git',
'protocol': 'git:',
'hostname': 'github.com',
'pathname': '/foo/bar.git'
},
{
'url': 'irc://myserver.com:6999/channel?passwd',
'protocol': 'irc:',
'hostname': 'myserver.com',
'port': '6999',
'pathname': '/channel',
'search': '?passwd'
},
{
'url': 'dns://fw.example.org:9999/foo.bar.org?type=TXT',
'protocol': 'dns:',
'hostname': 'fw.example.org',
'port': '9999',
'pathname': '/foo.bar.org',
'search': '?type=TXT'
},
{
'url': 'ldap://localhost:389/ou=People,o=JNDITutorial',
'protocol': 'ldap:',
'hostname': 'localhost',
'port': '389',
'pathname': '/ou=People,o=JNDITutorial'
},
{
'url': 'git+https://github.com/foo/bar',
'protocol': 'git+https:',
'hostname': 'github.com',
'pathname': '/foo/bar'
},
{
'url': 'urn:ietf:rfc:2648',
'protocol': 'urn:',
'pathname': 'ietf:rfc:2648'
},
{
'url': 'tag:joe@example.org,2001:foo/bar',
'protocol': 'tag:',
'pathname': 'joe@example.org,2001:foo/bar'
}
];
for (const test of additional_tests) { for (const test of additional_tests) {
const url = new URL(test.url); const url = new URL(test.url);
verifyURL(url, test); if (test.href) assert.strictEqual(url.href, test.href);
} if (test.origin) assert.strictEqual(url.origin, test.origin);
if (test.protocol) assert.strictEqual(url.protocol, test.protocol);
// test inspect if (test.username) assert.strictEqual(url.username, test.username);
const allTests = additional_tests.slice(); if (test.password) assert.strictEqual(url.password, test.password);
for (const test of tests) { if (test.hostname) assert.strictEqual(url.hostname, test.hostname);
if (test.failure || typeof test === 'string') continue; if (test.host) assert.strictEqual(url.host, test.host);
allTests.push(test); if (test.port !== undefined) assert.strictEqual(url.port, test.port);
} if (test.pathname) assert.strictEqual(url.pathname, test.pathname);
if (test.search) assert.strictEqual(url.search, test.search);
for (const test of allTests) { if (test.hash) assert.strictEqual(url.hash, test.hash);
const url = test.url ? new URL(test.url) : new URL(test.input, test.base);
for (const showHidden of [true, false]) {
const res = util.inspect(url, {
showHidden
});
const lines = res.split('\n');
const firstLine = lines[0];
assert.strictEqual(firstLine, 'URL {');
const lastLine = lines[lines.length - 1];
assert.strictEqual(lastLine, '}');
const innerLines = lines.slice(1, lines.length - 1);
const keys = new Set();
for (const line of innerLines) {
const i = line.indexOf(': ');
const k = line.slice(0, i).trim();
const v = line.slice(i + 2);
assert.strictEqual(keys.has(k), false, 'duplicate key found: ' + k);
keys.add(k);
const hidden = new Set([
'password',
'cannot-be-base',
'special'
]);
if (showHidden) {
if (!hidden.has(k)) {
assert.strictEqual(v, url[k], k);
continue;
}
if (k === 'password') {
assert.strictEqual(v, url[k], k);
}
if (k === 'cannot-be-base') {
assert.ok(v.match(/^true$|^false$/), k + ' is Boolean');
}
if (k === 'special') {
assert.ok(v.match(/^true$|^false$/), k + ' is Boolean');
}
continue;
}
// showHidden is false
if (k === 'password') {
assert.strictEqual(v, '--------', k);
continue;
}
assert.strictEqual(hidden.has(k), false, 'no hidden keys: ' + k);
assert.strictEqual(v, url[k], k);
}
}
} }

2
test/parallel/test-whatwg-url-properties.js

@ -2,11 +2,11 @@
'use strict'; 'use strict';
require('../common'); require('../common');
const URL = require('url').URL; const URL = require('url').URL;
const assert = require('assert'); const assert = require('assert');
const urlToOptions = require('internal/url').urlToOptions; const urlToOptions = require('internal/url').urlToOptions;
// Tests below are not from WPT.
const url = new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test'); const url = new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test');
const oldParams = url.searchParams; // for test of [SameObject] const oldParams = url.searchParams; // for test of [SameObject]

103
test/parallel/test-whatwg-url-searchparams-append.js

@ -1,55 +1,60 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const URLSearchParams = require('url').URLSearchParams; const URLSearchParams = require('url').URLSearchParams;
const { test, assert_equals, assert_true } = common.WPT;
let params; /* eslint-disable */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/8791bed/url/urlsearchparams-append.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
test(function() {
var params = new URLSearchParams();
params.append('a', 'b');
assert_equals(params + '', 'a=b');
params.append('a', 'b');
assert_equals(params + '', 'a=b&a=b');
params.append('a', 'c');
assert_equals(params + '', 'a=b&a=b&a=c');
}, 'Append same name');
test(function() {
var params = new URLSearchParams();
params.append('', '');
assert_equals(params + '', '=');
params.append('', '');
assert_equals(params + '', '=&=');
}, 'Append empty strings');
test(function() {
var params = new URLSearchParams();
params.append(null, null);
assert_equals(params + '', 'null=null');
params.append(null, null);
assert_equals(params + '', 'null=null&null=null');
}, 'Append null');
test(function() {
var params = new URLSearchParams();
params.append('first', 1);
params.append('second', 2);
params.append('third', '');
params.append('first', 10);
assert_true(params.has('first'), 'Search params object has name "first"');
assert_equals(params.get('first'), '1', 'Search params object has name "first" with value "1"');
assert_equals(params.get('second'), '2', 'Search params object has name "second" with value "2"');
assert_equals(params.get('third'), '', 'Search params object has name "third" with value ""');
params.append('first', 10);
assert_equals(params.get('first'), '1', 'Search params object has name "first" with value "1"');
}, 'Append multiple');
/* eslint-enable */
// Append same name // Tests below are not from WPT.
params = new URLSearchParams(); {
params.append('a', 'b'); const params = new URLSearchParams();
assert.strictEqual(params + '', 'a=b'); assert.throws(() => {
params.append('a', 'b'); params.append.call(undefined);
assert.strictEqual(params + '', 'a=b&a=b'); }, /^TypeError: Value of `this` is not a URLSearchParams$/);
params.append('a', 'c'); assert.throws(() => {
assert.strictEqual(params + '', 'a=b&a=b&a=c'); params.set('a');
}, /^TypeError: "name" and "value" arguments must be specified$/);
// Append empty strings }
params = new URLSearchParams();
params.append('', '');
assert.strictEqual(params + '', '=');
params.append('', '');
assert.strictEqual(params + '', '=&=');
// Append null
params = new URLSearchParams();
params.append(null, null);
assert.strictEqual(params + '', 'null=null');
params.append(null, null);
assert.strictEqual(params + '', 'null=null&null=null');
// Append multiple
params = new URLSearchParams();
params.append('first', 1);
params.append('second', 2);
params.append('third', '');
params.append('first', 10);
assert.strictEqual(true, params.has('first'),
'Search params object has name "first"');
assert.strictEqual(params.get('first'), '1',
'Search params object has name "first" with value "1"');
assert.strictEqual(params.get('second'), '2',
'Search params object has name "second" with value "2"');
assert.strictEqual(params.get('third'), '',
'Search params object has name "third" with value ""');
params.append('first', 10);
assert.strictEqual(params.get('first'), '1',
'Search params object has name "first" with value "1"');
assert.throws(() => {
params.append.call(undefined);
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
assert.throws(() => {
params.set('a');
}, /^TypeError: "name" and "value" arguments must be specified$/);

316
test/parallel/test-whatwg-url-searchparams-constructor.js

@ -1,130 +1,196 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const URLSearchParams = require('url').URLSearchParams; const URLSearchParams = require('url').URLSearchParams;
const {
test, assert_equals, assert_true, assert_false
} = common.WPT;
let params; /* eslint-disable */
var params; // Strict mode fix for WPT.
// Basic URLSearchParams construction /* WPT Refs:
params = new URLSearchParams(); https://github.com/w3c/web-platform-tests/blob/405394a/url/urlsearchparams-constructor.html
assert.strictEqual(params + '', ''); License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
params = new URLSearchParams(''); */
assert.strictEqual(params + '', ''); test(function() {
params = new URLSearchParams('a=b'); var params = new URLSearchParams();
assert.strictEqual(params + '', 'a=b'); assert_equals(params + '', '');
params = new URLSearchParams(params); params = new URLSearchParams('');
assert.strictEqual(params + '', 'a=b'); assert_equals(params + '', '');
params = new URLSearchParams('a=b');
// URLSearchParams constructor, empty. assert_equals(params + '', 'a=b');
assert.throws(() => URLSearchParams(), TypeError, params = new URLSearchParams(params);
'Calling \'URLSearchParams\' without \'new\' should throw.'); assert_equals(params + '', 'a=b');
// assert.throws(() => new URLSearchParams(DOMException.prototype), TypeError); }, 'Basic URLSearchParams construction');
assert.throws(() => {
new URLSearchParams({ test(function() {
toString() { throw new TypeError('Illegal invocation'); } var params = new URLSearchParams()
}); assert_equals(params.toString(), "")
}, TypeError); }, "URLSearchParams constructor, no arguments")
params = new URLSearchParams('');
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); // test(() => {
// eslint-disable-next-line no-proto // params = new URLSearchParams(DOMException.prototype);
assert.strictEqual(params.__proto__, URLSearchParams.prototype, // assert_equals(params.toString(), "INDEX_SIZE_ERR=1&DOMSTRING_SIZE_ERR=2&HIERARCHY_REQUEST_ERR=3&WRONG_DOCUMENT_ERR=4&INVALID_CHARACTER_ERR=5&NO_DATA_ALLOWED_ERR=6&NO_MODIFICATION_ALLOWED_ERR=7&NOT_FOUND_ERR=8&NOT_SUPPORTED_ERR=9&INUSE_ATTRIBUTE_ERR=10&INVALID_STATE_ERR=11&SYNTAX_ERR=12&INVALID_MODIFICATION_ERR=13&NAMESPACE_ERR=14&INVALID_ACCESS_ERR=15&VALIDATION_ERR=16&TYPE_MISMATCH_ERR=17&SECURITY_ERR=18&NETWORK_ERR=19&ABORT_ERR=20&URL_MISMATCH_ERR=21&QUOTA_EXCEEDED_ERR=22&TIMEOUT_ERR=23&INVALID_NODE_TYPE_ERR=24&DATA_CLONE_ERR=25")
'expected URLSearchParams.prototype as prototype.'); // }, "URLSearchParams constructor, DOMException.prototype as argument")
params = new URLSearchParams({});
// assert.strictEqual(params + '', '%5Bobject+Object%5D='); test(() => {
assert.strictEqual(params + '', '%5Bobject%20Object%5D='); params = new URLSearchParams('');
assert_true(params != null, 'constructor returned non-null value.');
// URLSearchParams constructor, string. assert_equals(params.__proto__, URLSearchParams.prototype, 'expected URLSearchParams.prototype as prototype.');
params = new URLSearchParams('a=b'); }, "URLSearchParams constructor, empty string as argument")
assert.notStrictEqual(params, null, 'constructor returned non-null value.');
assert.strictEqual(true, params.has('a'), // test(() => {
'Search params object has name "a"'); // params = new URLSearchParams({});
assert.strictEqual(false, params.has('b'), // assert_equals(params + '', "");
'Search params object has not got name "b"'); // }, 'URLSearchParams constructor, {} as argument');
params = new URLSearchParams('a=b&c');
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); test(function() {
assert.strictEqual(true, params.has('a'), var params = new URLSearchParams('a=b');
'Search params object has name "a"'); assert_true(params != null, 'constructor returned non-null value.');
assert.strictEqual(true, params.has('c'), assert_true(params.has('a'), 'Search params object has name "a"');
'Search params object has name "c"'); assert_false(params.has('b'), 'Search params object has not got name "b"');
params = new URLSearchParams('&a&&& &&&&&a+b=& c&m%c3%b8%c3%b8'); var params = new URLSearchParams('a=b&c');
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); assert_true(params != null, 'constructor returned non-null value.');
assert.strictEqual(true, params.has('a'), 'Search params object has name "a"'); assert_true(params.has('a'), 'Search params object has name "a"');
assert.strictEqual(true, params.has('a b'), assert_true(params.has('c'), 'Search params object has name "c"');
'Search params object has name "a b"'); var params = new URLSearchParams('&a&&& &&&&&a+b=& c&m%c3%b8%c3%b8');
assert.strictEqual(true, params.has(' '), assert_true(params != null, 'constructor returned non-null value.');
'Search params object has name " "'); assert_true(params.has('a'), 'Search params object has name "a"');
assert.strictEqual(false, params.has('c'), assert_true(params.has('a b'), 'Search params object has name "a b"');
'Search params object did not have the name "c"'); assert_true(params.has(' '), 'Search params object has name " "');
assert.strictEqual(true, params.has(' c'), assert_false(params.has('c'), 'Search params object did not have the name "c"');
'Search params object has name " c"'); assert_true(params.has(' c'), 'Search params object has name " c"');
assert.strictEqual(true, params.has('møø'), assert_true(params.has('møø'), 'Search params object has name "møø"');
'Search params object has name "møø"'); }, 'URLSearchParams constructor, string.');
// URLSearchParams constructor, object. test(function() {
const seed = new URLSearchParams('a=b&c=d'); var seed = new URLSearchParams('a=b&c=d');
params = new URLSearchParams(seed); var params = new URLSearchParams(seed);
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); assert_true(params != null, 'constructor returned non-null value.');
assert.strictEqual(params.get('a'), 'b'); assert_equals(params.get('a'), 'b');
assert.strictEqual(params.get('c'), 'd'); assert_equals(params.get('c'), 'd');
assert.strictEqual(false, params.has('d')); assert_false(params.has('d'));
// The name-value pairs are copied when created; later updates // The name-value pairs are copied when created; later updates
// should not be observable. // should not be observable.
seed.append('e', 'f'); seed.append('e', 'f');
assert.strictEqual(false, params.has('e')); assert_false(params.has('e'));
params.append('g', 'h'); params.append('g', 'h');
assert.strictEqual(false, seed.has('g')); assert_false(seed.has('g'));
}, 'URLSearchParams constructor, object.');
// Parse +
params = new URLSearchParams('a=b+c'); test(function() {
assert.strictEqual(params.get('a'), 'b c'); var params = new URLSearchParams('a=b+c');
params = new URLSearchParams('a+b=c'); assert_equals(params.get('a'), 'b c');
assert.strictEqual(params.get('a b'), 'c'); params = new URLSearchParams('a+b=c');
assert_equals(params.get('a b'), 'c');
// Parse space }, 'Parse +');
params = new URLSearchParams('a=b c');
assert.strictEqual(params.get('a'), 'b c'); test(function() {
params = new URLSearchParams('a b=c'); var params = new URLSearchParams('a=b c');
assert.strictEqual(params.get('a b'), 'c'); assert_equals(params.get('a'), 'b c');
params = new URLSearchParams('a b=c');
// Parse %20 assert_equals(params.get('a b'), 'c');
params = new URLSearchParams('a=b%20c'); }, 'Parse space');
assert.strictEqual(params.get('a'), 'b c');
params = new URLSearchParams('a%20b=c'); test(function() {
assert.strictEqual(params.get('a b'), 'c'); var params = new URLSearchParams('a=b%20c');
assert_equals(params.get('a'), 'b c');
// Parse \0 params = new URLSearchParams('a%20b=c');
params = new URLSearchParams('a=b\0c'); assert_equals(params.get('a b'), 'c');
assert.strictEqual(params.get('a'), 'b\0c'); }, 'Parse %20');
params = new URLSearchParams('a\0b=c');
assert.strictEqual(params.get('a\0b'), 'c'); test(function() {
var params = new URLSearchParams('a=b\0c');
// Parse %00 assert_equals(params.get('a'), 'b\0c');
params = new URLSearchParams('a=b%00c'); params = new URLSearchParams('a\0b=c');
assert.strictEqual(params.get('a'), 'b\0c'); assert_equals(params.get('a\0b'), 'c');
params = new URLSearchParams('a%00b=c'); }, 'Parse \\0');
assert.strictEqual(params.get('a\0b'), 'c');
test(function() {
// Parse \u2384 (Unicode Character 'COMPOSITION SYMBOL' (U+2384)) var params = new URLSearchParams('a=b%00c');
params = new URLSearchParams('a=b\u2384'); assert_equals(params.get('a'), 'b\0c');
assert.strictEqual(params.get('a'), 'b\u2384'); params = new URLSearchParams('a%00b=c');
params = new URLSearchParams('a\u2384b=c'); assert_equals(params.get('a\0b'), 'c');
assert.strictEqual(params.get('a\u2384b'), 'c'); }, 'Parse %00');
// Parse %e2%8e%84 (Unicode Character 'COMPOSITION SYMBOL' (U+2384)) test(function() {
params = new URLSearchParams('a=b%e2%8e%84'); var params = new URLSearchParams('a=b\u2384');
assert.strictEqual(params.get('a'), 'b\u2384'); assert_equals(params.get('a'), 'b\u2384');
params = new URLSearchParams('a%e2%8e%84b=c'); params = new URLSearchParams('a\u2384b=c');
assert.strictEqual(params.get('a\u2384b'), 'c'); assert_equals(params.get('a\u2384b'), 'c');
}, 'Parse \u2384'); // Unicode Character 'COMPOSITION SYMBOL' (U+2384)
// Parse \uD83D\uDCA9 (Unicode Character 'PILE OF POO' (U+1F4A9))
params = new URLSearchParams('a=b\uD83D\uDCA9c'); test(function() {
assert.strictEqual(params.get('a'), 'b\uD83D\uDCA9c'); var params = new URLSearchParams('a=b%e2%8e%84');
params = new URLSearchParams('a\uD83D\uDCA9b=c'); assert_equals(params.get('a'), 'b\u2384');
assert.strictEqual(params.get('a\uD83D\uDCA9b'), 'c'); params = new URLSearchParams('a%e2%8e%84b=c');
assert_equals(params.get('a\u2384b'), 'c');
// Parse %f0%9f%92%a9 (Unicode Character 'PILE OF POO' (U+1F4A9)) }, 'Parse %e2%8e%84'); // Unicode Character 'COMPOSITION SYMBOL' (U+2384)
params = new URLSearchParams('a=b%f0%9f%92%a9c');
assert.strictEqual(params.get('a'), 'b\uD83D\uDCA9c'); test(function() {
params = new URLSearchParams('a%f0%9f%92%a9b=c'); var params = new URLSearchParams('a=b\uD83D\uDCA9c');
assert.strictEqual(params.get('a\uD83D\uDCA9b'), 'c'); assert_equals(params.get('a'), 'b\uD83D\uDCA9c');
params = new URLSearchParams('a\uD83D\uDCA9b=c');
assert_equals(params.get('a\uD83D\uDCA9b'), 'c');
}, 'Parse \uD83D\uDCA9'); // Unicode Character 'PILE OF POO' (U+1F4A9)
test(function() {
var params = new URLSearchParams('a=b%f0%9f%92%a9c');
assert_equals(params.get('a'), 'b\uD83D\uDCA9c');
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)
// test(function() {
// var params = new URLSearchParams([]);
// assert_true(params != null, 'constructor returned non-null value.');
// params = new URLSearchParams([['a', 'b'], ['c', 'd']]);
// assert_equals(params.get("a"), "b");
// assert_equals(params.get("c"), "d");
// assert_throws(new TypeError(), function() { new URLSearchParams([[1]]); });
// assert_throws(new TypeError(), function() { new URLSearchParams([[1,2,3]]); });
// }, "Constructor with sequence of sequences of strings");
// [
// { "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(() => {
// params = new URLSearchParams()
// params[Symbol.iterator] = function *() {
// yield ["a", "b"]
// }
// let params2 = new URLSearchParams(params)
// assert_equals(params2.get("a"), "b")
// }, "Custom [Symbol.iterator]")
/* eslint-enable */
// Tests below are not from WPT.
{
// assert.throws(() => {
// new URLSearchParams({
// toString() { throw new TypeError('Illegal invocation'); }
// });
// }, TypeError);
}
{
let params;
// URLSearchParams constructor, undefined and null as argument
params = new URLSearchParams(undefined);
assert.strictEqual(params.toString(), '');
params = new URLSearchParams(null);
assert.strictEqual(params.toString(), 'null=');
}

91
test/parallel/test-whatwg-url-searchparams-delete.js

@ -1,52 +1,57 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const url = require('url'); const { URL, URLSearchParams } = require('url');
const URL = url.URL; const { test, assert_equals, assert_true, assert_false } = common.WPT;
const URLSearchParams = url.URLSearchParams;
let params; /* eslint-disable */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/8791bed/url/urlsearchparams-delete.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
test(function() {
var params = new URLSearchParams('a=b&c=d');
params.delete('a');
assert_equals(params + '', 'c=d');
params = new URLSearchParams('a=a&b=b&a=a&c=c');
params.delete('a');
assert_equals(params + '', 'b=b&c=c');
params = new URLSearchParams('a=a&=&b=b&c=c');
params.delete('');
assert_equals(params + '', 'a=a&b=b&c=c');
params = new URLSearchParams('a=a&null=null&b=b');
params.delete(null);
assert_equals(params + '', 'a=a&b=b');
params = new URLSearchParams('a=a&undefined=undefined&b=b');
params.delete(undefined);
assert_equals(params + '', 'a=a&b=b');
}, 'Delete basics');
// Delete basics test(function() {
params = new URLSearchParams('a=b&c=d'); var params = new URLSearchParams();
params.delete('a'); params.append('first', 1);
assert.strictEqual(params + '', 'c=d'); assert_true(params.has('first'), 'Search params object has name "first"');
params = new URLSearchParams('a=a&b=b&a=a&c=c'); assert_equals(params.get('first'), '1', 'Search params object has name "first" with value "1"');
params.delete('a'); params.delete('first');
assert.strictEqual(params + '', 'b=b&c=c'); assert_false(params.has('first'), 'Search params object has no "first" name');
params = new URLSearchParams('a=a&=&b=b&c=c'); params.append('first', 1);
params.delete(''); params.append('first', 10);
assert.strictEqual(params + '', 'a=a&b=b&c=c'); params.delete('first');
params = new URLSearchParams('a=a&null=null&b=b'); assert_false(params.has('first'), 'Search params object has no "first" name');
params.delete(null); }, 'Deleting appended multiple');
assert.strictEqual(params + '', 'a=a&b=b'); /* eslint-enable */
params = new URLSearchParams('a=a&undefined=undefined&b=b');
params.delete(undefined);
assert.strictEqual(params + '', 'a=a&b=b');
// Deleting appended multiple // Tests below are not from WPT.
params = new URLSearchParams(); {
params.append('first', 1); const params = new URLSearchParams();
assert.strictEqual(true, params.has('first'), assert.throws(() => {
'Search params object has name "first"'); params.delete.call(undefined);
assert.strictEqual(params.get('first'), '1', }, /^TypeError: Value of `this` is not a URLSearchParams$/);
'Search params object has name "first" with value "1"'); assert.throws(() => {
params.delete('first'); params.delete();
assert.strictEqual(false, params.has('first'), }, /^TypeError: "name" argument must be specified$/);
'Search params object has no "first" name'); }
params.append('first', 1);
params.append('first', 10);
params.delete('first');
assert.strictEqual(false, params.has('first'),
'Search params object has no "first" name');
assert.throws(() => {
params.delete.call(undefined);
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
assert.throws(() => {
params.delete();
}, /^TypeError: "name" argument must be specified$/);
// https://github.com/nodejs/node/issues/10480 // https://github.com/nodejs/node/issues/10480
// Emptying searchParams should correctly update url's query // Emptying searchParams should correctly update url's query

1
test/parallel/test-whatwg-url-searchparams-entries.js

@ -4,6 +4,7 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
const URLSearchParams = require('url').URLSearchParams; const URLSearchParams = require('url').URLSearchParams;
// Tests below are not from WPT.
const params = new URLSearchParams('a=b&c=d'); const params = new URLSearchParams('a=b&c=d');
const entries = params.entries(); const entries = params.entries();
assert.strictEqual(typeof entries[Symbol.iterator], 'function'); assert.strictEqual(typeof entries[Symbol.iterator], 'function');

79
test/parallel/test-whatwg-url-searchparams-foreach.js

@ -2,42 +2,53 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const url = require('url'); const { URL, URLSearchParams } = require('url');
const URL = url.URL; const { test, assert_array_equals, assert_unreached } = common.WPT;
const URLSearchParams = url.URLSearchParams;
let a, b, i; /* eslint-disable */
var i; // Strict mode fix for WPT.
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/a8b2b1e/url/urlsearchparams-foreach.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
test(function() {
var params = new URLSearchParams('a=1&b=2&c=3');
var keys = [];
var values = [];
params.forEach(function(value, key) {
keys.push(key);
values.push(value);
});
assert_array_equals(keys, ['a', 'b', 'c']);
assert_array_equals(values, ['1', '2', '3']);
}, "ForEach Check");
// ForEach Check test(function() {
const params = new URLSearchParams('a=1&b=2&c=3'); let a = new URL("http://a.b/c?a=1&b=2&c=3&d=4");
const keys = []; let b = a.searchParams;
const values = []; var c = [];
params.forEach((value, key) => { for (i of b) {
keys.push(key); a.search = "x=1&y=2&z=3";
values.push(value); c.push(i);
}); }
assert.deepStrictEqual(keys, ['a', 'b', 'c']); assert_array_equals(c[0], ["a","1"]);
assert.deepStrictEqual(values, ['1', '2', '3']); assert_array_equals(c[1], ["y","2"]);
assert_array_equals(c[2], ["z","3"]);
}, "For-of Check");
// For-of Check test(function() {
a = new URL('http://a.b/c?a=1&b=2&c=3&d=4'); let a = new URL("http://a.b/c");
b = a.searchParams; let b = a.searchParams;
const c = []; for (i of b) {
for (i of b) { assert_unreached(i);
a.search = 'x=1&y=2&z=3'; }
c.push(i); }, "empty");
} /* eslint-enable */
assert.deepStrictEqual(c[0], ['a', '1']);
assert.deepStrictEqual(c[1], ['y', '2']);
assert.deepStrictEqual(c[2], ['z', '3']);
// empty // Tests below are not from WPT.
a = new URL('http://a.b/c'); {
b = a.searchParams; const params = new URLSearchParams();
for (i of b) { assert.throws(() => {
common.fail('should not be reached'); params.forEach.call(undefined);
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
} }
assert.throws(() => {
params.forEach.call(undefined);
}, /^TypeError: Value of `this` is not a URLSearchParams$/);

69
test/parallel/test-whatwg-url-searchparams-get.js

@ -1,38 +1,45 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const URLSearchParams = require('url').URLSearchParams; const URLSearchParams = require('url').URLSearchParams;
const { test, assert_equals, assert_true } = common.WPT;
let params; /* eslint-disable */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/8791bed/url/urlsearchparams-get.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
test(function() {
var params = new URLSearchParams('a=b&c=d');
assert_equals(params.get('a'), 'b');
assert_equals(params.get('c'), 'd');
assert_equals(params.get('e'), null);
params = new URLSearchParams('a=b&c=d&a=e');
assert_equals(params.get('a'), 'b');
params = new URLSearchParams('=b&c=d');
assert_equals(params.get(''), 'b');
params = new URLSearchParams('a=&c=d&a=e');
assert_equals(params.get('a'), '');
}, 'Get basics');
// Get basics test(function() {
params = new URLSearchParams('a=b&c=d'); var params = new URLSearchParams('first=second&third&&');
assert.strictEqual(params.get('a'), 'b'); assert_true(params != null, 'constructor returned non-null value.');
assert.strictEqual(params.get('c'), 'd'); assert_true(params.has('first'), 'Search params object has name "first"');
assert.strictEqual(params.get('e'), null); assert_equals(params.get('first'), 'second', 'Search params object has name "first" with value "second"');
params = new URLSearchParams('a=b&c=d&a=e'); assert_equals(params.get('third'), '', 'Search params object has name "third" with the empty value.');
assert.strictEqual(params.get('a'), 'b'); assert_equals(params.get('fourth'), null, 'Search params object has no "fourth" name and value.');
params = new URLSearchParams('=b&c=d'); }, 'More get() basics');
assert.strictEqual(params.get(''), 'b'); /* eslint-enable */
params = new URLSearchParams('a=&c=d&a=e');
assert.strictEqual(params.get('a'), '');
// More get() basics // Tests below are not from WPT.
params = new URLSearchParams('first=second&third&&'); {
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); const params = new URLSearchParams();
assert.strictEqual(true, params.has('first'), assert.throws(() => {
'Search params object has name "first"'); params.get.call(undefined);
assert.strictEqual(params.get('first'), 'second', }, /^TypeError: Value of `this` is not a URLSearchParams$/);
'Search params object has name "first" with value "second"'); assert.throws(() => {
assert.strictEqual(params.get('third'), '', params.get();
'Search params object has name "third" with empty value.'); }, /^TypeError: "name" argument must be specified$/);
assert.strictEqual(params.get('fourth'), null, }
'Search params object has no "fourth" name and value.');
assert.throws(() => {
params.get.call(undefined);
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
assert.throws(() => {
params.get();
}, /^TypeError: "name" argument must be specified$/);

80
test/parallel/test-whatwg-url-searchparams-getall.js

@ -1,45 +1,49 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const URLSearchParams = require('url').URLSearchParams; const URLSearchParams = require('url').URLSearchParams;
const { test, assert_equals, assert_true, assert_array_equals } = common.WPT;
let params; /* eslint-disable */
let matches; /* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/8791bed/url/urlsearchparams-getall.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
test(function() {
var params = new URLSearchParams('a=b&c=d');
assert_array_equals(params.getAll('a'), ['b']);
assert_array_equals(params.getAll('c'), ['d']);
assert_array_equals(params.getAll('e'), []);
params = new URLSearchParams('a=b&c=d&a=e');
assert_array_equals(params.getAll('a'), ['b', 'e']);
params = new URLSearchParams('=b&c=d');
assert_array_equals(params.getAll(''), ['b']);
params = new URLSearchParams('a=&c=d&a=e');
assert_array_equals(params.getAll('a'), ['', 'e']);
}, 'getAll() basics');
// getAll() basics test(function() {
params = new URLSearchParams('a=b&c=d'); var params = new URLSearchParams('a=1&a=2&a=3&a');
assert.deepStrictEqual(params.getAll('a'), ['b']); assert_true(params.has('a'), 'Search params object has name "a"');
assert.deepStrictEqual(params.getAll('c'), ['d']); var matches = params.getAll('a');
assert.deepStrictEqual(params.getAll('e'), []); assert_true(matches && matches.length == 4, 'Search params object has values for name "a"');
params = new URLSearchParams('a=b&c=d&a=e'); assert_array_equals(matches, ['1', '2', '3', ''], 'Search params object has expected name "a" values');
assert.deepStrictEqual(params.getAll('a'), ['b', 'e']); params.set('a', 'one');
params = new URLSearchParams('=b&c=d'); assert_equals(params.get('a'), 'one', 'Search params object has name "a" with value "one"');
assert.deepStrictEqual(params.getAll(''), ['b']); var matches = params.getAll('a');
params = new URLSearchParams('a=&c=d&a=e'); assert_true(matches && matches.length == 1, 'Search params object has values for name "a"');
assert.deepStrictEqual(params.getAll('a'), ['', 'e']); assert_array_equals(matches, ['one'], 'Search params object has expected name "a" values');
}, 'getAll() multiples');
/* eslint-enable */
// getAll() multiples // Tests below are not from WPT.
params = new URLSearchParams('a=1&a=2&a=3&a'); {
assert.strictEqual(true, params.has('a'), const params = new URLSearchParams();
'Search params object has name "a"'); assert.throws(() => {
matches = params.getAll('a'); params.getAll.call(undefined);
assert(matches && matches.length == 4, }, /^TypeError: Value of `this` is not a URLSearchParams$/);
'Search params object has values for name "a"'); assert.throws(() => {
assert.deepStrictEqual(matches, ['1', '2', '3', ''], params.getAll();
'Search params object has expected name "a" values'); }, /^TypeError: "name" argument must be specified$/);
params.set('a', 'one'); }
assert.strictEqual(params.get('a'), 'one',
'Search params object has name "a" with value "one"');
matches = params.getAll('a');
assert(matches && matches.length == 1,
'Search params object has values for name "a"');
assert.deepStrictEqual(matches, ['one'],
'Search params object has expected name "a" values');
assert.throws(() => {
params.getAll.call(undefined);
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
assert.throws(() => {
params.getAll();
}, /^TypeError: "name" argument must be specified$/);

76
test/parallel/test-whatwg-url-searchparams-has.js

@ -1,42 +1,48 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const URLSearchParams = require('url').URLSearchParams; const URLSearchParams = require('url').URLSearchParams;
const { test, assert_false, assert_true } = common.WPT;
let params; /* eslint-disable */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/8791bed/url/urlsearchparams-has.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
test(function() {
var params = new URLSearchParams('a=b&c=d');
assert_true(params.has('a'));
assert_true(params.has('c'));
assert_false(params.has('e'));
params = new URLSearchParams('a=b&c=d&a=e');
assert_true(params.has('a'));
params = new URLSearchParams('=b&c=d');
assert_true(params.has(''));
params = new URLSearchParams('null=a');
assert_true(params.has(null));
}, 'Has basics');
// Has basics test(function() {
params = new URLSearchParams('a=b&c=d'); var params = new URLSearchParams('a=b&c=d&&');
assert.strictEqual(true, params.has('a')); params.append('first', 1);
assert.strictEqual(true, params.has('c')); params.append('first', 2);
assert.strictEqual(false, params.has('e')); assert_true(params.has('a'), 'Search params object has name "a"');
params = new URLSearchParams('a=b&c=d&a=e'); assert_true(params.has('c'), 'Search params object has name "c"');
assert.strictEqual(true, params.has('a')); assert_true(params.has('first'), 'Search params object has name "first"');
params = new URLSearchParams('=b&c=d'); assert_false(params.has('d'), 'Search params object has no name "d"');
assert.strictEqual(true, params.has('')); params.delete('first');
params = new URLSearchParams('null=a'); assert_false(params.has('first'), 'Search params object has no name "first"');
assert.strictEqual(true, params.has(null)); }, 'has() following delete()');
/* eslint-enable */
// has() following delete() // Tests below are not from WPT.
params = new URLSearchParams('a=b&c=d&&'); {
params.append('first', 1); const params = new URLSearchParams();
params.append('first', 2); assert.throws(() => {
assert.strictEqual(true, params.has('a'), params.has.call(undefined);
'Search params object has name "a"'); }, /^TypeError: Value of `this` is not a URLSearchParams$/);
assert.strictEqual(true, params.has('c'), assert.throws(() => {
'Search params object has name "c"'); params.has();
assert.strictEqual(true, params.has('first'), }, /^TypeError: "name" argument must be specified$/);
'Search params object has name "first"'); }
assert.strictEqual(false, params.has('d'),
'Search params object has no name "d"');
params.delete('first');
assert.strictEqual(false, params.has('first'),
'Search params object has no name "first"');
assert.throws(() => {
params.has.call(undefined);
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
assert.throws(() => {
params.has();
}, /^TypeError: "name" argument must be specified$/);

7
test/parallel/test-whatwg-url-searchparams-inspect.js

@ -3,12 +3,9 @@
require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const util = require('util'); const util = require('util');
const URL = require('url').URL; const URLSearchParams = require('url').URLSearchParams;
// Until we export URLSearchParams
const m = new URL('http://example.org');
const URLSearchParams = m.searchParams.constructor;
// Tests below are not from WPT.
const sp = new URLSearchParams('?a=a&b=b&b=c'); const sp = new URLSearchParams('?a=a&b=b&b=c');
assert.strictEqual(util.inspect(sp), assert.strictEqual(util.inspect(sp),
"URLSearchParams { 'a' => 'a', 'b' => 'b', 'b' => 'c' }"); "URLSearchParams { 'a' => 'a', 'b' => 'b', 'b' => 'c' }");

1
test/parallel/test-whatwg-url-searchparams-keys.js

@ -4,6 +4,7 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
const URLSearchParams = require('url').URLSearchParams; const URLSearchParams = require('url').URLSearchParams;
// Tests below are not from WPT.
const params = new URLSearchParams('a=b&c=d'); const params = new URLSearchParams('a=b&c=d');
const keys = params.keys(); const keys = params.keys();

73
test/parallel/test-whatwg-url-searchparams-set.js

@ -1,41 +1,46 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const URLSearchParams = require('url').URLSearchParams; const URLSearchParams = require('url').URLSearchParams;
const { test, assert_equals, assert_true } = common.WPT;
let params; /* eslint-disable */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/8791bed/url/urlsearchparams-set.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
test(function() {
var params = new URLSearchParams('a=b&c=d');
params.set('a', 'B');
assert_equals(params + '', 'a=B&c=d');
params = new URLSearchParams('a=b&c=d&a=e');
params.set('a', 'B');
assert_equals(params + '', 'a=B&c=d')
params.set('e', 'f');
assert_equals(params + '', 'a=B&c=d&e=f')
}, 'Set basics');
// Set basics test(function() {
params = new URLSearchParams('a=b&c=d'); var params = new URLSearchParams('a=1&a=2&a=3');
params.set('a', 'B'); assert_true(params.has('a'), 'Search params object has name "a"');
assert.strictEqual(params + '', 'a=B&c=d'); assert_equals(params.get('a'), '1', 'Search params object has name "a" with value "1"');
params = new URLSearchParams('a=b&c=d&a=e'); params.set('first', 4);
params.set('a', 'B'); assert_true(params.has('a'), 'Search params object has name "a"');
assert.strictEqual(params + '', 'a=B&c=d'); assert_equals(params.get('a'), '1', 'Search params object has name "a" with value "1"');
params.set('e', 'f'); params.set('a', 4);
assert.strictEqual(params + '', 'a=B&c=d&e=f'); assert_true(params.has('a'), 'Search params object has name "a"');
assert_equals(params.get('a'), '4', 'Search params object has name "a" with value "4"');
}, 'URLSearchParams.set');
/* eslint-enable */
// URLSearchParams.set // Tests below are not from WPT.
params = new URLSearchParams('a=1&a=2&a=3'); {
assert.strictEqual(true, params.has('a'), const params = new URLSearchParams();
'Search params object has name "a"'); assert.throws(() => {
assert.strictEqual(params.get('a'), '1', params.set.call(undefined);
'Search params object has name "a" with value "1"'); }, /^TypeError: Value of `this` is not a URLSearchParams$/);
params.set('first', 4); assert.throws(() => {
assert.strictEqual(true, params.has('a'), params.set('a');
'Search params object has name "a"'); }, /^TypeError: "name" and "value" arguments must be specified$/);
assert.strictEqual(params.get('a'), '1', }
'Search params object has name "a" with value "1"');
params.set('a', 4);
assert.strictEqual(true, params.has('a'),
'Search params object has name "a"');
assert.strictEqual(params.get('a'), '4',
'Search params object has name "a" with value "4"');
assert.throws(() => {
params.set.call(undefined);
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
assert.throws(() => {
params.set('a');
}, /^TypeError: "name" and "value" arguments must be specified$/);

238
test/parallel/test-whatwg-url-searchparams-stringifier.js

@ -1,115 +1,131 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const URLSearchParams = require('url').URLSearchParams; const URLSearchParams = require('url').URLSearchParams;
const { test, assert_equals } = common.WPT;
let params;
/* eslint-disable */
// Serialize space /* WPT Refs:
// querystring does not currently handle spaces intelligently https://github.com/w3c/web-platform-tests/blob/8791bed/url/urlsearchparams-stringifier.html
// params = new URLSearchParams(); License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
// params.append('a', 'b c'); */
// assert.strictEqual(params + '', 'a=b+c'); // test(function() {
// params.delete('a'); // var params = new URLSearchParams();
// params.append('a b', 'c'); // params.append('a', 'b c');
// assert.strictEqual(params + '', 'a+b=c'); // assert_equals(params + '', 'a=b+c');
// params.delete('a');
// Serialize empty value // params.append('a b', 'c');
params = new URLSearchParams(); // assert_equals(params + '', 'a+b=c');
params.append('a', ''); // }, 'Serialize space');
assert.strictEqual(params + '', 'a=');
params.append('a', ''); test(function() {
assert.strictEqual(params + '', 'a=&a='); var params = new URLSearchParams();
params.append('', 'b'); params.append('a', '');
assert.strictEqual(params + '', 'a=&a=&=b'); assert_equals(params + '', 'a=');
params.append('', ''); params.append('a', '');
assert.strictEqual(params + '', 'a=&a=&=b&='); assert_equals(params + '', 'a=&a=');
params.append('', ''); params.append('', 'b');
assert.strictEqual(params + '', 'a=&a=&=b&=&='); assert_equals(params + '', 'a=&a=&=b');
params.append('', '');
// Serialize empty name assert_equals(params + '', 'a=&a=&=b&=');
params = new URLSearchParams(); params.append('', '');
params.append('', 'b'); assert_equals(params + '', 'a=&a=&=b&=&=');
assert.strictEqual(params + '', '=b'); }, 'Serialize empty value');
params.append('', 'b');
assert.strictEqual(params + '', '=b&=b'); test(function() {
var params = new URLSearchParams();
// Serialize empty name and value params.append('', 'b');
params = new URLSearchParams(); assert_equals(params + '', '=b');
params.append('', ''); params.append('', 'b');
assert.strictEqual(params + '', '='); assert_equals(params + '', '=b&=b');
params.append('', ''); }, 'Serialize empty name');
assert.strictEqual(params + '', '=&=');
test(function() {
// Serialize + var params = new URLSearchParams();
params = new URLSearchParams(); params.append('', '');
params.append('a', 'b+c'); assert_equals(params + '', '=');
assert.strictEqual(params + '', 'a=b%2Bc'); params.append('', '');
params.delete('a'); assert_equals(params + '', '=&=');
params.append('a+b', 'c'); }, 'Serialize empty name and value');
assert.strictEqual(params + '', 'a%2Bb=c');
test(function() {
// Serialize = var params = new URLSearchParams();
params = new URLSearchParams(); params.append('a', 'b+c');
params.append('=', 'a'); assert_equals(params + '', 'a=b%2Bc');
assert.strictEqual(params + '', '%3D=a'); params.delete('a');
params.append('b', '='); params.append('a+b', 'c');
assert.strictEqual(params + '', '%3D=a&b=%3D'); assert_equals(params + '', 'a%2Bb=c');
}, 'Serialize +');
// Serialize &
params = new URLSearchParams(); test(function() {
params.append('&', 'a'); var params = new URLSearchParams();
assert.strictEqual(params + '', '%26=a'); params.append('=', 'a');
params.append('b', '&'); assert_equals(params + '', '%3D=a');
assert.strictEqual(params + '', '%26=a&b=%26'); params.append('b', '=');
assert_equals(params + '', '%3D=a&b=%3D');
// Serialize *-._ }, 'Serialize =');
params = new URLSearchParams();
params.append('a', '*-._'); test(function() {
assert.strictEqual(params + '', 'a=*-._'); var params = new URLSearchParams();
params.delete('a'); params.append('&', 'a');
params.append('*-._', 'c'); assert_equals(params + '', '%26=a');
assert.strictEqual(params + '', '*-._=c'); params.append('b', '&');
assert_equals(params + '', '%26=a&b=%26');
// Serialize % }, 'Serialize &');
params = new URLSearchParams();
params.append('a', 'b%c'); test(function() {
assert.strictEqual(params + '', 'a=b%25c'); var params = new URLSearchParams();
params.delete('a'); params.append('a', '*-._');
params.append('a%b', 'c'); assert_equals(params + '', 'a=*-._');
assert.strictEqual(params + '', 'a%25b=c'); params.delete('a');
params.append('*-._', 'c');
// Serialize \\0 assert_equals(params + '', '*-._=c');
params = new URLSearchParams(); }, 'Serialize *-._');
params.append('a', 'b\0c');
assert.strictEqual(params + '', 'a=b%00c'); test(function() {
params.delete('a'); var params = new URLSearchParams();
params.append('a\0b', 'c'); params.append('a', 'b%c');
assert.strictEqual(params + '', 'a%00b=c'); assert_equals(params + '', 'a=b%25c');
params.delete('a');
// Serialize \uD83D\uDCA9 params.append('a%b', 'c');
// Unicode Character 'PILE OF POO' (U+1F4A9) assert_equals(params + '', 'a%25b=c');
params = new URLSearchParams(); }, 'Serialize %');
params.append('a', 'b\uD83D\uDCA9c');
assert.strictEqual(params + '', 'a=b%F0%9F%92%A9c'); test(function() {
params.delete('a'); var params = new URLSearchParams();
params.append('a\uD83D\uDCA9b', 'c'); params.append('a', 'b\0c');
assert.strictEqual(params + '', 'a%F0%9F%92%A9b=c'); assert_equals(params + '', 'a=b%00c');
params.delete('a');
// URLSearchParams.toString params.append('a\0b', 'c');
assert_equals(params + '', 'a%00b=c');
// querystring parses `&&` as {'': ''} }, 'Serialize \\0');
// params = new URLSearchParams('a=b&c=d&&e&&');
// assert.strictEqual(params.toString(), 'a=b&c=d&e='); test(function() {
var params = new URLSearchParams();
// querystring does not currently handle spaces intelligently params.append('a', 'b\uD83D\uDCA9c');
// params = new URLSearchParams('a = b &a=b&c=d%20'); assert_equals(params + '', 'a=b%F0%9F%92%A9c');
// assert.strictEqual(params.toString(), 'a+=+b+&a=b&c=d+'); params.delete('a');
params.append('a\uD83D\uDCA9b', 'c');
// The lone '=' _does_ survive the roundtrip. assert_equals(params + '', 'a%F0%9F%92%A9b=c');
params = new URLSearchParams('a=&a=b'); }, 'Serialize \uD83D\uDCA9'); // Unicode Character 'PILE OF POO' (U+1F4A9)
assert.strictEqual(params.toString(), 'a=&a=b');
assert.throws(() => { test(function() {
params.toString.call(undefined); var params;
}, /^TypeError: Value of `this` is not a URLSearchParams$/); // params = new URLSearchParams('a=b&c=d&&e&&');
// assert_equals(params.toString(), 'a=b&c=d&e=');
// params = new URLSearchParams('a = b &a=b&c=d%20');
// assert_equals(params.toString(), 'a+=+b+&a=b&c=d+');
// The lone '=' _does_ survive the roundtrip.
params = new URLSearchParams('a=&a=b');
assert_equals(params.toString(), 'a=&a=b');
}, 'URLSearchParams.toString');
/* eslint-enable */
// Tests below are not from WPT.
{
const params = new URLSearchParams();
assert.throws(() => {
params.toString.call(undefined);
}, /^TypeError: Value of `this` is not a URLSearchParams$/);
}

1
test/parallel/test-whatwg-url-searchparams-values.js

@ -4,6 +4,7 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
const URLSearchParams = require('url').URLSearchParams; const URLSearchParams = require('url').URLSearchParams;
// Tests below are not from WPT.
const params = new URLSearchParams('a=b&c=d'); const params = new URLSearchParams('a=b&c=d');
const values = params.values(); const values = params.values();

1
test/parallel/test-whatwg-url-searchparams.js

@ -4,6 +4,7 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
const URL = require('url').URL; const URL = require('url').URL;
// Tests below are not from WPT.
const serialized = 'a=a&a=1&a=true&a=undefined&a=null&a=%5Bobject%20Object%5D'; const serialized = 'a=a&a=1&a=true&a=undefined&a=null&a=%5Bobject%20Object%5D';
const values = ['a', 1, true, undefined, null, {}]; const values = ['a', 1, true, undefined, null, {}];

82
test/parallel/test-whatwg-url-setters.js

@ -1,6 +1,9 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const path = require('path');
const URL = require('url').URL;
const { test, assert_equals } = common.WPT;
if (!common.hasIntl) { if (!common.hasIntl) {
// A handful of the tests fail when ICU is not included. // A handful of the tests fail when ICU is not included.
@ -8,25 +11,68 @@ if (!common.hasIntl) {
return; return;
} }
const path = require('path'); const request = {
const URL = require('url').URL; response: require(path.join(common.fixturesDir, 'url-setter-tests.json'))
const assert = require('assert'); };
const attrs = require(path.join(common.fixturesDir, 'url-setter-tests.json'));
for (const attr in attrs) { /* eslint-disable */
if (attr === 'comment') /* WPT Refs:
continue; https://github.com/w3c/web-platform-tests/blob/8791bed/url/url-setters.html
const tests = attrs[attr]; License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
let n = 0; */
for (const test of tests) { function startURLSettersTests() {
if (test.skip) continue; // var setup = async_test("Loading data…")
n++; // setup.step(function() {
const url = new URL(test.href); // var request = new XMLHttpRequest()
url[attr] = test.new_value; // request.open("GET", "setters_tests.json")
for (const test_attr in test.expected) { // request.send()
assert.strictEqual(test.expected[test_attr], url[test_attr], // request.responseType = "json"
`${n} ${attr} ${test_attr} ` + // request.onload = setup.step_func(function() {
`${test.href} ${test.comment}`); runURLSettersTests(request.response)
// setup.done()
// })
// })
}
function runURLSettersTests(all_test_cases) {
for (var attribute_to_be_set in all_test_cases) {
if (attribute_to_be_set == "comment") {
continue;
}
var test_cases = all_test_cases[attribute_to_be_set];
for(var i = 0, l = test_cases.length; i < l; i++) {
var test_case = test_cases[i];
var name = "Setting <" + test_case.href + ">." + attribute_to_be_set +
" = '" + test_case.new_value + "'";
if ("comment" in test_case) {
name += " " + test_case.comment;
}
test(function() {
var url = new URL(test_case.href);
url[attribute_to_be_set] = test_case.new_value;
for (var attribute in test_case.expected) {
assert_equals(url[attribute], test_case.expected[attribute])
}
}, "URL: " + name)
// test(function() {
// var url = document.createElement("a");
// url.href = test_case.href;
// url[attribute_to_be_set] = test_case.new_value;
// for (var attribute in test_case.expected) {
// assert_equals(url[attribute], test_case.expected[attribute])
// }
// }, "<a>: " + name)
// test(function() {
// var url = document.createElement("area");
// url.href = test_case.href;
// url[attribute_to_be_set] = test_case.new_value;
// for (var attribute in test_case.expected) {
// assert_equals(url[attribute], test_case.expected[attribute])
// }
// }, "<area>: " + name)
} }
} }
} }
startURLSettersTests()
/* eslint-enable */

2
test/parallel/test-whatwg-url-tostringtag.js

@ -3,6 +3,8 @@
require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const URL = require('url').URL; const URL = require('url').URL;
// Tests below are not from WPT.
const toString = Object.prototype.toString; const toString = Object.prototype.toString;
const url = new URL('http://example.org'); const url = new URL('http://example.org');

Loading…
Cancel
Save