2015-03-23 21:13:28 -07:00
|
|
|
<?php
|
|
|
|
namespace GuzzleHttp\Test\Handler;
|
|
|
|
|
2015-04-25 15:03:28 -07:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
2015-03-23 21:13:28 -07:00
|
|
|
use GuzzleHttp\Handler\CurlHandler;
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use GuzzleHttp\Tests\Server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \GuzzleHttp\Handler\CurlHandler
|
|
|
|
*/
|
|
|
|
class CurlHandlerTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
if (!function_exists('curl_reset')) {
|
|
|
|
$this->markTestSkipped('curl_reset() is not available');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getHandler($options = [])
|
|
|
|
{
|
|
|
|
return new CurlHandler($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \GuzzleHttp\Exception\ConnectException
|
|
|
|
* @expectedExceptionMessage cURL
|
|
|
|
*/
|
|
|
|
public function testCreatesCurlErrors()
|
|
|
|
{
|
|
|
|
$handler = new CurlHandler();
|
|
|
|
$request = new Request('GET', 'http://localhost:123');
|
|
|
|
$handler($request, ['timeout' => 0.001, 'connect_timeout' => 0.001])->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReusesHandles()
|
|
|
|
{
|
|
|
|
Server::flush();
|
|
|
|
$response = new response(200);
|
|
|
|
Server::enqueue([$response, $response]);
|
|
|
|
$a = new CurlHandler();
|
|
|
|
$request = new Request('GET', Server::$url);
|
|
|
|
$a($request, []);
|
|
|
|
$a($request, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoesSleep()
|
|
|
|
{
|
|
|
|
$response = new response(200);
|
|
|
|
Server::enqueue([$response]);
|
|
|
|
$a = new CurlHandler();
|
|
|
|
$request = new Request('GET', Server::$url);
|
|
|
|
$s = microtime(true);
|
|
|
|
$a($request, ['delay' => 0.1])->wait();
|
|
|
|
$this->assertGreaterThan(0.0001, microtime(true) - $s);
|
|
|
|
}
|
2015-04-25 15:03:28 -07:00
|
|
|
|
|
|
|
public function testCreatesCurlErrorsWithContext()
|
|
|
|
{
|
|
|
|
$handler = new CurlHandler();
|
|
|
|
$request = new Request('GET', 'http://localhost:123');
|
|
|
|
$called = false;
|
|
|
|
$p = $handler($request, ['timeout' => 0.001, 'connect_timeout' => 0.001])
|
|
|
|
->otherwise(function (ConnectException $e) use (&$called) {
|
|
|
|
$called = true;
|
|
|
|
$this->assertArrayHasKey('errno', $e->getHandlerContext());
|
|
|
|
});
|
|
|
|
$p->wait();
|
|
|
|
$this->assertTrue($called);
|
|
|
|
}
|
2015-03-23 21:13:28 -07:00
|
|
|
}
|