mirror of https://github.com/lukechilds/node.git
Browse Source
* 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/11079v7.x
Joyee Cheung
8 years ago
committed by
Italo A. Casas
25 changed files with 1194 additions and 703 deletions
@ -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' |
||||
|
} |
||||
|
]; |
@ -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 */ |
@ -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 */ |
@ -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); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -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 */ |
@ -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$/); |
|
||||
|
@ -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"A_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='); |
||||
|
} |
||||
|
@ -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$/); |
|
||||
|
@ -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$/); |
|
||||
|
@ -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$/); |
|
||||
|
@ -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$/); |
|
||||
|
@ -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$/); |
||||
|
} |
||||
|
Loading…
Reference in new issue