1 | "use strict";
|
2 |
|
3 | const fs = require("fs");
|
4 | const path = require("path");
|
5 | const chai = require("chai");
|
6 | const parsePubSuffixList = require("../lib/tries/parsePubSuffixList");
|
7 | const serializeTrie = require("../lib/tries/serializeTrie");
|
8 | const parseTrie = require("../lib/tries/parseTrie");
|
9 | const lookUp = require("../lib/tries/lookUp");
|
10 |
|
11 | const expect = chai.expect;
|
12 | const TEST_SNAPSHOT = true;
|
13 | const pathToFixtures = path.resolve(__dirname, "fixtures");
|
14 | const pathToSnapshots = path.resolve(__dirname, "snapshots");
|
15 | const pathToParsePubSuffixListSnapshot = path.resolve(pathToSnapshots, "parsePubSuffixList.json");
|
16 | const pathToSerializeTrieSnapshot = path.resolve(pathToSnapshots, "serializeTrie.json");
|
17 |
|
18 | describe("snapshots", () => {
|
19 | describe("parsePubSuffixList()", () => {
|
20 | it("matches the approved snapshot", () => {
|
21 | const pubSuffixList = fs.readFileSync(path.resolve(pathToFixtures, "pubSuffixList.txt"), "utf8");
|
22 | const parsedList = parsePubSuffixList(pubSuffixList);
|
23 | const snapshot = JSON.parse(fs.readFileSync(pathToParsePubSuffixListSnapshot, "utf8"));
|
24 |
|
25 | TEST_SNAPSHOT && expect(parsedList).to.eql(snapshot);
|
26 | fs.writeFileSync(pathToParsePubSuffixListSnapshot, JSON.stringify(parsedList));
|
27 | });
|
28 | });
|
29 | describe("serialieTrie()", () => {
|
30 | it("matches the approved snapshot", () => {
|
31 | const parsedList = JSON.parse(fs.readFileSync(pathToParsePubSuffixListSnapshot, "utf8"));
|
32 | const snapshot = JSON.parse(fs.readFileSync(pathToSerializeTrieSnapshot, "utf8"));
|
33 | const serializedTrie = serializeTrie(parsedList.icann);
|
34 |
|
35 | TEST_SNAPSHOT && expect(serializedTrie).to.eql(snapshot);
|
36 | fs.writeFileSync(pathToSerializeTrieSnapshot, JSON.stringify(serializedTrie));
|
37 | });
|
38 | });
|
39 | describe("parseTrie() and lookUp() calling lookUp() with the result from parseTrie(snapshot) and hostname", () => {
|
40 | const serializedTrie = JSON.parse(fs.readFileSync(pathToSerializeTrieSnapshot, "utf8"));
|
41 | const parsedTrie = parseTrie(serializedTrie);
|
42 |
|
43 | [
|
44 | ["example.com", "com"],
|
45 | ["example.a.com", "com"],
|
46 | ["example.uk", "uk"],
|
47 | ["example.co.uk", "co.uk"],
|
48 | ["example.ab.uk", "uk"],
|
49 | ].forEach(testArgs => {
|
50 | const hostname = testArgs[0];
|
51 | const expectedResult = testArgs[1];
|
52 |
|
53 | it(`'${ hostname }' returns ${ expectedResult }`, () => {
|
54 | expect(lookUp(parsedTrie, hostname)).to.equal(expectedResult);
|
55 | });
|
56 | });
|
57 | });
|
58 | });
|