Skip to content

Commit

Permalink
test, url: synchronize WPT url tests
Browse files Browse the repository at this point in the history
* 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
Backport-of: #11079
  • Loading branch information
joyeecheung authored and italoacasas committed Feb 22, 2017
1 parent 570c5e1 commit 7aaa960
Show file tree
Hide file tree
Showing 25 changed files with 1,194 additions and 703 deletions.
25 changes: 25 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,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 = [
{
'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

4 comments on commit 7aaa960

@MylesBorins
Copy link
Contributor

Choose a reason for hiding this comment

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

This did not land with the appropriate meta data

@sam-github
Copy link
Contributor

Choose a reason for hiding this comment

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

@joyeecheung Did you think the metadata wasn't needed because it was a backport?

@aqrln
Copy link
Contributor

@aqrln aqrln commented on 7aaa960 Apr 17, 2017

Choose a reason for hiding this comment

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

@sam-github I believe this is committed by @italoacasas, isn't this?

@sam-github
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you are right, sorry, I always get confused about the two sides of the with. Mostly, I'm wondering if the docs on the backport process are unclear, or whether its a "mistakes happen to us all" situation.

Please sign in to comment.