scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Tests;
4  
5 use GuzzleHttp\Query;
6 use GuzzleHttp\Url;
7  
8 /**
9 * @covers GuzzleHttp\Url
10 */
11 class UrlTest extends \PHPUnit_Framework_TestCase
12 {
13 const RFC3986_BASE = "http://a/b/c/d;p?q";
14  
15 public function testEmptyUrl()
16 {
17 $url = Url::fromString('');
18 $this->assertEquals('', (string) $url);
19 }
20  
21 public function testPortIsDeterminedFromScheme()
22 {
23 $this->assertEquals(80, Url::fromString('http://www.test.com/')->getPort());
24 $this->assertEquals(443, Url::fromString('https://www.test.com/')->getPort());
25 $this->assertEquals(21, Url::fromString('ftp://www.test.com/')->getPort());
26 $this->assertEquals(8192, Url::fromString('http://www.test.com:8192/')->getPort());
27 $this->assertEquals(null, Url::fromString('foo://www.test.com/')->getPort());
28 }
29  
30 public function testRemovesDefaultPortWhenSettingScheme()
31 {
32 $url = Url::fromString('http://www.test.com/');
33 $url->setPort(80);
34 $url->setScheme('https');
35 $this->assertEquals(443, $url->getPort());
36 }
37  
38 public function testCloneCreatesNewInternalObjects()
39 {
40 $u1 = Url::fromString('http://www.test.com/');
41 $u2 = clone $u1;
42 $this->assertNotSame($u1->getQuery(), $u2->getQuery());
43 }
44  
45 public function testValidatesUrlPartsInFactory()
46 {
47 $url = Url::fromString('/index.php');
48 $this->assertEquals('/index.php', (string) $url);
49 $this->assertFalse($url->isAbsolute());
50  
51 $url = 'http://michael:test@test.com:80/path/123?q=abc#test';
52 $u = Url::fromString($url);
53 $this->assertEquals('http://michael:test@test.com/path/123?q=abc#test', (string) $u);
54 $this->assertTrue($u->isAbsolute());
55 }
56  
57 public function testAllowsFalsyUrlParts()
58 {
59 $url = Url::fromString('http://a:50/0?0#0');
60 $this->assertSame('a', $url->getHost());
61 $this->assertEquals(50, $url->getPort());
62 $this->assertSame('/0', $url->getPath());
63 $this->assertEquals('0', (string) $url->getQuery());
64 $this->assertSame('0', $url->getFragment());
65 $this->assertEquals('http://a:50/0?0#0', (string) $url);
66  
67 $url = Url::fromString('');
68 $this->assertSame('', (string) $url);
69  
70 $url = Url::fromString('0');
71 $this->assertSame('0', (string) $url);
72 }
73  
74 public function testBuildsRelativeUrlsWithFalsyParts()
75 {
76 $url = Url::buildUrl(['path' => '/0']);
77 $this->assertSame('/0', $url);
78  
79 $url = Url::buildUrl(['path' => '0']);
80 $this->assertSame('0', $url);
81  
82 $url = Url::buildUrl(['host' => '', 'path' => '0']);
83 $this->assertSame('0', $url);
84 }
85  
86 public function testUrlStoresParts()
87 {
88 $url = Url::fromString('http://test:pass@www.test.com:8081/path/path2/?a=1&b=2#fragment');
89 $this->assertEquals('http', $url->getScheme());
90 $this->assertEquals('test', $url->getUsername());
91 $this->assertEquals('pass', $url->getPassword());
92 $this->assertEquals('www.test.com', $url->getHost());
93 $this->assertEquals(8081, $url->getPort());
94 $this->assertEquals('/path/path2/', $url->getPath());
95 $this->assertEquals('fragment', $url->getFragment());
96 $this->assertEquals('a=1&b=2', (string) $url->getQuery());
97  
98 $this->assertEquals(array(
99 'fragment' => 'fragment',
100 'host' => 'www.test.com',
101 'pass' => 'pass',
102 'path' => '/path/path2/',
103 'port' => 8081,
104 'query' => 'a=1&b=2',
105 'scheme' => 'http',
106 'user' => 'test'
107 ), $url->getParts());
108 }
109  
110 public function testHandlesPathsCorrectly()
111 {
112 $url = Url::fromString('http://www.test.com');
113 $this->assertEquals('', $url->getPath());
114 $url->setPath('test');
115 $this->assertEquals('test', $url->getPath());
116  
117 $url->setPath('/test/123/abc');
118 $this->assertEquals(array('', 'test', '123', 'abc'), $url->getPathSegments());
119  
120 $parts = parse_url('http://www.test.com/test');
121 $parts['path'] = '';
122 $this->assertEquals('http://www.test.com', Url::buildUrl($parts));
123 $parts['path'] = 'test';
124 $this->assertEquals('http://www.test.com/test', Url::buildUrl($parts));
125 }
126  
127 public function testAddsQueryIfPresent()
128 {
129 $this->assertEquals('?foo=bar', Url::buildUrl(array(
130 'query' => 'foo=bar'
131 )));
132 }
133  
134 public function testAddsToPath()
135 {
136 // Does nothing here
137 $this->assertEquals('http://e.com/base?a=1', (string) Url::fromString('http://e.com/base?a=1')->addPath(false));
138 $this->assertEquals('http://e.com/base?a=1', (string) Url::fromString('http://e.com/base?a=1')->addPath(''));
139 $this->assertEquals('http://e.com/base?a=1', (string) Url::fromString('http://e.com/base?a=1')->addPath('/'));
140 $this->assertEquals('http://e.com/base/0', (string) Url::fromString('http://e.com/base')->addPath('0'));
141  
142 $this->assertEquals('http://e.com/base/relative?a=1', (string) Url::fromString('http://e.com/base?a=1')->addPath('relative'));
143 $this->assertEquals('http://e.com/base/relative?a=1', (string) Url::fromString('http://e.com/base?a=1')->addPath('/relative'));
144 }
145  
146 /**
147 * URL combination data provider
148 *
149 * @return array
150 */
151 public function urlCombineDataProvider()
152 {
153 return [
154 // Specific test cases
155 ['http://www.example.com/', 'http://www.example.com/', 'http://www.example.com/'],
156 ['http://www.example.com/path', '/absolute', 'http://www.example.com/absolute'],
157 ['http://www.example.com/path', '/absolute?q=2', 'http://www.example.com/absolute?q=2'],
158 ['http://www.example.com/', '?q=1', 'http://www.example.com/?q=1'],
159 ['http://www.example.com/path', 'http://test.com', 'http://test.com'],
160 ['http://www.example.com:8080/path', 'http://test.com', 'http://test.com'],
161 ['http://www.example.com:8080/path', '?q=2#abc', 'http://www.example.com:8080/path?q=2#abc'],
162 ['http://www.example.com/path', 'http://u:a@www.example.com/', 'http://u:a@www.example.com/'],
163 ['/path?q=2', 'http://www.test.com/', 'http://www.test.com/path?q=2'],
164 ['http://api.flickr.com/services/', 'http://www.flickr.com/services/oauth/access_token', 'http://www.flickr.com/services/oauth/access_token'],
165 ['https://www.example.com/path', '//foo.com/abc', 'https://foo.com/abc'],
166 ['https://www.example.com/0/', 'relative/foo', 'https://www.example.com/0/relative/foo'],
167 ['', '0', '0'],
168 // RFC 3986 test cases
169 [self::RFC3986_BASE, 'g:h', 'g:h'],
170 [self::RFC3986_BASE, 'g', 'http://a/b/c/g'],
171 [self::RFC3986_BASE, './g', 'http://a/b/c/g'],
172 [self::RFC3986_BASE, 'g/', 'http://a/b/c/g/'],
173 [self::RFC3986_BASE, '/g', 'http://a/g'],
174 [self::RFC3986_BASE, '//g', 'http://g'],
175 [self::RFC3986_BASE, '?y', 'http://a/b/c/d;p?y'],
176 [self::RFC3986_BASE, 'g?y', 'http://a/b/c/g?y'],
177 [self::RFC3986_BASE, '#s', 'http://a/b/c/d;p?q#s'],
178 [self::RFC3986_BASE, 'g#s', 'http://a/b/c/g#s'],
179 [self::RFC3986_BASE, 'g?y#s', 'http://a/b/c/g?y#s'],
180 [self::RFC3986_BASE, ';x', 'http://a/b/c/;x'],
181 [self::RFC3986_BASE, 'g;x', 'http://a/b/c/g;x'],
182 [self::RFC3986_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s'],
183 [self::RFC3986_BASE, '', self::RFC3986_BASE],
184 [self::RFC3986_BASE, '.', 'http://a/b/c/'],
185 [self::RFC3986_BASE, './', 'http://a/b/c/'],
186 [self::RFC3986_BASE, '..', 'http://a/b/'],
187 [self::RFC3986_BASE, '../', 'http://a/b/'],
188 [self::RFC3986_BASE, '../g', 'http://a/b/g'],
189 [self::RFC3986_BASE, '../..', 'http://a/'],
190 [self::RFC3986_BASE, '../../', 'http://a/'],
191 [self::RFC3986_BASE, '../../g', 'http://a/g'],
192 [self::RFC3986_BASE, '../../../g', 'http://a/g'],
193 [self::RFC3986_BASE, '../../../../g', 'http://a/g'],
194 [self::RFC3986_BASE, '/./g', 'http://a/g'],
195 [self::RFC3986_BASE, '/../g', 'http://a/g'],
196 [self::RFC3986_BASE, 'g.', 'http://a/b/c/g.'],
197 [self::RFC3986_BASE, '.g', 'http://a/b/c/.g'],
198 [self::RFC3986_BASE, 'g..', 'http://a/b/c/g..'],
199 [self::RFC3986_BASE, '..g', 'http://a/b/c/..g'],
200 [self::RFC3986_BASE, './../g', 'http://a/b/g'],
201 [self::RFC3986_BASE, 'foo////g', 'http://a/b/c/foo////g'],
202 [self::RFC3986_BASE, './g/.', 'http://a/b/c/g/'],
203 [self::RFC3986_BASE, 'g/./h', 'http://a/b/c/g/h'],
204 [self::RFC3986_BASE, 'g/../h', 'http://a/b/c/h'],
205 [self::RFC3986_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y'],
206 [self::RFC3986_BASE, 'g;x=1/../y', 'http://a/b/c/y'],
207 [self::RFC3986_BASE, 'http:g', 'http:g'],
208 ];
209 }
210  
211 /**
212 * @dataProvider urlCombineDataProvider
213 */
214 public function testCombinesUrls($a, $b, $c)
215 {
216 $this->assertEquals($c, (string) Url::fromString($a)->combine($b));
217 }
218  
219 public function testHasGettersAndSetters()
220 {
221 $url = Url::fromString('http://www.test.com/');
222 $this->assertEquals('example.com', $url->setHost('example.com')->getHost());
223 $this->assertEquals('8080', $url->setPort(8080)->getPort());
224 $this->assertEquals('/foo/bar', $url->setPath('/foo/bar')->getPath());
225 $this->assertEquals('a', $url->setPassword('a')->getPassword());
226 $this->assertEquals('b', $url->setUsername('b')->getUsername());
227 $this->assertEquals('abc', $url->setFragment('abc')->getFragment());
228 $this->assertEquals('https', $url->setScheme('https')->getScheme());
229 $this->assertEquals('a=123', (string) $url->setQuery('a=123')->getQuery());
230 $this->assertEquals('https://b:a@example.com:8080/foo/bar?a=123#abc', (string) $url);
231 $this->assertEquals('b=boo', (string) $url->setQuery(new Query(array(
232 'b' => 'boo'
233 )))->getQuery());
234 $this->assertEquals('https://b:a@example.com:8080/foo/bar?b=boo#abc', (string) $url);
235 }
236  
237 public function testSetQueryAcceptsArray()
238 {
239 $url = Url::fromString('http://www.test.com');
240 $url->setQuery(array('a' => 'b'));
241 $this->assertEquals('http://www.test.com?a=b', (string) $url);
242 }
243  
244 /**
245 * @expectedException \InvalidArgumentException
246 */
247 public function testQueryMustBeValid()
248 {
249 $url = Url::fromString('http://www.test.com');
250 $url->setQuery(false);
251 }
252  
253 public function urlProvider()
254 {
255 return array(
256 array('/foo/..', '/'),
257 array('//foo//..', '//foo/'),
258 array('/foo//', '/foo//'),
259 array('/foo/../..', '/'),
260 array('/foo/../.', '/'),
261 array('/./foo/..', '/'),
262 array('/./foo', '/foo'),
263 array('/./foo/', '/foo/'),
264 array('*', '*'),
265 array('/foo', '/foo'),
266 array('/abc/123/../foo/', '/abc/foo/'),
267 array('/a/b/c/./../../g', '/a/g'),
268 array('/b/c/./../../g', '/g'),
269 array('/b/c/./../../g', '/g'),
270 array('/c/./../../g', '/g'),
271 array('/./../../g', '/g'),
272 array('foo', 'foo'),
273 );
274 }
275  
276 /**
277 * @dataProvider urlProvider
278 */
279 public function testRemoveDotSegments($path, $result)
280 {
281 $url = Url::fromString('http://www.example.com');
282 $url->setPath($path)->removeDotSegments();
283 $this->assertEquals($result, $url->getPath());
284 }
285  
286 public function testSettingHostWithPortModifiesPort()
287 {
288 $url = Url::fromString('http://www.example.com');
289 $url->setHost('foo:8983');
290 $this->assertEquals('foo', $url->getHost());
291 $this->assertEquals(8983, $url->getPort());
292 }
293  
294 /**
295 * @expectedException \InvalidArgumentException
296 */
297 public function testValidatesUrlCanBeParsed()
298 {
299 Url::fromString('foo:////');
300 }
301  
302 public function testConvertsSpecialCharsInPathWhenCastingToString()
303 {
304 $url = Url::fromString('http://foo.com/baz bar?a=b');
305 $url->addPath('?');
306 $this->assertEquals('http://foo.com/baz%20bar/%3F?a=b', (string) $url);
307 }
308 }