| Index: test/url.js | 
| =================================================================== | 
| --- a/test/url.js | 
| +++ b/test/url.js | 
| @@ -18,57 +18,244 @@ | 
| "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"); | 
| + | 
| + 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"); |