This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlToRelativeUrl.test.js
184 lines (160 loc) · 4.63 KB
/
urlToRelativeUrl.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
import { assert } from "@jsenv/assert"
import { urlToRelativeUrl } from "@jsenv/util"
// https://github.com/medialize/URI.js/blob/73c10412ce97b760d1cb143080bee25e99d12f5b/test/test.js#L1511
// https://github.com/nodejs/node/blob/e12f48ef07e837553ea9c537b08d3e4a44d3fad2/test/parallel/test-path-relative.js
// directory url
{
const actual = urlToRelativeUrl("file:///project/test/bar.js", "file:///project/test/dist/")
const expected = "../bar.js"
assert({ actual, expected })
}
// different protocols
{
const actual = urlToRelativeUrl(
"https://example.com/dir/file.js",
"http://example.com/dir/file.js",
)
const expected = "https://example.com/dir/file.js"
assert({ actual, expected })
}
// different credentials
{
const actual = urlToRelativeUrl(
"http://user:[email protected]/foo/bar",
"http://other:[email protected]/foo/",
)
const expected = "//user:[email protected]/foo/bar"
assert({ actual, expected })
}
// only url credentials
{
const actual = urlToRelativeUrl("http://user:[email protected]/foo/bar", "http://example.org/foo/")
const expected = "//user:[email protected]/foo/bar"
assert({ actual, expected })
}
// only base credentials
{
const actual = urlToRelativeUrl("http://example.org/foo/bar", "http://user:[email protected]/foo/")
const expected = "//example.org/foo/bar"
assert({ actual, expected })
}
// different topleveldomain
{
const actual = urlToRelativeUrl("http://example.com/dir/file.js", "http://example.fr/dir/file.js")
const expected = "//example.com/dir/file.js"
assert({ actual, expected })
}
// different port
{
const actual = urlToRelativeUrl("http://example.org:8081/foo/bar", "http://example.org/foo/bar")
const expected = "//example.org:8081/foo/bar"
assert({ actual, expected })
}
// same origins and ressources
{
const actual = urlToRelativeUrl("http://example.org", "http://example.org")
const expected = ""
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("http://example.org/", "http://example.org")
const expected = ""
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("http://example.org", "http://example.org/")
const expected = ""
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("http://example.org/", "http://example.org/")
const expected = ""
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///directory/foo/file.js", "file:///directory/file.js")
const expected = "foo/file.js"
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///directory/index.js", "file:///directory/foo/file.js")
const expected = "../index.js"
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///directory/file.js", "file:///directory/")
const expected = "file.js"
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///var", "file:///var/lib")
const expected = "../"
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///bin", "file:///var/lib")
const expected = "../bin"
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///var/lib", "file:///var/lib")
const expected = ""
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///var/lib", "file:///var/apache")
const expected = "lib"
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///var/lib", "file:///var/apache/")
const expected = "../lib"
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///", "file:///var/lib")
const expected = "var/lib"
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///page1/page2/foo", "file:///")
const expected = "page1/page2/foo"
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///Users/a/web/b", "file:///Users/a/web/b/test/mails")
const expected = "../../"
assert({ actual, expected })
}
{
const actual = urlToRelativeUrl("file:///c:/aaaa", "file:///c:/aaaa/bbbb")
const expected = "../"
assert({ actual, expected })
}
// url search
{
const actual = urlToRelativeUrl(
"http://www.example.com:8080/dir/file?abcd=123",
"http://www.example.com:8080/dir/file",
)
const expected = "?abcd=123"
assert({ actual, expected })
}
// url hash
{
const actual = urlToRelativeUrl(
"http://www.example.com:8080/dir/file#abcd",
"http://www.example.com:8080/dir/file",
)
const expected = "#abcd"
assert({ actual, expected })
}
// url search and hash
{
const actual = urlToRelativeUrl(
"http://www.example.com:8080/dir/file?abcd=123#alpha",
"http://www.example.com:8080/dir/file",
)
const expected = "?abcd=123#alpha"
assert({ actual, expected })
}