| 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(); |
| +}; |