-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
291 lines (256 loc) · 5.84 KB
/
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict'
const { test, skip } = require('zora')
const { URL, URLSearchParams, relative } = require('./index.js')
const isBrowser =
typeof window === 'object' &&
typeof document === 'object' &&
document.nodeType === 9
test('unspecified base should not throw', (t) => {
t.doesNotThrow(() => new URL('http://localhost'))
})
test('relative', (t) => {
const url = new URL('/test')
if (isBrowser) {
t.is(
url.href,
self.location.protocol + '//' + self.location.host + '/test'
)
} else {
t.is('http://localhost/test', url.href)
}
})
test('full properties', (t) => {
const url = new URL(
'https://user:[email protected]:8080/p/a/t/h?query=string#hash'
)
t.is(url.hash, '#hash')
t.is(url.host, 'sub.host.com:8080')
t.is(url.hostname, 'sub.host.com')
t.is(
url.href,
'https://user:[email protected]:8080/p/a/t/h?query=string#hash'
)
t.is(url.origin, 'https://sub.host.com:8080')
t.is(url.password, 'pass')
t.is(url.pathname, '/p/a/t/h')
t.is(url.port, '8080')
t.is(url.search, '?query=string')
t.ok(url.searchParams instanceof URLSearchParams)
t.is(url.username, 'user')
})
test('legacy properties', (t) => {
const url = new URL(
'https://user:[email protected]:8080/p/a/t/h?query=string#hash'
)
t.is(url.path, '/p/a/t/h?query=string')
t.is(url.auth, 'user:pass')
t.is(url.query, 'query=string')
})
test('last slash', (t) => {
const url = new URL('https://user:[email protected]:8080')
t.is(url.href, 'https://user:[email protected]:8080/')
})
test('throw with invalid href assign', (t) => {
const url = new URL('https://user:[email protected]:8080')
t.throws(() => {
url.href = '/ll'
}, Error)
})
test('format', (t) => {
const url = new URL('https://user:[email protected]:8080')
t.is(url.toString(), 'https://user:[email protected]:8080/')
t.is(url.format(), 'https://user:[email protected]:8080/')
})
test('format with options', (t) => {
const url = new URL('https://user:[email protected]:8080')
t.is(url.toString(), 'https://user:[email protected]:8080/')
t.is(url.format(), 'https://user:[email protected]:8080/')
})
test('suppport ws', (t) => {
const url = new URL('ws://localhost:2134')
t.is(url.toString(), 'ws://localhost:2134/')
t.is(url.format(), 'ws://localhost:2134/')
})
const map = {
http: 'ws',
https: 'wss'
}
const def = 'ws'
test('suppport ws in relative', (t) => {
t.is(relative('ws://localhost:2134'), 'ws://localhost:2134/')
})
test('suppport ws in relative with options', (t) => {
t.is(relative('ws://localhost:2134', {}, map, def), 'ws://localhost:2134/')
})
test('suppport wss in relative from an http location object', (t) => {
t.is(
relative(
'wss://localhost:4433',
{
host: 'localhost:3000',
hostname: 'localhost',
href: 'http://localhost:3000/',
origin: 'http://localhost:3000',
pathname: '/debug.html',
port: '3000',
protocol: 'http:'
},
map,
def
),
'wss://localhost:4433/'
)
})
test('test handle ipv6 with brackets', (t) => {
t.is(relative('wss://[::1]:4002', {}, map, def), 'wss://[::1]:4002/')
})
test('map from a relative url to one for this domain', (t) => {
const location = {
protocol: 'http',
host: 'foo.com',
pathname: '/whatever',
search: '?okay=true'
}
t.equal(relative('//bar.com', location, map, def), 'ws://bar.com/')
t.equal(relative('/this', location, map, def), 'ws://foo.com/this')
})
test('same path works on dev and deployed', (t) => {
t.equal(
relative(
'/',
{
protocol: 'http',
host: 'localhost:8000'
},
map,
def
),
'ws://localhost:8000/'
)
t.equal(
relative(
'/',
{
protocol: 'http',
host: 'server.com'
},
map,
def
),
'ws://server.com/'
)
t.equal(
relative(
'/',
{
protocol: 'https',
host: 'server.com'
},
map,
def
),
'wss://server.com/'
)
})
test('universal url still works', (t) => {
const location = {
protocol: 'http',
host: 'localhost:8000'
}
t.equal(
relative('ws://what.com/okay', location, map, def),
'ws://what.com/okay'
)
t.equal(
relative('wss://localhost/', location, map, def),
'wss://localhost/'
)
})
test('protocol defaults to not change', (t) => {
t.equal(
relative('/', {
protocol: 'http',
host: 'localhost:8000'
}),
'http://localhost:8000/'
)
t.equal(
relative('/', {
protocol: 'http',
host: 'server.com'
}),
'http://server.com/'
)
t.equal(
relative('/', {
protocol: 'https',
host: 'server.com'
}),
'https://server.com/'
)
})
test('query string, if you want that!', (t) => {
const location = {
protocol: 'http',
host: 'localhost:8000',
pathname: '/foo/bar'
}
t.equal(
relative('?whatever=true', location, map, def),
'ws://localhost:8000/foo/bar?whatever=true'
)
})
skip('same domain, different port', (t) => {
t.equal(
relative(
'//:9999/okay',
{
protocol: 'http',
host: 'localhost:8000',
hostname: 'localhost'
},
map,
def
),
'ws://localhost:9999/okay'
)
t.equal(
relative(
'//:9999/okay',
{
protocol: 'http',
host: 'server.com',
hostname: 'server.com'
},
map,
def
),
'ws://server.com:9999/okay'
)
})
test("don't mess with url that starts out absolute", (t) => {
t.equal(
relative(
'ws://localhost:8000/',
{
protocol: 'http',
host: 'localhost:8000'
},
map,
def
),
'ws://localhost:8000/'
)
t.equal(
relative(
'ws://localhost:8000/',
{
protocol: 'https',
host: 'server.com'
},
map,
def
),
'ws://localhost:8000/'
)
})