-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUriTest.php
More file actions
310 lines (292 loc) · 13.5 KB
/
Copy pathUriTest.php
File metadata and controls
310 lines (292 loc) · 13.5 KB
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
<?php
namespace bdk\Test\HttpMessage\Utility;
use bdk\HttpMessage\Uri;
use bdk\HttpMessage\Utility\Uri as UriUtils;
use PHPUnit\Framework\TestCase;
/**
* @covers bdk\HttpMessage\Uri
* @covers bdk\HttpMessage\Utility\Uri
*
* @phpcs:disable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder
*/
class UriTest extends TestCase
{
const RFC3986_BASE = 'http://a/b/c/d;p?q';
/**
* @dataProvider providerFromGlobals
*/
public function testFromGlobals(array $serverVars, array $getVars, $expectUriString)
{
$serverBackup = $_SERVER;
$getBackup = $_GET;
$_SERVER = $serverVars;
$_GET = $getVars;
self::assertSame($expectUriString, (string) Uri::fromGlobals());
$_SERVER = $serverBackup;
$_GET = $getBackup;
}
/**
* @dataProvider providerIsCrossOrigin
*/
public function testIsCrossOrigin($uri1, $uri2, $expect)
{
self::assertSame($expect, UriUtils::isCrossOrigin(new Uri($uri1), new Uri($uri2)));
}
/**
* @dataProvider providerParseUrl
*/
public function testParseUrl($url, $expect)
{
$previousLcType = \setlocale(LC_CTYPE, '0');
\setlocale(LC_CTYPE, 'en_GB');
$parts = UriUtils::parseUrl($url);
\setlocale(LC_CTYPE, $previousLcType);
self::assertSame($expect, $parts);
}
/**
* @dataProvider providerResolve
*/
public function testResolveUri($base, $rel, $expect)
{
$base = new Uri($base);
$rel = new Uri($rel);
$targetUri = UriUtils::resolve($base, $rel);
self::assertInstanceOf('Psr\\Http\\Message\\UriInterface', $targetUri);
self::assertSame($expect, (string) $targetUri);
// This ensures there are no test cases that only work in the resolve() direction but not the
// opposite via relativize(). This can happen when both base and rel URI are relative-path
// references resulting in another relative-path URI.
self::assertSame($expect, (string) UriUtils::resolve($base, $targetUri));
}
public static function providerFromGlobals()
{
$serverCommon = array(
'HTTP_CONTENT_TYPE' => 'application/json',
'HTTP_HOST' => 'www.test.com:8080',
'PHP_AUTH_PW' => '1234',
'PHP_AUTH_USER' => 'billybob',
'QUERY_STRING' => 'used=only_if_no_REQUEST_URI',
'REQUEST_METHOD' => 'POST',
'REQUEST_TIME_FLOAT' => $_SERVER['REQUEST_TIME_FLOAT'],
'REQUEST_URI' => '/path?ding=dong',
'SCRIPT_NAME' => isset($_SERVER['SCRIPT_NAME'])
? $_SERVER['SCRIPT_NAME']
: null,
);
return array(
array(
$serverCommon,
array(
'used' => 'only after REQUEST_URI and QUERY_STRING',
),
'http://www.test.com:8080/path?ding=dong',
),
array(
\array_merge($serverCommon, array(
'HTTPS' => 'on',
)),
array(),
'https://www.test.com:8080/path?ding=dong',
),
array(
array(
'REQUEST_METHOD' => 'GET',
'SERVER_NAME' => 'somedomain',
'SERVER_PORT' => '8080',
'QUERY_STRING' => 'ding=dong',
),
array(),
'http://somedomain:8080/?ding=dong',
),
array(
array(
'REQUEST_METHOD' => 'GET',
'SERVER_ADDR' => '192.168.100.42',
'SERVER_PORT' => '8080',
),
array(
'foo' => 'bar',
),
'http://192.168.100.42:8080/?foo=bar',
),
);
}
public static function providerIsCrossOrigin()
{
return [
['http://example.com/123', 'http://example.com/', false],
['http://example.com/123', 'http://example.com:80/', false],
['http://example.com:80/123', 'http://example.com/', false],
['http://example.com:80/123', 'http://example.com:80/', false],
['http://example.com/123', 'https://example.com/', true],
['http://example.com/123', 'http://www.example.com/', true],
['http://example.com/123', 'http://example.com:81/', true],
['http://example.com:80/123', 'http://example.com:81/', true],
['https://example.com/123', 'https://example.com/', false],
['https://example.com/123', 'https://example.com:443/', false],
['https://example.com:443/123', 'https://example.com/', false],
['https://example.com:443/123', 'https://example.com:443/', false],
['https://example.com/123', 'http://example.com/', true],
['https://example.com/123', 'https://www.example.com/', true],
['https://example.com/123', 'https://example.com:444/', true],
['https://example.com:443/123', 'https://example.com:444/', true],
];
}
public static function providerParseUrl()
{
// chars from parseUrl with %" \
$chars = '!*\'();:@&=$,/?#[]%" \\';
return array(
array('https://user:pass@example.com:80/path/È/💩/page.html?foo=bar&zip=zap#fragment', array(
'scheme' => 'https',
'host' => 'example.com',
'port' => 80,
'user' => 'user',
'pass' => 'pass',
'path' => '/path/È/💩/page.html',
'query' => 'foo=bar&zip=zap',
'fragment' => 'fragment',
)),
'encodedChars' => array('http://mydomain.com/' . \rawurlencode($chars), array(
'scheme' => 'http',
'host' => 'mydomain.com',
'path' => '/' . \rawurlencode($chars),
)),
'invalid' => array('http:///example.com', false),
'mixed' => array('/%È21%25È3*%(', array(
'path' => '/%È21%25È3*%(',
)),
'specialChars' => array('//example.com/' . $chars, array(
'host' => 'example.com',
'path' => '/' . \substr($chars, 0, \strpos($chars, '?')),
'query' => '',
'fragment' => '[]%" \\',
)),
'uriInterface' => array(
new Uri('//example.com/'),
array(
'host' => 'example.com',
'path' => '/',
),
),
'uriInterface2' => array(
new Uri('http://foo:bar@example.com:8080/path?zip=zap#frag'),
array(
'scheme' => 'http',
'host' => 'example.com',
'port' => 8080,
'user' => 'foo',
'pass' => 'bar',
'path' => '/path',
'query' => 'zip=zap',
'fragment' => 'frag',
),
),
);
}
public static function providerResolve()
{
return [
[self::RFC3986_BASE, 'g:h', 'g:h'],
[self::RFC3986_BASE, 'g', 'http://a/b/c/g'],
[self::RFC3986_BASE, './g', 'http://a/b/c/g'],
[self::RFC3986_BASE, 'g/', 'http://a/b/c/g/'],
[self::RFC3986_BASE, '/g', 'http://a/g'],
[self::RFC3986_BASE, '//g', 'http://g'],
[self::RFC3986_BASE, '?y', 'http://a/b/c/d;p?y'],
[self::RFC3986_BASE, 'g?y', 'http://a/b/c/g?y'],
[self::RFC3986_BASE, '#s', 'http://a/b/c/d;p?q#s'],
[self::RFC3986_BASE, 'g#s', 'http://a/b/c/g#s'],
[self::RFC3986_BASE, 'g?y#s', 'http://a/b/c/g?y#s'],
[self::RFC3986_BASE, ';x', 'http://a/b/c/;x'],
[self::RFC3986_BASE, 'g;x', 'http://a/b/c/g;x'],
[self::RFC3986_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s'],
[self::RFC3986_BASE, '', self::RFC3986_BASE],
[self::RFC3986_BASE, '.', 'http://a/b/c/'],
[self::RFC3986_BASE, './', 'http://a/b/c/'],
[self::RFC3986_BASE, '..', 'http://a/b/'],
[self::RFC3986_BASE, '../', 'http://a/b/'],
[self::RFC3986_BASE, '../g', 'http://a/b/g'],
[self::RFC3986_BASE, '../..', 'http://a/'],
[self::RFC3986_BASE, '../../', 'http://a/'],
[self::RFC3986_BASE, '../../g', 'http://a/g'],
[self::RFC3986_BASE, '../../../g', 'http://a/g'],
[self::RFC3986_BASE, '../../../../g', 'http://a/g'],
[self::RFC3986_BASE, '/./g', 'http://a/g'],
[self::RFC3986_BASE, '/../g', 'http://a/g'],
[self::RFC3986_BASE, 'g.', 'http://a/b/c/g.'],
[self::RFC3986_BASE, '.g', 'http://a/b/c/.g'],
[self::RFC3986_BASE, 'g..', 'http://a/b/c/g..'],
[self::RFC3986_BASE, '..g', 'http://a/b/c/..g'],
[self::RFC3986_BASE, './../g', 'http://a/b/g'],
[self::RFC3986_BASE, 'foo////g', 'http://a/b/c/foo////g'],
[self::RFC3986_BASE, './g/.', 'http://a/b/c/g/'],
[self::RFC3986_BASE, 'g/./h', 'http://a/b/c/g/h'],
[self::RFC3986_BASE, 'g/../h', 'http://a/b/c/h'],
[self::RFC3986_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y'],
[self::RFC3986_BASE, 'g;x=1/../y', 'http://a/b/c/y'],
// dot-segments in the query or fragment
[self::RFC3986_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x'],
[self::RFC3986_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x'],
[self::RFC3986_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x'],
[self::RFC3986_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x'],
[self::RFC3986_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x'],
[self::RFC3986_BASE, '?y#s', 'http://a/b/c/d;p?y#s'],
// base with fragment
['http://a/b/c?q#s', '?y', 'http://a/b/c?y'],
// base with user info
['http://u@a/b/c/d;p?q', '.', 'http://u@a/b/c/'],
['http://u:p@a/b/c/d;p?q', '.', 'http://u:p@a/b/c/'],
// path ending with slash or no slash at all
['http://a/b/c/d/', 'e', 'http://a/b/c/d/e'],
['urn:no-slash', 'e', 'urn:e'],
// path ending without slash and multi-segment relative part
['http://a/b/c', 'd/e', 'http://a/b/d/e'],
// falsey relative parts
[self::RFC3986_BASE, '//0', 'http://0'],
[self::RFC3986_BASE, '0', 'http://a/b/c/0'],
[self::RFC3986_BASE, '?0', 'http://a/b/c/d;p?0'],
[self::RFC3986_BASE, '#0', 'http://a/b/c/d;p?q#0'],
// absolute path base URI
['/a/b/', '', '/a/b/'],
['/a/b', '', '/a/b'],
['/', 'a', '/a'],
['/', 'a/b', '/a/b'],
['/a/b', 'g', '/a/g'],
['/a/b/c', './', '/a/b/'],
['/a/b/', '../', '/a/'],
['/a/b/c', '../', '/a/'],
['/a/b/', '../../x/y/z/', '/x/y/z/'],
['/a/b/c/d/e', '../../../c/d', '/a/c/d'],
['/a/b/c//', '../', '/a/b/c/'],
['/a/b/c/', './/', '/a/b/c//'],
['/a/b/c', '../../../../a', '/a'],
['/a/b/c', '../../../..', '/'],
// not actually a dot-segment
['/a/b/c', '..a/b..', '/a/b/..a/b..'],
// '' cannot be used as relative reference as it would inherit the base query component
['/a/b?q', 'b', '/a/b'],
['/a/b/?q', './', '/a/b/'],
// path with colon: "with:colon" would be the wrong relative reference
['/a/', './with:colon', '/a/with:colon'],
['/a/', 'b/with:colon', '/a/b/with:colon'],
['/a/', './:b/', '/a/:b/'],
// relative path references
['a', 'a/b', 'a/b'],
['', '', ''],
['', '..', ''],
['/', '..', '/'],
['urn:a/b', '..//a/b', 'urn:/a/b'],
// network path references
// empty base path and relative-path reference
['//example.com', 'a', '//example.com/a'],
// path starting with two slashes
['//example.com//two-slashes', './', '//example.com//'],
['//example.com', './/', '//example.com//'],
['//example.com/', './/', '//example.com//'],
// base URI has less components than relative URI
['/', '//a/b/c/../?q#h', '//a/b/?q#h'],
['/', 'urn:/', 'urn:/'],
];
}
}