aboutsummaryrefslogtreecommitdiff
path: root/vendors/simpletest/test/url_test.php
blob: 550717038daf08a2244abfdfb7440e98f0216454 (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
434
435
436
437
438
439
440
441
442
443
<?php
// $Id: url_test.php 1598 2007-12-24 10:44:09Z pp11 $
require_once(dirname(__FILE__) . '/../autorun.php');
require_once(dirname(__FILE__) . '/../url.php');

class TestOfUrl extends UnitTestCase {
    
    function testDefaultUrl() {
        $url = new SimpleUrl('');
        $this->assertEqual($url->getScheme(), '');
        $this->assertEqual($url->getHost(), '');
        $this->assertEqual($url->getScheme('http'), 'http');
        $this->assertEqual($url->getHost('localhost'), 'localhost');
        $this->assertEqual($url->getPath(), '');
    }
    
    function testBasicParsing() {
        $url = new SimpleUrl('https://www.lastcraft.com/test/');
        $this->assertEqual($url->getScheme(), 'https');
        $this->assertEqual($url->getHost(), 'www.lastcraft.com');
        $this->assertEqual($url->getPath(), '/test/');
    }
    
    function testRelativeUrls() {
        $url = new SimpleUrl('../somewhere.php');
        $this->assertEqual($url->getScheme(), false);
        $this->assertEqual($url->getHost(), false);
        $this->assertEqual($url->getPath(), '../somewhere.php');
    }
    
    function testParseBareParameter() {
        $url = new SimpleUrl('?a');
        $this->assertEqual($url->getPath(), '');
        $this->assertEqual($url->getEncodedRequest(), '?a');
        $url->addRequestParameter('x', 'X');
        $this->assertEqual($url->getEncodedRequest(), '?a=&x=X');
    }
    
    function testParseEmptyParameter() {
        $url = new SimpleUrl('?a=');
        $this->assertEqual($url->getPath(), '');
        $this->assertEqual($url->getEncodedRequest(), '?a=');
        $url->addRequestParameter('x', 'X');
        $this->assertEqual($url->getEncodedRequest(), '?a=&x=X');
    }
    
    function testParseParameterPair() {
        $url = new SimpleUrl('?a=A');
        $this->assertEqual($url->getPath(), '');
        $this->assertEqual($url->getEncodedRequest(), '?a=A');
        $url->addRequestParameter('x', 'X');
        $this->assertEqual($url->getEncodedRequest(), '?a=A&x=X');
    }
    
    function testParseMultipleParameters() {
        $url = new SimpleUrl('?a=A&b=B');
        $this->assertEqual($url->getEncodedRequest(), '?a=A&b=B');
        $url->addRequestParameter('x', 'X');
        $this->assertEqual($url->getEncodedRequest(), '?a=A&b=B&x=X');
    }
    
    function testParsingParameterMixture() {
        $url = new SimpleUrl('?a=A&b=&c');
        $this->assertEqual($url->getEncodedRequest(), '?a=A&b=&c');
        $url->addRequestParameter('x', 'X');
        $this->assertEqual($url->getEncodedRequest(), '?a=A&b=&c=&x=X');
    }
    
    function testAddParametersFromScratch() {
        $url = new SimpleUrl('');
        $url->addRequestParameter('a', 'A');
        $this->assertEqual($url->getEncodedRequest(), '?a=A');
        $url->addRequestParameter('b', 'B');
        $this->assertEqual($url->getEncodedRequest(), '?a=A&b=B');
        $url->addRequestParameter('a', 'aaa');
        $this->assertEqual($url->getEncodedRequest(), '?a=A&b=B&a=aaa');
    }
    
    function testClearingParameters() {
        $url = new SimpleUrl('');
        $url->addRequestParameter('a', 'A');
        $url->clearRequest();
        $this->assertIdentical($url->getEncodedRequest(), '');
    }
    
    function testEncodingParameters() {
        $url = new SimpleUrl('');
        $url->addRequestParameter('a', '?!"\'#~@[]{}:;<>,./|�$%^&*()_+-=');
        $this->assertIdentical(
                $request = $url->getEncodedRequest(),
                '?a=%3F%21%22%27%23%7E%40%5B%5D%7B%7D%3A%3B%3C%3E%2C.%2F%7C%A3%24%25%5E%26%2A%28%29_%2B-%3D');
    }
    
    function testDecodingParameters() {            
        $url = new SimpleUrl('?a=%3F%21%22%27%23%7E%40%5B%5D%7B%7D%3A%3B%3C%3E%2C.%2F%7C%A3%24%25%5E%26%2A%28%29_%2B-%3D');
        $this->assertEqual(
                $url->getEncodedRequest(),
                '?a=' . urlencode('?!"\'#~@[]{}:;<>,./|�$%^&*()_+-='));
    }
    
    function testUrlInQueryDoesNotConfuseParsing() {
        $url = new SimpleUrl('wibble/login.php?url=http://www.google.com/moo/');
        $this->assertFalse($url->getScheme());
        $this->assertFalse($url->getHost());
        $this->assertEqual($url->getPath(), 'wibble/login.php');
        $this->assertEqual($url->getEncodedRequest(), '?url=http://www.google.com/moo/');
    }
    
    function testSettingCordinates() {
        $url = new SimpleUrl('');
        $url->setCoordinates('32', '45');
        $this->assertIdentical($url->getX(), 32);
        $this->assertIdentical($url->getY(), 45);
        $this->assertEqual($url->getEncodedRequest(), '');
    }
    
    function testParseCordinates() {
        $url = new SimpleUrl('?32,45');
        $this->assertIdentical($url->getX(), 32);
        $this->assertIdentical($url->getY(), 45);
    }
    
    function testClearingCordinates() {
        $url = new SimpleUrl('?32,45');
        $url->setCoordinates();
        $this->assertIdentical($url->getX(), false);
        $this->assertIdentical($url->getY(), false);
    }
    
    function testParsingParameterCordinateMixture() {
        $url = new SimpleUrl('?a=A&b=&c?32,45');
        $this->assertIdentical($url->getX(), 32);
        $this->assertIdentical($url->getY(), 45);
        $this->assertEqual($url->getEncodedRequest(), '?a=A&b=&c');
    }
    
    function testParsingParameterWithBadCordinates() {
        $url = new SimpleUrl('?a=A&b=&c?32');
        $this->assertIdentical($url->getX(), false);
        $this->assertIdentical($url->getY(), false);
        $this->assertEqual($url->getEncodedRequest(), '?a=A&b=&c?32');
    }
    
    function testPageSplitting() {
        $url = new SimpleUrl('./here/../there/somewhere.php');
        $this->assertEqual($url->getPath(), './here/../there/somewhere.php');
        $this->assertEqual($url->getPage(), 'somewhere.php');
        $this->assertEqual($url->getBasePath(), './here/../there/');
    }
    
    function testAbsolutePathPageSplitting() {
        $url = new SimpleUrl("http://host.com/here/there/somewhere.php");
        $this->assertEqual($url->getPath(), "/here/there/somewhere.php");
        $this->assertEqual($url->getPage(), "somewhere.php");
        $this->assertEqual($url->getBasePath(), "/here/there/");
    }
    
    function testSplittingUrlWithNoPageGivesEmptyPage() {
        $url = new SimpleUrl('/here/there/');
        $this->assertEqual($url->getPath(), '/here/there/');
        $this->assertEqual($url->getPage(), '');
        $this->assertEqual($url->getBasePath(), '/here/there/');
    }
    
    function testPathNormalisation() {
        $this->assertEqual(
                SimpleUrl::normalisePath('https://host.com/I/am/here/../there/somewhere.php'),
                'https://host.com/I/am/there/somewhere.php');
    }

    // regression test for #1535407
    function testPathNormalisationWithSinglePeriod() {
        $this->assertEqual(
            SimpleUrl::normalisePath('https://host.com/I/am/here/./../there/somewhere.php'),
            'https://host.com/I/am/there/somewhere.php');
    }
    
    // regression test for #1852413
    function testHostnameExtractedFromUContainingAtSign() {
        $url = new SimpleUrl("http://localhost/name@example.com");
        $this->assertEqual($url->getScheme(), "http");
        $this->assertEqual($url->getUsername(), "");
        $this->assertEqual($url->getPassword(), "");
        $this->assertEqual($url->getHost(), "localhost");
        $this->assertEqual($url->getPath(), "/name@example.com");
    }

    function testHostnameInLocalhost() {
        $url = new SimpleUrl("http://localhost/name/example.com");
        $this->assertEqual($url->getScheme(), "http");
        $this->assertEqual($url->getUsername(), "");
        $this->assertEqual($url->getPassword(), "");
        $this->assertEqual($url->getHost(), "localhost");
        $this->assertEqual($url->getPath(), "/name/example.com");
    }

    function testUsernameAndPasswordAreUrlDecoded() {
        $url = new SimpleUrl('http://' . urlencode('test@test') .
                ':' . urlencode('$!�@*&%') . '@www.lastcraft.com');
        $this->assertEqual($url->getUsername(), 'test@test');
        $this->assertEqual($url->getPassword(), '$!�@*&%');
    }
    
    function testBlitz() {
        $this->assertUrl(
                "https://username:password@www.somewhere.com:243/this/that/here.php?a=1&b=2#anchor",
                array("https", "username", "password", "www.somewhere.com", 243, "/this/that/here.php", "com", "?a=1&b=2", "anchor"),
                array("a" => "1", "b" => "2"));
        $this->assertUrl(
                "username:password@www.somewhere.com/this/that/here.php?a=1",
                array(false, "username", "password", "www.somewhere.com", false, "/this/that/here.php", "com", "?a=1", false),
                array("a" => "1"));
        $this->assertUrl(
                "username:password@somewhere.com:243?1,2",
                array(false, "username", "password", "somewhere.com", 243, "/", "com", "", false),
                array(),
                array(1, 2));
        $this->assertUrl(
                "https://www.somewhere.com",
                array("https", false, false, "www.somewhere.com", false, "/", "com", "", false));
        $this->assertUrl(
                "username@www.somewhere.com:243#anchor",
                array(false, "username", false, "www.somewhere.com", 243, "/", "com", "", "anchor"));
        $this->assertUrl(
                "/this/that/here.php?a=1&b=2?3,4",
                array(false, false, false, false, false, "/this/that/here.php", false, "?a=1&b=2", false),
                array("a" => "1", "b" => "2"),
                array(3, 4));
        $this->assertUrl(
                "username@/here.php?a=1&b=2",
                array(false, "username", false, false, false, "/here.php", false, "?a=1&b=2", false),
                array("a" => "1", "b" => "2"));
    }
    
    function testAmbiguousHosts() {
        $this->assertUrl(
                "tigger",
                array(false, false, false, false, false, "tigger", false, "", false));
        $this->assertUrl(
                "/tigger",
                array(false, false, false, false, false, "/tigger", false, "", false));
        $this->assertUrl(
                "//tigger",
                array(false, false, false, "tigger", false, "/", false, "", false));
        $this->assertUrl(
                "//tigger/",
                array(false, false, false, "tigger", false, "/", false, "", false));
        $this->assertUrl(
                "tigger.com",
                array(false, false, false, "tigger.com", false, "/", "com", "", false));
        $this->assertUrl(
                "me.net/tigger",
                array(false, false, false, "me.net", false, "/tigger", "net", "", false));
    }
    
    function testAsString() {
        $this->assertPreserved('https://www.here.com');
        $this->assertPreserved('http://me:secret@www.here.com');
        $this->assertPreserved('http://here/there');
        $this->assertPreserved('http://here/there?a=A&b=B');
        $this->assertPreserved('http://here/there?a=1&a=2');
        $this->assertPreserved('http://here/there?a=1&a=2?9,8');
        $this->assertPreserved('http://host?a=1&a=2');
        $this->assertPreserved('http://host#stuff');
        $this->assertPreserved('http://me:secret@www.here.com/a/b/c/here.html?a=A?7,6');
        $this->assertPreserved('http://www.here.com/?a=A__b=B');
    }
    
    function assertUrl($raw, $parts, $params = false, $coords = false) {
        if (! is_array($params)) {
            $params = array();
        }
        $url = new SimpleUrl($raw);
        $this->assertIdentical($url->getScheme(), $parts[0], "[$raw] scheme -> %s");
        $this->assertIdentical($url->getUsername(), $parts[1], "[$raw] username -> %s");
        $this->assertIdentical($url->getPassword(), $parts[2], "[$raw] password -> %s");
        $this->assertIdentical($url->getHost(), $parts[3], "[$raw] host -> %s");
        $this->assertIdentical($url->getPort(), $parts[4], "[$raw] port -> %s");
        $this->assertIdentical($url->getPath(), $parts[5], "[$raw] path -> %s");
        $this->assertIdentical($url->getTld(), $parts[6], "[$raw] tld -> %s");
        $this->assertIdentical($url->getEncodedRequest(), $parts[7], "[$raw] encoded -> %s");
        $this->assertIdentical($url->getFragment(), $parts[8], "[$raw] fragment -> %s");
        if ($coords) {
            $this->assertIdentical($url->getX(), $coords[0], "[$raw] x -> %s");
            $this->assertIdentical($url->getY(), $coords[1], "[$raw] y -> %s");
        }
    }
    
    function testUrlWithTwoSlashesInPath() {
        $url = new SimpleUrl('/article/categoryedit/insert//');
        $this->assertEqual($url->getPath(), '/article/categoryedit/insert//');
    }
    
    function assertPreserved($string) {
        $url = new SimpleUrl($string);
        $this->assertEqual($url->asString(), $string);
    }
}

class TestOfAbsoluteUrls extends UnitTestCase {
    
	function testDirectoriesAfterFilename() {
		$string = '../../index.php/foo/bar';
		$url = new SimpleUrl($string);
		$this->assertEqual($url->asString(), $string);
		
		$absolute = $url->makeAbsolute('http://www.domain.com/some/path/');
		$this->assertEqual($absolute->asString(), 'http://www.domain.com/index.php/foo/bar');
	}

    function testMakingAbsolute() {
        $url = new SimpleUrl('../there/somewhere.php');
        $this->assertEqual($url->getPath(), '../there/somewhere.php');
        $absolute = $url->makeAbsolute('https://host.com:1234/I/am/here/');
        $this->assertEqual($absolute->getScheme(), 'https');
        $this->assertEqual($absolute->getHost(), 'host.com');
        $this->assertEqual($absolute->getPort(), 1234);
        $this->assertEqual($absolute->getPath(), '/I/am/there/somewhere.php');
    }
    
    function testMakingAnEmptyUrlAbsolute() {
        $url = new SimpleUrl('');
        $this->assertEqual($url->getPath(), '');
        $absolute = $url->makeAbsolute('http://host.com/I/am/here/page.html');
        $this->assertEqual($absolute->getScheme(), 'http');
        $this->assertEqual($absolute->getHost(), 'host.com');
        $this->assertEqual($absolute->getPath(), '/I/am/here/page.html');
    }
    
    function testMakingAnEmptyUrlAbsoluteWithMissingPageName() {
        $url = new SimpleUrl('');
        $this->assertEqual($url->getPath(), '');
        $absolute = $url->makeAbsolute('http://host.com/I/am/here/');
        $this->assertEqual($absolute->getScheme(), 'http');
        $this->assertEqual($absolute->getHost(), 'host.com');
        $this->assertEqual($absolute->getPath(), '/I/am/here/');
    }
    
    function testMakingAShortQueryUrlAbsolute() {
        $url = new SimpleUrl('?a#b');
        $this->assertEqual($url->getPath(), '');
        $absolute = $url->makeAbsolute('http://host.com/I/am/here/');
        $this->assertEqual($absolute->getScheme(), 'http');
        $this->assertEqual($absolute->getHost(), 'host.com');
        $this->assertEqual($absolute->getPath(), '/I/am/here/');
        $this->assertEqual($absolute->getEncodedRequest(), '?a');
        $this->assertEqual($absolute->getFragment(), 'b');
    }
    
    function testMakingADirectoryUrlAbsolute() {
        $url = new SimpleUrl('hello/');
        $this->assertEqual($url->getPath(), 'hello/');
        $this->assertEqual($url->getBasePath(), 'hello/');
        $this->assertEqual($url->getPage(), '');
        $absolute = $url->makeAbsolute('http://host.com/I/am/here/page.html');
        $this->assertEqual($absolute->getPath(), '/I/am/here/hello/');
    }
    
    function testMakingARootUrlAbsolute() {
        $url = new SimpleUrl('/');
        $this->assertEqual($url->getPath(), '/');
        $absolute = $url->makeAbsolute('http://host.com/I/am/here/page.html');
        $this->assertEqual($absolute->getPath(), '/');
    }
    
    function testMakingARootPageUrlAbsolute() {
        $url = new SimpleUrl('/here.html');
        $absolute = $url->makeAbsolute('http://host.com/I/am/here/page.html');
        $this->assertEqual($absolute->getPath(), '/here.html');
    }
    
    function testCarryAuthenticationFromRootPage() {
        $url = new SimpleUrl('here.html');
        $absolute = $url->makeAbsolute('http://test:secret@host.com/');
        $this->assertEqual($absolute->getPath(), '/here.html');
        $this->assertEqual($absolute->getUsername(), 'test');
        $this->assertEqual($absolute->getPassword(), 'secret');
    }
    
    function testMakingCoordinateUrlAbsolute() {
        $url = new SimpleUrl('?1,2');
        $this->assertEqual($url->getPath(), '');
        $absolute = $url->makeAbsolute('http://host.com/I/am/here/');
        $this->assertEqual($absolute->getScheme(), 'http');
        $this->assertEqual($absolute->getHost(), 'host.com');
        $this->assertEqual($absolute->getPath(), '/I/am/here/');
        $this->assertEqual($absolute->getX(), 1);
        $this->assertEqual($absolute->getY(), 2);
    }
    
    function testMakingAbsoluteAppendedPath() {
        $url = new SimpleUrl('./there/somewhere.php');
        $absolute = $url->makeAbsolute('https://host.com/here/');
        $this->assertEqual($absolute->getPath(), '/here/there/somewhere.php');
    }
    
    function testMakingAbsoluteBadlyFormedAppendedPath() {
        $url = new SimpleUrl('there/somewhere.php');
        $absolute = $url->makeAbsolute('https://host.com/here/');
        $this->assertEqual($absolute->getPath(), '/here/there/somewhere.php');
    }
    
    function testMakingAbsoluteHasNoEffectWhenAlreadyAbsolute() {
        $url = new SimpleUrl('https://test:secret@www.lastcraft.com:321/stuff/?a=1#f');
        $absolute = $url->makeAbsolute('http://host.com/here/');
        $this->assertEqual($absolute->getScheme(), 'https');
        $this->assertEqual($absolute->getUsername(), 'test');
        $this->assertEqual($absolute->getPassword(), 'secret');
        $this->assertEqual($absolute->getHost(), 'www.lastcraft.com');
        $this->assertEqual($absolute->getPort(), 321);
        $this->assertEqual($absolute->getPath(), '/stuff/');
        $this->assertEqual($absolute->getEncodedRequest(), '?a=1');
        $this->assertEqual($absolute->getFragment(), 'f');
    }
    
    function testMakingAbsoluteCarriesAuthenticationWhenAlreadyAbsolute() {
        $url = new SimpleUrl('https://www.lastcraft.com');
        $absolute = $url->makeAbsolute('http://test:secret@host.com/here/');
        $this->assertEqual($absolute->getHost(), 'www.lastcraft.com');
        $this->assertEqual($absolute->getUsername(), 'test');
        $this->assertEqual($absolute->getPassword(), 'secret');
    }
    
    function testMakingHostOnlyAbsoluteDoesNotCarryAnyOtherInformation() {
        $url = new SimpleUrl('http://www.lastcraft.com');
        $absolute = $url->makeAbsolute('https://host.com:81/here/');
        $this->assertEqual($absolute->getScheme(), 'http');
        $this->assertEqual($absolute->getHost(), 'www.lastcraft.com');
        $this->assertIdentical($absolute->getPort(), false);
        $this->assertEqual($absolute->getPath(), '/');
    }
}

class TestOfFrameUrl extends UnitTestCase {
    
    function testTargetAttachment() {
        $url = new SimpleUrl('http://www.site.com/home.html');
        $this->assertIdentical($url->getTarget(), false);
        $url->setTarget('A frame');
        $this->assertIdentical($url->getTarget(), 'A frame');
    }
}
?>