1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-26 11:05:28 +01:00
guzzle/tests/Guzzle/Tests/Http/UrlTest.php

235 lines
8.5 KiB
PHP
Raw Normal View History

<?php
namespace Guzzle\Tests\Http;
use Guzzle\Http\QueryString;
use Guzzle\Http\Url;
use Guzzle\Parser\ParserRegistry;
class UrlTest extends \Guzzle\Tests\GuzzleTestCase
{
/**
* @covers Guzzle\Http\Url::getPort
*/
public function testPortIsDeterminedFromScheme()
{
$this->assertEquals(80, Url::factory('http://www.test.com/')->getPort());
$this->assertEquals(443, Url::factory('https://www.test.com/')->getPort());
$this->assertEquals(null, Url::factory('ftp://www.test.com/')->getPort());
$this->assertEquals(8192, Url::factory('http://www.test.com:8192/')->getPort());
}
/**
* @covers Guzzle\Http\Url::__clone
*/
public function testCloneCreatesNewInternalObjects()
{
$u1 = Url::factory('http://www.test.com/');
$u2 = clone $u1;
$this->assertNotSame($u1->getQuery(), $u2->getQuery());
}
/**
* @covers Guzzle\Http\Url::__construct
* @covers Guzzle\Http\Url::factory
* @covers Guzzle\Http\Url::__toString
* @covers Guzzle\Http\Url::isAbsolute
*/
public function testValidatesUrlPartsInFactory()
{
$url = Url::factory('/index.php');
$this->assertEquals('/index.php', (string) $url);
$this->assertFalse($url->isAbsolute());
$url = 'http://michael:test@test.com:80/path/123?q=abc#test';
$u = Url::factory($url);
$this->assertEquals('http://michael:test@test.com/path/123?q=abc#test', (string) $u);
$this->assertTrue($u->isAbsolute());
}
/**
* @covers Guzzle\Http\Url
*/
public function testUrlStoresParts()
{
$url = Url::factory('http://test:pass@www.test.com:8081/path/path2/?a=1&b=2#fragment');
$this->assertEquals('http', $url->getScheme());
$this->assertEquals('test', $url->getUsername());
$this->assertEquals('pass', $url->getPassword());
$this->assertEquals('www.test.com', $url->getHost());
$this->assertEquals(8081, $url->getPort());
$this->assertEquals('/path/path2/', $url->getPath());
$this->assertEquals('fragment', $url->getFragment());
$this->assertEquals('?a=1&b=2', (string) $url->getQuery());
$this->assertEquals(array(
'fragment' => 'fragment',
'host' => 'www.test.com',
'pass' => 'pass',
'path' => '/path/path2/',
'port' => 8081,
'query' => '?a=1&b=2',
'query_prefix' => '?',
'scheme' => 'http',
'user' => 'test'
), $url->getParts());
}
/**
* @covers Guzzle\Http\Url::setPath
* @covers Guzzle\Http\Url::getPath
* @covers Guzzle\Http\Url::getPathSegments
* @covers Guzzle\Http\Url::buildUrl
*/
public function testHandlesPathsCorrectly()
{
$url = Url::factory('http://www.test.com');
$this->assertEquals('/', $url->getPath());
$url->setPath('test');
$this->assertEquals('/test', $url->getPath());
$url->setPath('/test/123/abc');
$this->assertEquals(array('test', '123', 'abc'), $url->getPathSegments());
$parts = parse_url('http://www.test.com/test');
$parts['path'] = '';
$this->assertEquals('http://www.test.com/', Url::buildUrl($parts));
$parts['path'] = 'test';
$this->assertEquals('http://www.test.com/test', Url::buildUrl($parts));
}
Changes to support a Guzzle\Http\Parser namespace [Common] Adding keySearch method to Collection [Http] Moving POST curl option logic from EntityEnclosingRequest to the curl factory method Directly using the HTTP request object when creating curl handles in the factory method No longer adding a read callback when sending POSTs through CURLOPT_POSTFIELDS In an effort to mitigate random segfaults and bus errors, using a queue for removing curl and close handles from a multi object so that they are only removed when the multi handle has finished sending all requests. Calling reset() each time a curl multi handle has finished all requests. EntityEnclosingRequest::getPostFields() now returns a Collection object Simplifying the EntityEnclosingRequest::getPostFiles method Adding an array cache to header objects Moving the header comparison DSL from the Tests namespace to Guzzle\Http\Message\HeaderComparison, and adding tests. Adding message, cookie, and url parser interfaces and default implementations. Using a parser registry to manage globally registered parsers. Removing parsing from Response and RequestFactory. Renaming protocol_version to version Changing the Guzzle\Http\Message\Response::setProtocol() method to accept a protocol and version in separate args. Simplifying the chunked encoding handling in RequestFactory Moving cookie parsing out of the cookie plugin and into Guzzle\Http\Parser\Cookie\CookieParser Removing regexps from the cookie parser and simply using stripos Moving the parseQuery method out of Url and on to QueryString::fromString() as a static factory method. Adding more logging to node.js test webserver Adding pecl_http message parser. Installing pecl when travis boots up
2012-05-10 21:30:09 -07:00
/**
* @covers Guzzle\Http\Url::buildUrl
*/
public function testAddsQueryStringIfPresent()
{
$this->assertEquals('/?foo=bar', Url::buildUrl(array(
'query' => 'foo=bar'
)));
}
/**
* @covers Guzzle\Http\Url::addPath
*/
public function testAddsToPath()
{
// Does nothing here
$this->assertEquals('http://e.com/base?a=1', (string) Url::factory('http://e.com/base?a=1')->addPath(false));
$this->assertEquals('http://e.com/base?a=1', (string) Url::factory('http://e.com/base?a=1')->addPath(''));
$this->assertEquals('http://e.com/base?a=1', (string) Url::factory('http://e.com/base?a=1')->addPath('/'));
$this->assertEquals('http://e.com/base/relative?a=1', (string) Url::factory('http://e.com/base?a=1')->addPath('relative'));
$this->assertEquals('http://e.com/base/relative?a=1', (string) Url::factory('http://e.com/base?a=1')->addPath('/relative'));
}
/**
* URL combination data provider
*
* @return array
*/
public function urlCombineDataProvider()
{
return array(
array('http://www.example.com/', 'http://www.example.com/', 'http://www.example.com/'),
array('http://www.example.com/path', '/absolute', 'http://www.example.com/absolute'),
array('http://www.example.com/path', '/absolute?q=2', 'http://www.example.com/absolute?q=2'),
array('http://www.example.com/path', 'more', 'http://www.example.com/path/more'),
array('http://www.example.com/path', 'more?q=1', 'http://www.example.com/path/more?q=1'),
array('http://www.example.com/', '?q=1', 'http://www.example.com/?q=1'),
array('http://www.example.com/path', 'http://test.com', 'http://test.com/path'),
array('http://www.example.com:8080/path', 'http://test.com', 'http://test.com/path'),
array('http://www.example.com:8080/path', '?q=2#abc', 'http://www.example.com:8080/path?q=2#abc'),
array('http://u:a@www.example.com/path', 'test', 'http://u:a@www.example.com/path/test'),
array('http://www.example.com/path', 'http://u:a@www.example.com/', 'http://u:a@www.example.com/path'),
array('/path?q=2', 'http://www.test.com/', 'http://www.test.com/path?q=2'),
);
}
/**
* @covers Guzzle\Http\Url::combine
* @dataProvider urlCombineDataProvider
*/
public function testCombinesUrls($a, $b, $c)
{
$this->assertEquals($c, (string) Url::factory($a)->combine($b));
}
/**
* @covers Guzzle\Http\Url
*/
public function testHasGettersAndSetters()
{
$url = Url::factory('http://www.test.com/');
$this->assertEquals('example.com', $url->setHost('example.com')->getHost());
$this->assertEquals('8080', $url->setPort(8080)->getPort());
$this->assertEquals('/foo/bar', $url->setPath(array('foo', 'bar'))->getPath());
$this->assertEquals('a', $url->setPassword('a')->getPassword());
$this->assertEquals('b', $url->setUsername('b')->getUsername());
$this->assertEquals('abc', $url->setFragment('abc')->getFragment());
$this->assertEquals('https', $url->setScheme('https')->getScheme());
$this->assertEquals('?a=123', (string) $url->setQuery('a=123')->getQuery());
$this->assertEquals('https://b:a@example.com:8080/foo/bar?a=123#abc', (string)$url);
$this->assertEquals('?b=boo', (string) $url->setQuery(new QueryString(array(
'b' => 'boo'
)))->getQuery());
$this->assertEquals('https://b:a@example.com:8080/foo/bar?b=boo#abc', (string)$url);
}
2011-08-28 19:33:42 -05:00
/**
* @covers Guzzle\Http\Url::setQuery
*/
public function testSetQueryAcceptsArray()
{
$url = Url::factory('http://www.test.com');
$url->setQuery(array('a' => 'b'));
$this->assertEquals('http://www.test.com/?a=b', (string) $url);
}
function urlProvider()
{
return array(
array('/foo/..', '/'),
array('//foo//..', '/'),
array('/foo/../..', '/'),
array('/foo/../.', '/'),
array('/./foo/..', '/'),
array('/./foo', '/foo'),
array('/./foo/', '/foo/'),
array('/./foo/bar/baz/pho/../..', '/foo/bar'),
array('*', '*')
);
}
/**
* @covers Guzzle\Http\Url::normalizePath
* @dataProvider urlProvider
*/
public function testNormalizesPaths($path, $result)
{
$url = Url::factory('http://www.example.com/');
$url->setPath($path)->normalizePath();
$this->assertEquals($result, $url->getPath());
}
Breaking / Potentially breaking changes: 1. Adopting a marker interface for Guzzle exceptions. A. All exceptions emitted from Guzzle are now wrapped with a Guzzle namespaced exception. B. Guzzle\Common\GuzzleExceptionInterface was renamed to Guzzle\Common\GuzzleException C. Guzzle\Common\ExceptionCollection was renamed to Guzzle\Common\Exception\ExceptionCollection 2. Using Header objects for Request and Response objects A. When you call $request->getHeader('X'), you will get back a Guzzle\Http\Message\Header object that contains all of the headers that case insensitively match. This object can be cast to a string or iterated like an array. You can pass true in the second argument to retrieve the header as a string. B. Removing the old Guzzle\Common\Collection based searching arguments from most of the request and response header methods. All retrievals are case-insensitive and return Header objects. 3. Changing the two headers added by the cache plugin to just one header with key and ttl. 4. Changing Guzzle\Http\Message\Response::factory() to fromMessage(). 5. Removing the NullObject return value from ServiceDescriptions and instead simply returning null New Features / enhancements: 1. Adding Guzzle\Http\Message\AbstractMessage::addHeaders() 2. Making it simpler to create service descriptions using a unified factory method that delegates to other factories. 3. Better handling of ports and hosts in Guzzle\Http\Url Note: This is a noisy diff because I'm removing trailing whitespace and adding a new line at the end of each source file.
2012-04-21 00:23:07 -07:00
/**
* @covers Guzzle\Http\Url::setHost
*/
public function testSettingHostWithPortModifiesPort()
{
$url = Url::factory('http://www.example.com');
$url->setHost('foo:8983');
$this->assertEquals('foo', $url->getHost());
$this->assertEquals(8983, $url->getPort());
}
/**
* @covers Guzzle\Http\Url::buildUrl
*/
public function testUrlOnlyContainsHashWhenHashIsNotEmpty()
{
$url = Url::factory('http://www.example.com/');
$url->setFragment('');
$this->assertEquals('http://www.example.com/', (string) $url);
}
Breaking / Potentially breaking changes: 1. Adopting a marker interface for Guzzle exceptions. A. All exceptions emitted from Guzzle are now wrapped with a Guzzle namespaced exception. B. Guzzle\Common\GuzzleExceptionInterface was renamed to Guzzle\Common\GuzzleException C. Guzzle\Common\ExceptionCollection was renamed to Guzzle\Common\Exception\ExceptionCollection 2. Using Header objects for Request and Response objects A. When you call $request->getHeader('X'), you will get back a Guzzle\Http\Message\Header object that contains all of the headers that case insensitively match. This object can be cast to a string or iterated like an array. You can pass true in the second argument to retrieve the header as a string. B. Removing the old Guzzle\Common\Collection based searching arguments from most of the request and response header methods. All retrievals are case-insensitive and return Header objects. 3. Changing the two headers added by the cache plugin to just one header with key and ttl. 4. Changing Guzzle\Http\Message\Response::factory() to fromMessage(). 5. Removing the NullObject return value from ServiceDescriptions and instead simply returning null New Features / enhancements: 1. Adding Guzzle\Http\Message\AbstractMessage::addHeaders() 2. Making it simpler to create service descriptions using a unified factory method that delegates to other factories. 3. Better handling of ports and hosts in Guzzle\Http\Url Note: This is a noisy diff because I'm removing trailing whitespace and adding a new line at the end of each source file.
2012-04-21 00:23:07 -07:00
}