Completed
Pull Request — master (#178)
by ignace nyamagana
03:19
created

HttpTest::invalidURI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * League.Uri (https://uri.thephpleague.com)
5
 *
6
 * (c) Ignace Nyamagana Butera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LeagueTest\Uri;
13
14
use InvalidArgumentException;
15
use League\Uri\Exceptions\SyntaxError;
16
use League\Uri\Http;
17
use League\Uri\Uri;
18
use PHPUnit\Framework\TestCase;
19
use TypeError;
20
21
/**
22
 * @group http
23
 * @coversDefaultClass League\Uri\Http
24
 */
25
class HttpTest extends TestCase
26
{
27
    /**
28
     * @var Http
29
     */
30
    private $uri;
31
32
    protected function setUp(): void
33
    {
34
        $this->uri = Http::createFromString(
35
            'http://login:[email protected]:443/test/query.php?kingkong=toto#doc3'
36
        );
37
    }
38
39
    protected function tearDown(): void
40
    {
41
        unset($this->uri);
42
    }
43
44
    public function testDefaultConstructor(): void
45
    {
46
        self::assertSame('', (string) Http::createFromString());
47
    }
48
49
    /**
50
     * @covers ::jsonSerialize
51
     */
52
    public function testJson(): void
53
    {
54
        self::assertSame(
55
            '"http:\/\/example.com"',
56
            json_encode(Http::createFromString('http://example.com'))
57
        );
58
    }
59
60
    /**
61
     * @covers ::__construct
62
     */
63
    public function testInvalidPort(): void
64
    {
65
        self::expectException(InvalidArgumentException::class);
66
        Http::createFromString('https://example.com:-1');
67
    }
68
69
    /**
70
     * @covers ::filterInput
71
     */
72
    public function testThrowTypeErrorOnWrongType(): void
73
    {
74
        self::expectException(TypeError::class);
75
        Http::createFromString('https://example.com')->withFragment([]);
76
    }
77
78
    /**
79
     * @covers ::filterInput
80
     */
81
    public function testThrowInvalidArgumentExceptionOnIllegalCharacters(): void
82
    {
83
        self::expectException(InvalidArgumentException::class);
84
        Http::createFromString('https://example.com')->withFragment("\0");
85
    }
86
87
    /**
88
     * @covers ::getPort
89
     * @covers ::withPort
90
     */
91
    public function testPortModification(): void
92
    {
93
        $uri = Http::createFromString('http://login:[email protected]:443/test/query.php?kingkong=toto#doc3');
94
        self::assertSame(443, $uri->getPort());
95
        self::assertSame($uri, $uri->withPort(443));
96
        self::assertNotEquals($uri, $uri->withPort(81));
97
        self::assertSame(
98
            'http://login:[email protected]/test/query.php?kingkong=toto#doc3',
99
            (string) $uri->withPort(null)
100
        );
101
    }
102
103
    /**
104
     * @covers ::getUserInfo
105
     * @covers ::withUserInfo
106
     */
107
    public function testUserInfoModification(): void
108
    {
109
        $uri = Http::createFromString('http://login:[email protected]:443/test/query.php?kingkong=toto#doc3');
110
        self::assertSame('login:pass', $uri->getUserInfo());
111
        self::assertSame($uri, $uri->withUserInfo('login', 'pass'));
112
        self::assertNotEquals($uri, $uri->withUserInfo('login', null));
113
        self::assertSame(
114
            'http://secure.example.com:443/test/query.php?kingkong=toto#doc3',
115
            (string) $uri->withUserInfo('')
116
        );
117
    }
118
119
    /**
120
     * @covers ::createFromComponents
121
     */
122
    public function testCreateFromComponents(): void
123
    {
124
        $uri = '//0:0@0/0?0#0';
125
        self::assertEquals(
126
            Http::createFromComponents(parse_url($uri)),
127
            Http::createFromString($uri)
128
        );
129
    }
130
131
    /**
132
     * @covers ::createFromBaseUri
133
     */
134
    public function testCreateFromBaseUri(): void
135
    {
136
        self::assertEquals(
137
            Http::createFromString('http://0:0@0/0?0#0'),
138
            Http::createFromBaseUri('0?0#0', 'http://0:0@0/')
139
        );
140
    }
141
142
    /**
143
     * @covers ::createFromUri
144
     */
145
    public function testCreateFromUri(): void
146
    {
147
        self::assertEquals(
148
            Http::createFromString('http://0:0@0/0?0#0'),
149
            Http::createFromUri(Uri::createFromString('http://0:0@0/0?0#0'))
150
        );
151
    }
152
153
    /**
154
     * @dataProvider setStateDataProvider
155
     *
156
     * @covers ::__set_state
157
     */
158
    public function testSetState(Http $uri): void
159
    {
160
        self::assertEquals($uri, eval('return '.var_export($uri, true).';'));
161
    }
162
163
    public function setStateDataProvider(): array
164
    {
165
        return [
166
            'all components' => [Http::createFromString('https://a:b@c:442/d?q=r#f')],
167
            'without scheme' => [Http::createFromString('//a:b@c:442/d?q=r#f')],
168
            'without userinfo' => [Http::createFromString('https://c:442/d?q=r#f')],
169
            'without port' => [Http::createFromString('https://a:b@c/d?q=r#f')],
170
            'without path' => [Http::createFromString('https://a:b@c:442?q=r#f')],
171
            'without query' => [Http::createFromString('https://a:b@c:442/d#f')],
172
            'without fragment' => [Http::createFromString('https://a:b@c:442/d?q=r')],
173
            'without pass' => [Http::createFromString('https://a@c:442/d?q=r#f')],
174
            'without authority' => [Http::createFromString('/d?q=r#f')],
175
       ];
176
    }
177
178
    /**
179
     * @covers \League\Uri\Uri::formatPort
180
     *
181
     * @dataProvider validUrlProvider
182
     */
183
    public function testCreateFromString(string $expected, string $uri): void
184
    {
185
        self::assertSame($expected, (string) Http::createFromString($uri));
186
    }
187
188
    public function validUrlProvider(): array
189
    {
190
        return [
191
            'with default port' => [
192
                'http://example.com/foo/bar?foo=bar#content',
193
                'http://example.com:80/foo/bar?foo=bar#content',
194
            ],
195
            'without scheme' => [
196
                '//example.com',
197
                '//example.com',
198
            ],
199
            'without scheme but with port' => [
200
                '//example.com:80',
201
                '//example.com:80',
202
            ],
203
            'with user info' => [
204
                'http://login:[email protected]/',
205
                'http://login:[email protected]/',
206
            ],
207
            'empty string' => [
208
                '',
209
                '',
210
            ],
211
        ];
212
    }
213
214
    /**
215
     * @dataProvider invalidUrlProvider
216
     */
217
    public function testIsValid(string $uri): void
218
    {
219
        self::expectException(SyntaxError::class);
220
        Http::createFromString($uri);
221
    }
222
223
    public function invalidUrlProvider(): array
224
    {
225
        return [
226
            //['wss://example.com'],
227
            ['http:example.com'],
228
            ['https:/example.com'],
229
            ['http://user@:80'],
230
            //['//user@:80'],
231
            ['http:///path'],
232
            ['http:path'],
233
        ];
234
    }
235
236
    /**
237
     * @dataProvider portProvider
238
     *
239
     * @covers \League\Uri\Uri::formatPort
240
     * @param ?int $port
0 ignored issues
show
Documentation introduced by
The doc-type ?int could not be parsed: Unknown type name "?int" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
241
     */
242
    public function testPort(string $uri, ?int $port): void
243
    {
244
        self::assertSame($port, Http::createFromString($uri)->getPort());
245
    }
246
247
    public function portProvider(): array
248
    {
249
        return [
250
            ['http://www.example.com:443/', 443],
251
            ['http://www.example.com:80/', null],
252
            ['http://www.example.com', null],
253
            ['//www.example.com:80/', 80],
254
        ];
255
    }
256
257
    /**
258
     * @dataProvider invalidPathProvider
259
     */
260
    public function testPathIsInvalid(string $path): void
261
    {
262
        self::expectException(SyntaxError::class);
263
        Http::createFromString('')->withPath($path);
264
    }
265
266
    public function invalidPathProvider(): array
267
    {
268
        return [
269
            ['data:go'],
270
            ['//data'],
271
            ['to://to'],
272
        ];
273
    }
274
275
    /**
276
     * @covers ::validate
277
     *
278
     * @dataProvider invalidURI
279
     */
280
    public function testCreateFromInvalidUrlKO(string $uri): void
281
    {
282
        self::expectException(SyntaxError::class);
283
        Http::createFromString($uri);
284
    }
285
286
    public function invalidURI(): array
287
    {
288
        return [
289
            ['http://user@:80'],
290
            ['http://example.com:655356'],
291
            ['http://example.com:-1'],
292
            ['///path?query'],
293
        ];
294
    }
295
296
    public function testModificationFailedWithEmptyAuthority(): void
297
    {
298
        self::expectException(SyntaxError::class);
299
        Http::createFromString('http://example.com/path')
300
            ->withScheme('')
301
            ->withHost('')
302
            ->withPath('//toto');
303
    }
304
}
305