-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
UriTest.php
648 lines (530 loc) · 18.7 KB
/
UriTest.php
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<?php
/**
* Slim Framework (http://slimframework.com)
*
* @link https://github.com/slimphp/Slim
* @copyright Copyright (c) 2011-2016 Josh Lockhart
* @license https://github.com/slimphp/Slim/blob/master/LICENSE.md (MIT License)
*/
namespace Slim\Tests\Http;
use InvalidArgumentException;
use Slim\Http\Environment;
use Slim\Http\Uri;
class UriTest extends \PHPUnit_Framework_TestCase
{
/**
* @var resource
*/
protected $uri;
public function uriFactory()
{
$scheme = 'https';
$host = 'example.com';
$port = 443;
$path = '/foo/bar';
$query = 'abc=123';
$fragment = 'section3';
$user = 'josh';
$password = 'sekrit';
return new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password);
}
/********************************************************************************
* Scheme
*******************************************************************************/
public function testGetScheme()
{
$this->assertEquals('https', $this->uriFactory()->getScheme());
}
public function testWithScheme()
{
$uri = $this->uriFactory()->withScheme('http');
$this->assertAttributeEquals('http', 'scheme', $uri);
}
public function testWithSchemeRemovesSuffix()
{
$uri = $this->uriFactory()->withScheme('http://');
$this->assertAttributeEquals('http', 'scheme', $uri);
}
public function testWithSchemeEmpty()
{
$uri = $this->uriFactory()->withScheme('');
$this->assertAttributeEquals('', 'scheme', $uri);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Uri scheme must be one of: "", "https", "http"
*/
public function testWithSchemeInvalid()
{
$this->uriFactory()->withScheme('ftp');
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Uri scheme must be a string
*/
public function testWithSchemeInvalidType()
{
$this->uriFactory()->withScheme([]);
}
/********************************************************************************
* Authority
*******************************************************************************/
public function testGetAuthorityWithUsernameAndPassword()
{
$this->assertEquals('josh:sekrit@example.com', $this->uriFactory()->getAuthority());
}
public function testGetAuthorityWithUsername()
{
$scheme = 'https';
$user = 'josh';
$password = '';
$host = 'example.com';
$path = '/foo/bar';
$port = 443;
$query = 'abc=123';
$fragment = 'section3';
$uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password);
$this->assertEquals('josh@example.com', $uri->getAuthority());
}
public function testGetAuthority()
{
$scheme = 'https';
$user = '';
$password = '';
$host = 'example.com';
$path = '/foo/bar';
$port = 443;
$query = 'abc=123';
$fragment = 'section3';
$uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password);
$this->assertEquals('example.com', $uri->getAuthority());
}
public function testGetAuthorityWithNonStandardPort()
{
$scheme = 'https';
$user = '';
$password = '';
$host = 'example.com';
$path = '/foo/bar';
$port = 400;
$query = 'abc=123';
$fragment = 'section3';
$uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password);
$this->assertEquals('example.com:400', $uri->getAuthority());
}
public function testGetUserInfoWithUsernameAndPassword()
{
$scheme = 'https';
$user = 'josh';
$password = 'sekrit';
$host = 'example.com';
$path = '/foo/bar';
$port = 443;
$query = 'abc=123';
$fragment = 'section3';
$uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password);
$this->assertEquals('josh:sekrit', $uri->getUserInfo());
}
public function testGetUserInfoWithUsername()
{
$scheme = 'https';
$user = 'josh';
$password = '';
$host = 'example.com';
$path = '/foo/bar';
$port = 443;
$query = 'abc=123';
$fragment = 'section3';
$uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password);
$this->assertEquals('josh', $uri->getUserInfo());
}
public function testGetUserInfoNone()
{
$scheme = 'https';
$user = '';
$password = '';
$host = 'example.com';
$path = '/foo/bar';
$port = 443;
$query = 'abc=123';
$fragment = 'section3';
$uri = new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password);
$this->assertEquals('', $uri->getUserInfo());
}
public function testWithUserInfo()
{
$uri = $this->uriFactory()->withUserInfo('bob', 'pass');
$this->assertAttributeEquals('bob', 'user', $uri);
$this->assertAttributeEquals('pass', 'password', $uri);
}
public function testWithUserInfoRemovesPassword()
{
$uri = $this->uriFactory()->withUserInfo('bob');
$this->assertAttributeEquals('bob', 'user', $uri);
$this->assertAttributeEquals('', 'password', $uri);
}
public function testGetHost()
{
$this->assertEquals('example.com', $this->uriFactory()->getHost());
}
public function testWithHost()
{
$uri = $this->uriFactory()->withHost('slimframework.com');
$this->assertAttributeEquals('slimframework.com', 'host', $uri);
}
public function testGetPortWithSchemeAndNonDefaultPort()
{
$uri = new Uri('https', 'www.example.com', 4000);
$this->assertEquals(4000, $uri->getPort());
}
public function testGetPortWithSchemeAndDefaultPort()
{
$uriHppt = new Uri('http', 'www.example.com', 80);
$uriHppts = new Uri('https', 'www.example.com', 443);
$this->assertNull($uriHppt->getPort());
$this->assertNull($uriHppts->getPort());
}
public function testGetPortWithoutSchemeAndPort()
{
$uri = new Uri('', 'www.example.com');
$this->assertNull($uri->getPort());
}
public function testGetPortWithSchemeWithoutPort()
{
$uri = new Uri('http', 'www.example.com');
$this->assertNull($uri->getPort());
}
public function testWithPort()
{
$uri = $this->uriFactory()->withPort(8000);
$this->assertAttributeEquals(8000, 'port', $uri);
}
public function testWithPortNull()
{
$uri = $this->uriFactory()->withPort(null);
$this->assertAttributeEquals(null, 'port', $uri);
}
/**
* @expectedException InvalidArgumentException
*/
public function testWithPortInvalidInt()
{
$this->uriFactory()->withPort(70000);
}
/**
* @expectedException InvalidArgumentException
*/
public function testWithPortInvalidString()
{
$this->uriFactory()->withPort('Foo');
}
/********************************************************************************
* Path
*******************************************************************************/
public function testGetBasePathNone()
{
$this->assertEquals('', $this->uriFactory()->getBasePath());
}
public function testWithBasePath()
{
$uri = $this->uriFactory()->withBasePath('/base');
$this->assertAttributeEquals('/base', 'basePath', $uri);
}
/**
* @covers Slim\Http\Uri::withBasePath
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Uri path must be a string
*/
public function testWithBasePathInvalidType()
{
$this->uriFactory()->withBasePath(['foo']);
}
public function testWithBasePathAddsPrefix()
{
$uri = $this->uriFactory()->withBasePath('base');
$this->assertAttributeEquals('/base', 'basePath', $uri);
}
public function testWithBasePathIgnoresSlash()
{
$uri = $this->uriFactory()->withBasePath('/');
$this->assertAttributeEquals('', 'basePath', $uri);
}
public function testGetPath()
{
$this->assertEquals('/foo/bar', $this->uriFactory()->getPath());
}
public function testWithPath()
{
$uri = $this->uriFactory()->withPath('/new');
$this->assertAttributeEquals('/new', 'path', $uri);
}
public function testWithPathWithoutPrefix()
{
$uri = $this->uriFactory()->withPath('new');
$this->assertAttributeEquals('new', 'path', $uri);
}
public function testWithPathEmptyValue()
{
$uri = $this->uriFactory()->withPath('');
$this->assertAttributeEquals('', 'path', $uri);
}
public function testWithPathUrlEncodesInput()
{
$uri = $this->uriFactory()->withPath('/includes?/new');
$this->assertAttributeEquals('/includes%3F/new', 'path', $uri);
}
public function testWithPathDoesNotDoubleEncodeInput()
{
$uri = $this->uriFactory()->withPath('/include%25s/new');
$this->assertAttributeEquals('/include%25s/new', 'path', $uri);
}
/**
* @covers Slim\Http\Uri::withPath
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Uri path must be a string
*/
public function testWithPathInvalidType()
{
$this->uriFactory()->withPath(['foo']);
}
/********************************************************************************
* Query
*******************************************************************************/
public function testGetQuery()
{
$this->assertEquals('abc=123', $this->uriFactory()->getQuery());
}
public function testWithQuery()
{
$uri = $this->uriFactory()->withQuery('xyz=123');
$this->assertAttributeEquals('xyz=123', 'query', $uri);
}
public function testWithQueryRemovesPrefix()
{
$uri = $this->uriFactory()->withQuery('?xyz=123');
$this->assertAttributeEquals('xyz=123', 'query', $uri);
}
public function testWithQueryEmpty()
{
$uri = $this->uriFactory()->withQuery('');
$this->assertAttributeEquals('', 'query', $uri);
}
/**
* @covers Slim\Http\Uri::withQuery
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Uri query must be a string
*/
public function testWithQueryInvalidType()
{
$this->uriFactory()->withQuery(['foo']);
}
/********************************************************************************
* Fragment
*******************************************************************************/
public function testGetFragment()
{
$this->assertEquals('section3', $this->uriFactory()->getFragment());
}
public function testWithFragment()
{
$uri = $this->uriFactory()->withFragment('other-fragment');
$this->assertAttributeEquals('other-fragment', 'fragment', $uri);
}
public function testWithFragmentRemovesPrefix()
{
$uri = $this->uriFactory()->withFragment('#other-fragment');
$this->assertAttributeEquals('other-fragment', 'fragment', $uri);
}
public function testWithFragmentEmpty()
{
$uri = $this->uriFactory()->withFragment('');
$this->assertAttributeEquals('', 'fragment', $uri);
}
/**
* @covers Slim\Http\Uri::withFragment
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Uri fragment must be a string
*/
public function testWithFragmentInvalidType()
{
$this->uriFactory()->withFragment(['foo']);
}
/********************************************************************************
* Helpers
*******************************************************************************/
public function testToString()
{
$uri = $this->uriFactory();
$this->assertEquals('https://josh:sekrit@example.com/foo/bar?abc=123#section3', (string) $uri);
$uri = $uri->withPath('bar');
$this->assertEquals('https://josh:sekrit@example.com/bar?abc=123#section3', (string) $uri);
$uri = $uri->withBasePath('foo/');
$this->assertEquals('https://josh:sekrit@example.com/foo/bar?abc=123#section3', (string) $uri);
$uri = $uri->withPath('/bar');
$this->assertEquals('https://josh:sekrit@example.com/bar?abc=123#section3', (string) $uri);
// ensure that a Uri with just a base path correctly converts to a string
// (This occurs via createFromEnvironment when index.php is in a subdirectory)
$environment = Environment::mock([
'SCRIPT_NAME' => '/foo/index.php',
'REQUEST_URI' => '/foo/',
'HTTP_HOST' => 'example.com',
]);
$uri = Uri::createFromEnvironment($environment);
$this->assertEquals('http://example.com/foo/', (string) $uri);
}
/**
* @covers Slim\Http\Uri::createFromString
*/
public function testCreateFromString()
{
$uri = Uri::createFromString('https://example.com:8080/foo/bar?abc=123');
$this->assertEquals('https', $uri->getScheme());
$this->assertEquals('example.com', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
}
/**
* @covers Slim\Http\Uri::createFromString
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Uri must be a string
*/
public function testCreateFromStringWithInvalidType()
{
Uri::createFromString(['https://example.com:8080/foo/bar?abc=123']);
}
public function testCreateEnvironment()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'PHP_AUTH_USER' => 'josh',
'PHP_AUTH_PW' => 'sekrit',
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => 'example.com:8080',
'SERVER_PORT' => 8080,
]);
$uri = Uri::createFromEnvironment($environment);
$this->assertEquals('josh:sekrit', $uri->getUserInfo());
$this->assertEquals('example.com', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());
}
public function testCreateEnvironmentWithIPv6Host()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'PHP_AUTH_USER' => 'josh',
'PHP_AUTH_PW' => 'sekrit',
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => '[2001:db8::1]:8080',
'REMOTE_ADDR' => '2001:db8::1',
'SERVER_PORT' => 8080,
]);
$uri = Uri::createFromEnvironment($environment);
$this->assertEquals('josh:sekrit', $uri->getUserInfo());
$this->assertEquals('[2001:db8::1]', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());
}
/**
* @covers Slim\Http\Uri::createFromEnvironment
* @ticket 1375
*/
public function testCreateEnvironmentWithBasePath()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/foo/index.php',
'REQUEST_URI' => '/foo/bar',
]);
$uri = Uri::createFromEnvironment($environment);
$this->assertEquals('/foo', $uri->getBasePath());
$this->assertEquals('bar', $uri->getPath());
$this->assertEquals('http://localhost/foo/bar', (string) $uri);
}
public function testGetBaseUrl()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/foo/index.php',
'REQUEST_URI' => '/foo/bar',
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => 'example.com:80',
'SERVER_PORT' => 80
]);
$uri = Uri::createFromEnvironment($environment);
$this->assertEquals('http://example.com/foo', $uri->getBaseUrl());
}
public function testGetBaseUrlWithNoBasePath()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => 'example.com:80',
'SERVER_PORT' => 80
]);
$uri = Uri::createFromEnvironment($environment);
$this->assertEquals('http://example.com', $uri->getBaseUrl());
}
public function testGetBaseUrlWithAuthority()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/foo/index.php',
'REQUEST_URI' => '/foo/bar',
'PHP_AUTH_USER' => 'josh',
'PHP_AUTH_PW' => 'sekrit',
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => 'example.com:8080',
'SERVER_PORT' => 8080
]);
$uri = Uri::createFromEnvironment($environment);
$this->assertEquals('http://josh:sekrit@example.com:8080/foo', $uri->getBaseUrl());
}
/**
* @covers Slim\Http\Uri::createFromEnvironment
* @ticket 1380
*/
public function testWithPathWhenBaseRootIsEmpty()
{
$environment = \Slim\Http\Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/bar',
]);
$uri = \Slim\Http\Uri::createFromEnvironment($environment);
$this->assertEquals('http://localhost/test', (string) $uri->withPath('test'));
}
/**
* When the URL is /foo/index.php/bar/baz, we need the baseURL to be
* /foo/index.php so that routing works correctly.
*
* @ticket 1639 as a fix to 1590 broke this.
*/
public function testRequestURIContainsIndexDotPhp()
{
$uri = Uri::createFromEnvironment(
Environment::mock(
[
'SCRIPT_NAME' => '/foo/index.php',
'REQUEST_URI' => '/foo/index.php/bar/baz',
]
)
);
$this->assertSame('/foo/index.php', $uri->getBasePath());
}
public function testRequestURICanContainParams()
{
$uri = Uri::createFromEnvironment(
Environment::mock(
[
'REQUEST_URI' => '/foo?abc=123',
]
)
);
$this->assertEquals('abc=123', $uri->getQuery());
}
}