Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url, test: synchronize web-platform-test url tests #11079

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,28 @@ Object.defineProperty(exports, 'hasIntl', {
return process.binding('config').hasIntl;
}
});

// https://github.com/w3c/testharness.js/blob/master/testharness.js
exports.WPT = {
test: (fn, desc) => {
try {
fn();
} catch (err) {
if (err instanceof Error)
err.message = `In ${desc}:\n ${err.message}`;
throw err;
}
},
assert_equals: assert.strictEqual,
assert_true: (value, message) => assert.strictEqual(value, true, message),
assert_false: (value, message) => assert.strictEqual(value, false, message),
assert_throws: (code, func, desc) => {
assert.throws(func, (err) => {
return typeof err === 'object' && 'name' in err && err.name === code.name;
}, desc);
},
assert_array_equals: assert.deepStrictEqual,
assert_unreached(desc) {
assert.fail(undefined, undefined, `Reached unreachable code: ${desc}`);
}
};
48 changes: 44 additions & 4 deletions test/fixtures/url-setter-tests.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"comment": [
"License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html",
"## Tests for setters of https://url.spec.whatwg.org/#urlutils-members",
"",
"This file contains a JSON object.",
Expand All @@ -19,8 +20,7 @@
" get the attribute `key` (invoke its getter).",
" The returned string must be equal to `value`.",
"",
"Note: the 'href' setter is already covered by urltestdata.json.",
"Source: https://github.com/w3c/web-platform-tests/tree/master/url"
"Note: the 'href' setter is already covered by urltestdata.json."
],
"protocol": [
{
Expand Down Expand Up @@ -103,14 +103,30 @@
}
},
{
"comment": "Can’t switch from special scheme to non-special. Note: this may change, see https://github.com/whatwg/url/issues/104",
"comment": "Can’t switch from special scheme to non-special",
"href": "http://example.net",
"new_value": "b",
"expected": {
"href": "http://example.net/",
"protocol": "http:"
}
},
{
"href": "https://example.net",
"new_value": "s",
"expected": {
"href": "https://example.net/",
"protocol": "https:"
}
},
{
"href": "ftp://example.net",
"new_value": "test",
"expected": {
"href": "ftp://example.net/",
"protocol": "ftp:"
}
},
{
"comment": "Cannot-be-a-base URL doesn’t have a host, but URL in a special scheme must.",
"href": "mailto:[email protected]",
Expand All @@ -121,14 +137,38 @@
}
},
{
"comment": "Can’t switch from non-special scheme to special. Note: this may change, see https://github.com/whatwg/url/issues/104",
"comment": "Can’t switch from non-special scheme to special",
"href": "ssh://[email protected]",
"new_value": "http",
"expected": {
"href": "ssh://[email protected]/",
"protocol": "ssh:"
}
},
{
"href": "ssh://[email protected]",
"new_value": "gopher",
"expected": {
"href": "ssh://[email protected]/",
"protocol": "ssh:"
}
},
{
"href": "ssh://[email protected]",
"new_value": "file",
"expected": {
"href": "ssh://[email protected]/",
"protocol": "ssh:"
}
},
{
"href": "nonsense:///test",
"new_value": "https",
"expected": {
"href": "nonsense:///test",
"protocol": "nonsense:"
}
},
{
"comment": "Stuff after the first ':' is ignored",
"href": "http://example.net",
Expand Down
87 changes: 87 additions & 0 deletions test/fixtures/url-tests-additional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module.exports = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we upstream these to web-platform-tests? I can do it if that'd be easier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can give it a try first :D. These test cases don't have base so is urltestdata.json the right place to go?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. No base is just a base of about:blank, so just add that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks!

{
'url': 'tftp://foobar.com/someconfig;mode=netascii',
'protocol': 'tftp:',
'hostname': 'foobar.com',
'pathname': '/someconfig;mode=netascii'
},
{
'url': 'telnet://user:[email protected]: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:[email protected],2001:foo/bar',
'protocol': 'tag:',
'pathname': '[email protected],2001:foo/bar'
}
];
142 changes: 142 additions & 0 deletions test/parallel/test-whatwg-url-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
'use strict';
const common = require('../common');
const path = require('path');
const { URL, URLSearchParams } = require('url');
const { test, assert_equals, assert_true, assert_throws } = common.WPT;

if (!common.hasIntl) {
// A handful of the tests fail when ICU is not included.
common.skip('missing Intl');
return;
}

const request = {
response: require(path.join(common.fixturesDir, 'url-tests.json'))
};

/* eslint-disable */
/* WPT Refs:
https://github.com/w3c/web-platform-tests/blob/8791bed/url/url-constructor.html
License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
*/
function runURLConstructorTests() {
// var setup = async_test("Loading data…")
// setup.step(function() {
// var request = new XMLHttpRequest()
// request.open("GET", "urltestdata.json")
// request.send()
// request.responseType = "json"
// request.onload = setup.step_func(function() {
runURLTests(request.response)
// setup.done()
// })
// })
}

function bURL(url, base) {
return new URL(url, base || "about:blank")
}


function runURLTests(urltests) {
for(var i = 0, l = urltests.length; i < l; i++) {
var expected = urltests[i]
if (typeof expected === "string") continue // skip comments

test(function() {
if (expected.failure) {
assert_throws(new TypeError(), function() {
bURL(expected.input, expected.base)
})
return
}

var url = bURL(expected.input, expected.base)
assert_equals(url.href, expected.href, "href")
assert_equals(url.protocol, expected.protocol, "protocol")
assert_equals(url.username, expected.username, "username")
assert_equals(url.password, expected.password, "password")
assert_equals(url.host, expected.host, "host")
assert_equals(url.hostname, expected.hostname, "hostname")
assert_equals(url.port, expected.port, "port")
assert_equals(url.pathname, expected.pathname, "pathname")
assert_equals(url.search, expected.search, "search")
if ("searchParams" in expected) {
assert_true("searchParams" in url)
// assert_equals(url.searchParams.toString(), expected.searchParams, "searchParams")
}
assert_equals(url.hash, expected.hash, "hash")
}, "Parsing: <" + expected.input + "> against <" + expected.base + ">")
}
}

function runURLSearchParamTests() {
test(function() {
var url = bURL('http://example.org/?a=b')
assert_true("searchParams" in url)
var searchParams = url.searchParams
assert_true(url.searchParams === searchParams, 'Object identity should hold.')
}, 'URL.searchParams getter')

test(function() {
var url = bURL('http://example.org/?a=b')
assert_true("searchParams" in url)
var searchParams = url.searchParams
assert_equals(searchParams.toString(), 'a=b')

searchParams.set('a', 'b')
assert_equals(url.searchParams.toString(), 'a=b')
assert_equals(url.search, '?a=b')
url.search = ''
assert_equals(url.searchParams.toString(), '')
assert_equals(url.search, '')
assert_equals(searchParams.toString(), '')
}, 'URL.searchParams updating, clearing')

test(function() {
'use strict'
var urlString = 'http://example.org'
var url = bURL(urlString)
assert_throws(TypeError(), function() { url.searchParams = new URLSearchParams(urlString) })
}, 'URL.searchParams setter, invalid values')

test(function() {
var url = bURL('http://example.org/file?a=b&c=d')
assert_true("searchParams" in url)
var searchParams = url.searchParams
assert_equals(url.search, '?a=b&c=d')
assert_equals(searchParams.toString(), 'a=b&c=d')

// Test that setting 'search' propagates to the URL object's query object.
url.search = 'e=f&g=h'
assert_equals(url.search, '?e=f&g=h')
assert_equals(searchParams.toString(), 'e=f&g=h')

// ..and same but with a leading '?'.
url.search = '?e=f&g=h'
assert_equals(url.search, '?e=f&g=h')
assert_equals(searchParams.toString(), 'e=f&g=h')

// And in the other direction, altering searchParams propagates
// back to 'search'.
// searchParams.append('i', ' j ')
// assert_equals(url.search, '?e=f&g=h&i=+j+')
// assert_equals(url.searchParams.toString(), 'e=f&g=h&i=+j+')
// assert_equals(searchParams.get('i'), ' j ')

// searchParams.set('e', 'updated')
// assert_equals(url.search, '?e=updated&g=h&i=+j+')
// assert_equals(searchParams.get('e'), 'updated')

// var url2 = bURL('http://example.org/file??a=b&c=d')
// assert_equals(url2.search, '??a=b&c=d')
// assert_equals(url2.searchParams.toString(), '%3Fa=b&c=d')

// url2.href = 'http://example.org/file??a=b'
// assert_equals(url2.search, '??a=b')
// assert_equals(url2.searchParams.toString(), '%3Fa=b')
}, 'URL.searchParams and URL.search setters, update propagation')
}
runURLSearchParamTests()
runURLConstructorTests()
/* eslint-enable */
46 changes: 46 additions & 0 deletions test/parallel/test-whatwg-url-historical.js
Original file line number Diff line number Diff line change
@@ -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 */
Loading