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