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:
<?php
namespace Guzzle\Tests\Http\Curl;
use Guzzle\Common\Event;
use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Http\Exception\MultiTransferException;
use Guzzle\Http\Client;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Message\RequestFactory;
use Guzzle\Http\Curl\CurlMulti;
use Guzzle\Http\Exception\CurlException;
use Guzzle\Tests\Mock\MockMulti;
class CurlMultiTest extends \Guzzle\Tests\GuzzleTestCase
{
private $multi;
protected function setUp()
{
parent::setUp();
$this->multi = new MockMulti();
}
public function tearDown()
{
unset($this->multi);
}
public function testConstructorCreateMultiHandle()
{
$this->assertInternalType('resource', $this->multi->getHandle());
$this->assertEquals('curl_multi', get_resource_type($this->multi->getHandle()));
}
public function testDestructorClosesMultiHandle()
{
$handle = $this->multi->getHandle();
$this->multi->__destruct();
$this->assertFalse(is_resource($handle));
}
public function testRequestsCanBeAddedAndCounted()
{
$multi = new CurlMulti();
$request1 = new Request('GET', 'http://www.google.com/');
$multi->add($request1);
$this->assertEquals(array($request1), $multi->all());
$request2 = new Request('POST', 'http://www.google.com/');
$multi->add($request2);
$this->assertEquals(array($request1, $request2), $multi->all());
$this->assertEquals(2, count($multi));
}
public function testRequestsCanBeRemoved()
{
$request1 = new Request('GET', 'http://www.google.com/');
$this->multi->add($request1);
$request2 = new Request('PUT', 'http://www.google.com/');
$this->multi->add($request2);
$this->assertEquals(array($request1, $request2), $this->multi->all());
$this->assertTrue($this->multi->remove($request1));
$this->assertFalse($this->multi->remove($request1));
$this->assertEquals(array($request2), $this->multi->all());
}
public function testsResetRemovesRequestsAndResetsState()
{
$this->multi->add(new Request('GET', 'http://www.google.com/'));
$this->multi->reset();
$this->assertEquals(array(), $this->multi->all());
}
public function testSendsRequestsThroughCurl()
{
$this->getServer()->enqueue(array(
"HTTP/1.1 204 No content\r\n" .
"Content-Length: 0\r\n" .
"Server: Jetty(6.1.3)\r\n\r\n",
"HTTP/1.1 200 OK\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
"Content-Length: 4\r\n" .
"Server: Jetty(6.1.3)\r\n\r\n" .
"data"
));
$request1 = new Request('GET', $this->getServer()->getUrl());
$request2 = new Request('GET', $this->getServer()->getUrl());
$this->multi->add($request1);
$this->multi->add($request2);
$this->multi->send();
$response1 = $request1->getResponse();
$response2 = $request2->getResponse();
$this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response1);
$this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response2);
$this->assertTrue($response1->getBody(true) == 'data' || $response2->getBody(true) == 'data');
$this->assertTrue($response1->getBody(true) == '' || $response2->getBody(true) == '');
$this->assertTrue($response1->getStatusCode() == '204' || $response2->getStatusCode() == '204');
$this->assertNotEquals((string) $response1, (string) $response2);
}
public function testSendsThroughCurlAndAggregatesRequestExceptions()
{
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
"Content-Length: 4\r\n" .
"Server: Jetty(6.1.3)\r\n" .
"\r\n" .
"data",
"HTTP/1.1 204 No content\r\n" .
"Content-Length: 0\r\n" .
"Server: Jetty(6.1.3)\r\n" .
"\r\n",
"HTTP/1.1 404 Not Found\r\n" .
"Content-Length: 0\r\n" .
"\r\n"
));
$request1 = new Request('GET', $this->getServer()->getUrl());
$request2 = new Request('HEAD', $this->getServer()->getUrl());
$request3 = new Request('GET', $this->getServer()->getUrl());
$this->multi->add($request1);
$this->multi->add($request2);
$this->multi->add($request3);
try {
$this->multi->send();
$this->fail('MultiTransferException not thrown when aggregating request exceptions');
} catch (MultiTransferException $e) {
$this->assertTrue($e->containsRequest($request1));
$this->assertTrue($e->containsRequest($request2));
$this->assertTrue($e->containsRequest($request3));
$this->assertInstanceOf('ArrayIterator', $e->getIterator());
$this->assertEquals(1, count($e));
$exceptions = $e->getIterator();
$response1 = $request1->getResponse();
$response2 = $request2->getResponse();
$response3 = $request3->getResponse();
$this->assertNotEquals((string) $response1, (string) $response2);
$this->assertNotEquals((string) $response3, (string) $response1);
$this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response1);
$this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response2);
$this->assertInstanceOf('Guzzle\\Http\\Message\\Response', $response3);
$failed = $exceptions[0]->getResponse();
$this->assertEquals(404, $failed->getStatusCode());
$this->assertEquals(1, count($e));
foreach ($e as $except) {
$this->assertEquals($failed, $except->getResponse());
}
$this->assertEquals(1, count($e->getFailedRequests()));
$this->assertEquals(2, count($e->getSuccessfulRequests()));
$this->assertEquals(3, count($e->getAllRequests()));
}
}
public function testCurlErrorsAreCaught()
{
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
try {
$request = RequestFactory::getInstance()->create('GET', 'http://127.0.0.1:9876/');
$request->setClient(new Client());
$request->getCurlOptions()->set(CURLOPT_FRESH_CONNECT, true);
$request->getCurlOptions()->set(CURLOPT_FORBID_REUSE, true);
$request->getCurlOptions()->set(CURLOPT_CONNECTTIMEOUT_MS, 5);
$request->send();
$this->fail('CurlException not thrown');
} catch (CurlException $e) {
$m = $e->getMessage();
$this->assertContains('[curl] ', $m);
$this->assertContains('[url] http://127.0.0.1:9876/', $m);
$this->assertInternalType('array', $e->getCurlInfo());
}
}
public function testRemovesQueuedRequests()
{
$request = RequestFactory::getInstance()->create('GET', 'http://127.0.0.1:9876/');
$r = new Response(200);
$request->setClient(new Client());
$request->setResponse($r, true);
$this->multi->add($request);
$this->multi->send();
$this->assertSame($r, $request->getResponse());
}
public function testRemovesQueuedRequestsAddedInTransit()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"));
$client = new Client($this->getServer()->getUrl());
$r = $client->get();
$r->getEventDispatcher()->addListener('request.receive.status_line', function (Event $event) use ($client) {
$request = $client->get()->setResponse(new Response(200), true);
$request->send();
});
$r->send();
$this->assertEquals(1, count($this->getServer()->getReceivedRequests(false)));
}
public function testCatchesExceptionsBeforeSendingSingleRequest()
{
$client = new Client($this->getServer()->getUrl());
$multi = new CurlMulti();
$client->setCurlMulti($multi);
$request = $client->get();
$request->getEventDispatcher()->addListener('request.before_send', function() {
throw new \RuntimeException('Testing!');
});
try {
$request->send();
$this->fail('Did not throw');
} catch (\RuntimeException $e) {
$this->assertEquals(0, count($multi));
}
}
public function testCatchesExceptionsBeforeSendingMultipleRequests()
{
$client = new Client($this->getServer()->getUrl());
$request = $client->get();
$request->getEventDispatcher()->addListener('request.before_send', function() {
throw new \RuntimeException('Thrown before sending!');
});
$client->send(array($request));
}
public function testCatchesExceptionsWhenRemovingQueuedRequests()
{
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
$client = new Client($this->getServer()->getUrl());
$r = $client->get();
$r->getEventDispatcher()->addListener('request.sent', function() use ($client) {
$client->get()->setResponse(new Response(404), true)->send();
});
try {
$r->send();
$this->fail('Did not throw');
} catch (BadResponseException $e) {
$this->assertCount(0, $client->getCurlMulti());
}
}
public function testCatchesExceptionsWhenRemovingQueuedRequestsBeforeSending()
{
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
$client = new Client($this->getServer()->getUrl());
$r = $client->get();
$r->getEventDispatcher()->addListener('request.before_send', function() use ($client) {
$client->get()->setResponse(new Response(404), true)->send();
});
try {
$r->send();
$this->fail('Did not throw');
} catch (BadResponseException $e) {
$this->assertCount(0, $client->getCurlMulti());
}
}
public function testDoesNotCatchRandomExceptionsThrownDuringPerform()
{
$client = new Client($this->getServer()->getUrl());
$multi = $this->getMock('Guzzle\\Http\\Curl\\CurlMulti', array('perform'));
$multi->expects($this->once())
->method('perform')
->will($this->throwException(new \RuntimeException('test')));
$multi->add($client->get());
$multi->send();
}
public function testDoesNotSendRequestsDecliningToBeSent()
{
if (!defined('CURLOPT_TIMEOUT_MS')) {
$this->markTestSkipped('Update curl');
}
$client = new Client('http://127.0.0.1:123', array(
'curl.CURLOPT_PORT' => 123,
'curl.CURLOPT_CONNECTTIMEOUT_MS' => 1,
));
$request = $client->get();
$multi = new CurlMulti();
$multi->add($request);
$request->getEventDispatcher()->addListener('request.exception', function(Event $event) use ($multi) {
$retries = $event['request']->getParams()->get('retries');
if ($retries == 0) {
$event['request']->setState('transfer');
$event['request']->getParams()->set('retries', 1);
$multi->remove($event['request']);
$multi->add($event['request']);
}
});
try {
$multi->send();
$this->fail('Did not throw an exception at all!?!');
} catch (\Exception $e) {
$this->assertEquals(1, $request->getParams()->get('retries'));
}
}
public function testDoesNotThrowExceptionsWhenRequestsRecoverWithRetry()
{
$this->getServer()->flush();
$client = new Client($this->getServer()->getUrl());
$request = $client->get();
$request->getEventDispatcher()->addListener('request.before_send', function(Event $event) {
$event['request']->setResponse(new Response(200));
});
$multi = new CurlMulti();
$multi->add($request);
$multi->send();
$this->assertEquals(0, count($this->getServer()->getReceivedRequests(false)));
}
public function testDoesNotThrowExceptionsWhenRequestsRecoverWithSuccess()
{
$client = new Client('http://127.0.0.1:123');
$request = $client->get();
$request->getCurlOptions()->set(CURLOPT_TIMEOUT_MS, 1)->set(CURLOPT_CONNECTTIMEOUT_MS, 1);
$request->getEventDispatcher()->addListener('request.exception', function(Event $event) use (&$count) {
$event['request']->setResponse(new Response(200));
});
$multi = new CurlMulti();
$multi->add($request);
$multi->send();
$this->assertEquals(200, $request->getResponse()->getStatusCode());
}
public function testHardResetReopensMultiHandle()
{
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
));
$stream = fopen('php://temp', 'w+');
$client = new Client($this->getServer()->getUrl());
$client->getConfig()->set('curl.CURLOPT_VERBOSE', true)->set('curl.CURLOPT_STDERR', $stream);
$request = $client->get();
$multi = new CurlMulti();
$multi->add($request);
$multi->send();
$multi->reset(true);
$multi->add($request);
$multi->send();
rewind($stream);
$this->assertNotContains('Re-using existing connection', stream_get_contents($stream));
}
public function testThrowsMeaningfulExceptionsForCurlMultiErrors()
{
$multi = new CurlMulti();
$reflector = new \ReflectionMethod('Guzzle\Http\Curl\CurlMulti', 'checkCurlResult');
$reflector->setAccessible(true);
$reflector->invoke($multi, 0);
try {
$reflector->invoke($multi, CURLM_BAD_HANDLE);
$this->fail('Expected an exception here');
} catch (CurlException $e) {
$this->assertContains('The passed-in handle is not a valid CURLM handle.', $e->getMessage());
$this->assertContains('CURLM_BAD_HANDLE', $e->getMessage());
$this->assertContains(strval(CURLM_BAD_HANDLE), $e->getMessage());
}
try {
$reflector->invoke($multi, 255);
$this->fail('Expected an exception here');
} catch (CurlException $e) {
$this->assertEquals('Unexpected cURL error: 255', $e->getMessage());
}
}
public function testRequestBeforeSendIncludesContentLengthHeaderIfEmptyBody()
{
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
$request = new Request('PUT', $this->getServer()->getUrl());
$that = $this;
$request->getEventDispatcher()->addListener('request.before_send', function ($event) use ($that) {
$that->assertEquals(0, $event['request']->getHeader('Content-Length'));
});
$this->multi->add($request);
$this->multi->send();
}
public function testRemovesConflictingTransferEncodingHeader()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ntest",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
));
$client = new Client($this->getServer()->getUrl());
$request = $client->put('/', null, fopen($this->getServer()->getUrl(), 'r'));
$request->setHeader('Content-Length', 4);
$request->send();
$received = $this->getServer()->getReceivedRequests(true);
$this->assertFalse($received[1]->hasHeader('Transfer-Encoding'));
$this->assertEquals(4, (string) $received[1]->getHeader('Content-Length'));
}
}