2012-01-14 13:57:05 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Guzzle\Tests\Http;
|
|
|
|
|
|
|
|
use Guzzle\Common\Collection;
|
2012-09-05 21:23:40 -07:00
|
|
|
use Guzzle\Log\ClosureLogAdapter;
|
2012-06-28 13:44:29 -07:00
|
|
|
use Guzzle\Parser\UriTemplate\UriTemplate;
|
2012-01-14 13:57:05 -06:00
|
|
|
use Guzzle\Http\Message\Response;
|
2012-09-05 21:23:40 -07:00
|
|
|
use Guzzle\Plugin\Log\LogPlugin;
|
|
|
|
use Guzzle\Plugin\Mock\MockPlugin;
|
2012-01-14 13:57:05 -06:00
|
|
|
use Guzzle\Http\Curl\CurlMulti;
|
|
|
|
use Guzzle\Http\Client;
|
2013-02-22 22:07:19 -08:00
|
|
|
use Guzzle\Common\Version;
|
2012-01-14 13:57:05 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group server
|
2013-05-29 22:53:59 -07:00
|
|
|
* @covers Guzzle\Http\Client
|
2012-01-14 13:57:05 -06:00
|
|
|
*/
|
|
|
|
class ClientTest extends \Guzzle\Tests\GuzzleTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return LogPlugin
|
|
|
|
*/
|
|
|
|
private function getLogPlugin()
|
|
|
|
{
|
|
|
|
return new LogPlugin(new ClosureLogAdapter(
|
|
|
|
function($message, $priority, $extras = null) {
|
|
|
|
echo $message . ' ' . $priority . ' ' . implode(' - ', (array) $extras) . "\n";
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAcceptsConfig()
|
|
|
|
{
|
|
|
|
$client = new Client('http://www.google.com/');
|
|
|
|
$this->assertEquals('http://www.google.com/', $client->getBaseUrl());
|
|
|
|
$this->assertSame($client, $client->setConfig(array(
|
|
|
|
'test' => '123'
|
|
|
|
)));
|
|
|
|
$this->assertEquals(array('test' => '123'), $client->getConfig()->getAll());
|
|
|
|
$this->assertEquals('123', $client->getConfig('test'));
|
2012-10-28 21:08:38 -07:00
|
|
|
$this->assertSame($client, $client->setBaseUrl('http://www.test.com/{test}'));
|
2012-01-14 13:57:05 -06:00
|
|
|
$this->assertEquals('http://www.test.com/123', $client->getBaseUrl());
|
2012-10-28 21:08:38 -07:00
|
|
|
$this->assertEquals('http://www.test.com/{test}', $client->getBaseUrl(false));
|
2012-01-14 13:57:05 -06:00
|
|
|
|
|
|
|
try {
|
|
|
|
$client->setConfig(false);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDescribesEvents()
|
|
|
|
{
|
|
|
|
$this->assertEquals(array('client.create_request'), Client::getAllEvents());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructorCanAcceptConfig()
|
|
|
|
{
|
|
|
|
$client = new Client('http://www.test.com/', array(
|
|
|
|
'data' => '123'
|
|
|
|
));
|
|
|
|
$this->assertEquals('123', $client->getConfig('data'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanUseCollectionAsConfig()
|
|
|
|
{
|
|
|
|
$client = new Client('http://www.google.com/');
|
|
|
|
$client->setConfig(new Collection(array(
|
|
|
|
'api' => 'v1',
|
|
|
|
'key' => 'value',
|
|
|
|
'base_url' => 'http://www.google.com/'
|
|
|
|
)));
|
|
|
|
$this->assertEquals('v1', $client->getConfig('api'));
|
|
|
|
}
|
|
|
|
|
2012-02-07 23:13:00 -06:00
|
|
|
public function testExpandsUriTemplatesUsingConfig()
|
2012-01-14 13:57:05 -06:00
|
|
|
{
|
|
|
|
$client = new Client('http://www.google.com/');
|
2013-06-09 13:41:02 -07:00
|
|
|
$client->setConfig(array('api' => 'v1', 'key' => 'value', 'foo' => 'bar'));
|
|
|
|
$ref = new \ReflectionMethod($client, 'expandTemplate');
|
|
|
|
$ref->setAccessible(true);
|
|
|
|
$this->assertEquals('Testing...api/v1/key/value', $ref->invoke($client, 'Testing...api/{api}/key/{key}'));
|
2012-01-14 13:57:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientAttachersObserversToRequests()
|
|
|
|
{
|
|
|
|
$this->getServer()->flush();
|
|
|
|
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
|
|
|
|
|
|
|
$client = new Client($this->getServer()->getUrl());
|
|
|
|
$logPlugin = $this->getLogPlugin();
|
|
|
|
$client->getEventDispatcher()->addSubscriber($logPlugin);
|
|
|
|
|
|
|
|
// Get a request from the client and ensure the the observer was
|
|
|
|
// attached to the new request
|
|
|
|
$request = $client->createRequest();
|
|
|
|
$this->assertTrue($this->hasSubscriber($request, $logPlugin));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientReturnsValidBaseUrls()
|
|
|
|
{
|
2012-10-28 21:08:38 -07:00
|
|
|
$client = new Client('http://www.{foo}.{data}/', array(
|
2012-01-14 13:57:05 -06:00
|
|
|
'data' => '123',
|
|
|
|
'foo' => 'bar'
|
|
|
|
));
|
|
|
|
$this->assertEquals('http://www.bar.123/', $client->getBaseUrl());
|
|
|
|
$client->setBaseUrl('http://www.google.com/');
|
|
|
|
$this->assertEquals('http://www.google.com/', $client->getBaseUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientAddsCurlOptionsToRequests()
|
|
|
|
{
|
|
|
|
$client = new Client('http://www.test.com/', array(
|
|
|
|
'api' => 'v1',
|
|
|
|
// Adds the option using the curl values
|
2012-09-24 11:34:30 -07:00
|
|
|
'curl.options' => array(
|
|
|
|
'CURLOPT_HTTPAUTH' => 'CURLAUTH_DIGEST',
|
|
|
|
'abc' => 'foo',
|
|
|
|
'blacklist' => 'abc',
|
2012-09-29 23:55:18 -07:00
|
|
|
'debug' => true
|
2012-09-24 11:34:30 -07:00
|
|
|
)
|
2012-01-14 13:57:05 -06:00
|
|
|
));
|
|
|
|
|
|
|
|
$request = $client->createRequest();
|
|
|
|
$options = $request->getCurlOptions();
|
|
|
|
$this->assertEquals(CURLAUTH_DIGEST, $options->get(CURLOPT_HTTPAUTH));
|
2012-09-24 11:34:30 -07:00
|
|
|
$this->assertEquals('foo', $options->get('abc'));
|
|
|
|
$this->assertEquals('abc', $options->get('blacklist'));
|
2012-09-29 23:55:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientAllowsFineGrainedSslControlButIsSecureByDefault()
|
|
|
|
{
|
2012-09-30 15:52:19 -07:00
|
|
|
$client = new Client('https://www.secure.com/');
|
2012-09-29 23:55:18 -07:00
|
|
|
|
|
|
|
// secure by default
|
|
|
|
$request = $client->createRequest();
|
|
|
|
$options = $request->getCurlOptions();
|
|
|
|
$this->assertTrue($options->get(CURLOPT_SSL_VERIFYPEER));
|
|
|
|
|
|
|
|
// set a capath if you prefer
|
2012-09-30 15:52:19 -07:00
|
|
|
$client = new Client('https://www.secure.com/');
|
2012-09-29 23:55:18 -07:00
|
|
|
$client->setSslVerification(__DIR__);
|
|
|
|
$request = $client->createRequest();
|
|
|
|
$options = $request->getCurlOptions();
|
|
|
|
$this->assertSame(__DIR__, $options->get(CURLOPT_CAPATH));
|
|
|
|
}
|
2013-05-29 22:53:59 -07:00
|
|
|
|
2012-09-30 15:52:19 -07:00
|
|
|
public function testConfigSettingsControlSslConfiguration()
|
|
|
|
{
|
|
|
|
// Use the default ca certs on the system
|
|
|
|
$client = new Client('https://www.secure.com/', array('ssl.certificate_authority' => 'system'));
|
|
|
|
$this->assertNull($client->getConfig('curl.options'));
|
|
|
|
// Can set the cacert value as well
|
|
|
|
$client = new Client('https://www.secure.com/', array('ssl.certificate_authority' => false));
|
|
|
|
$options = $client->getConfig('curl.options');
|
|
|
|
$this->assertArrayNotHasKey(CURLOPT_CAINFO, $options);
|
|
|
|
$this->assertSame(false, $options[CURLOPT_SSL_VERIFYPEER]);
|
2013-01-25 22:40:55 +00:00
|
|
|
$this->assertSame(2, $options[CURLOPT_SSL_VERIFYHOST]);
|
2012-09-30 15:52:19 -07:00
|
|
|
}
|
2012-09-29 23:55:18 -07:00
|
|
|
|
|
|
|
public function testClientAllowsUnsafeOperationIfRequested()
|
|
|
|
{
|
|
|
|
// be really unsafe if you insist
|
|
|
|
$client = new Client('https://www.secure.com/', array(
|
|
|
|
'api' => 'v1'
|
|
|
|
));
|
|
|
|
|
|
|
|
$client->setSslVerification(false);
|
|
|
|
$request = $client->createRequest();
|
|
|
|
$options = $request->getCurlOptions();
|
2012-09-24 11:34:30 -07:00
|
|
|
$this->assertFalse($options->get(CURLOPT_SSL_VERIFYPEER));
|
2012-09-29 23:55:18 -07:00
|
|
|
$this->assertNull($options->get(CURLOPT_CAINFO));
|
|
|
|
}
|
|
|
|
|
2013-03-31 23:35:10 -07:00
|
|
|
/**
|
|
|
|
* @expectedException \Guzzle\Common\Exception\RuntimeException
|
|
|
|
*/
|
|
|
|
public function testThrowsExceptionForInvalidCertificate()
|
|
|
|
{
|
|
|
|
$client = new Client('https://www.secure.com/');
|
|
|
|
$client->setSslVerification('/path/to/missing/file');
|
|
|
|
}
|
|
|
|
|
2012-09-29 23:55:18 -07:00
|
|
|
public function testClientAllowsSettingSpecificSslCaInfo()
|
|
|
|
{
|
|
|
|
// set a file other than the provided cacert.pem
|
|
|
|
$client = new Client('https://www.secure.com/', array(
|
|
|
|
'api' => 'v1'
|
|
|
|
));
|
|
|
|
|
|
|
|
$client->setSslVerification(__FILE__);
|
|
|
|
$request = $client->createRequest();
|
|
|
|
$options = $request->getCurlOptions();
|
|
|
|
$this->assertSame(__FILE__, $options->get(CURLOPT_CAINFO));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Guzzle\Common\Exception\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testClientPreventsInadvertentInsecureVerifyHostSetting()
|
|
|
|
{
|
|
|
|
// set a file other than the provided cacert.pem
|
|
|
|
$client = new Client('https://www.secure.com/', array(
|
|
|
|
'api' => 'v1'
|
|
|
|
));
|
|
|
|
$client->setSslVerification(__FILE__, true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Guzzle\Common\Exception\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testClientPreventsInvalidVerifyPeerSetting()
|
|
|
|
{
|
|
|
|
// set a file other than the provided cacert.pem
|
|
|
|
$client = new Client('https://www.secure.com/', array(
|
|
|
|
'api' => 'v1'
|
|
|
|
));
|
|
|
|
$client->setSslVerification(__FILE__, 'yes');
|
2012-01-14 13:57:05 -06:00
|
|
|
}
|
|
|
|
|
2012-04-24 18:22:20 -07:00
|
|
|
public function testClientAddsParamsToRequests()
|
|
|
|
{
|
2013-06-09 11:14:47 -07:00
|
|
|
Version::$emitWarnings = false;
|
2012-04-24 18:22:20 -07:00
|
|
|
$client = new Client('http://www.example.com', array(
|
|
|
|
'api' => 'v1',
|
2012-09-24 11:34:30 -07:00
|
|
|
'request.params' => array(
|
|
|
|
'foo' => 'bar',
|
|
|
|
'baz' => 'jar'
|
|
|
|
)
|
2012-04-24 18:22:20 -07:00
|
|
|
));
|
|
|
|
$request = $client->createRequest();
|
|
|
|
$this->assertEquals('bar', $request->getParams()->get('foo'));
|
|
|
|
$this->assertEquals('jar', $request->getParams()->get('baz'));
|
2013-06-09 11:14:47 -07:00
|
|
|
Version::$emitWarnings = true;
|
2012-04-24 18:22:20 -07:00
|
|
|
}
|
|
|
|
|
2012-01-14 13:57:05 -06:00
|
|
|
public function urlProvider()
|
|
|
|
{
|
|
|
|
$u = $this->getServer()->getUrl() . 'base/';
|
|
|
|
$u2 = $this->getServer()->getUrl() . 'base?z=1';
|
|
|
|
return array(
|
|
|
|
array($u, '', $u),
|
|
|
|
array($u, 'relative/path/to/resource', $u . 'relative/path/to/resource'),
|
|
|
|
array($u, 'relative/path/to/resource?a=b&c=d', $u . 'relative/path/to/resource?a=b&c=d'),
|
|
|
|
array($u, '/absolute/path/to/resource', $this->getServer()->getUrl() . 'absolute/path/to/resource'),
|
|
|
|
array($u, '/absolute/path/to/resource?a=b&c=d', $this->getServer()->getUrl() . 'absolute/path/to/resource?a=b&c=d'),
|
|
|
|
array($u2, '/absolute/path/to/resource?a=b&c=d', $this->getServer()->getUrl() . 'absolute/path/to/resource?a=b&c=d'),
|
2013-04-08 13:05:09 -07:00
|
|
|
array($u2, 'relative/path/to/resource', $this->getServer()->getUrl() . 'base/relative/path/to/resource'),
|
|
|
|
array($u2, 'relative/path/to/resource?another=query', $this->getServer()->getUrl() . 'base/relative/path/to/resource?another=query')
|
2012-01-14 13:57:05 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider urlProvider
|
|
|
|
*/
|
|
|
|
public function testBuildsRelativeUrls($baseUrl, $url, $result)
|
|
|
|
{
|
|
|
|
$client = new Client($baseUrl);
|
|
|
|
$this->assertEquals($client->get($url)->getUrl(), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllowsConfigsToBeChangedAndInjectedInBaseUrl()
|
|
|
|
{
|
2012-10-28 21:08:38 -07:00
|
|
|
$client = new Client('http://{a}/{b}');
|
2012-01-14 13:57:05 -06:00
|
|
|
$this->assertEquals('http:///', $client->getBaseUrl());
|
2012-10-28 21:08:38 -07:00
|
|
|
$this->assertEquals('http://{a}/{b}', $client->getBaseUrl(false));
|
2012-01-14 13:57:05 -06:00
|
|
|
$client->setConfig(array(
|
|
|
|
'a' => 'test.com',
|
|
|
|
'b' => 'index.html'
|
|
|
|
));
|
|
|
|
$this->assertEquals('http://test.com/index.html', $client->getBaseUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreatesRequestsWithDefaultValues()
|
|
|
|
{
|
|
|
|
$client = new Client($this->getServer()->getUrl() . 'base');
|
|
|
|
|
|
|
|
// Create a GET request
|
|
|
|
$request = $client->createRequest();
|
|
|
|
$this->assertEquals('GET', $request->getMethod());
|
|
|
|
$this->assertEquals($client->getBaseUrl(), $request->getUrl());
|
|
|
|
|
|
|
|
// Create a DELETE request
|
|
|
|
$request = $client->createRequest('DELETE');
|
|
|
|
$this->assertEquals('DELETE', $request->getMethod());
|
|
|
|
$this->assertEquals($client->getBaseUrl(), $request->getUrl());
|
|
|
|
|
|
|
|
// Create a HEAD request with custom headers
|
|
|
|
$request = $client->createRequest('HEAD', 'http://www.test.com/');
|
|
|
|
$this->assertEquals('HEAD', $request->getMethod());
|
|
|
|
$this->assertEquals('http://www.test.com/', $request->getUrl());
|
|
|
|
|
|
|
|
// Create a PUT request
|
|
|
|
$request = $client->createRequest('PUT');
|
|
|
|
$this->assertEquals('PUT', $request->getMethod());
|
|
|
|
|
|
|
|
// Create a PUT request with injected config
|
|
|
|
$client->getConfig()->set('a', 1)->set('b', 2);
|
2012-10-28 21:08:38 -07:00
|
|
|
$request = $client->createRequest('PUT', '/path/{a}?q={b}');
|
2012-01-14 13:57:05 -06:00
|
|
|
$this->assertEquals($request->getUrl(), $this->getServer()->getUrl() . 'path/1?q=2');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientHasHelperMethodsForCreatingRequests()
|
|
|
|
{
|
|
|
|
$url = $this->getServer()->getUrl();
|
|
|
|
$client = new Client($url . 'base');
|
|
|
|
$this->assertEquals('GET', $client->get()->getMethod());
|
|
|
|
$this->assertEquals('PUT', $client->put()->getMethod());
|
|
|
|
$this->assertEquals('POST', $client->post()->getMethod());
|
|
|
|
$this->assertEquals('HEAD', $client->head()->getMethod());
|
|
|
|
$this->assertEquals('DELETE', $client->delete()->getMethod());
|
|
|
|
$this->assertEquals('OPTIONS', $client->options()->getMethod());
|
2012-03-19 22:03:41 -05:00
|
|
|
$this->assertEquals('PATCH', $client->patch()->getMethod());
|
2012-01-14 13:57:05 -06:00
|
|
|
$this->assertEquals($url . 'base/abc', $client->get('abc')->getUrl());
|
|
|
|
$this->assertEquals($url . 'zxy', $client->put('/zxy')->getUrl());
|
|
|
|
$this->assertEquals($url . 'zxy?a=b', $client->post('/zxy?a=b')->getUrl());
|
|
|
|
$this->assertEquals($url . 'base?a=b', $client->head('?a=b')->getUrl());
|
|
|
|
$this->assertEquals($url . 'base?a=b', $client->delete('/base?a=b')->getUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientInjectsConfigsIntoUrls()
|
|
|
|
{
|
|
|
|
$client = new Client('http://www.test.com/api/v1', array(
|
|
|
|
'test' => '123'
|
|
|
|
));
|
2012-10-28 21:08:38 -07:00
|
|
|
$request = $client->get('relative/{test}');
|
2012-01-14 13:57:05 -06:00
|
|
|
$this->assertEquals('http://www.test.com/api/v1/relative/123', $request->getUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllowsEmptyBaseUrl()
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
$request = $client->get('http://www.google.com/');
|
|
|
|
$this->assertEquals('http://www.google.com/', $request->getUrl());
|
|
|
|
$request->setResponse(new Response(200), true);
|
|
|
|
$request->send();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllowsCustomCurlMultiObjects()
|
|
|
|
{
|
|
|
|
$mock = $this->getMock('Guzzle\\Http\\Curl\\CurlMulti', array('add', 'send'));
|
|
|
|
$mock->expects($this->once())
|
2013-06-07 23:08:40 -07:00
|
|
|
->method('add')
|
|
|
|
->will($this->returnSelf());
|
2012-01-14 13:57:05 -06:00
|
|
|
$mock->expects($this->once())
|
2013-06-07 23:08:40 -07:00
|
|
|
->method('send')
|
|
|
|
->will($this->returnSelf());
|
2012-01-14 13:57:05 -06:00
|
|
|
|
|
|
|
$client = new Client();
|
|
|
|
$client->setCurlMulti($mock);
|
|
|
|
|
|
|
|
$request = $client->get();
|
|
|
|
$request->setResponse(new Response(200), true);
|
|
|
|
$client->send($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientSendsMultipleRequests()
|
|
|
|
{
|
|
|
|
$client = new Client($this->getServer()->getUrl());
|
|
|
|
$mock = new MockPlugin();
|
|
|
|
|
|
|
|
$responses = array(
|
|
|
|
new Response(200),
|
|
|
|
new Response(201),
|
|
|
|
new Response(202)
|
|
|
|
);
|
|
|
|
|
|
|
|
$mock->addResponse($responses[0]);
|
|
|
|
$mock->addResponse($responses[1]);
|
|
|
|
$mock->addResponse($responses[2]);
|
|
|
|
|
|
|
|
$client->getEventDispatcher()->addSubscriber($mock);
|
|
|
|
|
|
|
|
$requests = array(
|
|
|
|
$client->get(),
|
|
|
|
$client->head(),
|
|
|
|
$client->put('/', null, 'test')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
$responses[0],
|
|
|
|
$responses[1],
|
|
|
|
$responses[2]
|
|
|
|
), $client->send($requests));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClientSendsSingleRequest()
|
|
|
|
{
|
|
|
|
$client = new Client($this->getServer()->getUrl());
|
|
|
|
$mock = new MockPlugin();
|
|
|
|
$response = new Response(200);
|
|
|
|
$mock->addResponse($response);
|
|
|
|
$client->getEventDispatcher()->addSubscriber($mock);
|
|
|
|
$this->assertEquals($response, $client->send($client->get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-29 22:53:59 -07:00
|
|
|
* @expectedException \Guzzle\Http\Exception\BadResponseException
|
2012-01-14 13:57:05 -06:00
|
|
|
*/
|
|
|
|
public function testClientThrowsExceptionForSingleRequest()
|
|
|
|
{
|
|
|
|
$client = new Client($this->getServer()->getUrl());
|
|
|
|
$mock = new MockPlugin();
|
|
|
|
$response = new Response(404);
|
|
|
|
$mock->addResponse($response);
|
|
|
|
$client->getEventDispatcher()->addSubscriber($mock);
|
|
|
|
$client->send($client->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-29 22:53:59 -07:00
|
|
|
* @expectedException \Guzzle\Common\Exception\ExceptionCollection
|
2012-01-14 13:57:05 -06:00
|
|
|
*/
|
|
|
|
public function testClientThrowsExceptionForMultipleRequests()
|
|
|
|
{
|
|
|
|
$client = new Client($this->getServer()->getUrl());
|
|
|
|
$mock = new MockPlugin();
|
|
|
|
$mock->addResponse(new Response(200));
|
|
|
|
$mock->addResponse(new Response(404));
|
|
|
|
$client->getEventDispatcher()->addSubscriber($mock);
|
|
|
|
$client->send(array($client->get(), $client->head()));
|
|
|
|
}
|
2012-02-07 23:13:00 -06:00
|
|
|
|
|
|
|
public function testQueryStringsAreNotDoubleEncoded()
|
|
|
|
{
|
|
|
|
$client = new Client('http://test.com', array(
|
|
|
|
'path' => array('foo', 'bar'),
|
|
|
|
'query' => 'hi there',
|
|
|
|
'data' => array(
|
|
|
|
'test' => 'a&b'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
$request = $client->get('{/path*}{?query,data*}');
|
|
|
|
$this->assertEquals('http://test.com/foo/bar?query=hi%20there&test=a%26b', $request->getUrl());
|
|
|
|
$this->assertEquals('hi there', $request->getQuery()->get('query'));
|
|
|
|
$this->assertEquals('a&b', $request->getQuery()->get('test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testQueryStringsAreNotDoubleEncodedUsingAbsolutePaths()
|
|
|
|
{
|
|
|
|
$client = new Client('http://test.com', array(
|
|
|
|
'path' => array('foo', 'bar'),
|
|
|
|
'query' => 'hi there',
|
|
|
|
));
|
|
|
|
$request = $client->get('http://test.com{?query}');
|
2013-01-30 17:15:05 -06:00
|
|
|
$this->assertEquals('http://test.com?query=hi%20there', $request->getUrl());
|
2012-02-07 23:13:00 -06:00
|
|
|
$this->assertEquals('hi there', $request->getQuery()->get('query'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllowsUriTemplateInjection()
|
|
|
|
{
|
2013-06-09 13:41:02 -07:00
|
|
|
$client = new Client('http://test.com');
|
|
|
|
$ref = new \ReflectionMethod($client, 'getUriTemplate');
|
|
|
|
$ref->setAccessible(true);
|
|
|
|
$a = $ref->invoke($client);
|
|
|
|
$this->assertSame($a, $ref->invoke($client));
|
2012-02-07 23:13:00 -06:00
|
|
|
$client->setUriTemplate(new UriTemplate());
|
2013-06-09 13:41:02 -07:00
|
|
|
$this->assertNotSame($a, $ref->invoke($client));
|
2012-02-07 23:13:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllowsCustomVariablesWhenExpandingTemplates()
|
|
|
|
{
|
2013-06-09 13:41:02 -07:00
|
|
|
$client = new Client('http://test.com', array('test' => 'hi'));
|
|
|
|
$ref = new \ReflectionMethod($client, 'expandTemplate');
|
|
|
|
$ref->setAccessible(true);
|
|
|
|
$uri = $ref->invoke($client, 'http://{test}{?query*}', array('query' => array('han' => 'solo')));
|
2012-02-07 23:13:00 -06:00
|
|
|
$this->assertEquals('http://hi?han=solo', $uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUriArrayAllowsCustomTemplateVariables()
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
$vars = array(
|
|
|
|
'var' => 'hi'
|
|
|
|
);
|
|
|
|
$this->assertEquals('/hi', (string) $client->createRequest('GET', array('/{var}', $vars))->getUrl());
|
|
|
|
$this->assertEquals('/hi', (string) $client->get(array('/{var}', $vars))->getUrl());
|
|
|
|
$this->assertEquals('/hi', (string) $client->put(array('/{var}', $vars))->getUrl());
|
|
|
|
$this->assertEquals('/hi', (string) $client->post(array('/{var}', $vars))->getUrl());
|
|
|
|
$this->assertEquals('/hi', (string) $client->head(array('/{var}', $vars))->getUrl());
|
|
|
|
$this->assertEquals('/hi', (string) $client->options(array('/{var}', $vars))->getUrl());
|
|
|
|
}
|
2012-03-25 22:23:47 -05:00
|
|
|
|
|
|
|
public function testAllowsDefaultHeaders()
|
|
|
|
{
|
2013-06-09 01:25:35 -07:00
|
|
|
Version::$emitWarnings = false;
|
|
|
|
$default = array('X-Test' => 'Hi!');
|
|
|
|
$other = array('X-Other' => 'Foo');
|
2012-03-25 22:23:47 -05:00
|
|
|
|
|
|
|
$client = new Client();
|
|
|
|
$client->setDefaultHeaders($default);
|
|
|
|
$this->assertEquals($default, $client->getDefaultHeaders()->getAll());
|
|
|
|
$client->setDefaultHeaders(new Collection($default));
|
|
|
|
$this->assertEquals($default, $client->getDefaultHeaders()->getAll());
|
|
|
|
|
|
|
|
$request = $client->createRequest('GET', null, $other);
|
|
|
|
$this->assertEquals('Hi!', $request->getHeader('X-Test'));
|
|
|
|
$this->assertEquals('Foo', $request->getHeader('X-Other'));
|
|
|
|
|
|
|
|
$request = $client->createRequest('GET', null, new Collection($other));
|
|
|
|
$this->assertEquals('Hi!', $request->getHeader('X-Test'));
|
|
|
|
$this->assertEquals('Foo', $request->getHeader('X-Other'));
|
|
|
|
|
|
|
|
$request = $client->createRequest('GET');
|
|
|
|
$this->assertEquals('Hi!', $request->getHeader('X-Test'));
|
2013-06-09 01:25:35 -07:00
|
|
|
Version::$emitWarnings = true;
|
2012-03-25 22:23:47 -05:00
|
|
|
}
|
2013-02-08 00:04:54 +01:00
|
|
|
|
|
|
|
public function testDontReuseCurlMulti()
|
|
|
|
{
|
|
|
|
$client1 = new Client();
|
|
|
|
$client2 = new Client();
|
|
|
|
$this->assertNotSame($client1->getCurlMulti(), $client2->getCurlMulti());
|
|
|
|
}
|
2013-02-22 22:07:19 -08:00
|
|
|
|
|
|
|
public function testGetDefaultUserAgent()
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
$agent = $this->readAttribute($client, 'userAgent');
|
|
|
|
$version = curl_version();
|
|
|
|
$testAgent = sprintf('Guzzle/%s curl/%s PHP/%s', Version::VERSION, $version['version'], PHP_VERSION);
|
|
|
|
$this->assertEquals($agent, $testAgent);
|
|
|
|
|
|
|
|
$client->setUserAgent('foo');
|
|
|
|
$this->assertEquals('foo', $this->readAttribute($client, 'userAgent'));
|
|
|
|
}
|
2013-03-11 15:42:27 -07:00
|
|
|
|
|
|
|
public function testOverwritesUserAgent()
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
$request = $client->createRequest('GET', 'http://www.foo.com', array('User-agent' => 'foo'));
|
|
|
|
$this->assertEquals('foo', (string) $request->getHeader('User-Agent'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUsesDefaultUserAgent()
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
$request = $client->createRequest('GET', 'http://www.foo.com');
|
|
|
|
$this->assertContains('Guzzle/', (string) $request->getHeader('User-Agent'));
|
|
|
|
}
|
2013-06-09 01:25:35 -07:00
|
|
|
|
|
|
|
public function testCanSetDefaultRequestOptions()
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
$client->getConfig()->set('request.options', array(
|
|
|
|
'query' => array('test' => '123', 'other' => 'abc'),
|
|
|
|
'headers' => array('Foo' => 'Bar', 'Baz' => 'Bam')
|
|
|
|
));
|
|
|
|
$request = $client->createRequest('GET', 'http://www.foo.com?test=hello', array('Foo' => 'Test'));
|
|
|
|
// Explicit options on a request should overrule default options
|
|
|
|
$this->assertEquals('Test', (string) $request->getHeader('Foo'));
|
|
|
|
$this->assertEquals('hello', $request->getQuery()->get('test'));
|
|
|
|
// Default options should still be set
|
|
|
|
$this->assertEquals('abc', $request->getQuery()->get('other'));
|
|
|
|
$this->assertEquals('Bam', (string) $request->getHeader('Baz'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanSetSetOptionsOnRequests()
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
$request = $client->createRequest('GET', 'http://www.foo.com?test=hello', array('Foo' => 'Test'), null, array(
|
|
|
|
'cookies' => array('michael' => 'test')
|
|
|
|
));
|
|
|
|
$this->assertEquals('test', $request->getCookie('michael'));
|
|
|
|
}
|
2013-06-10 17:17:36 -07:00
|
|
|
|
|
|
|
public function testHasDefaultOptionsHelperMethods()
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
// With path
|
|
|
|
$client->setDefaultOption('headers/foo', 'bar');
|
|
|
|
$this->assertEquals('bar', $client->getDefaultOption('headers/foo'));
|
|
|
|
// With simple key
|
|
|
|
$client->setDefaultOption('allow_redirects', false);
|
|
|
|
$this->assertFalse($client->getDefaultOption('allow_redirects'));
|
2013-06-26 11:46:43 -07:00
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'headers' => array('foo' => 'bar'),
|
|
|
|
'allow_redirects' => false
|
|
|
|
), $client->getConfig('request.options'));
|
|
|
|
|
|
|
|
$request = $client->get('/');
|
|
|
|
$this->assertEquals('bar', $request->getHeader('foo'));
|
2013-06-10 17:17:36 -07:00
|
|
|
}
|
2013-06-23 10:19:47 -07:00
|
|
|
|
|
|
|
public function testHeadCanUseOptions()
|
|
|
|
{
|
|
|
|
$client = new Client();
|
|
|
|
$head = $client->head('http://www.foo.com', array(), array('query' => array('foo' => 'bar')));
|
|
|
|
$this->assertEquals('bar', $head->getQuery()->get('foo'));
|
|
|
|
}
|
2012-04-21 00:23:07 -07:00
|
|
|
}
|