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 | fulldomain: "example.com",
|
17 | hostname: "example.com",
|
18 | valid_domain: true
|
19 | });
|
20 | expect(parseDomain("//example.com")).to.eql({
|
21 | subdomain: "",
|
22 | domain: "example",
|
23 | tld: "com",
|
24 | fulldomain: "example.com",
|
25 | hostname: "example.com",
|
26 | valid_domain: true
|
27 | });
|
28 | expect(parseDomain("https://example.com")).to.eql({
|
29 | subdomain: "",
|
30 | domain: "example",
|
31 | tld: "com",
|
32 | fulldomain: "example.com",
|
33 | hostname: "example.com",
|
34 | valid_domain: true
|
35 | });
|
36 | });
|
37 |
|
38 | it("should remove sub-domains", () => {
|
39 | expect(parseDomain("www.example.com")).to.eql({
|
40 | subdomain: "www",
|
41 | domain: "example",
|
42 | tld: "com",
|
43 | valid_domain: true,
|
44 | fulldomain: "example.com",
|
45 | hostname: "www.example.com"
|
46 |
|
47 | });
|
48 | expect(parseDomain("www.some.other.subdomain.example.com")).to.eql({
|
49 | subdomain: "www.some.other.subdomain",
|
50 | domain: "example",
|
51 | tld: "com",
|
52 | valid_domain: true,
|
53 | hostname: "www.some.other.subdomain.example.com",
|
54 | fulldomain: "example.com"
|
55 | });
|
56 | });
|
57 |
|
58 | it("should remove the path", () => {
|
59 | expect(parseDomain("example.com/some/path?and&query")).to.eql({
|
60 | subdomain: "",
|
61 | domain: "example",
|
62 | tld: "com",
|
63 |
|
64 | valid_domain: true,
|
65 | hostname: "example.com",
|
66 | fulldomain: "example.com"
|
67 | });
|
68 | expect(parseDomain("example.com/")).to.eql({
|
69 | subdomain: "",
|
70 | domain: "example",
|
71 | tld: "com",
|
72 | valid_domain: true,
|
73 | hostname: "example.com",
|
74 | fulldomain: "example.com"
|
75 | });
|
76 | });
|
77 |
|
78 | it("should remove the query string", () => {
|
79 | expect(parseDomain("example.com?and&query")).to.eql({
|
80 | subdomain: "",
|
81 | domain: "example",
|
82 | tld: "com",
|
83 | valid_domain: true,
|
84 | hostname: "example.com",
|
85 | fulldomain: "example.com"
|
86 | });
|
87 | });
|
88 |
|
89 | it("should remove special characters", () => {
|
90 | expect(parseDomain("http://m.example.com\r")).to.eql({
|
91 | subdomain: "m",
|
92 | domain: "example",
|
93 | tld: "com",
|
94 | valid_domain: true,
|
95 | hostname: "m.example.com",
|
96 | fulldomain: "example.com"
|
97 | });
|
98 | });
|
99 |
|
100 | it("should remove the port", () => {
|
101 | expect(parseDomain("example.com:8080")).to.eql({
|
102 | subdomain: "",
|
103 | domain: "example",
|
104 | tld: "com",
|
105 | valid_domain: true,
|
106 | hostname: "example.com",
|
107 | fulldomain: "example.com"
|
108 | });
|
109 | });
|
110 |
|
111 | it("should remove the authentication", () => {
|
112 | expect(parseDomain("user:password@example.com")).to.eql({
|
113 | subdomain: "",
|
114 | domain: "example",
|
115 | tld: "com",
|
116 | valid_domain: true,
|
117 | hostname: "example.com",
|
118 | fulldomain: "example.com"
|
119 | });
|
120 | });
|
121 |
|
122 | it("should allow @ characters in the path", () => {
|
123 | expect(parseDomain("https://medium.com/@username/")).to.eql({
|
124 | subdomain: "",
|
125 | domain: "medium",
|
126 | tld: "com",
|
127 | valid_domain: true,
|
128 | hostname: "medium.com",
|
129 | fulldomain: "medium.com"
|
130 | });
|
131 | });
|
132 |
|
133 | it("should also work with three-level domains like .co.uk", () => {
|
134 | expect(parseDomain("www.example.co.uk")).to.eql({
|
135 | subdomain: "www",
|
136 | domain: "example",
|
137 | tld: "co.uk",
|
138 | valid_domain: true,
|
139 | hostname: "www.example.co.uk",
|
140 | fulldomain: "example.co.uk"
|
141 | });
|
142 | });
|
143 |
|
144 | it("should not include private domains like blogspot.com by default", () => {
|
145 | expect(parseDomain("foo.blogspot.com")).to.eql({
|
146 | subdomain: "foo",
|
147 | domain: "blogspot",
|
148 | tld: "com",
|
149 | valid_domain: true,
|
150 | hostname: "foo.blogspot.com",
|
151 | fulldomain: "blogspot.com"
|
152 | });
|
153 | });
|
154 |
|
155 | it("should include private tlds", () => {
|
156 | expect(parseDomain("foo.blogspot.com", { privateTlds: true })).to.eql({
|
157 | subdomain: "",
|
158 | domain: "foo",
|
159 | tld: "blogspot.com",
|
160 | valid_domain: true,
|
161 | hostname: "foo.blogspot.com",
|
162 | fulldomain: "foo.blogspot.com"
|
163 | });
|
164 | });
|
165 |
|
166 | it("should work when all url parts are present", () => {
|
167 | expect(parseDomain("https://user@www.some.other.subdomain.example.co.uk:8080/some/path?and&query#hash")).to.eql(
|
168 | {
|
169 | subdomain: "www.some.other.subdomain",
|
170 | domain: "example",
|
171 | tld: "co.uk",
|
172 | valid_domain: true,
|
173 | hostname: "www.some.other.subdomain.example.co.uk",
|
174 | fulldomain: "example.co.uk"
|
175 | }
|
176 | );
|
177 | });
|
178 |
|
179 | it("should also work with the minimum", () => {
|
180 | expect(parseDomain("example.com")).to.eql({
|
181 | subdomain: "",
|
182 | domain: "example",
|
183 | tld: "com",
|
184 | valid_domain: true,
|
185 | hostname: "example.com",
|
186 | fulldomain: "example.com"
|
187 | });
|
188 | });
|
189 |
|
190 | it("should return null if the given url contains an unsupported top-level domain", () => {
|
191 | expect(parseDomain("example.kk")).to.equal(null);
|
192 | });
|
193 |
|
194 | it("should return null if the given value is not a string", () => {
|
195 | expect(parseDomain(undefined)).to.equal(null);
|
196 | expect(parseDomain({})).to.equal(null);
|
197 | expect(parseDomain("")).to.equal(null);
|
198 | });
|
199 |
|
200 | it("should work with domains that could match multiple tlds", () => {
|
201 | expect(parseDomain("http://hello.de.ibm.com")).to.eql({
|
202 | subdomain: "hello.de",
|
203 | domain: "ibm",
|
204 | tld: "com",
|
205 | valid_domain: true,
|
206 | hostname: "hello.de.ibm.com",
|
207 | fulldomain: "ibm.com"
|
208 | });
|
209 | });
|
210 |
|
211 | it("should work with custom top-level domains (eg .local)", () => {
|
212 | const options = { customTlds: ["local"] };
|
213 |
|
214 | expect(parseDomain("mymachine.local", options)).to.eql({
|
215 | subdomain: "",
|
216 | domain: "mymachine",
|
217 | tld: "local",
|
218 | valid_domain: true,
|
219 | hostname: "mymachine.local",
|
220 | fulldomain: "mymachine.local"
|
221 | });
|
222 |
|
223 |
|
224 | expect(parseDomain("mymachine.local")).to.eql(null);
|
225 | expect(parseDomain("http://example.com", options)).to.eql({
|
226 | subdomain: "",
|
227 | domain: "example",
|
228 | tld: "com",
|
229 | valid_domain: true,
|
230 | hostname: "example.com",
|
231 | fulldomain: "example.com"
|
232 | });
|
233 | });
|
234 |
|
235 | it("should also work with custom top-level domains passed as regexps", () => {
|
236 | const options = { customTlds: /(\.local|localhost)$/ };
|
237 |
|
238 | expect(parseDomain("mymachine.local", options)).to.eql({
|
239 | subdomain: "",
|
240 | domain: "mymachine",
|
241 | tld: "local",
|
242 | valid_domain: true,
|
243 | hostname: "mymachine.local",
|
244 | fulldomain: "mymachine.local"
|
245 | });
|
246 | expect(parseDomain("localhost", options)).to.eql({
|
247 | subdomain: "",
|
248 | domain: "",
|
249 | tld: "localhost",
|
250 | valid_domain: false,
|
251 | hostname: "localhost",
|
252 | fulldomain: "localhost"
|
253 | });
|
254 | expect(parseDomain("localhost:8080", options)).to.eql({
|
255 | subdomain: "",
|
256 | domain: "",
|
257 | tld: "localhost",
|
258 | valid_domain: false,
|
259 | hostname: "localhost",
|
260 | fulldomain: "localhost"
|
261 | });
|
262 |
|
263 |
|
264 | expect(parseDomain("mymachine.local")).to.eql(null);
|
265 | expect(parseDomain("http://example.com", options)).to.eql({
|
266 | subdomain: "",
|
267 | domain: "example",
|
268 | tld: "com",
|
269 | valid_domain: true,
|
270 | hostname: "example.com",
|
271 | fulldomain: "example.com"
|
272 | });
|
273 | });
|
274 | });
|