1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602:
<?php
namespace Guzzle\Tests\Http;
use Guzzle\Common\Collection;
use Guzzle\Log\ClosureLogAdapter;
use Guzzle\Parser\UriTemplate\UriTemplate;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Log\LogPlugin;
use Guzzle\Plugin\Mock\MockPlugin;
use Guzzle\Http\Curl\CurlMulti;
use Guzzle\Http\Client;
use Guzzle\Common\Version;
class ClientTest extends \Guzzle\Tests\GuzzleTestCase
{
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'));
$this->assertSame($client, $client->setBaseUrl('http://www.test.com/{test}'));
$this->assertEquals('http://www.test.com/123', $client->getBaseUrl());
$this->assertEquals('http://www.test.com/{test}', $client->getBaseUrl(false));
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'));
}
public function testExpandsUriTemplatesUsingConfig()
{
$client = new Client('http://www.google.com/');
$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}'));
}
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);
$request = $client->createRequest();
$this->assertTrue($this->hasSubscriber($request, $logPlugin));
}
public function testClientReturnsValidBaseUrls()
{
$client = new Client('http://www.{foo}.{data}/', array(
'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',
'curl.options' => array(
'CURLOPT_HTTPAUTH' => 'CURLAUTH_DIGEST',
'abc' => 'foo',
'blacklist' => 'abc',
'debug' => true
)
));
$request = $client->createRequest();
$options = $request->getCurlOptions();
$this->assertEquals(CURLAUTH_DIGEST, $options->get(CURLOPT_HTTPAUTH));
$this->assertEquals('foo', $options->get('abc'));
$this->assertEquals('abc', $options->get('blacklist'));
}
public function testClientAllowsFineGrainedSslControlButIsSecureByDefault()
{
$client = new Client('https://www.secure.com/');
$request = $client->createRequest();
$options = $request->getCurlOptions();
$this->assertTrue($options->get(CURLOPT_SSL_VERIFYPEER));
$client = new Client('https://www.secure.com/');
$client->setSslVerification(__DIR__);
$request = $client->createRequest();
$options = $request->getCurlOptions();
$this->assertSame(__DIR__, $options->get(CURLOPT_CAPATH));
}
public function testConfigSettingsControlSslConfiguration()
{
$client = new Client('https://www.secure.com/', array('ssl.certificate_authority' => 'system'));
$this->assertNull($client->getConfig('curl.options'));
$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]);
$this->assertSame(0, $options[CURLOPT_SSL_VERIFYHOST]);
}
public function testClientAllowsUnsafeOperationIfRequested()
{
$client = new Client('https://www.secure.com/', array(
'api' => 'v1'
));
$client->setSslVerification(false);
$request = $client->createRequest();
$options = $request->getCurlOptions();
$this->assertFalse($options->get(CURLOPT_SSL_VERIFYPEER));
$this->assertNull($options->get(CURLOPT_CAINFO));
}
public function testThrowsExceptionForInvalidCertificate()
{
$client = new Client('https://www.secure.com/');
$client->setSslVerification('/path/to/missing/file');
}
public function testClientAllowsSettingSpecificSslCaInfo()
{
$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));
}
public function testClientPreventsInadvertentInsecureVerifyHostSetting()
{
$client = new Client('https://www.secure.com/', array(
'api' => 'v1'
));
$client->setSslVerification(__FILE__, true, true);
}
public function testClientPreventsInvalidVerifyPeerSetting()
{
$client = new Client('https://www.secure.com/', array(
'api' => 'v1'
));
$client->setSslVerification(__FILE__, 'yes');
}
public function testClientAddsParamsToRequests()
{
Version::$emitWarnings = false;
$client = new Client('http://www.example.com', array(
'api' => 'v1',
'request.params' => array(
'foo' => 'bar',
'baz' => 'jar'
)
));
$request = $client->createRequest();
$this->assertEquals('bar', $request->getParams()->get('foo'));
$this->assertEquals('jar', $request->getParams()->get('baz'));
Version::$emitWarnings = true;
}
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&z=1'),
array($u2, 'relative/path/to/resource', $this->getServer()->getUrl() . 'base/relative/path/to/resource?z=1'),
array($u2, 'relative/path/to/resource?another=query', $this->getServer()->getUrl() . 'base/relative/path/to/resource?another=query&z=1')
);
}
public function testBuildsRelativeUrls($baseUrl, $url, $result)
{
$client = new Client($baseUrl);
$this->assertEquals($result, $client->get($url)->getUrl());
}
public function testAllowsConfigsToBeChangedAndInjectedInBaseUrl()
{
$client = new Client('http://{a}/{b}');
$this->assertEquals('http:///', $client->getBaseUrl());
$this->assertEquals('http://{a}/{b}', $client->getBaseUrl(false));
$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');
$request = $client->createRequest();
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals($client->getBaseUrl(), $request->getUrl());
$request = $client->createRequest('DELETE');
$this->assertEquals('DELETE', $request->getMethod());
$this->assertEquals($client->getBaseUrl(), $request->getUrl());
$request = $client->createRequest('HEAD', 'http://www.test.com/');
$this->assertEquals('HEAD', $request->getMethod());
$this->assertEquals('http://www.test.com/', $request->getUrl());
$request = $client->createRequest('PUT');
$this->assertEquals('PUT', $request->getMethod());
$client->getConfig()->set('a', 1)->set('b', 2);
$request = $client->createRequest('PUT', '/path/{a}?q={b}');
$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());
$this->assertEquals('PATCH', $client->patch()->getMethod());
$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'
));
$request = $client->get('relative/{test}');
$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())
->method('add')
->will($this->returnSelf());
$mock->expects($this->once())
->method('send')
->will($this->returnSelf());
$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()));
}
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());
}
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()));
}
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}');
$this->assertEquals('http://test.com?query=hi%20there', $request->getUrl());
$this->assertEquals('hi there', $request->getQuery()->get('query'));
}
public function testAllowsUriTemplateInjection()
{
$client = new Client('http://test.com');
$ref = new \ReflectionMethod($client, 'getUriTemplate');
$ref->setAccessible(true);
$a = $ref->invoke($client);
$this->assertSame($a, $ref->invoke($client));
$client->setUriTemplate(new UriTemplate());
$this->assertNotSame($a, $ref->invoke($client));
}
public function testAllowsCustomVariablesWhenExpandingTemplates()
{
$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')));
$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());
}
public function testAllowsDefaultHeaders()
{
Version::$emitWarnings = false;
$default = array('X-Test' => 'Hi!');
$other = array('X-Other' => 'Foo');
$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'));
Version::$emitWarnings = true;
}
public function testDontReuseCurlMulti()
{
$client1 = new Client();
$client2 = new Client();
$this->assertNotSame($client1->getCurlMulti(), $client2->getCurlMulti());
}
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'));
}
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'));
}
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'));
$this->assertEquals('Test', (string) $request->getHeader('Foo'));
$this->assertEquals('hello', $request->getQuery()->get('test'));
$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'));
}
public function testHasDefaultOptionsHelperMethods()
{
$client = new Client();
$client->setDefaultOption('headers/foo', 'bar');
$this->assertEquals('bar', $client->getDefaultOption('headers/foo'));
$client->setDefaultOption('allow_redirects', false);
$this->assertFalse($client->getDefaultOption('allow_redirects'));
$this->assertEquals(array(
'headers' => array('foo' => 'bar'),
'allow_redirects' => false
), $client->getConfig('request.options'));
$request = $client->get('/');
$this->assertEquals('bar', $request->getHeader('foo'));
}
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'));
}
public function testCanSetRelativeUrlStartingWithHttp()
{
$client = new Client('http://www.foo.com');
$this->assertEquals(
'http://www.foo.com/httpfoo',
$client->createRequest('GET', 'httpfoo')->getUrl()
);
}
}