summaryrefslogtreecommitdiff
path: root/tests/jasmine/spec/mediawiki.Uri.spec.js
blob: 721ccb38012479d616460d63a9910e32195ed61f (plain)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
( function() {

	// ensure we have a generic URI parser if not running in a browser
	if ( !mw.Uri ) {
		mw.Uri = mw.UriRelative( 'http://example.com/' );
	}

	describe( "mw.Uri", function() {

		describe( "should work well in loose and strict mode", function() {

			function basicTests( strict ) {
			
				describe( "should parse a simple HTTP URI correctly", function() { 

					var uriString = 'http://www.ietf.org/rfc/rfc2396.txt';
					var uri;
					if ( strict ) {
						uri = new mw.Uri( uriString, strict );
					} else {
						uri = new mw.Uri( uriString );
					}

					it( "should have basic object properties", function() {
						expect( uri.protocol ).toEqual( 'http' );
						expect( uri.host ).toEqual( 'www.ietf.org' );
						expect( uri.port ).not.toBeDefined();
						expect( uri.path ).toEqual( '/rfc/rfc2396.txt' );
						expect( uri.query ).toEqual( {} );
						expect( uri.fragment ).not.toBeDefined();
					} );

					describe( "should construct composite components of URI on request", function() { 
						it( "should have empty userinfo", function() { 
							expect( uri.getUserInfo() ).toEqual( '' );
						} );

						it( "should have authority equal to host", function() { 
							expect( uri.getAuthority() ).toEqual( 'www.ietf.org' );
						} );

						it( "should have hostport equal to host", function() { 
							expect( uri.getHostPort() ).toEqual( 'www.ietf.org' );
						} );

						it( "should have empty string as query string", function() { 
							expect( uri.getQueryString() ).toEqual( '' );
						} );

						it( "should have path as relative path", function() { 
							expect( uri.getRelativePath() ).toEqual( '/rfc/rfc2396.txt' );
						} );

						it( "should return a uri string equivalent to original", function() { 
							expect( uri.toString() ).toEqual( uriString );
						} );
					} );
				} );
			}

			describe( "should work in loose mode", function() { 
				basicTests( false );
			} );

			describe( "should work in strict mode", function() {
				basicTests( true );
			} );

		} );

		it( "should parse a simple ftp URI correctly with user and password", function() {
			var uri = new mw.Uri( 'ftp://usr:pwd@192.0.2.16/' );
			expect( uri.protocol ).toEqual( 'ftp' );
			expect( uri.user ).toEqual( 'usr' );
			expect( uri.password ).toEqual( 'pwd' );
			expect( uri.host ).toEqual( '192.0.2.16' );
			expect( uri.port ).not.toBeDefined();
			expect( uri.path ).toEqual( '/' );
			expect( uri.query ).toEqual( {} );
			expect( uri.fragment ).not.toBeDefined();
		} );

		it( "should parse a simple querystring", function() {
			var uri = new mw.Uri( 'http://www.google.com/?q=uri' );
			expect( uri.protocol ).toEqual( 'http' );
			expect( uri.host ).toEqual( 'www.google.com' );
			expect( uri.port ).not.toBeDefined();
			expect( uri.path ).toEqual( '/' );
			expect( uri.query ).toBeDefined();
			expect( uri.query ).toEqual( { q: 'uri' } );
			expect( uri.fragment ).not.toBeDefined();
			expect( uri.getQueryString() ).toEqual( 'q=uri' );
		} );

		describe( "should handle multiple value query args (overrideKeys on)", function() {
			var uri = new mw.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', { overrideKeys: true } );
			it ( "should parse with multiple values", function() {
				expect( uri.query.m ).toEqual( 'bar' );
				expect( uri.query.n ).toEqual( '1' );
			} );
			it ( "should accept multiple values", function() {
				uri.query.n = [ "x", "y", "z" ];
				expect( uri.toString() ).toContain( 'm=bar' );
				expect( uri.toString() ).toContain( 'n=x&n=y&n=z' );
				expect( uri.toString().length ).toEqual( 'http://www.example.com/dir/?m=bar&n=x&n=y&n=z'.length );
			} );
		} );

		describe( "should handle multiple value query args (overrideKeys off)", function() {
			var uri = new mw.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', { overrideKeys: false } );
			it ( "should parse with multiple values", function() {
				expect( uri.query.m.length ).toEqual( 2 );
				expect( uri.query.m[0] ).toEqual( 'foo' );
				expect( uri.query.m[1] ).toEqual( 'bar' );
				expect( uri.query.n ).toEqual( '1' );
			} );
			it ( "should accept multiple values", function() {
				uri.query.n = [ "x", "y", "z" ];
				expect( uri.toString() ).toContain( 'm=foo&m=bar' );
				expect( uri.toString() ).toContain( 'n=x&n=y&n=z' );
				expect( uri.toString().length ).toEqual( 'http://www.example.com/dir/?m=foo&m=bar&n=x&n=y&n=z'.length );
			} );
			it ( "should be okay with removing values", function() {
				uri.query.m.splice( 0, 1 );
				delete uri.query.n;
				expect( uri.toString() ).toEqual( 'http://www.example.com/dir/?m=bar' );
				uri.query.m.splice( 0, 1 );
				expect( uri.toString() ).toEqual( 'http://www.example.com/dir/' );
			} );
		} );

		describe( "should deal with an all-dressed URI with everything", function() {
			var uri = new mw.Uri( 'http://auth@www.example.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value+%28escaped%29#top' );

			it( "should have basic object properties", function() {
				expect( uri.protocol ).toEqual( 'http' );
				expect( uri.user ).toEqual( 'auth' );
				expect( uri.password ).not.toBeDefined();
				expect( uri.host ).toEqual( 'www.example.com' );
				expect( uri.port ).toEqual( '81' );
				expect( uri.path ).toEqual( '/dir/dir.2/index.htm' );
				expect( uri.query ).toEqual( { q1: '0', test1: null, test2: 'value (escaped)' } );
				expect( uri.fragment ).toEqual( 'top' );
			} );

			describe( "should construct composite components of URI on request", function() { 
				it( "should have userinfo", function() { 
					expect( uri.getUserInfo() ).toEqual( 'auth' );
				} );

				it( "should have authority equal to auth@hostport", function() { 
					expect( uri.getAuthority() ).toEqual( 'auth@www.example.com:81' );
				} );

				it( "should have hostport equal to host:port", function() { 
					expect( uri.getHostPort() ).toEqual( 'www.example.com:81' );
				} );

				it( "should have query string which contains all components", function() { 
					var queryString = uri.getQueryString();
					expect( queryString ).toContain( 'q1=0' );
					expect( queryString ).toContain( 'test1' );
					expect( queryString ).not.toContain( 'test1=' );
					expect( queryString ).toContain( 'test2=value+%28escaped%29' );
				} );

				it( "should have path as relative path", function() { 
					expect( uri.getRelativePath() ).toContain( uri.path );
					expect( uri.getRelativePath() ).toContain( uri.getQueryString() );
					expect( uri.getRelativePath() ).toContain( uri.fragment );
				} );

			} );
		} );

		describe( "should be able to clone itself", function() {
			var original = new mw.Uri( 'http://en.wiki.local/w/api.php?action=query&foo=bar' );			
			var clone = original.clone();

			it( "should make clones equivalent", function() { 
				expect( original ).toEqual( clone );
				expect( original.toString() ).toEqual( clone.toString() );
			} );

			it( "should be able to manipulate clones independently", function() { 
				// but they are still different objects
				expect( original ).not.toBe( clone );
				// and can diverge
				clone.host = 'fr.wiki.local';
				expect( original.host ).not.toEqual( clone.host );
				expect( original.toString() ).not.toEqual( clone.toString() );
			} );
		} );

		describe( "should be able to construct URL from object", function() {
			it ( "should construct given basic arguments", function() {  
				var uri = new mw.Uri( { protocol: 'http', host: 'www.foo.local',  path: '/this' } );
				expect( uri.toString() ).toEqual( 'http://www.foo.local/this' );
			} );
		
			it ( "should construct given more complex arguments", function() {  
				var uri = new mw.Uri( { 
					protocol: 'http', 
					host: 'www.foo.local',  
					path: '/this', 
					query: { hi: 'there' },
					fragment: 'blah'  
				} );
				expect( uri.toString() ).toEqual( 'http://www.foo.local/this?hi=there#blah' );
			} );	

			it ( "should fail to construct without required properties", function() {  
				expect( function() { 
					var uri = new mw.Uri( { protocol: 'http', host: 'www.foo.local' } );
				} ).toThrow( "Bad constructor arguments" );
			} );
		} );

		describe( "should be able to manipulate properties", function() { 
			var uri;

			beforeEach( function() { 
				uri = new mw.Uri( 'http://en.wiki.local/w/api.php' );			
			} );

			it( "can add a fragment", function() {
				uri.fragment = 'frag';
				expect( uri.toString() ).toEqual( 'http://en.wiki.local/w/api.php#frag' );
			} );

			it( "can change host and port", function() {
				uri.host = 'fr.wiki.local';
				uri.port = '8080';
				expect( uri.toString() ).toEqual( 'http://fr.wiki.local:8080/w/api.php' );
			} );

			it ( "can add query arguments", function() {
				uri.query.foo = 'bar';
				expect( uri.toString() ).toEqual( 'http://en.wiki.local/w/api.php?foo=bar' );
			} );

			it ( "can extend query arguments", function() {
				uri.query.foo = 'bar';
				expect( uri.toString() ).toEqual( 'http://en.wiki.local/w/api.php?foo=bar' );
				uri.extend( { foo: 'quux', pif: 'paf' } );
				expect( uri.toString() ).toContain( 'foo=quux' );
				expect( uri.toString() ).not.toContain( 'foo=bar' );
				expect( uri.toString() ).toContain( 'pif=paf' );
			} );

			it ( "can remove query arguments", function() {
				uri.query.foo = 'bar';
				expect( uri.toString() ).toEqual( 'http://en.wiki.local/w/api.php?foo=bar' );	
				delete( uri.query.foo );
				expect( uri.toString() ).toEqual( 'http://en.wiki.local/w/api.php' );	
			} );

		} );

		describe( "should handle protocol-relative URLs", function() { 

			it ( "should create protocol-relative URLs with same protocol as document", function() {
				var uriRel = mw.UriRelative( 'glork://en.wiki.local/foo.php' );
				var uri = new uriRel( '//en.wiki.local/w/api.php' );
				expect( uri.protocol ).toEqual( 'glork' );
			} );

		} );

		it( "should throw error on no arguments to constructor", function() {
			expect( function() { 
				var uri = new mw.Uri();
			} ).toThrow( "Bad constructor arguments" );
		} );

		it( "should throw error on empty string as argument to constructor", function() {
			expect( function() { 
				var uri = new mw.Uri( '' );
			} ).toThrow( "Bad constructor arguments" );
		} );

		it( "should throw error on non-URI as argument to constructor", function() {
			expect( function() { 
				var uri = new mw.Uri( 'glaswegian penguins' );
			} ).toThrow( "Bad constructor arguments" );
		} );

		it( "should throw error on improper URI as argument to constructor", function() {
			expect( function() { 
				var uri = new mw.Uri( 'http:/foo.com' );
			} ).toThrow( "Bad constructor arguments" );
		} );

		it( "should throw error on URI without protocol or // in strict mode", function() {
			expect( function() { 
				var uri = new mw.Uri( 'foo.com/bar/baz', true );
			} ).toThrow( "Bad constructor arguments" );
		} );

		it( "should normalize URI without protocol or // in loose mode", function() {
			var uri = new mw.Uri( 'foo.com/bar/baz', false );
			expect( uri.toString() ).toEqual( 'http://foo.com/bar/baz' );
		} );

	} );

} )();