Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: test/url.js

Issue 30013574: Issue 7296 - Implement lightweight URL parsing (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Feb. 21, 2019, 11:20 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/url.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/url.js
===================================================================
--- a/test/url.js
+++ b/test/url.js
@@ -18,57 +18,200 @@
"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)
+{
+ let {protocol, hostname} = parseURL(url);
+
+ // We need to ensure only that our implementation matches that of the URL
+ // object.
+ let urlObject = new URL(url);
+
+ test.equal(protocol, urlObject.protocol);
+ test.equal(hostname, urlObject.hostname);
+}
+
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.com/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.com/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.com/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.com/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.com/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.com/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.com/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.com/foo/bar"
+ );
+
+ testURLParsing(test, "https://us@er:pa@ss@example.com");
+ testURLParsing(test, "https://us@er:pa@ss@example.com/");
+ testURLParsing(test, "https://us@er:pa@ss@example.com/foo");
+ testURLParsing(test, "https://us@er:pa@ss@example.com/foo/bar");
+ testURLParsing(
+ test,
+ "https://us@er:pa@ss@example.com/foo/bar?https://random.com/foo/bar"
+ );
+
+ testURLParsing(test, "https://us@er:pa@ss@example.com:8080");
+ testURLParsing(test, "https://us@er:pa@ss@example.com:8080/");
+ testURLParsing(test, "https://us@er:pa@ss@example.com:8080/foo");
+ testURLParsing(test, "https://us@er:pa@ss@example.com:8080/foo/bar");
+ testURLParsing(
+ test,
+ "https://us@er:pa@ss@example.com:8080/foo/bar?https://random.com/foo/bar"
+ );
+
+ testURLParsing(test, "ftp://user:pass@example.com:21");
+ testURLParsing(test, "ftp://user:pass@example.com:21/");
+ testURLParsing(test, "ftp://user:pass@example.com:21/foo");
+ testURLParsing(test, "ftp://user:pass@example.com:21/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:119/newsgroup");
+ testURLParsing(test, "nntp://example.com:119/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");
« no previous file with comments | « lib/url.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld