summaryrefslogtreecommitdiff
path: root/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js
blob: 9913f5e64014096e20d90fec88cb3a7950f078e7 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
( function ( mw, $ ) {
	QUnit.module( 'mediawiki.Uri', QUnit.newMwEnvironment( {
		setup: function () {
			this.mwUriOrg = mw.Uri;
			mw.Uri = mw.UriRelative( 'http://example.org/w/index.php' );
		},
		teardown: function () {
			mw.Uri = this.mwUriOrg;
			delete this.mwUriOrg;
		}
	} ) );

	$.each( [true, false], function ( i, strictMode ) {
		QUnit.test( 'Basic construction and properties (' + ( strictMode ? '' : 'non-' ) + 'strict mode)', 2, function ( assert ) {
			var uriString, uri;
			uriString = 'http://www.ietf.org/rfc/rfc2396.txt';
			uri = new mw.Uri( uriString, {
				strictMode: strictMode
			} );

			assert.deepEqual(
				{
					protocol: uri.protocol,
					host: uri.host,
					port: uri.port,
					path: uri.path,
					query: uri.query,
					fragment: uri.fragment
				}, {
					protocol: 'http',
					host: 'www.ietf.org',
					port: undefined,
					path: '/rfc/rfc2396.txt',
					query: {},
					fragment: undefined
				},
				'basic object properties'
			);

			assert.deepEqual(
				{
					userInfo: uri.getUserInfo(),
					authority: uri.getAuthority(),
					hostPort: uri.getHostPort(),
					queryString: uri.getQueryString(),
					relativePath: uri.getRelativePath(),
					toString: uri.toString()
				},
				{
					userInfo: '',
					authority: 'www.ietf.org',
					hostPort: 'www.ietf.org',
					queryString: '',
					relativePath: '/rfc/rfc2396.txt',
					toString: uriString
				},
				'construct composite components of URI on request'
			);
		} );
	} );

	QUnit.test( 'Constructor( String[, Object ] )', 10, function ( assert ) {
		var uri;

		uri = new mw.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
			overrideKeys: true
		} );

		// Strict comparison to assert that numerical values stay strings
		assert.strictEqual( uri.query.n, '1', 'Simple parameter with overrideKeys:true' );
		assert.strictEqual( uri.query.m, 'bar', 'Last key overrides earlier keys with overrideKeys:true' );

		uri = new mw.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
			overrideKeys: false
		} );

		assert.strictEqual( uri.query.n, '1', 'Simple parameter with overrideKeys:false' );
		assert.strictEqual( uri.query.m[0], 'foo', 'Order of multi-value parameters with overrideKeys:true' );
		assert.strictEqual( uri.query.m[1], 'bar', 'Order of multi-value parameters with overrideKeys:true' );
		assert.strictEqual( uri.query.m.length, 2, 'Number of mult-value field is correct' );

		uri = new mw.Uri( 'ftp://usr:pwd@192.0.2.16/' );

		assert.deepEqual(
			{
				protocol: uri.protocol,
				user: uri.user,
				password: uri.password,
				host: uri.host,
				port: uri.port,
				path: uri.path,
				query: uri.query,
				fragment: uri.fragment
			},
			{
				protocol: 'ftp',
				user: 'usr',
				password: 'pwd',
				host: '192.0.2.16',
				port: undefined,
				path: '/',
				query: {},
				fragment: undefined
			},
			'Parse an ftp URI correctly with user and password'
		);

		assert.throws(
			function () {
				return new mw.Uri( 'glaswegian penguins' );
			},
			function ( e ) {
				return e.message === 'Bad constructor arguments';
			},
			'throw error on non-URI as argument to constructor'
		);

		assert.throws(
			function () {
				return new mw.Uri( 'foo.com/bar/baz', {
					strictMode: true
				} );
			},
			function ( e ) {
				return e.message === 'Bad constructor arguments';
			},
			'throw error on URI without protocol or // or leading / in strict mode'
		);

		uri = new mw.Uri( 'foo.com/bar/baz', {
			strictMode: false
		} );
		assert.equal( uri.toString(), 'http://foo.com/bar/baz', 'normalize URI without protocol or // in loose mode' );
	} );

	QUnit.test( 'Constructor( Object )', 3, function ( assert ) {
		var uri = new mw.Uri( {
			protocol: 'http',
			host: 'www.foo.local',
			path: '/this'
		} );
		assert.equal( uri.toString(), 'http://www.foo.local/this', 'Basic properties' );

		uri = new mw.Uri( {
			protocol: 'http',
			host: 'www.foo.local',
			path: '/this',
			query: { hi: 'there' },
			fragment: 'blah'
		} );
		assert.equal( uri.toString(), 'http://www.foo.local/this?hi=there#blah', 'More complex properties' );

		assert.throws(
			function () {
				return new mw.Uri( {
					protocol: 'http',
					host: 'www.foo.local'
				} );
			},
			function ( e ) {
				return e.message === 'Bad constructor arguments';
			},
			'Construction failed when missing required properties'
		);
	} );

	QUnit.test( 'Constructor( empty )', 4, function ( assert ) {
		var testuri, MyUri, uri;

		testuri = 'http://example.org/w/index.php';
		MyUri = mw.UriRelative( testuri );

		uri = new MyUri();
		assert.equal( uri.toString(), testuri, 'no arguments' );

		uri = new MyUri( undefined );
		assert.equal( uri.toString(), testuri, 'undefined' );

		uri = new MyUri( null );
		assert.equal( uri.toString(), testuri, 'null' );

		uri = new MyUri( '' );
		assert.equal( uri.toString(), testuri, 'empty string' );
	} );

	QUnit.test( 'Properties', 8, function ( assert ) {
		var uriBase, uri;

		uriBase = new mw.Uri( 'http://en.wiki.local/w/api.php' );

		uri = uriBase.clone();
		uri.fragment = 'frag';
		assert.equal( uri.toString(), 'http://en.wiki.local/w/api.php#frag', 'add a fragment' );

		uri = uriBase.clone();
		uri.host = 'fr.wiki.local';
		uri.port = '8080';
		assert.equal( uri.toString(), 'http://fr.wiki.local:8080/w/api.php', 'change host and port' );

		uri = uriBase.clone();
		uri.query.foo = 'bar';
		assert.equal( uri.toString(), 'http://en.wiki.local/w/api.php?foo=bar', 'add query arguments' );

		delete uri.query.foo;
		assert.equal( uri.toString(), 'http://en.wiki.local/w/api.php', 'delete query arguments' );

		uri = uriBase.clone();
		uri.query.foo = 'bar';
		assert.equal( uri.toString(), 'http://en.wiki.local/w/api.php?foo=bar', 'extend query arguments' );
		uri.extend( {
			foo: 'quux',
			pif: 'paf'
		} );
		assert.ok( uri.toString().indexOf( 'foo=quux' ) >= 0, 'extend query arguments' );
		assert.ok( uri.toString().indexOf( 'foo=bar' ) === -1, 'extend query arguments' );
		assert.ok( uri.toString().indexOf( 'pif=paf' ) >= 0, 'extend query arguments' );
	} );

	QUnit.test( '.getQueryString()', 2, function ( assert ) {
		var uri = new mw.Uri( 'http://www.google.com/?q=uri' );

		assert.deepEqual(
			{
				protocol: uri.protocol,
				host: uri.host,
				port: uri.port,
				path: uri.path,
				query: uri.query,
				fragment: uri.fragment,
				queryString: uri.getQueryString()
			},
			{
				protocol: 'http',
				host: 'www.google.com',
				port: undefined,
				path: '/',
				query: { q: 'uri' },
				fragment: undefined,
				queryString: 'q=uri'
			},
			'basic object properties'
		);

		uri = new mw.Uri( 'https://example.org/mw/index.php?title=Sandbox/7&other=Sandbox/7&foo' );
		assert.equal(
			uri.getQueryString(),
			'title=Sandbox/7&other=Sandbox%2F7&foo',
			'title parameter is escaped the wiki-way'
		);

	} );

	QUnit.test( '.clone()', 6, function ( assert ) {
		var original, clone;

		original = new mw.Uri( 'http://foo.example.org/index.php?one=1&two=2' );
		clone = original.clone();

		assert.deepEqual( clone, original, 'clone has equivalent properties' );
		assert.equal( original.toString(), clone.toString(), 'toString matches original' );

		assert.notStrictEqual( clone, original, 'clone is a different object when compared by reference' );

		clone.host = 'bar.example.org';
		assert.notEqual( original.host, clone.host, 'manipulating clone did not effect original' );
		assert.notEqual( original.toString(), clone.toString(), 'Stringified url no longer matches original' );

		clone.query.three = 3;

		assert.deepEqual(
			original.query,
			{ 'one': '1', 'two': '2' },
			'Properties is deep cloned (bug 37708)'
		);
	} );

	QUnit.test( '.toString() after query manipulation', 8, function ( assert ) {
		var uri;

		uri = new mw.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
			overrideKeys: true
		} );

		uri.query.n = [ 'x', 'y', 'z' ];

		// Verify parts and total length instead of entire string because order
		// of iteration can vary.
		assert.ok( uri.toString().indexOf( 'm=bar' ), 'toString preserves other values' );
		assert.ok( uri.toString().indexOf( 'n=x&n=y&n=z' ), 'toString parameter includes all values of an array query parameter' );
		assert.equal( uri.toString().length, 'http://www.example.com/dir/?m=bar&n=x&n=y&n=z'.length, 'toString matches expected string' );

		uri = new mw.Uri( 'http://www.example.com/dir/?m=foo&m=bar&n=1', {
			overrideKeys: false
		} );

		// Change query values
		uri.query.n = [ 'x', 'y', 'z' ];

		// Verify parts and total length instead of entire string because order
		// of iteration can vary.
		assert.ok( uri.toString().indexOf( 'm=foo&m=bar' ) >= 0, 'toString preserves other values' );
		assert.ok( uri.toString().indexOf( 'n=x&n=y&n=z' ) >= 0, 'toString parameter includes all values of an array query parameter' );
		assert.equal( uri.toString().length, 'http://www.example.com/dir/?m=foo&m=bar&n=x&n=y&n=z'.length, 'toString matches expected string' );

		// Remove query values
		uri.query.m.splice( 0, 1 );
		delete uri.query.n;

		assert.equal( uri.toString(), 'http://www.example.com/dir/?m=bar', 'deletion properties' );

		// Remove more query values, leaving an empty array
		uri.query.m.splice( 0, 1 );
		assert.equal( uri.toString(), 'http://www.example.com/dir/', 'empty array value is ommitted' );
	} );

	QUnit.test( 'Advanced URL', 11, function ( assert ) {
		var uri, queryString, relativePath;

		uri = new mw.Uri( 'http://auth@www.example.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value+%28escaped%29#top' );

		assert.deepEqual(
			{
				protocol: uri.protocol,
				user: uri.user,
				password: uri.password,
				host: uri.host,
				port: uri.port,
				path: uri.path,
				query: uri.query,
				fragment: uri.fragment
			},
			{
				protocol: 'http',
				user: 'auth',
				password: undefined,
				host: 'www.example.com',
				port: '81',
				path: '/dir/dir.2/index.htm',
				query: { q1: '0', test1: null, test2: 'value (escaped)' },
				fragment: 'top'
			},
			'basic object properties'
		);

		assert.equal( uri.getUserInfo(), 'auth', 'user info' );

		assert.equal( uri.getAuthority(), 'auth@www.example.com:81', 'authority equal to auth@hostport' );

		assert.equal( uri.getHostPort(), 'www.example.com:81', 'hostport equal to host:port' );

		queryString = uri.getQueryString();
		assert.ok( queryString.indexOf( 'q1=0' ) >= 0, 'query param with numbers' );
		assert.ok( queryString.indexOf( 'test1' ) >= 0, 'query param with null value is included' );
		assert.ok( queryString.indexOf( 'test1=' ) === -1, 'query param with null value does not generate equals sign' );
		assert.ok( queryString.indexOf( 'test2=value+%28escaped%29' ) >= 0, 'query param is url escaped' );

		relativePath = uri.getRelativePath();
		assert.ok( relativePath.indexOf( uri.path ) >= 0, 'path in relative path' );
		assert.ok( relativePath.indexOf( uri.getQueryString() ) >= 0, 'query string in relative path' );
		assert.ok( relativePath.indexOf( uri.fragment ) >= 0, 'fragement in relative path' );
	} );

	QUnit.test( 'Parse a uri with an @ symbol in the path and query', 1, function ( assert ) {
		var uri = new mw.Uri( 'http://www.example.com/test@test?x=@uri&y@=uri&z@=@' );

		assert.deepEqual(
			{
				protocol: uri.protocol,
				user: uri.user,
				password: uri.password,
				host: uri.host,
				port: uri.port,
				path: uri.path,
				query: uri.query,
				fragment: uri.fragment,
				queryString: uri.getQueryString()
			},
			{
				protocol: 'http',
				user: undefined,
				password: undefined,
				host: 'www.example.com',
				port: undefined,
				path: '/test@test',
				query: { x: '@uri', 'y@': 'uri', 'z@': '@' },
				fragment: undefined,
				queryString: 'x=%40uri&y%40=uri&z%40=%40'
			},
			'basic object properties'
		);
	} );

	QUnit.test( 'Handle protocol-relative URLs', 5, function ( assert ) {
		var UriRel, uri;

		UriRel = mw.UriRelative( 'glork://en.wiki.local/foo.php' );

		uri = new UriRel( '//en.wiki.local/w/api.php' );
		assert.equal( uri.protocol, 'glork', 'create protocol-relative URLs with same protocol as document' );

		uri = new UriRel( '/foo.com' );
		assert.equal( uri.toString(), 'glork://en.wiki.local/foo.com', 'handle absolute paths by supplying protocol and host from document in loose mode' );

		uri = new UriRel( 'http:/foo.com' );
		assert.equal( uri.toString(), 'http://en.wiki.local/foo.com', 'handle absolute paths by supplying host from document in loose mode' );

		uri = new UriRel( '/foo.com', true );
		assert.equal( uri.toString(), 'glork://en.wiki.local/foo.com', 'handle absolute paths by supplying protocol and host from document in strict mode' );

		uri = new UriRel( 'http:/foo.com', true );
		assert.equal( uri.toString(), 'http://en.wiki.local/foo.com', 'handle absolute paths by supplying host from document in strict mode' );
	} );

	QUnit.test( 'bug 35658', 2, function ( assert ) {
		var testProtocol, testServer, testPort, testPath, UriClass, uri, href;

		testProtocol = 'https://';
		testServer = 'foo.example.org';
		testPort = '3004';
		testPath = '/!1qy';

		UriClass = mw.UriRelative( testProtocol + testServer + '/some/path/index.html' );
		uri = new UriClass( testPath );
		href = uri.toString();
		assert.equal( href, testProtocol + testServer + testPath, 'Root-relative URL gets host & protocol supplied' );

		UriClass = mw.UriRelative( testProtocol + testServer + ':' + testPort + '/some/path.php' );
		uri = new UriClass( testPath );
		href = uri.toString();
		assert.equal( href, testProtocol + testServer + ':' + testPort + testPath, 'Root-relative URL gets host, protocol, and port supplied' );

	} );
}( mediaWiki, jQuery ) );