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'; |
|||
|
|||
require('../common'); |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
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
|
|||
params = new URLSearchParams(); |
|||
params.append('a', 'b'); |
|||
assert.strictEqual(params + '', 'a=b'); |
|||
params.append('a', 'b'); |
|||
assert.strictEqual(params + '', 'a=b&a=b'); |
|||
params.append('a', 'c'); |
|||
assert.strictEqual(params + '', 'a=b&a=b&a=c'); |
|||
|
|||
// 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$/); |
|||
// Tests below are not from WPT.
|
|||
{ |
|||
const params = new URLSearchParams(); |
|||
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'; |
|||
|
|||
require('../common'); |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const URLSearchParams = require('url').URLSearchParams; |
|||
const { |
|||
test, assert_equals, assert_true, assert_false |
|||
} = common.WPT; |
|||
|
|||
let params; |
|||
|
|||
// Basic URLSearchParams construction
|
|||
params = new URLSearchParams(); |
|||
assert.strictEqual(params + '', ''); |
|||
params = new URLSearchParams(''); |
|||
assert.strictEqual(params + '', ''); |
|||
params = new URLSearchParams('a=b'); |
|||
assert.strictEqual(params + '', 'a=b'); |
|||
params = new URLSearchParams(params); |
|||
assert.strictEqual(params + '', 'a=b'); |
|||
|
|||
// URLSearchParams constructor, empty.
|
|||
assert.throws(() => URLSearchParams(), TypeError, |
|||
'Calling \'URLSearchParams\' without \'new\' should throw.'); |
|||
// assert.throws(() => new URLSearchParams(DOMException.prototype), TypeError);
|
|||
assert.throws(() => { |
|||
new URLSearchParams({ |
|||
toString() { throw new TypeError('Illegal invocation'); } |
|||
}); |
|||
}, TypeError); |
|||
params = new URLSearchParams(''); |
|||
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); |
|||
// eslint-disable-next-line no-proto
|
|||
assert.strictEqual(params.__proto__, URLSearchParams.prototype, |
|||
'expected URLSearchParams.prototype as prototype.'); |
|||
params = new URLSearchParams({}); |
|||
// assert.strictEqual(params + '', '%5Bobject+Object%5D=');
|
|||
assert.strictEqual(params + '', '%5Bobject%20Object%5D='); |
|||
|
|||
// URLSearchParams constructor, string.
|
|||
params = new URLSearchParams('a=b'); |
|||
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); |
|||
assert.strictEqual(true, params.has('a'), |
|||
'Search params object has name "a"'); |
|||
assert.strictEqual(false, params.has('b'), |
|||
'Search params object has not got name "b"'); |
|||
params = new URLSearchParams('a=b&c'); |
|||
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); |
|||
assert.strictEqual(true, params.has('a'), |
|||
'Search params object has name "a"'); |
|||
assert.strictEqual(true, params.has('c'), |
|||
'Search params object has name "c"'); |
|||
params = new URLSearchParams('&a&&& &&&&&a+b=& c&m%c3%b8%c3%b8'); |
|||
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); |
|||
assert.strictEqual(true, params.has('a'), 'Search params object has name "a"'); |
|||
assert.strictEqual(true, params.has('a b'), |
|||
'Search params object has name "a b"'); |
|||
assert.strictEqual(true, params.has(' '), |
|||
'Search params object has name " "'); |
|||
assert.strictEqual(false, params.has('c'), |
|||
'Search params object did not have the name "c"'); |
|||
assert.strictEqual(true, params.has(' c'), |
|||
'Search params object has name " c"'); |
|||
assert.strictEqual(true, params.has('møø'), |
|||
'Search params object has name "møø"'); |
|||
|
|||
// URLSearchParams constructor, object.
|
|||
const seed = new URLSearchParams('a=b&c=d'); |
|||
params = new URLSearchParams(seed); |
|||
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); |
|||
assert.strictEqual(params.get('a'), 'b'); |
|||
assert.strictEqual(params.get('c'), 'd'); |
|||
assert.strictEqual(false, params.has('d')); |
|||
// The name-value pairs are copied when created; later updates
|
|||
// should not be observable.
|
|||
seed.append('e', 'f'); |
|||
assert.strictEqual(false, params.has('e')); |
|||
params.append('g', 'h'); |
|||
assert.strictEqual(false, seed.has('g')); |
|||
|
|||
// Parse +
|
|||
params = new URLSearchParams('a=b+c'); |
|||
assert.strictEqual(params.get('a'), 'b c'); |
|||
params = new URLSearchParams('a+b=c'); |
|||
assert.strictEqual(params.get('a b'), 'c'); |
|||
|
|||
// Parse space
|
|||
params = new URLSearchParams('a=b c'); |
|||
assert.strictEqual(params.get('a'), 'b c'); |
|||
params = new URLSearchParams('a b=c'); |
|||
assert.strictEqual(params.get('a b'), 'c'); |
|||
|
|||
// Parse %20
|
|||
params = new URLSearchParams('a=b%20c'); |
|||
assert.strictEqual(params.get('a'), 'b c'); |
|||
params = new URLSearchParams('a%20b=c'); |
|||
assert.strictEqual(params.get('a b'), 'c'); |
|||
|
|||
// Parse \0
|
|||
params = new URLSearchParams('a=b\0c'); |
|||
assert.strictEqual(params.get('a'), 'b\0c'); |
|||
params = new URLSearchParams('a\0b=c'); |
|||
assert.strictEqual(params.get('a\0b'), 'c'); |
|||
|
|||
// Parse %00
|
|||
params = new URLSearchParams('a=b%00c'); |
|||
assert.strictEqual(params.get('a'), 'b\0c'); |
|||
params = new URLSearchParams('a%00b=c'); |
|||
assert.strictEqual(params.get('a\0b'), 'c'); |
|||
|
|||
// Parse \u2384 (Unicode Character 'COMPOSITION SYMBOL' (U+2384))
|
|||
params = new URLSearchParams('a=b\u2384'); |
|||
assert.strictEqual(params.get('a'), 'b\u2384'); |
|||
params = new URLSearchParams('a\u2384b=c'); |
|||
assert.strictEqual(params.get('a\u2384b'), 'c'); |
|||
|
|||
// Parse %e2%8e%84 (Unicode Character 'COMPOSITION SYMBOL' (U+2384))
|
|||
params = new URLSearchParams('a=b%e2%8e%84'); |
|||
assert.strictEqual(params.get('a'), 'b\u2384'); |
|||
params = new URLSearchParams('a%e2%8e%84b=c'); |
|||
assert.strictEqual(params.get('a\u2384b'), 'c'); |
|||
|
|||
// Parse \uD83D\uDCA9 (Unicode Character 'PILE OF POO' (U+1F4A9))
|
|||
params = new URLSearchParams('a=b\uD83D\uDCA9c'); |
|||
assert.strictEqual(params.get('a'), 'b\uD83D\uDCA9c'); |
|||
params = new URLSearchParams('a\uD83D\uDCA9b=c'); |
|||
assert.strictEqual(params.get('a\uD83D\uDCA9b'), 'c'); |
|||
|
|||
// Parse %f0%9f%92%a9 (Unicode Character 'PILE OF POO' (U+1F4A9))
|
|||
params = new URLSearchParams('a=b%f0%9f%92%a9c'); |
|||
assert.strictEqual(params.get('a'), 'b\uD83D\uDCA9c'); |
|||
params = new URLSearchParams('a%f0%9f%92%a9b=c'); |
|||
assert.strictEqual(params.get('a\uD83D\uDCA9b'), 'c'); |
|||
/* eslint-disable */ |
|||
var params; // Strict mode fix for WPT.
|
|||
/* WPT Refs: |
|||
https://github.com/w3c/web-platform-tests/blob/405394a/url/urlsearchparams-constructor.html
|
|||
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
|
|||
*/ |
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
assert_equals(params + '', ''); |
|||
params = new URLSearchParams(''); |
|||
assert_equals(params + '', ''); |
|||
params = new URLSearchParams('a=b'); |
|||
assert_equals(params + '', 'a=b'); |
|||
params = new URLSearchParams(params); |
|||
assert_equals(params + '', 'a=b'); |
|||
}, 'Basic URLSearchParams construction'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams() |
|||
assert_equals(params.toString(), "") |
|||
}, "URLSearchParams constructor, no arguments") |
|||
|
|||
// test(() => {
|
|||
// params = new URLSearchParams(DOMException.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")
|
|||
// }, "URLSearchParams constructor, DOMException.prototype as argument")
|
|||
|
|||
test(() => { |
|||
params = new URLSearchParams(''); |
|||
assert_true(params != null, 'constructor returned non-null value.'); |
|||
assert_equals(params.__proto__, URLSearchParams.prototype, 'expected URLSearchParams.prototype as prototype.'); |
|||
}, "URLSearchParams constructor, empty string as argument") |
|||
|
|||
// test(() => {
|
|||
// params = new URLSearchParams({});
|
|||
// assert_equals(params + '', "");
|
|||
// }, 'URLSearchParams constructor, {} as argument');
|
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams('a=b'); |
|||
assert_true(params != null, 'constructor returned non-null value.'); |
|||
assert_true(params.has('a'), 'Search params object has name "a"'); |
|||
assert_false(params.has('b'), 'Search params object has not got name "b"'); |
|||
var params = new URLSearchParams('a=b&c'); |
|||
assert_true(params != null, 'constructor returned non-null value.'); |
|||
assert_true(params.has('a'), 'Search params object has name "a"'); |
|||
assert_true(params.has('c'), 'Search params object has name "c"'); |
|||
var params = new URLSearchParams('&a&&& &&&&&a+b=& c&m%c3%b8%c3%b8'); |
|||
assert_true(params != null, 'constructor returned non-null value.'); |
|||
assert_true(params.has('a'), 'Search params object has name "a"'); |
|||
assert_true(params.has('a b'), 'Search params object has name "a b"'); |
|||
assert_true(params.has(' '), 'Search params object has name " "'); |
|||
assert_false(params.has('c'), 'Search params object did not have the name "c"'); |
|||
assert_true(params.has(' c'), 'Search params object has name " c"'); |
|||
assert_true(params.has('møø'), 'Search params object has name "møø"'); |
|||
}, 'URLSearchParams constructor, string.'); |
|||
|
|||
test(function() { |
|||
var seed = new URLSearchParams('a=b&c=d'); |
|||
var params = new URLSearchParams(seed); |
|||
assert_true(params != null, 'constructor returned non-null value.'); |
|||
assert_equals(params.get('a'), 'b'); |
|||
assert_equals(params.get('c'), 'd'); |
|||
assert_false(params.has('d')); |
|||
// The name-value pairs are copied when created; later updates
|
|||
// should not be observable.
|
|||
seed.append('e', 'f'); |
|||
assert_false(params.has('e')); |
|||
params.append('g', 'h'); |
|||
assert_false(seed.has('g')); |
|||
}, 'URLSearchParams constructor, object.'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams('a=b+c'); |
|||
assert_equals(params.get('a'), 'b c'); |
|||
params = new URLSearchParams('a+b=c'); |
|||
assert_equals(params.get('a b'), 'c'); |
|||
}, 'Parse +'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams('a=b c'); |
|||
assert_equals(params.get('a'), 'b c'); |
|||
params = new URLSearchParams('a b=c'); |
|||
assert_equals(params.get('a b'), 'c'); |
|||
}, 'Parse space'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams('a=b%20c'); |
|||
assert_equals(params.get('a'), 'b c'); |
|||
params = new URLSearchParams('a%20b=c'); |
|||
assert_equals(params.get('a b'), 'c'); |
|||
}, 'Parse %20'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams('a=b\0c'); |
|||
assert_equals(params.get('a'), 'b\0c'); |
|||
params = new URLSearchParams('a\0b=c'); |
|||
assert_equals(params.get('a\0b'), 'c'); |
|||
}, 'Parse \\0'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams('a=b%00c'); |
|||
assert_equals(params.get('a'), 'b\0c'); |
|||
params = new URLSearchParams('a%00b=c'); |
|||
assert_equals(params.get('a\0b'), 'c'); |
|||
}, 'Parse %00'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams('a=b\u2384'); |
|||
assert_equals(params.get('a'), 'b\u2384'); |
|||
params = new URLSearchParams('a\u2384b=c'); |
|||
assert_equals(params.get('a\u2384b'), 'c'); |
|||
}, 'Parse \u2384'); // Unicode Character 'COMPOSITION SYMBOL' (U+2384)
|
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams('a=b%e2%8e%84'); |
|||
assert_equals(params.get('a'), 'b\u2384'); |
|||
params = new URLSearchParams('a%e2%8e%84b=c'); |
|||
assert_equals(params.get('a\u2384b'), 'c'); |
|||
}, 'Parse %e2%8e%84'); // Unicode Character 'COMPOSITION SYMBOL' (U+2384)
|
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams('a=b\uD83D\uDCA9c'); |
|||
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'; |
|||
|
|||
require('../common'); |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
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
|
|||
params = new URLSearchParams('a=b&c=d'); |
|||
assert.strictEqual(params.get('a'), 'b'); |
|||
assert.strictEqual(params.get('c'), 'd'); |
|||
assert.strictEqual(params.get('e'), null); |
|||
params = new URLSearchParams('a=b&c=d&a=e'); |
|||
assert.strictEqual(params.get('a'), 'b'); |
|||
params = new URLSearchParams('=b&c=d'); |
|||
assert.strictEqual(params.get(''), 'b'); |
|||
params = new URLSearchParams('a=&c=d&a=e'); |
|||
assert.strictEqual(params.get('a'), ''); |
|||
test(function() { |
|||
var params = new URLSearchParams('first=second&third&&'); |
|||
assert_true(params != null, 'constructor returned non-null value.'); |
|||
assert_true(params.has('first'), 'Search params object has name "first"'); |
|||
assert_equals(params.get('first'), 'second', 'Search params object has name "first" with value "second"'); |
|||
assert_equals(params.get('third'), '', 'Search params object has name "third" with the empty value.'); |
|||
assert_equals(params.get('fourth'), null, 'Search params object has no "fourth" name and value.'); |
|||
}, 'More get() basics'); |
|||
/* eslint-enable */ |
|||
|
|||
// More get() basics
|
|||
params = new URLSearchParams('first=second&third&&'); |
|||
assert.notStrictEqual(params, null, 'constructor returned non-null value.'); |
|||
assert.strictEqual(true, params.has('first'), |
|||
'Search params object has name "first"'); |
|||
assert.strictEqual(params.get('first'), 'second', |
|||
'Search params object has name "first" with value "second"'); |
|||
assert.strictEqual(params.get('third'), '', |
|||
'Search params object has name "third" with empty value.'); |
|||
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$/); |
|||
// Tests below are not from WPT.
|
|||
{ |
|||
const params = new URLSearchParams(); |
|||
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'; |
|||
|
|||
require('../common'); |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const URLSearchParams = require('url').URLSearchParams; |
|||
const { test, assert_equals, assert_true, assert_array_equals } = common.WPT; |
|||
|
|||
let params; |
|||
let matches; |
|||
/* eslint-disable */ |
|||
/* 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
|
|||
params = new URLSearchParams('a=b&c=d'); |
|||
assert.deepStrictEqual(params.getAll('a'), ['b']); |
|||
assert.deepStrictEqual(params.getAll('c'), ['d']); |
|||
assert.deepStrictEqual(params.getAll('e'), []); |
|||
params = new URLSearchParams('a=b&c=d&a=e'); |
|||
assert.deepStrictEqual(params.getAll('a'), ['b', 'e']); |
|||
params = new URLSearchParams('=b&c=d'); |
|||
assert.deepStrictEqual(params.getAll(''), ['b']); |
|||
params = new URLSearchParams('a=&c=d&a=e'); |
|||
assert.deepStrictEqual(params.getAll('a'), ['', 'e']); |
|||
test(function() { |
|||
var params = new URLSearchParams('a=1&a=2&a=3&a'); |
|||
assert_true(params.has('a'), 'Search params object has name "a"'); |
|||
var matches = params.getAll('a'); |
|||
assert_true(matches && matches.length == 4, 'Search params object has values for name "a"'); |
|||
assert_array_equals(matches, ['1', '2', '3', ''], 'Search params object has expected name "a" values'); |
|||
params.set('a', 'one'); |
|||
assert_equals(params.get('a'), 'one', 'Search params object has name "a" with value "one"'); |
|||
var matches = params.getAll('a'); |
|||
assert_true(matches && matches.length == 1, 'Search params object has values for name "a"'); |
|||
assert_array_equals(matches, ['one'], 'Search params object has expected name "a" values'); |
|||
}, 'getAll() multiples'); |
|||
/* eslint-enable */ |
|||
|
|||
// getAll() multiples
|
|||
params = new URLSearchParams('a=1&a=2&a=3&a'); |
|||
assert.strictEqual(true, params.has('a'), |
|||
'Search params object has name "a"'); |
|||
matches = params.getAll('a'); |
|||
assert(matches && matches.length == 4, |
|||
'Search params object has values for name "a"'); |
|||
assert.deepStrictEqual(matches, ['1', '2', '3', ''], |
|||
'Search params object has expected name "a" values'); |
|||
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$/); |
|||
// Tests below are not from WPT.
|
|||
{ |
|||
const params = new URLSearchParams(); |
|||
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'; |
|||
|
|||
require('../common'); |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
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
|
|||
params = new URLSearchParams('a=b&c=d'); |
|||
assert.strictEqual(true, params.has('a')); |
|||
assert.strictEqual(true, params.has('c')); |
|||
assert.strictEqual(false, params.has('e')); |
|||
params = new URLSearchParams('a=b&c=d&a=e'); |
|||
assert.strictEqual(true, params.has('a')); |
|||
params = new URLSearchParams('=b&c=d'); |
|||
assert.strictEqual(true, params.has('')); |
|||
params = new URLSearchParams('null=a'); |
|||
assert.strictEqual(true, params.has(null)); |
|||
test(function() { |
|||
var params = new URLSearchParams('a=b&c=d&&'); |
|||
params.append('first', 1); |
|||
params.append('first', 2); |
|||
assert_true(params.has('a'), 'Search params object has name "a"'); |
|||
assert_true(params.has('c'), 'Search params object has name "c"'); |
|||
assert_true(params.has('first'), 'Search params object has name "first"'); |
|||
assert_false(params.has('d'), 'Search params object has no name "d"'); |
|||
params.delete('first'); |
|||
assert_false(params.has('first'), 'Search params object has no name "first"'); |
|||
}, 'has() following delete()'); |
|||
/* eslint-enable */ |
|||
|
|||
// has() following delete()
|
|||
params = new URLSearchParams('a=b&c=d&&'); |
|||
params.append('first', 1); |
|||
params.append('first', 2); |
|||
assert.strictEqual(true, params.has('a'), |
|||
'Search params object has name "a"'); |
|||
assert.strictEqual(true, params.has('c'), |
|||
'Search params object has name "c"'); |
|||
assert.strictEqual(true, params.has('first'), |
|||
'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$/); |
|||
// Tests below are not from WPT.
|
|||
{ |
|||
const params = new URLSearchParams(); |
|||
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'; |
|||
|
|||
require('../common'); |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
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
|
|||
params = new URLSearchParams('a=b&c=d'); |
|||
params.set('a', 'B'); |
|||
assert.strictEqual(params + '', 'a=B&c=d'); |
|||
params = new URLSearchParams('a=b&c=d&a=e'); |
|||
params.set('a', 'B'); |
|||
assert.strictEqual(params + '', 'a=B&c=d'); |
|||
params.set('e', 'f'); |
|||
assert.strictEqual(params + '', 'a=B&c=d&e=f'); |
|||
test(function() { |
|||
var params = new URLSearchParams('a=1&a=2&a=3'); |
|||
assert_true(params.has('a'), 'Search params object has name "a"'); |
|||
assert_equals(params.get('a'), '1', 'Search params object has name "a" with value "1"'); |
|||
params.set('first', 4); |
|||
assert_true(params.has('a'), 'Search params object has name "a"'); |
|||
assert_equals(params.get('a'), '1', 'Search params object has name "a" with value "1"'); |
|||
params.set('a', 4); |
|||
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
|
|||
params = new URLSearchParams('a=1&a=2&a=3'); |
|||
assert.strictEqual(true, params.has('a'), |
|||
'Search params object has name "a"'); |
|||
assert.strictEqual(params.get('a'), '1', |
|||
'Search params object has name "a" with value "1"'); |
|||
params.set('first', 4); |
|||
assert.strictEqual(true, params.has('a'), |
|||
'Search params object has name "a"'); |
|||
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$/); |
|||
// Tests below are not from WPT.
|
|||
{ |
|||
const params = new URLSearchParams(); |
|||
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'; |
|||
|
|||
require('../common'); |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const URLSearchParams = require('url').URLSearchParams; |
|||
|
|||
let params; |
|||
|
|||
// Serialize space
|
|||
// querystring does not currently handle spaces intelligently
|
|||
// params = new URLSearchParams();
|
|||
// params.append('a', 'b c');
|
|||
// assert.strictEqual(params + '', 'a=b+c');
|
|||
// params.delete('a');
|
|||
// params.append('a b', 'c');
|
|||
// assert.strictEqual(params + '', 'a+b=c');
|
|||
|
|||
// Serialize empty value
|
|||
params = new URLSearchParams(); |
|||
params.append('a', ''); |
|||
assert.strictEqual(params + '', 'a='); |
|||
params.append('a', ''); |
|||
assert.strictEqual(params + '', 'a=&a='); |
|||
params.append('', 'b'); |
|||
assert.strictEqual(params + '', 'a=&a=&=b'); |
|||
params.append('', ''); |
|||
assert.strictEqual(params + '', 'a=&a=&=b&='); |
|||
params.append('', ''); |
|||
assert.strictEqual(params + '', 'a=&a=&=b&=&='); |
|||
|
|||
// Serialize empty name
|
|||
params = new URLSearchParams(); |
|||
params.append('', 'b'); |
|||
assert.strictEqual(params + '', '=b'); |
|||
params.append('', 'b'); |
|||
assert.strictEqual(params + '', '=b&=b'); |
|||
|
|||
// Serialize empty name and value
|
|||
params = new URLSearchParams(); |
|||
params.append('', ''); |
|||
assert.strictEqual(params + '', '='); |
|||
params.append('', ''); |
|||
assert.strictEqual(params + '', '=&='); |
|||
|
|||
// Serialize +
|
|||
params = new URLSearchParams(); |
|||
params.append('a', 'b+c'); |
|||
assert.strictEqual(params + '', 'a=b%2Bc'); |
|||
params.delete('a'); |
|||
params.append('a+b', 'c'); |
|||
assert.strictEqual(params + '', 'a%2Bb=c'); |
|||
|
|||
// Serialize =
|
|||
params = new URLSearchParams(); |
|||
params.append('=', 'a'); |
|||
assert.strictEqual(params + '', '%3D=a'); |
|||
params.append('b', '='); |
|||
assert.strictEqual(params + '', '%3D=a&b=%3D'); |
|||
|
|||
// Serialize &
|
|||
params = new URLSearchParams(); |
|||
params.append('&', 'a'); |
|||
assert.strictEqual(params + '', '%26=a'); |
|||
params.append('b', '&'); |
|||
assert.strictEqual(params + '', '%26=a&b=%26'); |
|||
|
|||
// Serialize *-._
|
|||
params = new URLSearchParams(); |
|||
params.append('a', '*-._'); |
|||
assert.strictEqual(params + '', 'a=*-._'); |
|||
params.delete('a'); |
|||
params.append('*-._', 'c'); |
|||
assert.strictEqual(params + '', '*-._=c'); |
|||
|
|||
// Serialize %
|
|||
params = new URLSearchParams(); |
|||
params.append('a', 'b%c'); |
|||
assert.strictEqual(params + '', 'a=b%25c'); |
|||
params.delete('a'); |
|||
params.append('a%b', 'c'); |
|||
assert.strictEqual(params + '', 'a%25b=c'); |
|||
|
|||
// Serialize \\0
|
|||
params = new URLSearchParams(); |
|||
params.append('a', 'b\0c'); |
|||
assert.strictEqual(params + '', 'a=b%00c'); |
|||
params.delete('a'); |
|||
params.append('a\0b', 'c'); |
|||
assert.strictEqual(params + '', 'a%00b=c'); |
|||
|
|||
// Serialize \uD83D\uDCA9
|
|||
// Unicode Character 'PILE OF POO' (U+1F4A9)
|
|||
params = new URLSearchParams(); |
|||
params.append('a', 'b\uD83D\uDCA9c'); |
|||
assert.strictEqual(params + '', 'a=b%F0%9F%92%A9c'); |
|||
params.delete('a'); |
|||
params.append('a\uD83D\uDCA9b', 'c'); |
|||
assert.strictEqual(params + '', 'a%F0%9F%92%A9b=c'); |
|||
|
|||
// URLSearchParams.toString
|
|||
|
|||
// querystring parses `&&` as {'': ''}
|
|||
// params = new URLSearchParams('a=b&c=d&&e&&');
|
|||
// assert.strictEqual(params.toString(), 'a=b&c=d&e=');
|
|||
|
|||
// querystring does not currently handle spaces intelligently
|
|||
// params = new URLSearchParams('a = b &a=b&c=d%20');
|
|||
// assert.strictEqual(params.toString(), 'a+=+b+&a=b&c=d+');
|
|||
|
|||
// The lone '=' _does_ survive the roundtrip.
|
|||
params = new URLSearchParams('a=&a=b'); |
|||
assert.strictEqual(params.toString(), 'a=&a=b'); |
|||
assert.throws(() => { |
|||
params.toString.call(undefined); |
|||
}, /^TypeError: Value of `this` is not a URLSearchParams$/); |
|||
const { test, assert_equals } = common.WPT; |
|||
|
|||
/* eslint-disable */ |
|||
/* WPT Refs: |
|||
https://github.com/w3c/web-platform-tests/blob/8791bed/url/urlsearchparams-stringifier.html
|
|||
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
|
|||
*/ |
|||
// test(function() {
|
|||
// var params = new URLSearchParams();
|
|||
// params.append('a', 'b c');
|
|||
// assert_equals(params + '', 'a=b+c');
|
|||
// params.delete('a');
|
|||
// params.append('a b', 'c');
|
|||
// assert_equals(params + '', 'a+b=c');
|
|||
// }, 'Serialize space');
|
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
params.append('a', ''); |
|||
assert_equals(params + '', 'a='); |
|||
params.append('a', ''); |
|||
assert_equals(params + '', 'a=&a='); |
|||
params.append('', 'b'); |
|||
assert_equals(params + '', 'a=&a=&=b'); |
|||
params.append('', ''); |
|||
assert_equals(params + '', 'a=&a=&=b&='); |
|||
params.append('', ''); |
|||
assert_equals(params + '', 'a=&a=&=b&=&='); |
|||
}, 'Serialize empty value'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
params.append('', 'b'); |
|||
assert_equals(params + '', '=b'); |
|||
params.append('', 'b'); |
|||
assert_equals(params + '', '=b&=b'); |
|||
}, 'Serialize empty name'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
params.append('', ''); |
|||
assert_equals(params + '', '='); |
|||
params.append('', ''); |
|||
assert_equals(params + '', '=&='); |
|||
}, 'Serialize empty name and value'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
params.append('a', 'b+c'); |
|||
assert_equals(params + '', 'a=b%2Bc'); |
|||
params.delete('a'); |
|||
params.append('a+b', 'c'); |
|||
assert_equals(params + '', 'a%2Bb=c'); |
|||
}, 'Serialize +'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
params.append('=', 'a'); |
|||
assert_equals(params + '', '%3D=a'); |
|||
params.append('b', '='); |
|||
assert_equals(params + '', '%3D=a&b=%3D'); |
|||
}, 'Serialize ='); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
params.append('&', 'a'); |
|||
assert_equals(params + '', '%26=a'); |
|||
params.append('b', '&'); |
|||
assert_equals(params + '', '%26=a&b=%26'); |
|||
}, 'Serialize &'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
params.append('a', '*-._'); |
|||
assert_equals(params + '', 'a=*-._'); |
|||
params.delete('a'); |
|||
params.append('*-._', 'c'); |
|||
assert_equals(params + '', '*-._=c'); |
|||
}, 'Serialize *-._'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
params.append('a', 'b%c'); |
|||
assert_equals(params + '', 'a=b%25c'); |
|||
params.delete('a'); |
|||
params.append('a%b', 'c'); |
|||
assert_equals(params + '', 'a%25b=c'); |
|||
}, 'Serialize %'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
params.append('a', 'b\0c'); |
|||
assert_equals(params + '', 'a=b%00c'); |
|||
params.delete('a'); |
|||
params.append('a\0b', 'c'); |
|||
assert_equals(params + '', 'a%00b=c'); |
|||
}, 'Serialize \\0'); |
|||
|
|||
test(function() { |
|||
var params = new URLSearchParams(); |
|||
params.append('a', 'b\uD83D\uDCA9c'); |
|||
assert_equals(params + '', 'a=b%F0%9F%92%A9c'); |
|||
params.delete('a'); |
|||
params.append('a\uD83D\uDCA9b', 'c'); |
|||
assert_equals(params + '', 'a%F0%9F%92%A9b=c'); |
|||
}, 'Serialize \uD83D\uDCA9'); // Unicode Character 'PILE OF POO' (U+1F4A9)
|
|||
|
|||
test(function() { |
|||
var params; |
|||
// 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