source: externals/php/vendor/guzzlehttp/psr7/tests/UriTest.php@ 256102b

Last change on this file since 256102b was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

  • Property mode set to 100644
File size: 9.3 KB
RevLine 
[01a620f]1<?php
2namespace GuzzleHttp\Tests\Psr7;
3
4use GuzzleHttp\Psr7\Uri;
5
6/**
7 * @covers GuzzleHttp\Psr7\Uri
8 */
9class 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:test@test.com:443/path/123?q=abc#test');
16
17 // Standard port 443 for https gets ignored.
18 $this->assertEquals(
19 'https://michael:test@test.com/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 new Uri('///');
39 }
40
41 public function testCanTransformAndRetrievePartsIndividually()
42 {
43 $uri = (new Uri(''))
44 ->withFragment('#test')
45 ->withHost('example.com')
46 ->withPath('path/123')
47 ->withPort(8080)
48 ->withQuery('?q=abc')
49 ->withScheme('http')
50 ->withUserInfo('user', 'pass');
51
52 // Test getters.
53 $this->assertEquals('user:pass@example.com:8080', $uri->getAuthority());
54 $this->assertEquals('test', $uri->getFragment());
55 $this->assertEquals('example.com', $uri->getHost());
56 $this->assertEquals('path/123', $uri->getPath());
57 $this->assertEquals(8080, $uri->getPort());
58 $this->assertEquals('q=abc', $uri->getQuery());
59 $this->assertEquals('http', $uri->getScheme());
60 $this->assertEquals('user:pass', $uri->getUserInfo());
61 }
62
63 /**
64 * @expectedException \InvalidArgumentException
65 */
66 public function testPortMustBeValid()
67 {
68 (new Uri(''))->withPort(100000);
69 }
70
71 /**
72 * @expectedException \InvalidArgumentException
73 */
74 public function testPathMustBeValid()
75 {
76 (new Uri(''))->withPath([]);
77 }
78
79 /**
80 * @expectedException \InvalidArgumentException
81 */
82 public function testQueryMustBeValid()
83 {
84 (new Uri(''))->withQuery(new \stdClass);
85 }
86
87 public function testAllowsFalseyUrlParts()
88 {
89 $url = new Uri('http://a:1/0?0#0');
90 $this->assertSame('a', $url->getHost());
91 $this->assertEquals(1, $url->getPort());
92 $this->assertSame('/0', $url->getPath());
93 $this->assertEquals('0', (string) $url->getQuery());
94 $this->assertSame('0', $url->getFragment());
95 $this->assertEquals('http://a:1/0?0#0', (string) $url);
96 $url = new Uri('');
97 $this->assertSame('', (string) $url);
98 $url = new Uri('0');
99 $this->assertSame('0', (string) $url);
100 $url = new Uri('/');
101 $this->assertSame('/', (string) $url);
102 }
103
104 /**
105 * @dataProvider getResolveTestCases
106 */
107 public function testResolvesUris($base, $rel, $expected)
108 {
109 $uri = new Uri($base);
110 $actual = Uri::resolve($uri, $rel);
111 $this->assertEquals($expected, (string) $actual);
112 }
113
114 public function getResolveTestCases()
115 {
116 return [
117 //[self::RFC3986_BASE, 'g:h', 'g:h'],
118 [self::RFC3986_BASE, 'g', 'http://a/b/c/g'],
119 [self::RFC3986_BASE, './g', 'http://a/b/c/g'],
120 [self::RFC3986_BASE, 'g/', 'http://a/b/c/g/'],
121 [self::RFC3986_BASE, '/g', 'http://a/g'],
122 [self::RFC3986_BASE, '//g', 'http://g'],
123 [self::RFC3986_BASE, '?y', 'http://a/b/c/d;p?y'],
124 [self::RFC3986_BASE, 'g?y', 'http://a/b/c/g?y'],
125 [self::RFC3986_BASE, '#s', 'http://a/b/c/d;p?q#s'],
126 [self::RFC3986_BASE, 'g#s', 'http://a/b/c/g#s'],
127 [self::RFC3986_BASE, 'g?y#s', 'http://a/b/c/g?y#s'],
128 [self::RFC3986_BASE, ';x', 'http://a/b/c/;x'],
129 [self::RFC3986_BASE, 'g;x', 'http://a/b/c/g;x'],
130 [self::RFC3986_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s'],
131 [self::RFC3986_BASE, '', self::RFC3986_BASE],
132 [self::RFC3986_BASE, '.', 'http://a/b/c/'],
133 [self::RFC3986_BASE, './', 'http://a/b/c/'],
134 [self::RFC3986_BASE, '..', 'http://a/b/'],
135 [self::RFC3986_BASE, '../', 'http://a/b/'],
136 [self::RFC3986_BASE, '../g', 'http://a/b/g'],
137 [self::RFC3986_BASE, '../..', 'http://a/'],
138 [self::RFC3986_BASE, '../../', 'http://a/'],
139 [self::RFC3986_BASE, '../../g', 'http://a/g'],
140 [self::RFC3986_BASE, '../../../g', 'http://a/g'],
141 [self::RFC3986_BASE, '../../../../g', 'http://a/g'],
142 [self::RFC3986_BASE, '/./g', 'http://a/g'],
143 [self::RFC3986_BASE, '/../g', 'http://a/g'],
144 [self::RFC3986_BASE, 'g.', 'http://a/b/c/g.'],
145 [self::RFC3986_BASE, '.g', 'http://a/b/c/.g'],
146 [self::RFC3986_BASE, 'g..', 'http://a/b/c/g..'],
147 [self::RFC3986_BASE, '..g', 'http://a/b/c/..g'],
148 [self::RFC3986_BASE, './../g', 'http://a/b/g'],
149 [self::RFC3986_BASE, 'foo////g', 'http://a/b/c/foo////g'],
150 [self::RFC3986_BASE, './g/.', 'http://a/b/c/g/'],
151 [self::RFC3986_BASE, 'g/./h', 'http://a/b/c/g/h'],
152 [self::RFC3986_BASE, 'g/../h', 'http://a/b/c/h'],
153 [self::RFC3986_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y'],
154 [self::RFC3986_BASE, 'g;x=1/../y', 'http://a/b/c/y'],
155 ['http://u@a/b/c/d;p?q', '.', 'http://u@a/b/c/'],
156 ['http://u:p@a/b/c/d;p?q', '.', 'http://u:p@a/b/c/'],
157 //[self::RFC3986_BASE, 'http:g', 'http:g'],
158 ];
159 }
160
161 public function testAddAndRemoveQueryValues()
162 {
163 $uri = new Uri('http://foo.com/bar');
164 $uri = Uri::withQueryValue($uri, 'a', 'b');
165 $uri = Uri::withQueryValue($uri, 'c', 'd');
166 $uri = Uri::withQueryValue($uri, 'e', null);
167 $this->assertEquals('a=b&c=d&e', $uri->getQuery());
168
169 $uri = Uri::withoutQueryValue($uri, 'c');
170 $uri = Uri::withoutQueryValue($uri, 'e');
171 $this->assertEquals('a=b', $uri->getQuery());
172 $uri = Uri::withoutQueryValue($uri, 'a');
173 $uri = Uri::withoutQueryValue($uri, 'a');
174 $this->assertEquals('', $uri->getQuery());
175 }
176
177 public function testGetAuthorityReturnsCorrectPort()
178 {
179 // HTTPS non-standard port
180 $uri = new Uri('https://foo.co:99');
181 $this->assertEquals('foo.co:99', $uri->getAuthority());
182
183 // HTTP non-standard port
184 $uri = new Uri('http://foo.co:99');
185 $this->assertEquals('foo.co:99', $uri->getAuthority());
186
187 // No scheme
188 $uri = new Uri('foo.co:99');
189 $this->assertEquals('foo.co:99', $uri->getAuthority());
190
191 // No host or port
192 $uri = new Uri('http:');
193 $this->assertEquals('', $uri->getAuthority());
194
195 // No host or port
196 $uri = new Uri('http://foo.co');
197 $this->assertEquals('foo.co', $uri->getAuthority());
198 }
199
200 public function pathTestProvider()
201 {
202 return [
203 // Percent encode spaces.
204 ['http://foo.com/baz bar', 'http://foo.com/baz%20bar'],
205 // Don't encoding something that's already encoded.
206 ['http://foo.com/baz%20bar', 'http://foo.com/baz%20bar'],
207 // Percent encode invalid percent encodings
208 ['http://foo.com/baz%2-bar', 'http://foo.com/baz%252-bar'],
209 // Don't encode path segments
210 ['http://foo.com/baz/bar/bam?a', 'http://foo.com/baz/bar/bam?a'],
211 ['http://foo.com/baz+bar', 'http://foo.com/baz+bar'],
212 ['http://foo.com/baz:bar', 'http://foo.com/baz:bar'],
213 ['http://foo.com/baz@bar', 'http://foo.com/baz@bar'],
214 ['http://foo.com/baz(bar);bam/', 'http://foo.com/baz(bar);bam/'],
215 ['http://foo.com/a-zA-Z0-9.-_~!$&\'()*+,;=:@', 'http://foo.com/a-zA-Z0-9.-_~!$&\'()*+,;=:@'],
216 ];
217 }
218
219 /**
220 * @dataProvider pathTestProvider
221 */
222 public function testUriEncodesPathProperly($input, $output)
223 {
224 $uri = new Uri($input);
225 $this->assertEquals((string) $uri, $output);
226 }
227
228 public function testDoesNotAddPortWhenNoPort()
229 {
230 $this->assertEquals('bar', new Uri('//bar'));
231 $this->assertEquals('bar', (new Uri('//bar'))->getHost());
232 }
233
234 public function testAllowsForRelativeUri()
235 {
236 $uri = (new Uri)->withPath('foo');
237 $this->assertEquals('foo', $uri->getPath());
238 $this->assertEquals('foo', (string) $uri);
239 }
240
241 public function testAddsSlashForRelativeUriStringWithHost()
242 {
243 $uri = (new Uri)->withPath('foo')->withHost('bar.com');
244 $this->assertEquals('foo', $uri->getPath());
245 $this->assertEquals('bar.com/foo', (string) $uri);
246 }
247}
Note: See TracBrowser for help on using the repository browser.