forked from thephpleague/uri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UriInfoTest.php
233 lines (221 loc) · 9.27 KB
/
UriInfoTest.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
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Uri;
use Nyholm\Psr7\Uri as Psr7Uri;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
/**
* @group modifier
* @coversDefaultClass \League\Uri\UriInfo
*/
final class UriInfoTest extends TestCase
{
/**
* @dataProvider uriProvider
*
* @param bool[] $infos
*/
public function testInfo(
Psr7UriInterface|Uri $uri,
Psr7UriInterface|Uri|null $base_uri,
array $infos
): void {
if (null !== $base_uri) {
self::assertSame($infos['same_document'], UriInfo::isSameDocument($uri, $base_uri));
}
self::assertSame($infos['relative_path'], UriInfo::isRelativePath($uri));
self::assertSame($infos['absolute_path'], UriInfo::isAbsolutePath($uri));
self::assertSame($infos['absolute_uri'], UriInfo::isAbsolute($uri));
self::assertSame($infos['network_path'], UriInfo::isNetworkPath($uri));
}
public function uriProvider(): array
{
return [
'absolute uri' => [
'uri' => Http::createFromString('http://a/p?q#f'),
'base_uri' => null,
'infos' => [
'absolute_uri' => true,
'network_path' => false,
'absolute_path' => false,
'relative_path' => false,
'same_document' => false,
],
],
'network relative uri' => [
'uri' => Http::createFromString('//스타벅스코리아.com/p?q#f'),
'base_uri' => Http::createFromString('//xn--oy2b35ckwhba574atvuzkc.com/p?q#z'),
'infos' => [
'absolute_uri' => false,
'network_path' => true,
'absolute_path' => false,
'relative_path' => false,
'same_document' => true,
],
],
'path relative uri with non empty path' => [
'uri' => Http::createFromString('p?q#f'),
'base_uri' => null,
'infos' => [
'absolute_uri' => false,
'network_path' => false,
'absolute_path' => false,
'relative_path' => true,
'same_document' => false,
],
],
'path relative uri with empty' => [
'uri' => Http::createFromString('?q#f'),
'base_uri' => null,
'infos' => [
'absolute_uri' => false,
'network_path' => false,
'absolute_path' => false,
'relative_path' => true,
'same_document' => false,
],
],
];
}
public function testIsFunctionsThrowsTypeError(): void
{
self::assertTrue(UriInfo::isAbsolute('http://example.com'));
self::assertFalse(UriInfo::isNetworkPath('http://example.com'));
self::assertTrue(UriInfo::isAbsolutePath('/example.com'));
self::assertTrue(UriInfo::isRelativePath('example.com#foobar'));
}
/**
* @dataProvider sameValueAsProvider
*/
public function testSameValueAs(Psr7UriInterface|Uri $uri1, Psr7UriInterface|Uri $uri2, bool $expected): void
{
self::assertSame($expected, UriInfo::isSameDocument($uri1, $uri2));
}
public function sameValueAsProvider(): array
{
return [
'2 disctincts URIs' => [
Http::createFromString('http://example.com'),
Uri::createFromString('ftp://example.com'),
false,
],
'2 identical URIs' => [
Http::createFromString('http://example.com'),
Http::createFromString('http://example.com'),
true,
],
'2 identical URIs after removing dot segment' => [
Http::createFromString('http://example.org/~foo/'),
Http::createFromString('http://example.ORG/bar/./../~foo/'),
true,
],
'2 distincts relative URIs' => [
Http::createFromString('~foo/'),
Http::createFromString('../~foo/'),
false,
],
'2 identical relative URIs' => [
Http::createFromString('../%7efoo/'),
Http::createFromString('../~foo/'),
true,
],
'2 identical URIs after normalization (1)' => [
Http::createFromString('HtTp://مثال.إختبار:80/%7efoo/%7efoo/'),
Http::createFromString('http://xn--mgbh0fb.xn--kgbechtv/%7Efoo/~foo/'),
true,
],
'2 identical URIs after normalization (2)' => [
Http::createFromString('http://www.example.com'),
Http::createFromString('http://www.example.com/'),
true,
],
'2 identical URIs after normalization (3)' => [
Http::createFromString('http://www.example.com'),
Http::createFromString('http://www.example.com:/'),
true,
],
'2 identical URIs after normalization (4)' => [
Http::createFromString('http://www.example.com'),
Http::createFromString('http://www.example.com:80/'),
true,
],
];
}
/**
* @dataProvider getOriginProvider
*/
public function testGetOrigin(Psr7UriInterface|Uri $uri, ?string $expectedOrigin): void
{
self::assertSame($expectedOrigin, UriInfo::getOrigin($uri));
}
public function getOriginProvider(): array
{
return [
'http uri' => [
'uri' => Uri::createFromString('https://example.com/path?query#fragment'),
'expectedOrigin' => 'https://example.com',
],
'http uri with non standard port' => [
'uri' => Uri::createFromString('https://example.com:81/path?query#fragment'),
'expectedOrigin' => 'https://example.com:81',
],
'relative uri' => [
'uri' => Uri::createFromString('//example.com:81/path?query#fragment'),
'expectedOrigin' => null,
],
'absolute uri with user info' => [
'uri' => Uri::createFromString('https://user:[email protected]:81/path?query#fragment'),
'expectedOrigin' => 'https://example.com:81',
],
'opaque URI' => [
'uri' => Uri::createFromString('mailto:[email protected]'),
'expectedOrigin' => null,
],
'file URI' => [
'uri' => Uri::createFromString('file:///usr/bin/test'),
'expectedOrigin' => null,
],
'blob' => [
'uri' => Uri::createFromString('blob:https://mozilla.org:443/'),
'expectedOrigin' => 'https://mozilla.org',
],
];
}
/**
* @dataProvider getCrossOriginExamples
*/
public function testIsCrossOrigin(string $original, string $modified, bool $expected): void
{
self::assertSame($expected, UriInfo::isCrossOrigin(Uri::createFromString($original), Http::createFromString($modified)));
self::assertSame($expected, UriInfo::isCrossOrigin(new Psr7Uri($original), new Psr7Uri($modified)));
}
/**
* @return array<string, array{0:string, 1:string, 2:bool}>
*/
public function getCrossOriginExamples(): array
{
return [
'different path' => ['http://example.com/123', 'http://example.com/', false],
'same port with default value (1)' => ['https://example.com/123', 'https://example.com:443/', false],
'same port with default value (2)' => ['ws://example.com:80/123', 'ws://example.com/', false],
'same explicit port' => ['wss://example.com:443/123', 'wss://example.com:443/', false],
'same origin with i18n host' => ['https://xn--bb-bjab.be./path', 'https://Bébé.BE./path', false],
'same origin using a blob' => ['blob:https://mozilla.org:443/', 'https://mozilla.org/123', false],
'different scheme' => ['https://example.com/123', 'ftp://example.com/', true],
'different host' => ['ftp://example.com/123', 'ftp://www.example.com/123', true],
'different port implicit' => ['https://example.com/123', 'https://example.com:81/', true],
'different port explicit' => ['https://example.com:80/123', 'https://example.com:81/', true],
'same scheme different port' => ['https://example.com:443/123', 'https://example.com:444/', true],
'comparing two opaque URI' => ['ldap://ldap.example.net', 'ldap://ldap.example.net', true],
'comparing a URI with an origin and one with an opaque origin' => ['https://example.com:443/123', 'ldap://ldap.example.net', true],
'cross origin using a blob' => ['blob:http://mozilla.org:443/', 'https://mozilla.org/123', true],
];
}
}