Index: test/url.js |
=================================================================== |
--- a/test/url.js |
+++ b/test/url.js |
@@ -18,57 +18,295 @@ |
"use strict"; |
// Only starting NodeJS 10 that URL is in the global space. |
const {URL} = require("url"); |
const {createSandbox} = require("./_common"); |
const publicSuffixes = require("../data/publicSuffixList.json"); |
+let parseURL = null; |
let normalizeHostname = null; |
let domainSuffixes = null; |
let isThirdParty = null; |
let getBaseDomain = null; |
exports.setUp = function(callback) |
{ |
let sandboxedRequire = createSandbox({ |
extraExports: { |
domain: ["getBaseDomain"] |
} |
}); |
( |
- {normalizeHostname, domainSuffixes, isThirdParty, |
+ {parseURL, normalizeHostname, domainSuffixes, isThirdParty, |
getBaseDomain} = sandboxedRequire("../lib/url") |
); |
callback(); |
}; |
function hostnameToURL(hostname) |
{ |
return new URL("http://" + hostname); |
} |
+function testURLParsing(test, url) |
+{ |
+ // The function expects a normalized URL. |
+ url = new URL(url).href; |
+ |
+ let urlInfo = parseURL(url); |
+ |
+ // We need to ensure only that our implementation matches that of the URL |
+ // object. |
+ let urlObject = new URL(url); |
+ |
+ test.equal(urlInfo.href, urlObject.href); |
+ test.equal(urlInfo.protocol, urlObject.protocol); |
+ test.equal(urlInfo.hostname, urlObject.hostname); |
+ |
+ test.equal(urlInfo.toString(), urlObject.toString()); |
+ test.equal(String(urlInfo), String(urlObject)); |
+ test.equal(urlInfo + "", urlObject + ""); |
+} |
+ |
function testThirdParty(test, requestHostname, documentHostname, expected, |
message) |
{ |
test.equal( |
isThirdParty( |
hostnameToURL(requestHostname), |
// Chrome's URL object normalizes IP addresses. So some test |
// will fail if we don't normalize the document host as well. |
hostnameToURL(documentHostname).hostname |
), |
expected, |
message |
); |
} |
+exports.testParseURL = function(test) |
+{ |
+ testURLParsing(test, "https://example.com"); |
+ testURLParsing(test, "https://example.com/"); |
+ testURLParsing(test, "https://example.com/foo"); |
+ testURLParsing(test, "https://example.com/foo/bar"); |
+ testURLParsing( |
+ test, |
+ "https://example.com/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "https://example.com:8080"); |
+ testURLParsing(test, "https://example.com:8080/"); |
+ testURLParsing(test, "https://example.com:8080/foo"); |
+ testURLParsing(test, "https://example.com:8080/foo/bar"); |
+ testURLParsing( |
+ test, |
+ "https://example.com:8080/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "http://localhost"); |
+ testURLParsing(test, "http://localhost/"); |
+ testURLParsing(test, "http://localhost/foo"); |
+ testURLParsing(test, "http://localhost/foo/bar"); |
+ testURLParsing( |
+ test, |
+ "http://localhost/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "https://user@example.com"); |
+ testURLParsing(test, "https://user@example.com/"); |
+ testURLParsing(test, "https://user@example.com/foo"); |
+ testURLParsing(test, "https://user@example.com/foo/bar"); |
+ testURLParsing( |
+ test, |
+ "https://user@example.com/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "https://user@example.com:8080"); |
+ testURLParsing(test, "https://user@example.com:8080/"); |
+ testURLParsing(test, "https://user@example.com:8080/foo"); |
+ testURLParsing(test, "https://user@example.com:8080/foo/bar"); |
+ testURLParsing( |
+ test, |
+ "https://user@example.com:8080/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "https://user:pass@example.com"); |
+ testURLParsing(test, "https://user:pass@example.com/"); |
+ testURLParsing(test, "https://user:pass@example.com/foo"); |
+ testURLParsing(test, "https://user:pass@example.com/foo/bar"); |
+ testURLParsing( |
+ test, |
+ "https://user:pass@example.com/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "https://user:pass@example.com:8080"); |
+ testURLParsing(test, "https://user:pass@example.com:8080/"); |
+ testURLParsing(test, "https://user:pass@example.com:8080/foo"); |
+ testURLParsing(test, "https://user:pass@example.com:8080/foo/bar"); |
+ testURLParsing( |
+ test, |
+ "https://user:pass@example.com:8080/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "https://us%40er:pa%40ss@example.com"); |
+ testURLParsing(test, "https://us%40er:pa%40ss@example.com/"); |
+ testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo"); |
+ testURLParsing(test, "https://us%40er:pa%40ss@example.com/foo/bar"); |
+ testURLParsing( |
+ test, |
+ "https://us%40er:pa%40ss@example.com/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080"); |
+ testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/"); |
+ testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo"); |
+ testURLParsing(test, "https://us%40er:pa%40ss@example.com:8080/foo/bar"); |
+ testURLParsing( |
+ test, |
+ "https://us%40er:pa%40ss@example.com:8080/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "http://192.168.1.1"); |
+ testURLParsing(test, "http://192.168.1.1/"); |
+ testURLParsing(test, "http://192.168.1.1/foo"); |
+ testURLParsing(test, "http://192.168.1.1/foo/bar"); |
+ testURLParsing( |
+ test, |
+ "http://192.168.1.1/foo/bar?https://random/foo/bar" |
+ ); |
+ testURLParsing( |
+ test, |
+ "http://192.168.1.1:8080/foo/bar?https://random/foo/bar" |
+ ); |
+ testURLParsing( |
+ test, |
+ "http://user@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
+ ); |
+ testURLParsing( |
+ test, |
+ "http://user:pass@192.168.1.1:8080/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]"); |
+ testURLParsing(test, "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/"); |
+ testURLParsing(test, "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/foo"); |
+ testURLParsing( |
+ test, |
+ "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/foo/bar" |
+ ); |
+ testURLParsing( |
+ test, |
+ "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]/foo/bar?https://random/foo/bar" |
+ ); |
+ testURLParsing( |
+ test, |
+ "http://[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080/foo/bar?https://random/foo/bar" |
+ ); |
+ testURLParsing( |
+ test, |
+ "http://user@[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080/foo/bar?https://random/foo/bar" |
+ ); |
+ testURLParsing( |
+ test, |
+ "http://user:pass@[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080/foo/bar?https://random/foo/bar" |
+ ); |
+ |
+ testURLParsing(test, "ftp://user:pass@example.com:8021"); |
+ testURLParsing(test, "ftp://user:pass@example.com:8021/"); |
+ testURLParsing(test, "ftp://user:pass@example.com:8021/foo"); |
+ testURLParsing(test, "ftp://user:pass@example.com:8021/foo/bar"); |
+ |
+ testURLParsing(test, "about:blank"); |
+ testURLParsing(test, "chrome://extensions"); |
+ testURLParsing( |
+ test, |
+ "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html" |
+ ); |
+ testURLParsing(test, "mailto:john.doe@mail.example.com"); |
+ |
+ testURLParsing(test, "news:newsgroup"); |
+ testURLParsing(test, "news:message-id"); |
+ testURLParsing(test, "nntp://example.com:8119/newsgroup"); |
+ testURLParsing(test, "nntp://example.com:8119/message-id"); |
+ |
+ testURLParsing(test, "data:,"); |
+ testURLParsing( |
+ test, |
+ "data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh" |
+ ); |
+ testURLParsing( |
+ test, |
+ "data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678" |
+ ); |
+ |
+ testURLParsing(test, "javascript:"); |
+ testURLParsing(test, "javascript:alert();"); |
+ testURLParsing(test, "javascript:foo/bar/"); |
+ testURLParsing(test, "javascript://foo/bar/"); |
+ |
+ testURLParsing(test, "file:///dev/random"); |
+ |
+ testURLParsing(test, "wss://example.com/"); |
+ testURLParsing(test, "wss://example.com:8080/"); |
+ testURLParsing(test, "wss://user@example.com:8080/"); |
+ testURLParsing(test, "wss://user:pass@example.com:8080/"); |
+ |
+ testURLParsing(test, "stuns:stuns.example.com/"); |
+ testURLParsing(test, "stuns:stuns.example.com:8080/"); |
+ testURLParsing(test, "stuns:user@stuns.example.com:8080/"); |
+ testURLParsing(test, "stuns:user:pass@stuns.example.com:8080/"); |
+ |
+ // The following tests are based on |
+ // https://cs.chromium.org/chromium/src/url/gurl_unittest.cc?rcl=9ec7bc85e0f6a0bf28eff6b2eca678067da547e9 |
+ // Note: We do not check for "canonicalization" (normalization). parseURL() |
+ // should be used with normalized URLs only. |
+ |
+ testURLParsing(test, "something:///example.com/"); |
+ testURLParsing(test, "something://example.com/"); |
+ |
+ testURLParsing(test, "file:///C:/foo.txt"); |
+ testURLParsing(test, "file://server/foo.txt"); |
+ |
+ testURLParsing(test, "http://user:pass@example.com:99/foo;bar?q=a#ref"); |
+ |
+ testURLParsing(test, "http://user:%40!$&'()*+,%3B%3D%3A@example.com:12345/"); |
+ |
+ testURLParsing(test, "filesystem:http://example.com/temporary/"); |
+ testURLParsing( |
+ test, |
+ "filesystem:http://user:%40!$&'()*+,%3B%3D%3A@example.com:12345/" |
+ ); |
+ |
+ testURLParsing(test, "javascript:window.alert('hello, world');"); |
+ testURLParsing(test, "javascript:#"); |
+ |
+ testURLParsing( |
+ test, |
+ "blob:https://example.com/7ce70a1e-9681-4148-87a8-43cb9171b994" |
+ ); |
+ |
+ testURLParsing(test, "http://[2001:db8::1]"); |
+ testURLParsing(test, "http://[2001:db8::1]:8080"); |
+ testURLParsing(test, "http://[::]:8080"); |
+ |
+ testURLParsing(test, "not-a-standard-scheme:this is arbitrary content"); |
+ testURLParsing(test, "view-source:http://example.com/path"); |
+ |
+ testURLParsing( |
+ test, |
+ "data:text/html,Question?%3Cdiv%20style=%22color:%20#bad%22%3Eidea%3C/div%3E" |
+ ); |
+ |
+ test.done(); |
+}; |
+ |
exports.testNormalizeHostname = function(test) |
{ |
test.equal(normalizeHostname("example.com"), "example.com"); |
test.equal(normalizeHostname("example.com."), "example.com"); |
test.equal(normalizeHostname("example.com.."), "example.com"); |
test.equal(normalizeHostname("example.com..."), "example.com"); |
test.equal(normalizeHostname("Example.com"), "example.com"); |