UNPKG

2.6 kBJavaScriptView Raw
1"use strict";
2
3const fs = require("fs");
4const path = require("path");
5const chai = require("chai");
6const parsePubSuffixList = require("../lib/tries/parsePubSuffixList");
7const serializeTrie = require("../lib/tries/serializeTrie");
8const parseTrie = require("../lib/tries/parseTrie");
9const lookUp = require("../lib/tries/lookUp");
10
11const expect = chai.expect;
12const TEST_SNAPSHOT = true;
13const pathToFixtures = path.resolve(__dirname, "fixtures");
14const pathToSnapshots = path.resolve(__dirname, "snapshots");
15const pathToParsePubSuffixListSnapshot = path.resolve(pathToSnapshots, "parsePubSuffixList.json");
16const pathToSerializeTrieSnapshot = path.resolve(pathToSnapshots, "serializeTrie.json");
17
18describe("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("serializeTrie()", () => {
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});