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

Unified Diff: test/domain.js

Issue 29993591: Noissue - Add tests for base domain extraction (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Jan. 30, 2019, 6:26 p.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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/domain.js
===================================================================
--- a/test/domain.js
+++ b/test/domain.js
@@ -16,23 +16,30 @@
*/
"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 isThirdParty = null;
+let getDomain = null;
exports.setUp = function(callback)
{
- let sandboxedRequire = createSandbox();
+ let sandboxedRequire = createSandbox({
+ extraExports: {
+ domain: ["getDomain"]
+ }
+ });
(
- {isThirdParty} = sandboxedRequire("../lib/domain")
+ {isThirdParty, getDomain} = sandboxedRequire("../lib/domain")
);
callback();
};
function hostnameToURL(hostname)
{
return new URL("http://" + hostname);
@@ -117,8 +124,59 @@
);
testThirdParty(test, "xn--f-1gaa.com", "f\u00f6\u00f6.com", false,
"same IDN isn't third-party");
testThirdParty(test, "example.com..", "example.com....", false,
"traling dots are ignored");
test.done();
};
+
+exports.testGetDomain = function(test)
+{
+ let parts = ["aaa", "bbb", "ccc", "ddd", "eee"];
+ let levels = 3;
+
+ for (let suffix in publicSuffixes)
+ {
+ let offset = publicSuffixes[suffix];
+
+ // If this fails, add more parts.
+ test.ok(offset <= parts.length - levels,
+ "Not enough domain parts for testing");
+
+ for (let i = 0; i < offset + levels; i++)
+ {
+ let hostname = parts.slice(0, i).join(".");
+ hostname += (hostname ? "." : "") + suffix;
+
+ let expected = parts.slice(Math.max(0, i - offset), i).join(".");
+ expected += (expected ? "." : "") + suffix;
+
+ test.equal(getDomain(hostname), expected,
+ `getDomain("${hostname}") == "${expected}"` +
+ ` with {suffix: "${suffix}", offset: ${offset}}`);
+ }
+ }
+
+ // Test unknown suffixes.
+ test.equal(typeof publicSuffixes["localhost"], "undefined");
+ test.equal(typeof publicSuffixes["localhost.localdomain"], "undefined");
+
+ test.equal(getDomain("localhost"), "localhost");
+ test.equal(getDomain("localhost.localdomain"), "localhost.localdomain");
+ test.equal(getDomain("mail.localhost.localdomain"), "localhost.localdomain");
+ test.equal(getDomain("www.example.localhost.localdomain"),
+ "localhost.localdomain");
+
+ // Test unknown suffixes that overlap partly with known suffixes.
+ test.equal(typeof publicSuffixes["example.com"], "undefined");
+ test.equal(typeof publicSuffixes["africa.com"], "number");
+ test.equal(typeof publicSuffixes["compute.amazonaws.com"], "number");
+
+ test.equal(getDomain("example.com"), "example.com");
+ test.equal(getDomain("mail.example.com"), "example.com");
+ test.equal(getDomain("secure.mail.example.com"), "example.com");
+
+ test.equal(getDomain(""), "");
+
+ test.done();
+};
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld