parseDomain.test.js
3.12 KB
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
"use strict";
var chai = require("chai"),
expect = chai.expect,
parseDomain = require("../lib/parseDomain.js");
chai.config.includeStack = true;
describe("parseDomain(url)", function () {
it("should remove the protocol", function () {
expect(parseDomain("http://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", function () {
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", function () {
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 port", function () {
expect(parseDomain("example.com:8080")).to.eql({
subdomain: "",
domain: "example",
tld: "com"
});
});
it("should remove the authentication", function () {
expect(parseDomain("user:password@example.com")).to.eql({
subdomain: "",
domain: "example",
tld: "com"
});
});
it("should also work with three-level domains like .co.uk", function () {
expect(parseDomain("www.example.co.uk")).to.eql({
subdomain: "www",
domain: "example",
tld: "co.uk"
});
});
it("should work when all url parts are present", function () {
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", function () {
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", function () {
expect(parseDomain("example.kk")).to.equal(null);
});
it("should return null if the given value is not a string", function () {
expect(parseDomain(undefined)).to.equal(null);
expect(parseDomain({})).to.equal(null);
expect(parseDomain("")).to.equal(null);
});
it("should work with domains that could match multiple tlds", function() {
expect(parseDomain("http://hello.de.ibm.com")).to.eql({
subdomain: "hello.de",
domain: "ibm",
tld: "com"
});
});
});