forked from thephpleague/uri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FactoryTest.php
453 lines (427 loc) · 16.2 KB
/
FactoryTest.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
<?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 League\Uri\Exceptions\SyntaxError;
use PHPUnit\Framework\TestCase;
/**
* @group factory
* @coversDefaultClass \League\Uri\Uri
*/
final class FactoryTest extends TestCase
{
/**
* @covers ::createFromDataPath
*
* @dataProvider invalidDataPath
*/
public function testCreateFromPathFailed(string $path): void
{
self::expectException(SyntaxError::class);
Uri::createFromDataPath($path);
}
public function invalidDataPath(): array
{
return [
'invalid format' => ['/usr/bin/yeah'],
];
}
/**
* @covers ::createFromDataPath
*
* @dataProvider validFilePath
*/
public function testCreateFromPath(string $path, string $expected): void
{
$context = stream_context_create([
'http'=> [
'method' => 'GET',
'header' => "Accept-language: en\r\nCookie: foo=bar\r\n",
],
]);
$uri = Uri::createFromDataPath(dirname(__DIR__).'/test_files/'.$path, $context);
self::assertStringContainsString($expected, $uri->getPath());
}
public function validFilePath(): array
{
return [
'text file' => ['hello-world.txt', 'text/plain'],
'img file' => ['red-nose.gif', 'image/gif'],
];
}
/**
* @covers ::createFromUnixPath
*
* @dataProvider unixpathProvider
*/
public function testCreateFromUnixPath(string $uri, string $expected): void
{
self::assertSame($expected, (string) Uri::createFromUnixPath($uri));
}
public function unixpathProvider(): array
{
return [
'relative path' => [
'input' => 'path',
'expected' => 'path',
],
'absolute path' => [
'input' => '/path',
'expected' => 'file:///path',
],
'path with empty char' => [
'input' => '/path empty/bar',
'expected' => 'file:///path%20empty/bar',
],
'relative path with dot segments' => [
'input' => 'path/./relative',
'expected' => 'path/./relative',
],
'absolute path with dot segments' => [
'input' => '/path/./../relative',
'expected' => 'file:///path/./../relative',
],
];
}
/**
* @covers ::createFromWindowsPath
*
* @dataProvider windowLocalPathProvider
*/
public function testCreateFromWindowsLocalPath(string $uri, string $expected): void
{
self::assertSame($expected, (string) Uri::createFromWindowsPath($uri));
}
public function windowLocalPathProvider(): array
{
return [
'relative path' => [
'input' => 'path',
'expected' => 'path',
],
'relative path with dot segments' => [
'input' => 'path\.\relative',
'expected' => 'path/./relative',
],
'absolute path' => [
'input' => 'c:\windows\My Documents 100%20\foo.txt',
'expected' => 'file:///c:/windows/My%20Documents%20100%2520/foo.txt',
],
'windows relative path' => [
'input' => 'c:My Documents 100%20\foo.txt',
'expected' => 'file:///c:My%20Documents%20100%2520/foo.txt',
],
'absolute path with `|`' => [
'input' => 'c|\windows\My Documents 100%20\foo.txt',
'expected' => 'file:///c:/windows/My%20Documents%20100%2520/foo.txt',
],
'windows relative path with `|`' => [
'input' => 'c:My Documents 100%20\foo.txt',
'expected' => 'file:///c:My%20Documents%20100%2520/foo.txt',
],
'absolute path with dot segments' => [
'input' => '\path\.\..\relative',
'expected' => '/path/./../relative',
],
'absolute UNC path' => [
'input' => '\\\\server\share\My Documents 100%20\foo.txt',
'expected' => 'file://server/share/My%20Documents%20100%2520/foo.txt',
],
];
}
public function testCreateFromUri(): void
{
$expected = 'https://login:[email protected]:443/test/query.php?kingkong=toto#doc3';
$psr7 = Http::createFromString($expected);
$leagueUri = Uri::createFromString($expected);
$uriFromPsr7 = Uri::createFromUri($psr7);
$uriFromLeagueUri = Uri::createFromUri($leagueUri);
self::assertSame((string) $psr7, (string) $uriFromPsr7);
self::assertSame((string) $psr7, (string) $uriFromLeagueUri);
$uribis = Http::createFromString();
self::assertSame((string) $uribis, Uri::createFromUri($uribis)->__toString());
}
/**
* @covers ::createFromServer
* @covers \League\Uri\Http::createFromServer
* @covers ::fetchScheme
* @covers ::fetchUserInfo
* @covers ::fetchHostname
* @covers ::fetchRequestUri
*
* @dataProvider validServerArray
*/
public function testCreateFromServer(string $expected, array $input): void
{
self::assertSame($expected, (string) Uri::createFromServer($input));
self::assertSame($expected, (string) Http::createFromServer($input));
}
public function validServerArray(): array
{
return [
'with host' => [
'https://example.com:23',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => 'on',
'SERVER_PORT' => '23',
'HTTP_HOST' => 'example.com',
],
],
'server address IPv4' => [
'https://127.0.0.1:23',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => 'on',
'SERVER_PORT' => 23,
],
],
'server address IPv6' => [
'https://[::1]:23',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '::1',
'HTTPS' => 'on',
'SERVER_PORT' => 23,
],
],
'with port attached to host' => [
'https://localhost:23',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => 'on',
'SERVER_PORT' => 80,
'HTTP_HOST' => 'localhost:23',
],
],
'with standard apache HTTP server' => [
'http://localhost:23',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => '',
'SERVER_PORT' => 80,
'HTTP_HOST' => 'localhost:23',
],
],
'with IIS HTTP server' => [
'http://localhost:23',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => 'off',
'SERVER_PORT' => 80,
'HTTP_HOST' => 'localhost:23',
],
],
'with IIS Rewritting server' => [
'http://localhost:23/foo/bar?foo=bar',
[
'PHP_SELF' => '',
'IIS_WasUrlRewritten' => '1',
'UNENCODED_URL' => '/foo/bar?foo=bar',
'REQUEST_URI' => 'toto',
'SERVER_PORT' => 23,
'HTTP_HOST' => 'localhost',
],
],
'with standard port setting' => [
'https://localhost:23',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => 'on',
'SERVER_PORT' => 23,
'HTTP_HOST' => 'localhost',
],
],
'without port' => [
'https://localhost',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => 'on',
'HTTP_HOST' => 'localhost',
],
],
'with user info' => [
'https://foo:bar@localhost:23',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '127.0.0.1',
'PHP_AUTH_USER' => 'foo',
'PHP_AUTH_PW' => 'bar',
'HTTPS' => 'on',
'SERVER_PORT' => 23,
'HTTP_HOST' => 'localhost:23',
],
],
'with user info and HTTP AUTHORIZATION' => [
'https://foo:bar@localhost:23',
[
'PHP_SELF' => '',
'REQUEST_URI' => '',
'SERVER_ADDR' => '127.0.0.1',
'HTTP_AUTHORIZATION' => 'basic '.base64_encode('foo:bar'),
'HTTPS' => 'on',
'SERVER_PORT' => 23,
'HTTP_HOST' => 'localhost:23',
],
],
'without request uri' => [
'https://127.0.0.1:23/toto?foo=bar',
[
'PHP_SELF' => '/toto',
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => 'on',
'SERVER_PORT' => 23,
'QUERY_STRING' => 'foo=bar',
],
],
'without request uri and server host' => [
'https://127.0.0.1:23',
[
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => 'on',
'SERVER_PORT' => 23,
],
],
];
}
/**
* @covers ::fetchHostname
*/
public function testFailCreateFromServerWithoutHost(): void
{
self::expectException(SyntaxError::class);
Uri::createFromServer([
'PHP_SELF' => '',
'REQUEST_URI' => '',
'HTTPS' => 'on',
'SERVER_PORT' => 23,
]);
}
/**
* @covers ::fetchUserInfo
*/
public function testFailCreateFromServerWithoutInvalidUserInfo(): void
{
self::expectException(SyntaxError::class);
Uri::createFromServer([
'PHP_SELF' => '/toto',
'SERVER_ADDR' => '127.0.0.1',
'HTTPS' => 'on',
'SERVER_PORT' => 23,
'QUERY_STRING' => 'foo=bar',
'HTTP_AUTHORIZATION' => 'basic foo:bar',
]);
}
/**
* @covers ::createFromBaseUri
*
* @dataProvider createProvider
*/
public function testCreateFromBaseUri(string $base_uri, string $uri, string $expected): void
{
self::assertSame($expected, (string) Uri::createFromBaseUri($uri, $base_uri));
}
public function createProvider(): array
{
$base_uri = 'http://a/b/c/d;p?q';
return [
'base uri' => [$base_uri, '', $base_uri],
'scheme' => [$base_uri, 'http://d/e/f', 'http://d/e/f'],
'path 1' => [$base_uri, 'g', 'http://a/b/c/g'],
'path 2' => [$base_uri, './g', 'http://a/b/c/g'],
'path 3' => [$base_uri, 'g/', 'http://a/b/c/g/'],
'path 4' => [$base_uri, '/g', 'http://a/g'],
'authority' => [$base_uri, '//g', 'http://g'],
'query' => [$base_uri, '?y', 'http://a/b/c/d;p?y'],
'path + query' => [$base_uri, 'g?y', 'http://a/b/c/g?y'],
'fragment' => [$base_uri, '#s', 'http://a/b/c/d;p?q#s'],
'path + fragment' => [$base_uri, 'g#s', 'http://a/b/c/g#s'],
'path + query + fragment' => [$base_uri, 'g?y#s', 'http://a/b/c/g?y#s'],
'single dot 1' => [$base_uri, '.', 'http://a/b/c/'],
'single dot 2' => [$base_uri, './', 'http://a/b/c/'],
'single dot 3' => [$base_uri, './g/.', 'http://a/b/c/g/'],
'single dot 4' => [$base_uri, 'g/./h', 'http://a/b/c/g/h'],
'double dot 1' => [$base_uri, '..', 'http://a/b/'],
'double dot 2' => [$base_uri, '../', 'http://a/b/'],
'double dot 3' => [$base_uri, '../g', 'http://a/b/g'],
'double dot 4' => [$base_uri, '../..', 'http://a/'],
'double dot 5' => [$base_uri, '../../', 'http://a/'],
'double dot 6' => [$base_uri, '../../g', 'http://a/g'],
'double dot 7' => [$base_uri, '../../../g', 'http://a/g'],
'double dot 8' => [$base_uri, '../../../../g', 'http://a/g'],
'double dot 9' => [$base_uri, 'g/../h' , 'http://a/b/c/h'],
'mulitple slashes' => [$base_uri, 'foo////g', 'http://a/b/c/foo////g'],
'complex path 1' => [$base_uri, ';x', 'http://a/b/c/;x'],
'complex path 2' => [$base_uri, 'g;x', 'http://a/b/c/g;x'],
'complex path 3' => [$base_uri, 'g;x?y#s', 'http://a/b/c/g;x?y#s'],
'complex path 4' => [$base_uri, 'g;x=1/./y', 'http://a/b/c/g;x=1/y'],
'complex path 5' => [$base_uri, 'g;x=1/../y', 'http://a/b/c/y'],
'dot segments presence 1' => [$base_uri, '/./g', 'http://a/g'],
'dot segments presence 2' => [$base_uri, '/../g', 'http://a/g'],
'dot segments presence 3' => [$base_uri, 'g.', 'http://a/b/c/g.'],
'dot segments presence 4' => [$base_uri, '.g', 'http://a/b/c/.g'],
'dot segments presence 5' => [$base_uri, 'g..', 'http://a/b/c/g..'],
'dot segments presence 6' => [$base_uri, '..g', 'http://a/b/c/..g'],
'origin uri without path' => ['http://h:b@a', 'b/../y', 'http://h:b@a/y'],
'uri without auhtority' => ['mailto:[email protected]', '[email protected]?subject=baz', 'mailto:[email protected]?subject=baz'],
];
}
/**
* @covers ::createFromBaseUri
*/
public function testCreateThrowExceptionWithBaseUriNotAbsolute(): void
{
self::expectException(SyntaxError::class);
Uri::createFromBaseUri('/path/to/you', '//example.com');
}
/**
* @covers ::createFromBaseUri
*/
public function testCreateThrowExceptionWithUriNotAbsolute(): void
{
self::expectException(SyntaxError::class);
Uri::createFromBaseUri('/path/to/you');
}
/**
* @covers ::createFromBaseUri
*/
public function testCreateWithUriWithoutAuthority(): void
{
self::assertSame(
'data:text/plain;charset=us-ascii,',
(string) Uri::createFromBaseUri('data:text/plain;charset=us-ascii,')
);
}
/**
* @covers ::createFromBaseUri
*/
public function testCreateWithAbasoluteUriWithoutBaseUri(): void
{
self::assertSame(
'scheme://host/sky?q#f',
(string) Uri::createFromBaseUri('scheme://host/path/../sky?q#f')
);
}
}