forked from IonicaBizau/parse-url
/
index.js
110 lines (107 loc) · 2.67 KB
/
index.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
// Dependencies
const parseUrl = require("../lib")
, tester = require("tester")
, normalizeUrl = require("normalize-url")
, qs = require("querystring")
;
const INPUTS = [
[
"http://ionicabizau.net/blog"
, {
protocols: [ "http" ]
, protocol: "http"
, port: null
, resource: "ionicabizau.net"
, user: ""
, pathname: "/blog"
, hash: ""
, search: ""
}
]
, [
"//ionicabizau.net/foo.js"
, {
protocols: ["http"]
, protocol: "http"
, port: null
, resource: "ionicabizau.net"
, user: ""
, pathname: "/foo.js"
, hash: ""
, search: ""
}
]
, [
"http://domain.com/path/name#some-hash?foo=bar"
, {
protocols: ["http"]
, protocol: "http"
, port: null
, resource: "domain.com"
, user: ""
, pathname: "/path/name"
, hash: "some-hash?foo=bar"
, search: ""
}
]
, [
["git+ssh://git@host.xz/path/name.git", false]
, {
protocols: ["git", "ssh"]
, protocol: "git"
, port: null
, resource: "host.xz"
, user: "git"
, pathname: "/path/name.git"
, hash: ""
, search: ""
}
]
, [
["git@github.com:IonicaBizau/git-stats.git", false]
, {
protocols: []
, protocol: "ssh"
, port: null
, resource: "github.com"
, user: "git"
, pathname: "/IonicaBizau/git-stats.git"
, hash: ""
, search: ""
}
]
, [
["http://ionicabizau.net/with-true-normalize", true]
, {
protocols: [ "http" ]
, protocol: "http"
, port: null
, resource: "ionicabizau.net"
, user: ""
, pathname: "/with-true-normalize"
, hash: ""
, search: ""
}
]
];
tester.describe("check urls", test => {
INPUTS.forEach(function (c) {
let url = Array.isArray(c[0]) ? c[0][0] : c[0]
test.should("support " + url, () => {
const res = parseUrl(url, c[0][1] !== false);
if (c[0][1] !== false) {
url = normalizeUrl(url, {
stripHash: false
})
}
c[1].query = qs.parse(c[1].search)
c[1].href = url
test.expect(res).toEqual(c[1]);
});
});
test.should("throw if url is empty", () => {
test.expect(() => {
parseUrl("")
}).toThrow(/invalid url/i)
})
});