forked from thephpleague/uri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UriResolverTest.php
180 lines (166 loc) · 10.4 KB
/
UriResolverTest.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
<?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 PHPUnit\Framework\TestCase;
/**
* @group modifier
* @coversDefaultClass \League\Uri\UriResolver
*/
final class UriResolverTest extends TestCase
{
private const BASE_URI = 'http://a/b/c/d;p?q';
public function testResolveLetThrowResolvedInvalidUri(): void
{
$http = Uri::createFromString('https://example.com/path/to/file');
$ftp = Http::createFromString('ftp://a/b/c/d;p');
self::assertEquals(UriResolver::resolve($ftp, $http), $ftp);
}
/**
* @dataProvider resolveProvider
*/
public function testCreateResolve(string $base_uri, string $uri, string $expected): void
{
self::assertSame($expected, (string) UriResolver::resolve(
Uri::createFromString($uri),
$base_uri
));
}
public static function resolveProvider(): array
{
return [
'base uri' => [self::BASE_URI, '', self::BASE_URI],
'scheme' => [self::BASE_URI, 'http://d/e/f', 'http://d/e/f'],
'path 1' => [self::BASE_URI, 'g', 'http://a/b/c/g'],
'path 2' => [self::BASE_URI, './g', 'http://a/b/c/g'],
'path 3' => [self::BASE_URI, 'g/', 'http://a/b/c/g/'],
'path 4' => [self::BASE_URI, '/g', 'http://a/g'],
'authority' => [self::BASE_URI, '//g', 'http://g'],
'query' => [self::BASE_URI, '?y', 'http://a/b/c/d;p?y'],
'path + query' => [self::BASE_URI, 'g?y', 'http://a/b/c/g?y'],
'fragment' => [self::BASE_URI, '#s', 'http://a/b/c/d;p?q#s'],
'path + fragment' => [self::BASE_URI, 'g#s', 'http://a/b/c/g#s'],
'path + query + fragment' => [self::BASE_URI, 'g?y#s', 'http://a/b/c/g?y#s'],
'single dot 1' => [self::BASE_URI, '.', 'http://a/b/c/'],
'single dot 2' => [self::BASE_URI, './', 'http://a/b/c/'],
'single dot 3' => [self::BASE_URI, './g/.', 'http://a/b/c/g/'],
'single dot 4' => [self::BASE_URI, 'g/./h', 'http://a/b/c/g/h'],
'double dot 1' => [self::BASE_URI, '..', 'http://a/b/'],
'double dot 2' => [self::BASE_URI, '../', 'http://a/b/'],
'double dot 3' => [self::BASE_URI, '../g', 'http://a/b/g'],
'double dot 4' => [self::BASE_URI, '../..', 'http://a/'],
'double dot 5' => [self::BASE_URI, '../../', 'http://a/'],
'double dot 6' => [self::BASE_URI, '../../g', 'http://a/g'],
'double dot 7' => [self::BASE_URI, '../../../g', 'http://a/g'],
'double dot 8' => [self::BASE_URI, '../../../../g', 'http://a/g'],
'double dot 9' => [self::BASE_URI, 'g/../h' , 'http://a/b/c/h'],
'mulitple slashes' => [self::BASE_URI, 'foo////g', 'http://a/b/c/foo////g'],
'complex path 1' => [self::BASE_URI, ';x', 'http://a/b/c/;x'],
'complex path 2' => [self::BASE_URI, 'g;x', 'http://a/b/c/g;x'],
'complex path 3' => [self::BASE_URI, 'g;x?y#s', 'http://a/b/c/g;x?y#s'],
'complex path 4' => [self::BASE_URI, 'g;x=1/./y', 'http://a/b/c/g;x=1/y'],
'complex path 5' => [self::BASE_URI, 'g;x=1/../y', 'http://a/b/c/y'],
'dot segments presence 1' => [self::BASE_URI, '/./g', 'http://a/g'],
'dot segments presence 2' => [self::BASE_URI, '/../g', 'http://a/g'],
'dot segments presence 3' => [self::BASE_URI, 'g.', 'http://a/b/c/g.'],
'dot segments presence 4' => [self::BASE_URI, '.g', 'http://a/b/c/.g'],
'dot segments presence 5' => [self::BASE_URI, 'g..', 'http://a/b/c/g..'],
'dot segments presence 6' => [self::BASE_URI, '..g', 'http://a/b/c/..g'],
'origin uri without path' => ['http://h:b@a', 'b/../y', 'http://h:b@a/y'],
];
}
public function testRelativizeIsNotMade(): void
{
$uri = Uri::createFromString('//path#fragment');
$base_uri = Http::createFromString('https://example.com/path');
self::assertEquals($uri, UriResolver::relativize($uri, $base_uri));
}
/**
* @dataProvider relativizeProvider
*/
public function testRelativize(string $uri, string $resolved, string $expected): void
{
self::assertSame(
$expected,
(string) UriResolver::relativize(Uri::createFromString($resolved), Http::createFromString($uri))
);
}
public static function relativizeProvider(): array
{
return [
'different scheme' => [self::BASE_URI, 'https://a/b/c/d;p?q', 'https://a/b/c/d;p?q'],
'different authority' => [self::BASE_URI, 'https://g/b/c/d;p?q', 'https://g/b/c/d;p?q'],
'empty uri' => [self::BASE_URI, '', ''],
'same uri' => [self::BASE_URI, self::BASE_URI, ''],
'same path' => [self::BASE_URI, 'http://a/b/c/d;p', 'd;p'],
'parent path 1' => [self::BASE_URI, 'http://a/b/c/', './'],
'parent path 2' => [self::BASE_URI, 'http://a/b/', '../'],
'parent path 3' => [self::BASE_URI, 'http://a/', '../../'],
'parent path 4' => [self::BASE_URI, 'http://a', '../../'],
'sibling path 1' => [self::BASE_URI, 'http://a/b/c/g', 'g'],
'sibling path 2' => [self::BASE_URI, 'http://a/b/c/g/h', 'g/h'],
'sibling path 3' => [self::BASE_URI, 'http://a/b/g', '../g'],
'sibling path 4' => [self::BASE_URI, 'http://a/g', '../../g'],
'query' => [self::BASE_URI, 'http://a/b/c/d;p?y', '?y'],
'fragment' => [self::BASE_URI, 'http://a/b/c/d;p?q#s', '#s'],
'path + query' => [self::BASE_URI, 'http://a/b/c/g?y', 'g?y'],
'path + fragment' => [self::BASE_URI, 'http://a/b/c/g#s', 'g#s'],
'path + query + fragment' => [self::BASE_URI, 'http://a/b/c/g?y#s', 'g?y#s'],
'empty segments' => [self::BASE_URI, 'http://a/b/c/foo////g', 'foo////g'],
'empty segments 1' => [self::BASE_URI, 'http://a/b////c/foo/g', '..////c/foo/g'],
'relative single dot 1' => [self::BASE_URI, '.', '.'],
'relative single dot 2' => [self::BASE_URI, './', './'],
'relative double dot 1' => [self::BASE_URI, '..', '..'],
'relative double dot 2' => [self::BASE_URI, '../', '../'],
'path with colon 1' => ['http://a/', 'http://a/d:p', './d:p'],
'path with colon 2' => [self::BASE_URI, 'http://a/b/c/g/d:p', 'g/d:p'],
'scheme + auth 1' => ['http://a', 'http://a?q#s', '?q#s'],
'scheme + auth 2' => ['http://a/', 'http://a?q#s', '/?q#s'],
'2 relative paths 1' => ['a/b', '../..', '../..'],
'2 relative paths 2' => ['a/b', './.', './.'],
'2 relative paths 3' => ['a/b', '../c', '../c'],
'2 relative paths 4' => ['a/b', 'c/..', 'c/..'],
'2 relative paths 5' => ['a/b', 'c/.', 'c/.'],
'baseUri with query' => ['/a/b/?q', '/a/b/#h', './#h'],
'targetUri with fragment' => ['/', '/#h', '#h'],
'same document' => ['/', '/', ''],
'same URI normalized' => ['http://a', 'http://a/', ''],
];
}
/**
* @dataProvider relativizeAndResolveProvider
*/
public function testRelativizeAndResolve(
string $baseUri,
string $uri,
string $expectedRelativize
): void {
self::assertSame(
$expectedRelativize,
(string) UriResolver::relativize($uri, Uri::createFromString($baseUri))
);
}
public static function relativizeAndResolveProvider(): array
{
return [
'empty path' => [self::BASE_URI, 'http://a/', '../../', 'http://a/'],
'absolute empty path' => [self::BASE_URI, 'http://a', '../../', 'http://a/'],
'relative single dot 1' => [self::BASE_URI, '.', '.', 'http://a/b/c/'],
'relative single dot 2' => [self::BASE_URI, './', './', 'http://a/b/c/'],
'relative double dot 1' => [self::BASE_URI, '..', '..', 'http://a/b/'],
'relative double dot 2' => [self::BASE_URI, '../', '../', 'http://a/b/'],
'2 relative paths 1' => ['a/b', '../..', '../..', '/'],
'2 relative paths 2' => ['a/b', './.', './.', 'a/'],
'2 relative paths 3' => ['a/b', '../c', '../c', 'c'],
'2 relative paths 4' => ['a/b', 'c/..', 'c/..', 'a/'],
'2 relative paths 5' => ['a/b', 'c/.', 'c/.', 'a/c/'],
'path with colon' => ['http://a/', 'http://a/d:p', './d:p', 'http://a/d:p'],
];
}
}