forked from websanova/js-url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
265 lines (241 loc) · 13.4 KB
/
tests.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
(function() {
var url = 'http://rob:[email protected]/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese';
var urlHttps = 'https://rob:[email protected]/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese';
var urlIp = 'https://rob:[email protected]/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese';
module('url');
test('url', function() {
deepEqual( window.url( ), window.location.href );
});
if (window.url('tld?')) {
test('obj', function() {
deepEqual( window.url('{}', url), {
'auth': 'rob:abcd1234',
'domain': 'domain.com',
'file': 'index.html',
'fileext': 'html',
'filename': 'index',
'hash': 'test=hash&chucky=cheese',
'hostname': 'www.domain.com',
'pass': 'abcd1234',
'path': '/path/index.html',
'port': '80',
'protocol': 'http',
'query': 'query1=test&silly=willy',
'sub': 'www',
'tld': 'com',
'user': 'rob'
});
});
test('tld', function() {
deepEqual( window.url('tld', 'http://sub.www.domain.co.uk'), 'co.uk' );
deepEqual( window.url('tld', 'http://www.domain.org.uk'), 'org.uk' );
deepEqual( window.url('tld', 'http://domain.la'), 'la' );
deepEqual( window.url('tld', 'http://in'), 'in' );
deepEqual( window.url('tld', 'http://.asia'), 'asia' );
deepEqual( window.url('tld', 'http://.cao.uk'), undefined );
deepEqual( window.url('tld', 'http://'), undefined );
deepEqual( window.url('tld', 'http://domain.zoo'), undefined );
deepEqual( window.url('tld', url), 'com' );
});
test('domain', function() {
deepEqual( window.url('domain', 'http://sub.www.domain.co.uk'), 'domain.co.uk' );
deepEqual( window.url('domain', 'http://www.domain.org.uk'), 'domain.org.uk' );
deepEqual( window.url('domain', 'http://domain.la'), 'domain.la' );
deepEqual( window.url('domain', 'http://in'), undefined );
deepEqual( window.url('domain', 'http://.asia'), undefined );
deepEqual( window.url('domain', 'http://.cao.uk'), undefined );
deepEqual( window.url('domain', 'http://'), undefined );
deepEqual( window.url('domain', 'http://domain.zoo'), undefined );
deepEqual( window.url('domain', url), 'domain.com' );
});
test('sub', function() {
deepEqual( window.url('sub', 'http://sub.www.domain.co.uk'), 'sub.www' );
deepEqual( window.url('sub', 'http://www.domain.org.uk'), 'www' );
deepEqual( window.url('sub', 'http://domain.la'), undefined );
deepEqual( window.url('sub', 'http://in'), undefined );
deepEqual( window.url('sub', 'http://.asia'), undefined );
deepEqual( window.url('sub', 'http://.cao.uk'), undefined );
deepEqual( window.url('sub', 'http://'), undefined );
deepEqual( window.url('sub', 'http://domain.zoo'), undefined );
deepEqual( window.url('sub', url), 'www' );
});
}
else {
test('obj', function() {
deepEqual( window.url('{}', url), {
'auth': 'rob:abcd1234',
'file': 'index.html',
'fileext': 'html',
'filename': 'index',
'hash': 'test=hash&chucky=cheese',
'hostname': 'www.domain.com',
'pass': 'abcd1234',
'path': '/path/index.html',
'port': '80',
'protocol': 'http',
'query': 'query1=test&silly=willy',
'user': 'rob'
});
});
}
test('mailto', function() {
deepEqual( window.url( 'email', 'mailto:[email protected]' ), '[email protected]' );
deepEqual( window.url( 'protocol', 'mailto:[email protected]' ), 'mailto' );
deepEqual( window.url( 'email', 'mailto:/[email protected]' ), undefined );
deepEqual( window.url( 'email', 'mailto://[email protected]' ), undefined );
deepEqual( window.url( 'protocol', 'mailto:/[email protected]' ), 'http' );
deepEqual( window.url( 'mailto:', 'mailto:/[email protected]' ), undefined );
});
test('hostname', function() {
deepEqual( window.url( 'hostname', url ), 'www.domain.com' );
deepEqual( window.url( 'hostname', urlIp ), '1.2.3.4' );
});
test('domain parts', function() {
deepEqual( window.url( '.', url ), ['www', 'domain', 'com'] );
deepEqual( window.url( '.0', url ), undefined );
deepEqual( window.url( '.1', url ), 'www' );
deepEqual( window.url( '.2', url ), 'domain' );
deepEqual( window.url( '.-1', url ), 'com' );
});
test('auth', function() {
deepEqual( window.url( 'auth', url ), 'rob:abcd1234' );
deepEqual( window.url( 'user', url ), 'rob' );
deepEqual( window.url( 'pass', url ), 'abcd1234' );
});
test('port', function() {
deepEqual( window.url( 'port', url ), '80' );
deepEqual( window.url( 'port', url.toUpperCase() ), '80' );
deepEqual( window.url( 'port', 'http://example.com:80' ), '80' );
deepEqual( window.url( 'port', urlHttps ), '443' );
deepEqual( window.url( 'port', urlHttps.toUpperCase() ), '443' );
deepEqual( window.url( 'port', 'https://example.com:443' ), '443' );
deepEqual( window.url( 'port', 'http://domain.com:400?poo=a:b' ), '400' );
deepEqual( window.url( 'port', 'https://domain.com:80' ), '80' );
deepEqual( window.url( 'port', 'http://domain.com:443' ), '443' );
deepEqual( window.url( 'port', 'http://domain.com' ), '80' );
deepEqual( window.url( 'port', 'https://domain.com' ), '443' );
deepEqual( window.url( 'port', '[email protected]' ), '80' );
});
test('protocol', function() {
deepEqual( window.url( 'protocol', 'http://domain.com' ), 'http' );
deepEqual( window.url( 'protocol', 'http://domain.com:80' ), 'http' );
deepEqual( window.url( 'protocol', 'http://domain.com:443' ), 'http' );
deepEqual( window.url( 'protocol', 'domain.com' ), 'http' );
deepEqual( window.url( 'protocol', 'domain.com:80' ), 'http' );
deepEqual( window.url( 'protocol', 'domain.com:443' ), 'https' );
deepEqual( window.url( 'protocol', 'https://domain.com:443' ), 'https' );
deepEqual( window.url( 'protocol', 'https://domain.com:80' ), 'https' );
deepEqual( window.url( 'protocol', '[email protected]' ), 'http' );
});
test('path', function() {
deepEqual( window.url( 'path', url ), '/path/index.html' );
deepEqual( window.url( 'path', 'http://www.domain.com/first/second' ), '/first/second' );
deepEqual( window.url( 'path', 'http://www.domain.com/first/second/' ), '/first/second/' );
deepEqual( window.url( 'path', 'http://www.domain.com:8080/first/second' ), '/first/second' );
deepEqual( window.url( 'path', 'http://www.domain.com:8080/first/second/' ), '/first/second/' );
deepEqual( window.url( 'path', 'http://www.domain.com/first/second?test=foo' ), '/first/second' );
deepEqual( window.url( 'path', 'http://www.domain.com/first/second/?test=foo' ), '/first/second/' );
deepEqual( window.url( 'path', 'http://www.domain.com/path#anchor' ), '/path' );
deepEqual( window.url( 'path', 'http://www.domain.com/path/#anchor' ), '/path/' );
deepEqual( window.url( 'path', 'http://www.domain.com' ), '' );
deepEqual( window.url( 'path', 'http://www.domain.com/' ), '/' );
deepEqual( window.url( 'path', 'http://www.domain.com#anchor' ), '' );
deepEqual( window.url( 'path', 'http://www.domain.com/#anchor' ), '/' );
deepEqual( window.url( 'path', 'http://www.domain.com?test=foo' ), '' );
deepEqual( window.url( 'path', 'http://www.domain.com/?test=foo' ), '/' );
deepEqual( window.url( 'path', 'http://www.domain.com:80' ), '' );
deepEqual( window.url( 'path', 'http://www.domain.com:80/' ), '/' );
deepEqual( window.url( 'path', 'http://www.domain.com:80#anchor' ), '' );
deepEqual( window.url( 'path', 'http://www.domain.com:80/#anchor' ), '/' );
deepEqual( window.url( 'path', 'http://www.domain.com:80?test=foo' ), '' );
deepEqual( window.url( 'path', 'http://www.domain.com:80/?test=foo' ), '/' );
});
test('file', function() {
deepEqual( window.url( 'file', url ), 'index.html' );
deepEqual( window.url( 'filename', url ), 'index' );
deepEqual( window.url( 'fileext', url ), 'html' );
deepEqual( window.url( 'file', 'http://domain.com' ), undefined );
deepEqual( window.url( 'filename', 'http://domain.com' ), undefined );
deepEqual( window.url( 'fileext', 'http://domain.com' ), undefined );
});
test('url parts', function() {
deepEqual( window.url( '/', url ), ['path', 'index.html'] );
deepEqual( window.url( '0', url ), undefined );
deepEqual( window.url( '-4', url ), undefined );
deepEqual( window.url( '1', url ), 'path' );
deepEqual( window.url( 1, url ), 'path' );
deepEqual( window.url( '2', url ), 'index.html' );
deepEqual( window.url( '3', url ), undefined );
deepEqual( window.url( '-1', url ), 'index.html' );
deepEqual( window.url( '1', 'http://www.domain.com/first/second' ), 'first' );
deepEqual( window.url( '1', 'http://www.domain.com/first/second/' ), 'first' );
deepEqual( window.url( '-1', 'http://www.domain.com/first/second?test=foo' ), 'second' );
deepEqual( window.url( '-1', 'http://www.domain.com/first/second/?test=foo' ), '' );
deepEqual( window.url( '-2', 'http://www.domain.com/first/second/?test=foo' ), 'second' );
});
test('query string', function() {
deepEqual( window.url( 'query', url ), 'query1=test&silly=willy' );
deepEqual( window.url( '?', url ), {'query1': 'test', 'silly': 'willy'} );
deepEqual( window.url( '?silly', url ), 'willy' );
deepEqual( window.url( '?poo', url ), undefined );
deepEqual( window.url( '?poo', 'http://domain.com?poo=' ), '' );
deepEqual( window.url( '?poo', 'http://domain.com/?poo' ), '' );
deepEqual( window.url( '?poo', 'http://domain.com?poo' ), '' );
deepEqual( window.url( '?poo', 'http://domain.com?' ), undefined );
deepEqual( window.url( '?poo', 'http://domain.com' ), undefined );
deepEqual( window.url( '?poo', 'http://domain.com?poo=a+b' ), 'a b' );
deepEqual( window.url( '?poo', 'http://domain.com?poo=javascript%20decode%20uri%20%2B%20sign%20to%20space' ), 'javascript decode uri + sign to space' );
deepEqual( window.url( '?key', 'http://domain.com?key=value=va?key2=value' ), 'value=va?key2=value');
deepEqual( window.url( '?poo', 'http://domain.com:400?poo=a:b' ), 'a:b' );
deepEqual( window.url( '?poo', 'http://domain.com:400? & & &' ), undefined );
deepEqual( window.url( '?test', 'http://domain.com?test=45#5' ), '45' );
deepEqual( window.url( '?test', 'http://domain.com?test=45?5' ), '45?5' );
deepEqual( window.url( '?', 'http://domain.com?silly=willy&field[0]=zero&field[1]=one' ), {'silly': 'willy', 'field': ['zero', 'one']} );
deepEqual( window.url( '?', 'http://domain.com?silly=willy&field[0]=zero&field[2]=two' ), {'silly': 'willy', 'field': ['zero', undefined, 'two']} );
deepEqual( window.url( '?field[0]', 'http://domain.com?field[0]=zero&firled[1]=one' ), 'zero' );
deepEqual( window.url( '?field', 'http://domain.com?field[0]=zero&field[1]=one&var=test' ), ['zero', 'one'] );
deepEqual( window.url( '?field', 'http://domain.com?field[0]=zero&field[3]=one&var=test' ), ['zero', undefined, undefined, 'one'] );
deepEqual( window.url( '?var', 'http://domain.com?field[0]=zero&field[3]=one&var=test' ), 'test' );
deepEqual( window.url( '?', 'http://domain.com?field[0]=zero&field[1]=one&var=test' ), {'field': ['zero', 'one'], 'var': 'test'} );
});
test('hash string', function() {
deepEqual( window.url( 'hash', url ), 'test=hash&chucky=cheese' );
deepEqual( window.url( '#', url ), {'chucky': 'cheese', 'test': 'hash'} );
deepEqual( window.url( '#chucky', url ), 'cheese' );
deepEqual( window.url( '#poo', url ), undefined );
deepEqual( window.url( '#poo', 'http://domain.com#poo=' ), '' );
deepEqual( window.url( '#poo', 'http://domain.com/#poo' ), '' );
deepEqual( window.url( '#poo', 'http://domain.com#poo' ), '' );
deepEqual( window.url( '#poo', 'http://domain.com#' ), undefined );
deepEqual( window.url( '#poo', 'http://domain.com' ), undefined );
deepEqual( window.url( '#test', 'http://domain.com#test=45#5' ), '45#5' );
deepEqual( window.url( '#field[0]', 'http://domain.com#field[0]=zero&firled[1]=one' ), 'zero' );
deepEqual( window.url( '#field', 'http://domain.com#field[0]=zero&field[1]=one&var=test' ), ['zero', 'one'] );
deepEqual( window.url( '#field', 'http://domain.com#field[0]=zero&field[3]=one&var=test' ), ['zero', undefined, undefined, 'one'] );
deepEqual( window.url( '#var', 'http://domain.com#field[0]=zero&field[3]=one&var=test' ), 'test' );
deepEqual( window.url( '#', 'http://domain.com#field[0]=zero&field[1]=one&var=test' ), {'field': ['zero', 'one'], 'var': 'test'} );
});
test('hash bangs', function() {
deepEqual( window.url( '?poo', 'http://domain.com/#!?' ), undefined );
deepEqual( window.url( '?poo', 'http://domain.com/#!/' ), undefined );
deepEqual( window.url( '-1', 'http://www.domain.com/#!/first/second/?test=foo' ), '' );
deepEqual( window.url( '-2', 'http://www.domain.com/#!/first/second/?test=foo' ), 'second' );
deepEqual( window.url( '?', 'http://domain.com/#!/?field[0]=zero&field[1]=one&var=test' ), {'field': ['zero', 'one'], 'var': 'test'} );
deepEqual( window.url( '?', 'http://domain.com/#!?field[0]=zero&field[1]=one&var=test' ), {'field': ['zero', 'one'], 'var': 'test'} );
deepEqual( window.url( '#', 'http://domain.com/#!/#field[0]=zero&field[1]=one&var=test' ), {'field': ['zero', 'one'], 'var': 'test'} );
});
if (window.url('tld?')) {
test('tld', function() {
deepEqual( window.url( 'tld', 'www.example.ly' ), 'ly' );
deepEqual( window.url( 'tld', 'www.example.xyz' ), 'xyz' );
});
}
if (typeof jQuery !== 'undefined') {
test('jQuery', function() {
deepEqual( $.url( 'hostname', url ), 'www.domain.com' );
deepEqual( $.url( 'path', url ), '/path/index.html' );
deepEqual( $.url( '?silly', url ), 'willy' );
deepEqual( $.url( '#poo', url ), undefined );
});
}
}());