forked from laminas/laminas-diactoros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUriTest.php
645 lines (559 loc) · 22.2 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
<?php
declare(strict_types=1);
namespace LaminasTest\Diactoros;
use InvalidArgumentException;
use Laminas\Diactoros\Uri;
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use function sprintf;
class UriTest extends TestCase
{
public function testConstructorSetsAllProperties(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$this->assertSame('https', $uri->getScheme());
$this->assertSame('user:pass', $uri->getUserInfo());
$this->assertSame('local.example.com', $uri->getHost());
$this->assertSame(3001, $uri->getPort());
$this->assertSame('user:pass@local.example.com:3001', $uri->getAuthority());
$this->assertSame('/foo', $uri->getPath());
$this->assertSame('bar=baz', $uri->getQuery());
$this->assertSame('quz', $uri->getFragment());
}
public function testCanSerializeToString(): void
{
$url = 'https://user:pass@local.example.com:3001/foo?bar=baz#quz';
$uri = new Uri($url);
$this->assertSame($url, (string) $uri);
}
public function testWithSchemeReturnsNewInstanceWithNewScheme(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withScheme('http');
$this->assertNotSame($uri, $new);
$this->assertSame('http', $new->getScheme());
$this->assertSame('http://user:pass@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
public function testWithSchemeReturnsSameInstanceWithSameScheme(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withScheme('https');
$this->assertSame($uri, $new);
$this->assertSame('https', $new->getScheme());
$this->assertSame('https://user:pass@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
public function testWithUserInfoReturnsNewInstanceWithProvidedUser(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withUserInfo('matthew');
$this->assertNotSame($uri, $new);
$this->assertSame('matthew', $new->getUserInfo());
$this->assertSame('https://matthew@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
public function testWithUserInfoReturnsNewInstanceWithProvidedUserAndPassword(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withUserInfo('matthew', 'laminas');
$this->assertNotSame($uri, $new);
$this->assertSame('matthew:laminas', $new->getUserInfo());
$this->assertSame('https://matthew:laminas@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
public function testWithUserInfoReturnsSameInstanceIfUserAndPasswordAreSameAsBefore(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withUserInfo('user', 'pass');
$this->assertSame($uri, $new);
$this->assertSame('user:pass', $new->getUserInfo());
$this->assertSame('https://user:pass@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
/** @return non-empty-array<non-empty-string, array{non-empty-string, non-empty-string, non-empty-string}> */
public function userInfoProvider(): array
{
// @codingStandardsIgnoreStart
return [
// name => [ user, credential, expected ]
'valid-chars' => ['foo', 'bar', 'foo:bar'],
'colon' => ['foo:bar', 'baz:bat', 'foo%3Abar:baz%3Abat'],
'at' => ['user@example.com', 'cred@foo', 'user%40example.com:cred%40foo'],
'percent' => ['%25', '%25', '%25:%25'],
'invalid-enc' => ['%ZZ', '%GG', '%25ZZ:%25GG'],
'invalid-utf' => ["\x21\x92", '!?', '!%92:!%3F'],
];
// @codingStandardsIgnoreEnd
}
/**
* @dataProvider userInfoProvider
* @param non-empty-string $user
* @param non-empty-string $credential
* @param non-empty-string $expected
*/
public function testWithUserInfoEncodesUsernameAndPassword(string $user, string $credential, string $expected): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withUserInfo($user, $credential);
$this->assertSame($expected, $new->getUserInfo());
}
public function testWithHostReturnsNewInstanceWithProvidedHost(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withHost('getlaminas.org');
$this->assertNotSame($uri, $new);
$this->assertSame('getlaminas.org', $new->getHost());
$this->assertSame('https://user:pass@getlaminas.org:3001/foo?bar=baz#quz', (string) $new);
}
public function testWithHostReturnsSameInstanceWithProvidedHostIsSameAsBefore(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withHost('local.example.com');
$this->assertSame($uri, $new);
$this->assertSame('local.example.com', $new->getHost());
$this->assertSame('https://user:pass@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
/** @return non-empty-array<non-empty-string, array{null|positive-int|numeric-string}> */
public function validPorts(): array
{
return [
'null' => [null],
'int' => [3000],
];
}
/**
* @dataProvider validPorts
* @param null|positive-int|numeric-string $port
*/
public function testWithPortReturnsNewInstanceWithProvidedPort($port): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
/** @psalm-suppress PossiblyInvalidArgument */
$new = $uri->withPort($port);
$this->assertNotSame($uri, $new);
$this->assertEquals($port, $new->getPort());
$this->assertSame(
sprintf('https://user:pass@local.example.com%s/foo?bar=baz#quz', $port === null ? '' : ':' . $port),
(string) $new
);
}
public function testWithPortReturnsSameInstanceWithProvidedPortIsSameAsBefore(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withPort(3001);
$this->assertSame($uri, $new);
$this->assertSame(3001, $new->getPort());
}
/** @return non-empty-array<non-empty-string, array{mixed}> */
public function invalidPorts(): array
{
return [
'zero' => [0],
'too-small' => [-1],
'too-big' => [65536],
];
}
/**
* @dataProvider invalidPorts
*/
public function testWithPortRaisesExceptionForInvalidPorts(mixed $port): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid port');
/** @psalm-suppress MixedArgument */
$uri->withPort($port);
}
public function testWithPathReturnsNewInstanceWithProvidedPath(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withPath('/bar/baz');
$this->assertNotSame($uri, $new);
$this->assertSame('/bar/baz', $new->getPath());
$this->assertSame('https://user:pass@local.example.com:3001/bar/baz?bar=baz#quz', (string) $new);
}
public function testWithPathReturnsSameInstanceWithProvidedPathSameAsBefore(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withPath('/foo');
$this->assertSame($uri, $new);
$this->assertSame('/foo', $new->getPath());
$this->assertSame('https://user:pass@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
/** @return non-empty-array<non-empty-string, array{mixed}> */
public function invalidPaths(): array
{
return [
'query' => ['/bar/baz?bat=quz'],
'fragment' => ['/bar/baz#bat'],
];
}
/**
* @dataProvider invalidPaths
*/
public function testWithPathRaisesExceptionForInvalidPaths(mixed $path): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid path');
/** @psalm-suppress MixedArgument */
$uri->withPath($path);
}
public function testWithQueryReturnsNewInstanceWithProvidedQuery(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withQuery('baz=bat');
$this->assertNotSame($uri, $new);
$this->assertSame('baz=bat', $new->getQuery());
$this->assertSame('https://user:pass@local.example.com:3001/foo?baz=bat#quz', (string) $new);
}
/** @return non-empty-array<non-empty-string, array{mixed}> */
public function invalidQueryStrings(): array
{
return [
'fragment' => ['baz=bat#quz'],
];
}
/**
* @dataProvider invalidQueryStrings
*/
public function testWithQueryRaisesExceptionForInvalidQueryStrings(mixed $query): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Query string');
/** @psalm-suppress MixedArgument */
$uri->withQuery($query);
}
public function testWithFragmentReturnsNewInstanceWithProvidedFragment(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withFragment('qat');
$this->assertNotSame($uri, $new);
$this->assertSame('qat', $new->getFragment());
$this->assertSame('https://user:pass@local.example.com:3001/foo?bar=baz#qat', (string) $new);
}
public function testWithFragmentReturnsSameInstanceWithProvidedFragmentSameAsBefore(): void
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withFragment('quz');
$this->assertSame($uri, $new);
$this->assertSame('quz', $new->getFragment());
$this->assertSame('https://user:pass@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
/** @return non-empty-array<non-empty-string, array{non-empty-string, non-empty-string}> */
public function authorityInfo(): array
{
return [
'host-only' => ['http://foo.com/bar', 'foo.com'],
'host-port' => ['http://foo.com:3000/bar', 'foo.com:3000'],
'user-host' => ['http://me@foo.com/bar', 'me@foo.com'],
'user-host-port' => ['http://me@foo.com:3000/bar', 'me@foo.com:3000'],
];
}
/**
* @dataProvider authorityInfo
* @param non-empty-string $url
* @param non-empty-string $expected
*/
public function testRetrievingAuthorityReturnsExpectedValues(string $url, string $expected): void
{
$uri = new Uri($url);
$this->assertSame($expected, $uri->getAuthority());
}
public function testCanEmitOriginFormUrl(): void
{
$url = '/foo/bar?baz=bat';
$uri = new Uri($url);
$this->assertSame($url, (string) $uri);
}
public function testSettingEmptyPathOnAbsoluteUriReturnsAnEmptyPath(): void
{
$uri = new Uri('http://example.com/foo');
$new = $uri->withPath('');
$this->assertSame('', $new->getPath());
}
public function testStringRepresentationOfAbsoluteUriWithNoPathSetsAnEmptyPath(): void
{
$uri = new Uri('http://example.com');
$this->assertSame('http://example.com', (string) $uri);
}
public function testEmptyPathOnOriginFormRemainsAnEmptyPath(): void
{
$uri = new Uri('?foo=bar');
$this->assertSame('', $uri->getPath());
}
public function testStringRepresentationOfOriginFormWithNoPathRetainsEmptyPath(): void
{
$uri = new Uri('?foo=bar');
$this->assertSame('?foo=bar', (string) $uri);
}
public function testConstructorRaisesExceptionForSeriouslyMalformedURI(): void
{
$this->expectException(InvalidArgumentException::class);
new Uri('http:///www.php-fig.org/');
}
public function testMutatingSchemeStripsOffDelimiter(): void
{
$uri = new Uri('http://example.com');
$new = $uri->withScheme('https://');
$this->assertSame('https', $new->getScheme());
}
public function testESchemeStripsOffDelimiter(): void
{
$uri = new Uri('https://example.com');
$new = $uri->withScheme('://');
$this->assertSame('', $new->getScheme());
}
/** @return non-empty-array<non-empty-string, array{non-empty-string}> */
public function invalidSchemes(): array
{
return [
'mailto' => ['mailto'],
'ftp' => ['ftp'],
'telnet' => ['telnet'],
'ssh' => ['ssh'],
'git' => ['git'],
];
}
/**
* @dataProvider invalidSchemes
* @param non-empty-string $scheme
*/
public function testConstructWithUnsupportedSchemeRaisesAnException(string $scheme): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unsupported scheme');
new Uri($scheme . '://example.com');
}
/**
* @dataProvider invalidSchemes
* @param non-empty-string $scheme
*/
public function testMutatingWithUnsupportedSchemeRaisesAnException(string $scheme): void
{
$uri = new Uri('http://example.com');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unsupported scheme');
$uri->withScheme($scheme);
}
public function testPathIsNotPrefixedWithSlashIfSetWithoutOne(): void
{
$uri = new Uri('http://example.com');
$new = $uri->withPath('foo/bar');
$this->assertSame('foo/bar', $new->getPath());
}
public function testPathNotSlashPrefixedIsEmittedWithSlashDelimiterWhenUriIsCastToString(): void
{
$uri = new Uri('http://example.com');
$new = $uri->withPath('foo/bar');
$this->assertSame('http://example.com/foo/bar', $new->__toString());
}
public function testStripsQueryPrefixIfPresent(): void
{
$uri = new Uri('http://example.com');
$new = $uri->withQuery('?foo=bar');
$this->assertSame('foo=bar', $new->getQuery());
}
public function testEncodeFragmentPrefixIfPresent(): void
{
$uri = new Uri('http://example.com');
$new = $uri->withFragment('#/foo/bar');
$this->assertSame('%23/foo/bar', $new->getFragment());
}
/** @return non-empty-array<non-empty-string, array{non-empty-string, positive-int}> */
public function standardSchemePortCombinations(): array
{
return [
'http' => ['http', 80],
'https' => ['https', 443],
];
}
/**
* @dataProvider standardSchemePortCombinations
* @param non-empty-string $scheme
* @param positive-int $port
*/
public function testAuthorityOmitsPortForStandardSchemePortCombinations(string $scheme, int $port): void
{
$uri = (new Uri())
->withHost('example.com')
->withScheme($scheme)
->withPort($port);
$this->assertSame('example.com', $uri->getAuthority());
}
/** @return non-empty-array<string, array{'withScheme'|'withUserInfo'|'withHost'|'withPort'|'withPath'|'withQuery'|'withFragment', non-empty-string|positive-int}> */
public function mutations(): array
{
return [
'scheme' => ['withScheme', 'https'],
'user-info' => ['withUserInfo', 'foo'],
'host' => ['withHost', 'www.example.com'],
'port' => ['withPort', 8080],
'path' => ['withPath', '/changed'],
'query' => ['withQuery', 'changed=value'],
'fragment' => ['withFragment', 'changed'],
];
}
/**
* @dataProvider mutations
* @param 'withScheme'|'withUserInfo'|'withHost'|'withPort'|'withPath'|'withQuery'|'withFragment' $method
* @param non-empty-string|positive-int $value
*/
public function testMutationResetsUriStringPropertyInClone(string $method, $value): void
{
$uri = new Uri('http://example.com/path?query=string#fragment');
$string = (string) $uri;
$r = new ReflectionObject($uri);
$p = $r->getProperty('uriString');
$p->setAccessible(true);
$this->assertSame($string, $p->getValue($uri));
$test = $uri->{$method}($value);
$r2 = new ReflectionObject($uri);
$p2 = $r2->getProperty('uriString');
$p2->setAccessible(true);
$this->assertNull($p2->getValue($test));
$this->assertSame($string, $p->getValue($uri));
}
/**
* @group 40
*/
public function testPathIsProperlyEncoded(): void
{
$uri = (new Uri())->withPath('/foo^bar');
$expected = '/foo%5Ebar';
$this->assertSame($expected, $uri->getPath());
}
public function testPathDoesNotBecomeDoubleEncoded(): void
{
$uri = (new Uri())->withPath('/foo%5Ebar');
$expected = '/foo%5Ebar';
$this->assertSame($expected, $uri->getPath());
}
/** @return non-empty-array<non-empty-string, array{non-empty-string, non-empty-string}> */
public function queryStringsForEncoding(): array
{
return [
'key-only' => ['k^ey', 'k%5Eey'],
'key-value' => ['k^ey=valu`', 'k%5Eey=valu%60'],
'array-key-only' => ['key[]', 'key%5B%5D'],
'array-key-value' => ['key[]=valu`', 'key%5B%5D=valu%60'],
'complex' => ['k^ey&key[]=valu`&f<>=`bar', 'k%5Eey&key%5B%5D=valu%60&f%3C%3E=%60bar'],
];
}
/**
* @dataProvider queryStringsForEncoding
* @param non-empty-string $query
* @param non-empty-string $expected
*/
public function testQueryIsProperlyEncoded(string $query, string $expected): void
{
$uri = (new Uri())->withQuery($query);
$this->assertSame($expected, $uri->getQuery());
}
/**
* @dataProvider queryStringsForEncoding
* @param non-empty-string $query
* @param non-empty-string $expected
*/
public function testQueryIsNotDoubleEncoded(string $query, string $expected): void
{
$uri = (new Uri())->withQuery($expected);
$this->assertSame($expected, $uri->getQuery());
}
/**
* @group 40
*/
public function testFragmentIsProperlyEncoded(): void
{
$uri = (new Uri())->withFragment('/p^th?key^=`bar#b@z');
$expected = '/p%5Eth?key%5E=%60bar%23b@z';
$this->assertSame($expected, $uri->getFragment());
}
/**
* @group 40
*/
public function testFragmentIsNotDoubleEncoded(): void
{
$expected = '/p%5Eth?key%5E=%60bar%23b@z';
$uri = (new Uri())->withFragment($expected);
$this->assertSame($expected, $uri->getFragment());
}
public function testUtf8Uri(): void
{
$uri = new Uri('http://ουτοπία.δπθ.gr/');
$this->assertSame('ουτοπία.δπθ.gr', $uri->getHost());
}
/**
* @dataProvider utf8PathsDataProvider
* @param non-empty-string $url
* @param non-empty-string $result
*/
public function testUtf8Path(string $url, string $result): void
{
$uri = new Uri($url);
$this->assertSame($result, $uri->getPath());
}
/** @return non-empty-list<array{non-empty-string, non-empty-string}> */
public function utf8PathsDataProvider(): array
{
return [
['http://example.com/тестовый_путь/', '/тестовый_путь/'],
['http://example.com/ουτοπία/', '/ουτοπία/'],
["http://example.com/\x21\x92", '/%21%92'],
['http://example.com/!?', '/%21'],
];
}
/**
* @dataProvider utf8QueryStringsDataProvider
* @param non-empty-string $url
* @param non-empty-string $result
*/
public function testUtf8Query(string $url, string $result): void
{
$uri = new Uri($url);
$this->assertSame($result, $uri->getQuery());
}
/** @return non-empty-list<array{non-empty-string, non-empty-string}> */
public function utf8QueryStringsDataProvider(): array
{
return [
['http://example.com/?q=тестовый_путь', 'q=тестовый_путь'],
['http://example.com/?q=ουτοπία', 'q=ουτοπία'],
["http://example.com/?q=\x21\x92", 'q=!%92'],
];
}
public function testUriDoesNotAppendColonToHostIfPortIsEmpty(): void
{
$uri = (new Uri())->withHost('google.com');
$this->assertSame('//google.com', (string) $uri);
}
public function testAuthorityIsPrefixedByDoubleSlashIfPresent(): void
{
$uri = (new Uri())->withHost('example.com');
$this->assertSame('//example.com', (string) $uri);
}
public function testReservedCharsInPathUnencoded(): void
{
$uri = (new Uri())
->withScheme('https')
->withHost('api.linkedin.com')
->withPath('/v1/people/~:(first-name,last-name,email-address,picture-url)');
$this->assertStringContainsString(
'/v1/people/~:(first-name,last-name,email-address,picture-url)',
(string) $uri
);
}
public function testHostIsLowercase(): void
{
$uri = new Uri('http://HOST.LOC/path?q=1');
$this->assertSame('host.loc', $uri->getHost());
}
public function testHostIsLowercaseWhenIsSetViwWithHost(): void
{
$uri = (new Uri())->withHost('NEW-HOST.COM');
$this->assertSame('new-host.com', $uri->getHost());
}
public function testUriDistinguishZeroFromEmptyString(): void
{
$expected = 'https://0:0@0:1/0?0#0';
$uri = new Uri($expected);
$this->assertSame($expected, (string) $uri);
}
}