1 | "use strict";
|
2 |
|
3 | const chai = require("chai");
|
4 |
|
5 | const expect = chai.expect;
|
6 | const parseDomain = require("../lib/parseDomain.js");
|
7 |
|
8 | chai.config.includeStack = true;
|
9 |
|
10 | describe("parseDomain(url)", () => {
|
11 | it("should remove the protocol", () => {
|
12 | expect(parseDomain("http://example.com")).to.eql({
|
13 | subdomain: "",
|
14 | domain: "example",
|
15 | tld: "com",
|
16 | });
|
17 | expect(parseDomain("//example.com")).to.eql({
|
18 | subdomain: "",
|
19 | domain: "example",
|
20 | tld: "com",
|
21 | });
|
22 | expect(parseDomain("https://example.com")).to.eql({
|
23 | subdomain: "",
|
24 | domain: "example",
|
25 | tld: "com",
|
26 | });
|
27 | });
|
28 |
|
29 | it("should remove sub-domains", () => {
|
30 | expect(parseDomain("www.example.com")).to.eql({
|
31 | subdomain: "www",
|
32 | domain: "example",
|
33 | tld: "com",
|
34 | });
|
35 | expect(parseDomain("www.some.other.subdomain.example.com")).to.eql({
|
36 | subdomain: "www.some.other.subdomain",
|
37 | domain: "example",
|
38 | tld: "com",
|
39 | });
|
40 | });
|
41 |
|
42 | it("should remove the path", () => {
|
43 | expect(parseDomain("example.com/some/path?and&query")).to.eql({
|
44 | subdomain: "",
|
45 | domain: "example",
|
46 | tld: "com",
|
47 | });
|
48 | expect(parseDomain("example.com/")).to.eql({
|
49 | subdomain: "",
|
50 | domain: "example",
|
51 | tld: "com",
|
52 | });
|
53 | });
|
54 |
|
55 | it("should remove the query string", () => {
|
56 | expect(parseDomain("example.com?and&query")).to.eql({
|
57 | subdomain: "",
|
58 | domain: "example",
|
59 | tld: "com",
|
60 | });
|
61 | });
|
62 |
|
63 | it("should remove special characters", () => {
|
64 | expect(parseDomain("http://m.example.com\r")).to.eql({
|
65 | subdomain: "m",
|
66 | domain: "example",
|
67 | tld: "com",
|
68 | });
|
69 | });
|
70 |
|
71 | it("should remove the port", () => {
|
72 | expect(parseDomain("example.com:8080")).to.eql({
|
73 | subdomain: "",
|
74 | domain: "example",
|
75 | tld: "com",
|
76 | });
|
77 | });
|
78 |
|
79 | it("should remove the authentication", () => {
|
80 | expect(parseDomain("user:password@example.com")).to.eql({
|
81 | subdomain: "",
|
82 | domain: "example",
|
83 | tld: "com",
|
84 | });
|
85 | });
|
86 |
|
87 | it("should allow @ characters in the path", () => {
|
88 | expect(parseDomain("https://medium.com/@username/")).to.eql({
|
89 | subdomain: "",
|
90 | domain: "medium",
|
91 | tld: "com",
|
92 | });
|
93 | });
|
94 |
|
95 | it("should also work with three-level domains like .co.uk", () => {
|
96 | expect(parseDomain("www.example.co.uk")).to.eql({
|
97 | subdomain: "www",
|
98 | domain: "example",
|
99 | tld: "co.uk",
|
100 | });
|
101 | });
|
102 |
|
103 | it("should not include private domains like blogspot.com by default", () => {
|
104 | expect(parseDomain("foo.blogspot.com")).to.eql({
|
105 | subdomain: "foo",
|
106 | domain: "blogspot",
|
107 | tld: "com",
|
108 | });
|
109 | });
|
110 |
|
111 | it("should include private tlds", () => {
|
112 | expect(parseDomain("foo.blogspot.com", {privateTlds: true})).to.eql({
|
113 | subdomain: "",
|
114 | domain: "foo",
|
115 | tld: "blogspot.com",
|
116 | });
|
117 | });
|
118 |
|
119 | it("should work when all url parts are present", () => {
|
120 | expect(parseDomain("https://user@www.some.other.subdomain.example.co.uk:8080/some/path?and&query#hash")).to.eql(
|
121 | {
|
122 | subdomain: "www.some.other.subdomain",
|
123 | domain: "example",
|
124 | tld: "co.uk",
|
125 | }
|
126 | );
|
127 | });
|
128 |
|
129 | it("should also work with the minimum", () => {
|
130 | expect(parseDomain("example.com")).to.eql({
|
131 | subdomain: "",
|
132 | domain: "example",
|
133 | tld: "com",
|
134 | });
|
135 | });
|
136 |
|
137 | it("should return null if the given url contains an unsupported top-level domain", () => {
|
138 | expect(parseDomain("example.kk")).to.equal(null);
|
139 | });
|
140 |
|
141 | it("should return null if the given value is not a string", () => {
|
142 | expect(parseDomain(undefined)).to.equal(null);
|
143 | expect(parseDomain({})).to.equal(null);
|
144 | });
|
145 |
|
146 | it("should return null if the given string is not a valid URL", () => {
|
147 | expect(parseDomain("\xa0")).to.equal(null);
|
148 | expect(parseDomain("")).to.equal(null);
|
149 | expect(parseDomain(" ")).to.equal(null);
|
150 | expect(parseDomain("http://hell.d\ne.ibm.com")).to.equal(null);
|
151 | });
|
152 |
|
153 | it("should return null if the given is an empty string with a space character", () => {
|
154 | expect(parseDomain(" ")).to.equal(null);
|
155 | });
|
156 |
|
157 | it("should work with domains that could match multiple tlds", () => {
|
158 | expect(parseDomain("http://hello.de.ibm.com")).to.eql({
|
159 | subdomain: "hello.de",
|
160 | domain: "ibm",
|
161 | tld: "com",
|
162 | });
|
163 | });
|
164 |
|
165 | it("should work with custom top-level domains (eg .local)", () => {
|
166 | const options = {customTlds: ["local"]};
|
167 |
|
168 | expect(parseDomain("mymachine.local", options)).to.eql({
|
169 | subdomain: "",
|
170 | domain: "mymachine",
|
171 | tld: "local",
|
172 | });
|
173 |
|
174 |
|
175 | expect(parseDomain("mymachine.local")).to.eql(null);
|
176 | expect(parseDomain("http://example.com", options)).to.eql({
|
177 | subdomain: "",
|
178 | domain: "example",
|
179 | tld: "com",
|
180 | });
|
181 | });
|
182 |
|
183 | it("should also work with custom top-level domains passed as regexps", () => {
|
184 | const options = {customTlds: /(\.local|localhost)$/};
|
185 |
|
186 | expect(parseDomain("mymachine.local", options)).to.eql({
|
187 | subdomain: "",
|
188 | domain: "mymachine",
|
189 | tld: "local",
|
190 | });
|
191 | expect(parseDomain("localhost", options)).to.eql({
|
192 | subdomain: "",
|
193 | domain: "",
|
194 | tld: "localhost",
|
195 | });
|
196 | expect(parseDomain("localhost:8080", options)).to.eql({
|
197 | subdomain: "",
|
198 | domain: "",
|
199 | tld: "localhost",
|
200 | });
|
201 |
|
202 |
|
203 | expect(parseDomain("mymachine.local")).to.eql(null);
|
204 | expect(parseDomain("http://example.com", options)).to.eql({
|
205 | subdomain: "",
|
206 | domain: "example",
|
207 | tld: "com",
|
208 | });
|
209 | });
|
210 |
|
211 | describe("real-world use cases", () => {
|
212 |
|
213 | it("should parse police.uk as tld", () => {
|
214 | expect(parseDomain("example.police.uk")).to.eql({
|
215 | subdomain: "",
|
216 | domain: "example",
|
217 | tld: "police.uk",
|
218 | });
|
219 | });
|
220 |
|
221 |
|
222 | it.skip("should parse gouv.fr as tld", () => {
|
223 | expect(parseDomain("dev.classea12.beta.gouv.fr", {privateTlds: true})).to.eql({
|
224 | tld: "gouv.fr",
|
225 | domain: "beta",
|
226 | subdomain: "dev.classea12",
|
227 | });
|
228 | });
|
229 | });
|
230 | });
|