forked from thephpleague/uri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpTest.php
334 lines (289 loc) · 9.51 KB
/
HttpTest.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
<?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 Http\Psr7Test\UriIntegrationTest;
use InvalidArgumentException;
use League\Uri\Exceptions\SyntaxError;
use Psr\Http\Message\UriInterface;
/**
* @group http
* @coversDefaultClass \League\Uri\Http
*/
final class HttpTest extends UriIntegrationTest
{
public function createUri($uri): UriInterface
{
return (new HttpFactory())->createUri($uri);
}
public function testDefaultConstructor(): void
{
self::assertSame('', (string) Http::createFromString());
}
/**
* @covers ::jsonSerialize
*/
public function testJson(): void
{
self::assertSame(
'"http:\/\/example.com"',
json_encode(Http::createFromString('http://example.com'))
);
}
/**
* @covers ::__construct
*/
public function testInvalidPort(): void
{
self::expectException(InvalidArgumentException::class);
Http::createFromString('https://example.com:-1');
}
/**
* @covers ::filterInput
*/
public function testThrowTypeErrorOnWrongType(): void
{
self::expectException(InvalidArgumentException::class);
Http::createFromString('https://example.com')->withFragment([]); /* @phpstan-ignore-line */
}
/**
* @covers ::filterInput
*/
public function testThrowInvalidArgumentExceptionOnIllegalCharacters(): void
{
self::expectException(InvalidArgumentException::class);
Http::createFromString('https://example.com')->withFragment("\0");
}
/**
* @covers ::getPort
* @covers ::withPort
*/
public function testPortModification(): void
{
$uri = Http::createFromString('http://login:[email protected]:443/test/query.php?kingkong=toto#doc3');
self::assertSame(443, $uri->getPort());
self::assertSame($uri, $uri->withPort(443));
self::assertNotEquals($uri, $uri->withPort(81));
self::assertSame(
'http://login:[email protected]/test/query.php?kingkong=toto#doc3',
(string) $uri->withPort(null)
);
}
/**
* @covers ::getUserInfo
* @covers ::withUserInfo
*/
public function testUserInfoModification(): void
{
$uri = Http::createFromString('http://login:[email protected]:443/test/query.php?kingkong=toto#doc3');
self::assertSame('login:pass', $uri->getUserInfo());
self::assertSame($uri, $uri->withUserInfo('login', 'pass'));
self::assertNotEquals($uri, $uri->withUserInfo('login'));
self::assertSame(
'http://secure.example.com:443/test/query.php?kingkong=toto#doc3',
(string) $uri->withUserInfo('')
);
}
/**
* @covers ::createFromComponents
*/
public function testCreateFromComponents(): void
{
$uri = '//0:0@0/0?0#0';
self::assertEquals(
Http::createFromComponents(parse_url($uri)),
Http::createFromString($uri)
);
}
/**
* @covers ::createFromBaseUri
*/
public function testCreateFromBaseUri(): void
{
self::assertEquals(
Http::createFromString('http://0:0@0/0?0#0'),
Http::createFromBaseUri('0?0#0', 'http://0:0@0/')
);
}
/**
* @covers ::createFromUri
*/
public function testCreateFromUri(): void
{
self::assertEquals(
Http::createFromString('http://0:0@0/0?0#0'),
Http::createFromUri(Uri::createFromString('http://0:0@0/0?0#0'))
);
self::assertEquals(
Http::createFromString('http://0:0@0/0?0#0'),
Http::createFromUri(Http::createFromString('http://0:0@0/0?0#0'))
);
}
/**
* @dataProvider setStateDataProvider
*
* @covers ::__set_state
*/
public function testSetState(Http $uri): void
{
self::assertEquals($uri, eval('return '.var_export($uri, true).';'));
}
public static function setStateDataProvider(): array
{
return [
'all components' => [Http::createFromString('https://a:b@c:442/d?q=r#f')],
'without scheme' => [Http::createFromString('//a:b@c:442/d?q=r#f')],
'without userinfo' => [Http::createFromString('https://c:442/d?q=r#f')],
'without port' => [Http::createFromString('https://a:b@c/d?q=r#f')],
'without path' => [Http::createFromString('https://a:b@c:442?q=r#f')],
'without query' => [Http::createFromString('https://a:b@c:442/d#f')],
'without fragment' => [Http::createFromString('https://a:b@c:442/d?q=r')],
'without pass' => [Http::createFromString('https://a@c:442/d?q=r#f')],
'without authority' => [Http::createFromString('/d?q=r#f')],
];
}
/**
* @covers \League\Uri\Uri::formatPort
*
* @dataProvider validUrlProvider
*/
public function testCreateFromString(string $expected, string $uri): void
{
self::assertSame($expected, (string) Http::createFromString($uri));
}
public static function validUrlProvider(): array
{
return [
'with default port' => [
'http://example.com/foo/bar?foo=bar#content',
'http://example.com:80/foo/bar?foo=bar#content',
],
'without scheme' => [
'//example.com',
'//example.com',
],
'without scheme but with port' => [
'//example.com:80',
'//example.com:80',
],
'with user info' => [
'http://login:[email protected]/',
'http://login:[email protected]/',
],
'empty string' => [
'',
'',
],
];
}
/**
* @dataProvider invalidUrlProvider
*/
public function testIsValid(string $uri): void
{
self::expectException(SyntaxError::class);
Http::createFromString($uri);
}
public static function invalidUrlProvider(): array
{
return [
['http:example.com'],
['https:/example.com'],
['http://user@:80'],
['//user@:80'],
['http:///path'],
['http:path'],
];
}
/**
* @dataProvider portProvider
*
* @covers \League\Uri\Uri::formatPort
*/
public function testValidPort(string $uri, ?int $port): void
{
self::assertSame($port, Http::createFromString($uri)->getPort());
}
public static function portProvider(): array
{
return [
['http://www.example.com:443/', 443],
['http://www.example.com:80/', null],
['http://www.example.com', null],
['//www.example.com:80/', 80],
];
}
/**
* @dataProvider invalidPathProvider
*/
public function testPathIsInvalid(string $path): void
{
self::expectException(SyntaxError::class);
Http::createFromString()->withPath($path);
}
public static function invalidPathProvider(): array
{
return [
['data:go'],
['//data'],
['to://to'],
];
}
/**
* @covers ::validate
*
* @dataProvider invalidURI
*/
public function testCreateFromInvalidUrlKO(string $uri): void
{
self::expectException(SyntaxError::class);
Http::createFromString($uri);
}
public static function invalidURI(): array
{
return [
['http://user@:80'],
['http://example.com:655356'],
['http://example.com:-1'],
['///path?query'],
];
}
public function testModificationFailedWithEmptyAuthority(): void
{
self::expectException(SyntaxError::class);
Http::createFromString('http://example.com/path')
->withScheme('')
->withHost('')
->withPath('//toto');
}
public function testItStripMultipleLeadingSlashOnGetPath(): void
{
$uri = Http::createFromString('https://example.com///miscillaneous.tld');
self::assertSame('https://example.com///miscillaneous.tld', (string) $uri);
self::assertSame('/miscillaneous.tld', $uri->getPath());
$modifiedUri = $uri->withPath('///foobar');
self::assertSame('https://example.com///foobar', (string) $modifiedUri);
self::assertSame('/foobar', $modifiedUri->getPath());
self::assertSame('//example.com///foobar', (string) $modifiedUri->withScheme(''));
$this->expectException(SyntaxError::class);
$modifiedUri->withScheme('')->withHost('');
}
public function testItPreservesMultipleLeadingSlashesOnMutation(): void
{
$uri = Http::createFromString('https://www.example.com///google.com');
self::assertSame('https://www.example.com///google.com', (string) $uri);
self::assertSame('/google.com', $uri->getPath());
$modifiedUri = $uri->withPath('/google.com');
self::assertSame('https://www.example.com/google.com', (string) $modifiedUri);
self::assertSame('/google.com', $modifiedUri->getPath());
$modifiedUri2 = $uri->withPath('///google.com');
self::assertSame('https://www.example.com///google.com', (string) $modifiedUri2);
self::assertSame('/google.com', $modifiedUri2->getPath());
}
}