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