forked from IonicaBizau/parse-url
/
index.js
131 lines (126 loc) · 3.16 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Dependencies
import parseUrl from "../lib/index.js";
import tester from "tester";
import normalizeUrl from "normalize-url";
const INPUTS = [
[
"http://ionicabizau.net/blog"
, {
protocols: [ "http" ]
, protocol: "http"
, port: ""
, resource: "ionicabizau.net"
, user: ""
, pathname: "/blog"
, hash: ""
, search: ""
, query: {}
}
]
, [
"//ionicabizau.net/foo.js"
, {
protocols: ["http"]
, protocol: "http"
, port: ""
, resource: "ionicabizau.net"
, user: ""
, pathname: "/foo.js"
, hash: ""
, search: ""
, query: {}
}
]
, [
"http://domain.com/path/name#some-hash?foo=bar"
, {
protocols: ["http"]
, protocol: "http"
, port: ""
, resource: "domain.com"
, user: ""
, pathname: "/path/name"
, hash: "some-hash?foo=bar"
, search: ""
, query: {}
}
]
, [
["git+ssh://git@host.xz/path/name.git", false]
, {
protocols: ["git", "ssh"]
, protocol: "git"
, port: ""
, resource: "host.xz"
, user: "git"
, pathname: "/path/name.git"
, hash: ""
, search: ""
, query: {}
}
]
, [
["git@github.com:IonicaBizau/git-stats.git", false]
, {
protocols: ["ssh"]
, protocol: "ssh"
, port: ""
, resource: "github.com"
, user: "git"
, pathname: "/IonicaBizau/git-stats.git"
, hash: ""
, search: ""
, query: {}
}
]
, [
["http://ionicabizau.net/with-true-normalize", true]
, {
protocols: [ "http" ]
, protocol: "http"
, port: ""
, resource: "ionicabizau.net"
, user: ""
, pathname: "/with-true-normalize"
, hash: ""
, search: ""
, query: {}
}
]
, [
["git@github.my-enterprise.com:my-org/my-repo.git", false],
{
protocols: [ 'ssh' ]
, protocol: 'ssh'
, port: ''
, resource: 'github.my-enterprise.com'
, user: 'git'
, password: ''
, pathname: '/my-org/my-repo.git'
, hash: ''
, search: ''
, query: {}
}
]
];
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].href = c[1].href || url
c[1].password = c[1].password || ""
test.expect(res).toEqual(c[1]);
});
});
test.should("throw if url is empty", () => {
test.expect(() => {
parseUrl("")
}).toThrow(/invalid url/i)
})
});