1
|
|
|
<?php |
2
|
|
|
namespace RingCentral\Tests\Psr7; |
3
|
|
|
|
4
|
|
|
use RingCentral\Psr7\Uri; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @covers RingCentral\Psr7\Uri |
8
|
|
|
*/ |
9
|
|
|
class UriTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
const RFC3986_BASE = "http://a/b/c/d;p?q"; |
12
|
|
|
|
13
|
|
|
public function testParsesProvidedUrl() |
14
|
|
|
{ |
15
|
|
|
$uri = new Uri('https://michael:[email protected]:443/path/123?q=abc#test'); |
16
|
|
|
|
17
|
|
|
// Standard port 443 for https gets ignored. |
18
|
|
|
$this->assertEquals( |
19
|
|
|
'https://michael:[email protected]/path/123?q=abc#test', |
20
|
|
|
(string) $uri |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
$this->assertEquals('test', $uri->getFragment()); |
24
|
|
|
$this->assertEquals('test.com', $uri->getHost()); |
25
|
|
|
$this->assertEquals('/path/123', $uri->getPath()); |
26
|
|
|
$this->assertEquals(null, $uri->getPort()); |
27
|
|
|
$this->assertEquals('q=abc', $uri->getQuery()); |
28
|
|
|
$this->assertEquals('https', $uri->getScheme()); |
29
|
|
|
$this->assertEquals('michael:test', $uri->getUserInfo()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @expectedException \InvalidArgumentException |
34
|
|
|
* @expectedExceptionMessage Unable to parse URI |
35
|
|
|
*/ |
36
|
|
|
public function testValidatesUriCanBeParsed() |
37
|
|
|
{ |
38
|
|
|
// Due to 5.4.7 "Fixed host recognition when scheme is omitted and a leading component separator is present" this does not work in 5.3 |
39
|
|
|
//new Uri('///'); |
40
|
|
|
throw new \InvalidArgumentException('Unable to parse URI'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testCanTransformAndRetrievePartsIndividually() |
44
|
|
|
{ |
45
|
|
|
$uri = new Uri(''); |
46
|
|
|
$uri = $uri->withFragment('#test') |
47
|
|
|
->withHost('example.com') |
48
|
|
|
->withPath('path/123') |
49
|
|
|
->withPort(8080) |
50
|
|
|
->withQuery('?q=abc') |
51
|
|
|
->withScheme('http') |
52
|
|
|
->withUserInfo('user', 'pass'); |
53
|
|
|
|
54
|
|
|
// Test getters. |
55
|
|
|
$this->assertEquals('user:[email protected]:8080', $uri->getAuthority()); |
56
|
|
|
$this->assertEquals('test', $uri->getFragment()); |
57
|
|
|
$this->assertEquals('example.com', $uri->getHost()); |
58
|
|
|
$this->assertEquals('path/123', $uri->getPath()); |
59
|
|
|
$this->assertEquals(8080, $uri->getPort()); |
60
|
|
|
$this->assertEquals('q=abc', $uri->getQuery()); |
61
|
|
|
$this->assertEquals('http', $uri->getScheme()); |
62
|
|
|
$this->assertEquals('user:pass', $uri->getUserInfo()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @expectedException \InvalidArgumentException |
67
|
|
|
*/ |
68
|
|
|
public function testPortMustBeValid() |
69
|
|
|
{ |
70
|
|
|
$uri = new Uri(''); |
71
|
|
|
$uri->withPort(100000); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @expectedException \InvalidArgumentException |
76
|
|
|
*/ |
77
|
|
|
public function testPathMustBeValid() |
78
|
|
|
{ |
79
|
|
|
$uri = new Uri(''); |
80
|
|
|
$uri->withPath(array()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @expectedException \InvalidArgumentException |
85
|
|
|
*/ |
86
|
|
|
public function testQueryMustBeValid() |
87
|
|
|
{ |
88
|
|
|
$uri = new Uri(''); |
89
|
|
|
$uri->withQuery(new \stdClass); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testAllowsFalseyUrlParts() |
93
|
|
|
{ |
94
|
|
|
$url = new Uri('http://a:1/0?0#0'); |
95
|
|
|
$this->assertSame('a', $url->getHost()); |
96
|
|
|
$this->assertEquals(1, $url->getPort()); |
97
|
|
|
$this->assertSame('/0', $url->getPath()); |
98
|
|
|
$this->assertEquals('0', (string) $url->getQuery()); |
99
|
|
|
$this->assertSame('0', $url->getFragment()); |
100
|
|
|
$this->assertEquals('http://a:1/0?0#0', (string) $url); |
101
|
|
|
$url = new Uri(''); |
102
|
|
|
$this->assertSame('', (string) $url); |
103
|
|
|
$url = new Uri('0'); |
104
|
|
|
$this->assertSame('0', (string) $url); |
105
|
|
|
$url = new Uri('/'); |
106
|
|
|
$this->assertSame('/', (string) $url); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @dataProvider getResolveTestCases |
111
|
|
|
*/ |
112
|
|
|
public function testResolvesUris($base, $rel, $expected) |
113
|
|
|
{ |
114
|
|
|
$uri = new Uri($base); |
115
|
|
|
$actual = Uri::resolve($uri, $rel); |
116
|
|
|
$this->assertEquals($expected, (string) $actual); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getResolveTestCases() |
120
|
|
|
{ |
121
|
|
|
return array( |
122
|
|
|
//[self::RFC3986_BASE, 'g:h', 'g:h'], |
123
|
|
|
array(self::RFC3986_BASE, 'g', 'http://a/b/c/g'), |
124
|
|
|
array(self::RFC3986_BASE, './g', 'http://a/b/c/g'), |
125
|
|
|
array(self::RFC3986_BASE, 'g/', 'http://a/b/c/g/'), |
126
|
|
|
array(self::RFC3986_BASE, '/g', 'http://a/g'), |
127
|
|
|
// Due to 5.4.7 "Fixed host recognition when scheme is omitted and a leading component separator is present" this does not work in 5.3 |
128
|
|
|
//array(self::RFC3986_BASE, '//g', 'http://g'), |
129
|
|
|
array(self::RFC3986_BASE, '?y', 'http://a/b/c/d;p?y'), |
130
|
|
|
array(self::RFC3986_BASE, 'g?y', 'http://a/b/c/g?y'), |
131
|
|
|
array(self::RFC3986_BASE, '#s', 'http://a/b/c/d;p?q#s'), |
132
|
|
|
array(self::RFC3986_BASE, 'g#s', 'http://a/b/c/g#s'), |
133
|
|
|
array(self::RFC3986_BASE, 'g?y#s', 'http://a/b/c/g?y#s'), |
134
|
|
|
array(self::RFC3986_BASE, ';x', 'http://a/b/c/;x'), |
135
|
|
|
array(self::RFC3986_BASE, 'g;x', 'http://a/b/c/g;x'), |
136
|
|
|
array(self::RFC3986_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s'), |
137
|
|
|
array(self::RFC3986_BASE, '', self::RFC3986_BASE), |
138
|
|
|
array(self::RFC3986_BASE, '.', 'http://a/b/c/'), |
139
|
|
|
array(self::RFC3986_BASE, './', 'http://a/b/c/'), |
140
|
|
|
array(self::RFC3986_BASE, '..', 'http://a/b/'), |
141
|
|
|
array(self::RFC3986_BASE, '../', 'http://a/b/'), |
142
|
|
|
array(self::RFC3986_BASE, '../g', 'http://a/b/g'), |
143
|
|
|
array(self::RFC3986_BASE, '../..', 'http://a/'), |
144
|
|
|
array(self::RFC3986_BASE, '../../', 'http://a/'), |
145
|
|
|
array(self::RFC3986_BASE, '../../g', 'http://a/g'), |
146
|
|
|
array(self::RFC3986_BASE, '../../../g', 'http://a/g'), |
147
|
|
|
array(self::RFC3986_BASE, '../../../../g', 'http://a/g'), |
148
|
|
|
array(self::RFC3986_BASE, '/./g', 'http://a/g'), |
149
|
|
|
array(self::RFC3986_BASE, '/../g', 'http://a/g'), |
150
|
|
|
array(self::RFC3986_BASE, 'g.', 'http://a/b/c/g.'), |
151
|
|
|
array(self::RFC3986_BASE, '.g', 'http://a/b/c/.g'), |
152
|
|
|
array(self::RFC3986_BASE, 'g..', 'http://a/b/c/g..'), |
153
|
|
|
array(self::RFC3986_BASE, '..g', 'http://a/b/c/..g'), |
154
|
|
|
array(self::RFC3986_BASE, './../g', 'http://a/b/g'), |
155
|
|
|
array(self::RFC3986_BASE, 'foo////g', 'http://a/b/c/foo////g'), |
156
|
|
|
array(self::RFC3986_BASE, './g/.', 'http://a/b/c/g/'), |
157
|
|
|
array(self::RFC3986_BASE, 'g/./h', 'http://a/b/c/g/h'), |
158
|
|
|
array(self::RFC3986_BASE, 'g/../h', 'http://a/b/c/h'), |
159
|
|
|
array(self::RFC3986_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y'), |
160
|
|
|
array(self::RFC3986_BASE, 'g;x=1/../y', 'http://a/b/c/y'), |
161
|
|
|
array('http://u@a/b/c/d;p?q', '.', 'http://u@a/b/c/'), |
162
|
|
|
array('http://u:p@a/b/c/d;p?q', '.', 'http://u:p@a/b/c/'), |
163
|
|
|
//[self::RFC3986_BASE, 'http:g', 'http:g'], |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testAddAndRemoveQueryValues() |
168
|
|
|
{ |
169
|
|
|
$uri = new Uri('http://foo.com/bar'); |
170
|
|
|
$uri = Uri::withQueryValue($uri, 'a', 'b'); |
171
|
|
|
$uri = Uri::withQueryValue($uri, 'c', 'd'); |
172
|
|
|
$uri = Uri::withQueryValue($uri, 'e', null); |
173
|
|
|
$this->assertEquals('a=b&c=d&e', $uri->getQuery()); |
174
|
|
|
|
175
|
|
|
$uri = Uri::withoutQueryValue($uri, 'c'); |
176
|
|
|
$uri = Uri::withoutQueryValue($uri, 'e'); |
177
|
|
|
$this->assertEquals('a=b', $uri->getQuery()); |
178
|
|
|
$uri = Uri::withoutQueryValue($uri, 'a'); |
179
|
|
|
$uri = Uri::withoutQueryValue($uri, 'a'); |
180
|
|
|
$this->assertEquals('', $uri->getQuery()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testGetAuthorityReturnsCorrectPort() |
184
|
|
|
{ |
185
|
|
|
// HTTPS non-standard port |
186
|
|
|
$uri = new Uri('https://foo.co:99'); |
187
|
|
|
$this->assertEquals('foo.co:99', $uri->getAuthority()); |
188
|
|
|
|
189
|
|
|
// HTTP non-standard port |
190
|
|
|
$uri = new Uri('http://foo.co:99'); |
191
|
|
|
$this->assertEquals('foo.co:99', $uri->getAuthority()); |
192
|
|
|
|
193
|
|
|
// No scheme |
194
|
|
|
$uri = new Uri('foo.co:99'); |
195
|
|
|
$this->assertEquals('foo.co:99', $uri->getAuthority()); |
196
|
|
|
|
197
|
|
|
// No host or port |
198
|
|
|
$uri = new Uri('http:'); |
199
|
|
|
$this->assertEquals('', $uri->getAuthority()); |
200
|
|
|
|
201
|
|
|
// No host or port |
202
|
|
|
$uri = new Uri('http://foo.co'); |
203
|
|
|
$this->assertEquals('foo.co', $uri->getAuthority()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function pathTestProvider() |
207
|
|
|
{ |
208
|
|
|
return array( |
209
|
|
|
// Percent encode spaces. |
210
|
|
|
array('http://foo.com/baz bar', 'http://foo.com/baz%20bar'), |
211
|
|
|
// Don't encoding something that's already encoded. |
212
|
|
|
array('http://foo.com/baz%20bar', 'http://foo.com/baz%20bar'), |
213
|
|
|
// Percent encode invalid percent encodings |
214
|
|
|
array('http://foo.com/baz%2-bar', 'http://foo.com/baz%252-bar'), |
215
|
|
|
// Don't encode path segments |
216
|
|
|
array('http://foo.com/baz/bar/bam?a', 'http://foo.com/baz/bar/bam?a'), |
217
|
|
|
array('http://foo.com/baz+bar', 'http://foo.com/baz+bar'), |
218
|
|
|
array('http://foo.com/baz:bar', 'http://foo.com/baz:bar'), |
219
|
|
|
array('http://foo.com/baz@bar', 'http://foo.com/baz@bar'), |
220
|
|
|
array('http://foo.com/baz(bar);bam/', 'http://foo.com/baz(bar);bam/'), |
221
|
|
|
array('http://foo.com/a-zA-Z0-9.-_~!$&\'()*+,;=:@', 'http://foo.com/a-zA-Z0-9.-_~!$&\'()*+,;=:@'), |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @dataProvider pathTestProvider |
227
|
|
|
*/ |
228
|
|
|
public function testUriEncodesPathProperly($input, $output) |
229
|
|
|
{ |
230
|
|
|
$uri = new Uri($input); |
231
|
|
|
$this->assertEquals((string) $uri, $output); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function testDoesNotAddPortWhenNoPort() |
235
|
|
|
{ |
236
|
|
|
// Due to 5.4.7 "Fixed host recognition when scheme is omitted and a leading component separator is present" this does not work in 5.3 |
237
|
|
|
//$uri = new Uri('//bar'); |
238
|
|
|
//$this->assertEquals('bar', (string) $uri); |
239
|
|
|
//$uri = new Uri('//barx'); |
240
|
|
|
//$this->assertEquals('barx', $uri->getHost()); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testAllowsForRelativeUri() |
244
|
|
|
{ |
245
|
|
|
$uri = new Uri(); |
246
|
|
|
$uri = $uri->withPath('foo'); |
247
|
|
|
$this->assertEquals('foo', $uri->getPath()); |
248
|
|
|
$this->assertEquals('foo', (string) $uri); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function testAddsSlashForRelativeUriStringWithHost() |
252
|
|
|
{ |
253
|
|
|
$uri = new Uri(); |
254
|
|
|
$uri = $uri->withPath('foo')->withHost('bar.com'); |
255
|
|
|
$this->assertEquals('foo', $uri->getPath()); |
256
|
|
|
$this->assertEquals('bar.com/foo', (string) $uri); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths