Skip to content

Commit 10b687b

Browse files
joyeecheungjasnell
authored andcommitted
test, url: synchronize WPT url tests
* attributon of WPT in url-setter-tests * add WPT test utilities * synchronize WPT URLSearchParams tests * synchronize WPT url tests * split whatwg-url-inspect test * port historical url tests from WPT * protocol setter and special URLs Refs: web-platform-tests/wpt#4413 Refs: whatwg/url#104 PR-URL: #11079 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent 2a88738 commit 10b687b

25 files changed

+1205
-761
lines changed

test/common.js

+25
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,28 @@ Object.defineProperty(exports, 'hasIntl', {
585585
return process.binding('config').hasIntl;
586586
}
587587
});
588+
589+
// https://github.com/w3c/testharness.js/blob/master/testharness.js
590+
exports.WPT = {
591+
test: (fn, desc) => {
592+
try {
593+
fn();
594+
} catch (err) {
595+
if (err instanceof Error)
596+
err.message = `In ${desc}:\n ${err.message}`;
597+
throw err;
598+
}
599+
},
600+
assert_equals: assert.strictEqual,
601+
assert_true: (value, message) => assert.strictEqual(value, true, message),
602+
assert_false: (value, message) => assert.strictEqual(value, false, message),
603+
assert_throws: (code, func, desc) => {
604+
assert.throws(func, (err) => {
605+
return typeof err === 'object' && 'name' in err && err.name === code.name;
606+
}, desc);
607+
},
608+
assert_array_equals: assert.deepStrictEqual,
609+
assert_unreached(desc) {
610+
assert.fail(undefined, undefined, `Reached unreachable code: ${desc}`);
611+
}
612+
};

test/fixtures/url-setter-tests.json

+44-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"comment": [
3+
"License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html",
34
"## Tests for setters of https://url.spec.whatwg.org/#urlutils-members",
45
"",
56
"This file contains a JSON object.",
@@ -19,8 +20,7 @@
1920
" get the attribute `key` (invoke its getter).",
2021
" The returned string must be equal to `value`.",
2122
"",
22-
"Note: the 'href' setter is already covered by urltestdata.json.",
23-
"Source: https://github.com/w3c/web-platform-tests/tree/master/url"
23+
"Note: the 'href' setter is already covered by urltestdata.json."
2424
],
2525
"protocol": [
2626
{
@@ -103,14 +103,30 @@
103103
}
104104
},
105105
{
106-
"comment": "Can’t switch from special scheme to non-special. Note: this may change, see https://github.com/whatwg/url/issues/104",
106+
"comment": "Can’t switch from special scheme to non-special",
107107
"href": "http://example.net",
108108
"new_value": "b",
109109
"expected": {
110110
"href": "http://example.net/",
111111
"protocol": "http:"
112112
}
113113
},
114+
{
115+
"href": "https://example.net",
116+
"new_value": "s",
117+
"expected": {
118+
"href": "https://example.net/",
119+
"protocol": "https:"
120+
}
121+
},
122+
{
123+
"href": "ftp://example.net",
124+
"new_value": "test",
125+
"expected": {
126+
"href": "ftp://example.net/",
127+
"protocol": "ftp:"
128+
}
129+
},
114130
{
115131
"comment": "Cannot-be-a-base URL doesn’t have a host, but URL in a special scheme must.",
116132
"href": "mailto:[email protected]",
@@ -121,14 +137,38 @@
121137
}
122138
},
123139
{
124-
"comment": "Can’t switch from non-special scheme to special. Note: this may change, see https://github.com/whatwg/url/issues/104",
140+
"comment": "Can’t switch from non-special scheme to special",
125141
"href": "ssh://[email protected]",
126142
"new_value": "http",
127143
"expected": {
128144
"href": "ssh://[email protected]/",
129145
"protocol": "ssh:"
130146
}
131147
},
148+
{
149+
"href": "ssh://[email protected]",
150+
"new_value": "gopher",
151+
"expected": {
152+
"href": "ssh://[email protected]/",
153+
"protocol": "ssh:"
154+
}
155+
},
156+
{
157+
"href": "ssh://[email protected]",
158+
"new_value": "file",
159+
"expected": {
160+
"href": "ssh://[email protected]/",
161+
"protocol": "ssh:"
162+
}
163+
},
164+
{
165+
"href": "nonsense:///test",
166+
"new_value": "https",
167+
"expected": {
168+
"href": "nonsense:///test",
169+
"protocol": "nonsense:"
170+
}
171+
},
132172
{
133173
"comment": "Stuff after the first ':' is ignored",
134174
"href": "http://example.net",

test/fixtures/url-tests-additional.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
module.exports = [
2+
{
3+
'url': 'tftp://foobar.com/someconfig;mode=netascii',
4+
'protocol': 'tftp:',
5+
'hostname': 'foobar.com',
6+
'pathname': '/someconfig;mode=netascii'
7+
},
8+
{
9+
'url': 'telnet://user:[email protected]:23/',
10+
'protocol': 'telnet:',
11+
'username': 'user',
12+
'password': 'pass',
13+
'hostname': 'foobar.com',
14+
'port': '23',
15+
'pathname': '/'
16+
},
17+
{
18+
'url': 'ut2004://10.10.10.10:7777/Index.ut2',
19+
'protocol': 'ut2004:',
20+
'hostname': '10.10.10.10',
21+
'port': '7777',
22+
'pathname': '/Index.ut2'
23+
},
24+
{
25+
'url': 'redis://foo:bar@somehost:6379/0?baz=bam&qux=baz',
26+
'protocol': 'redis:',
27+
'username': 'foo',
28+
'password': 'bar',
29+
'hostname': 'somehost',
30+
'port': '6379',
31+
'pathname': '/0',
32+
'search': '?baz=bam&qux=baz'
33+
},
34+
{
35+
'url': 'rsync://foo@host:911/sup',
36+
'protocol': 'rsync:',
37+
'username': 'foo',
38+
'hostname': 'host',
39+
'port': '911',
40+
'pathname': '/sup'
41+
},
42+
{
43+
'url': 'git://github.com/foo/bar.git',
44+
'protocol': 'git:',
45+
'hostname': 'github.com',
46+
'pathname': '/foo/bar.git'
47+
},
48+
{
49+
'url': 'irc://myserver.com:6999/channel?passwd',
50+
'protocol': 'irc:',
51+
'hostname': 'myserver.com',
52+
'port': '6999',
53+
'pathname': '/channel',
54+
'search': '?passwd'
55+
},
56+
{
57+
'url': 'dns://fw.example.org:9999/foo.bar.org?type=TXT',
58+
'protocol': 'dns:',
59+
'hostname': 'fw.example.org',
60+
'port': '9999',
61+
'pathname': '/foo.bar.org',
62+
'search': '?type=TXT'
63+
},
64+
{
65+
'url': 'ldap://localhost:389/ou=People,o=JNDITutorial',
66+
'protocol': 'ldap:',
67+
'hostname': 'localhost',
68+
'port': '389',
69+
'pathname': '/ou=People,o=JNDITutorial'
70+
},
71+
{
72+
'url': 'git+https://github.com/foo/bar',
73+
'protocol': 'git+https:',
74+
'hostname': 'github.com',
75+
'pathname': '/foo/bar'
76+
},
77+
{
78+
'url': 'urn:ietf:rfc:2648',
79+
'protocol': 'urn:',
80+
'pathname': 'ietf:rfc:2648'
81+
},
82+
{
83+
'url': 'tag:[email protected],2001:foo/bar',
84+
'protocol': 'tag:',
85+
'pathname': '[email protected],2001:foo/bar'
86+
}
87+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
'use strict';
2+
const common = require('../common');
3+
const path = require('path');
4+
const { URL, URLSearchParams } = require('url');
5+
const { test, assert_equals, assert_true, assert_throws } = common.WPT;
6+
7+
if (!common.hasIntl) {
8+
// A handful of the tests fail when ICU is not included.
9+
common.skip('missing Intl');
10+
return;
11+
}
12+
13+
const request = {
14+
response: require(path.join(common.fixturesDir, 'url-tests.json'))
15+
};
16+
17+
/* eslint-disable */
18+
/* WPT Refs:
19+
https://github.com/w3c/web-platform-tests/blob/8791bed/url/url-constructor.html
20+
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
21+
*/
22+
function runURLConstructorTests() {
23+
// var setup = async_test("Loading data…")
24+
// setup.step(function() {
25+
// var request = new XMLHttpRequest()
26+
// request.open("GET", "urltestdata.json")
27+
// request.send()
28+
// request.responseType = "json"
29+
// request.onload = setup.step_func(function() {
30+
runURLTests(request.response)
31+
// setup.done()
32+
// })
33+
// })
34+
}
35+
36+
function bURL(url, base) {
37+
return new URL(url, base || "about:blank")
38+
}
39+
40+
41+
function runURLTests(urltests) {
42+
for(var i = 0, l = urltests.length; i < l; i++) {
43+
var expected = urltests[i]
44+
if (typeof expected === "string") continue // skip comments
45+
46+
test(function() {
47+
if (expected.failure) {
48+
assert_throws(new TypeError(), function() {
49+
bURL(expected.input, expected.base)
50+
})
51+
return
52+
}
53+
54+
var url = bURL(expected.input, expected.base)
55+
assert_equals(url.href, expected.href, "href")
56+
assert_equals(url.protocol, expected.protocol, "protocol")
57+
assert_equals(url.username, expected.username, "username")
58+
assert_equals(url.password, expected.password, "password")
59+
assert_equals(url.host, expected.host, "host")
60+
assert_equals(url.hostname, expected.hostname, "hostname")
61+
assert_equals(url.port, expected.port, "port")
62+
assert_equals(url.pathname, expected.pathname, "pathname")
63+
assert_equals(url.search, expected.search, "search")
64+
if ("searchParams" in expected) {
65+
assert_true("searchParams" in url)
66+
// assert_equals(url.searchParams.toString(), expected.searchParams, "searchParams")
67+
}
68+
assert_equals(url.hash, expected.hash, "hash")
69+
}, "Parsing: <" + expected.input + "> against <" + expected.base + ">")
70+
}
71+
}
72+
73+
function runURLSearchParamTests() {
74+
test(function() {
75+
var url = bURL('http://example.org/?a=b')
76+
assert_true("searchParams" in url)
77+
var searchParams = url.searchParams
78+
assert_true(url.searchParams === searchParams, 'Object identity should hold.')
79+
}, 'URL.searchParams getter')
80+
81+
test(function() {
82+
var url = bURL('http://example.org/?a=b')
83+
assert_true("searchParams" in url)
84+
var searchParams = url.searchParams
85+
assert_equals(searchParams.toString(), 'a=b')
86+
87+
searchParams.set('a', 'b')
88+
assert_equals(url.searchParams.toString(), 'a=b')
89+
assert_equals(url.search, '?a=b')
90+
url.search = ''
91+
assert_equals(url.searchParams.toString(), '')
92+
assert_equals(url.search, '')
93+
assert_equals(searchParams.toString(), '')
94+
}, 'URL.searchParams updating, clearing')
95+
96+
test(function() {
97+
'use strict'
98+
var urlString = 'http://example.org'
99+
var url = bURL(urlString)
100+
assert_throws(TypeError(), function() { url.searchParams = new URLSearchParams(urlString) })
101+
}, 'URL.searchParams setter, invalid values')
102+
103+
test(function() {
104+
var url = bURL('http://example.org/file?a=b&c=d')
105+
assert_true("searchParams" in url)
106+
var searchParams = url.searchParams
107+
assert_equals(url.search, '?a=b&c=d')
108+
assert_equals(searchParams.toString(), 'a=b&c=d')
109+
110+
// Test that setting 'search' propagates to the URL object's query object.
111+
url.search = 'e=f&g=h'
112+
assert_equals(url.search, '?e=f&g=h')
113+
assert_equals(searchParams.toString(), 'e=f&g=h')
114+
115+
// ..and same but with a leading '?'.
116+
url.search = '?e=f&g=h'
117+
assert_equals(url.search, '?e=f&g=h')
118+
assert_equals(searchParams.toString(), 'e=f&g=h')
119+
120+
// And in the other direction, altering searchParams propagates
121+
// back to 'search'.
122+
// searchParams.append('i', ' j ')
123+
// assert_equals(url.search, '?e=f&g=h&i=+j+')
124+
// assert_equals(url.searchParams.toString(), 'e=f&g=h&i=+j+')
125+
// assert_equals(searchParams.get('i'), ' j ')
126+
127+
// searchParams.set('e', 'updated')
128+
// assert_equals(url.search, '?e=updated&g=h&i=+j+')
129+
// assert_equals(searchParams.get('e'), 'updated')
130+
131+
// var url2 = bURL('http://example.org/file??a=b&c=d')
132+
// assert_equals(url2.search, '??a=b&c=d')
133+
// assert_equals(url2.searchParams.toString(), '%3Fa=b&c=d')
134+
135+
// url2.href = 'http://example.org/file??a=b'
136+
// assert_equals(url2.search, '??a=b')
137+
// assert_equals(url2.searchParams.toString(), '%3Fa=b')
138+
}, 'URL.searchParams and URL.search setters, update propagation')
139+
}
140+
runURLSearchParamTests()
141+
runURLConstructorTests()
142+
/* eslint-enable */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
const common = require('../common');
3+
const URL = require('url').URL;
4+
const { test, assert_equals, assert_throws } = common.WPT;
5+
6+
if (!common.hasIntl) {
7+
// A handful of the tests fail when ICU is not included.
8+
common.skip('missing Intl');
9+
return;
10+
}
11+
12+
/* eslint-disable */
13+
/* WPT Refs:
14+
https://github.com/w3c/web-platform-tests/blob/8791bed/url/historical.html
15+
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
16+
*/
17+
// var objects = [
18+
// [function() { return window.location }, "location object"],
19+
// [function() { return document.createElement("a") }, "a element"],
20+
// [function() { return document.createElement("area") }, "area element"],
21+
// ];
22+
23+
// objects.forEach(function(o) {
24+
// test(function() {
25+
// var object = o[0]();
26+
// assert_false("searchParams" in object,
27+
// o[1] + " should not have a searchParams attribute");
28+
// }, "searchParams on " + o[1]);
29+
// });
30+
31+
test(function() {
32+
var url = new URL("./foo", "http://www.example.org");
33+
assert_equals(url.href, "http://www.example.org/foo");
34+
assert_throws(new TypeError(), function() {
35+
url.href = "./bar";
36+
});
37+
}, "Setting URL's href attribute and base URLs");
38+
39+
test(function() {
40+
assert_equals(URL.domainToASCII, undefined);
41+
}, "URL.domainToASCII should be undefined");
42+
43+
test(function() {
44+
assert_equals(URL.domainToUnicode, undefined);
45+
}, "URL.domainToUnicode should be undefined");
46+
/* eslint-enable */

0 commit comments

Comments
 (0)