/vendor/guzzlehttp/guzzle/tests/Adapter/Curl/AbstractCurl.php |
@@ -0,0 +1,92 @@ |
<?php |
|
namespace GuzzleHttp\Tests\Adapter\Curl; |
|
use GuzzleHttp\Adapter\Transaction; |
use GuzzleHttp\Client; |
use GuzzleHttp\Event\CompleteEvent; |
use GuzzleHttp\Event\ErrorEvent; |
use GuzzleHttp\Exception\RequestException; |
use GuzzleHttp\Message\Request; |
use GuzzleHttp\Message\Response; |
use GuzzleHttp\Tests\Server; |
use GuzzleHttp\Url; |
|
abstract class AbstractCurl extends \PHPUnit_Framework_TestCase |
{ |
abstract protected function getAdapter($factory = null, $options = []); |
|
public function testSendsRequest() |
{ |
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\nFoo: bar\r\nContent-Length: 0\r\n\r\n"); |
$t = new Transaction(new Client(), new Request('GET', Server::$url)); |
$a = $this->getAdapter(); |
$response = $a->send($t); |
$this->assertEquals(200, $response->getStatusCode()); |
$this->assertEquals('bar', $response->getHeader('Foo')); |
} |
|
/** |
* @expectedException \GuzzleHttp\Exception\RequestException |
*/ |
public function testCatchesErrorWhenPreparing() |
{ |
$r = new Request('GET', Server::$url); |
$f = $this->getMockBuilder('GuzzleHttp\Adapter\Curl\CurlFactory') |
->setMethods(['__invoke']) |
->getMock(); |
$f->expects($this->once()) |
->method('__invoke') |
->will($this->throwException(new RequestException('foo', $r))); |
|
$t = new Transaction(new Client(), $r); |
$a = $this->getAdapter(null, ['handle_factory' => $f]); |
$a->send($t); |
} |
|
public function testDispatchesAfterSendEvent() |
{ |
Server::flush(); |
Server::enqueue("HTTP/1.1 201 OK\r\nContent-Length: 0\r\n\r\n"); |
$r = new Request('GET', Server::$url); |
$t = new Transaction(new Client(), $r); |
$a = $this->getAdapter(); |
$ev = null; |
$r->getEmitter()->on('complete', function (CompleteEvent $e) use (&$ev) { |
$ev = $e; |
$e->intercept(new Response(200, ['Foo' => 'bar'])); |
}); |
$response = $a->send($t); |
$this->assertEquals(200, $response->getStatusCode()); |
$this->assertEquals('bar', $response->getHeader('Foo')); |
} |
|
public function testDispatchesErrorEventAndRecovers() |
{ |
Server::flush(); |
Server::enqueue("HTTP/1.1 201 OK\r\nContent-Length: 0\r\n\r\n"); |
$r = new Request('GET', Server::$url); |
$t = new Transaction(new Client(), $r); |
$a = $this->getAdapter(); |
$r->getEmitter()->once('complete', function (CompleteEvent $e) { |
throw new RequestException('Foo', $e->getRequest()); |
}); |
$r->getEmitter()->on('error', function (ErrorEvent $e) { |
$e->intercept(new Response(200, ['Foo' => 'bar'])); |
}); |
$response = $a->send($t); |
$this->assertEquals(200, $response->getStatusCode()); |
$this->assertEquals('bar', $response->getHeader('Foo')); |
} |
|
public function testStripsFragmentFromHost() |
{ |
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\n\r\nContent-Length: 0\r\n\r\n"); |
// This will fail if the removal of the #fragment is not performed |
$url = Url::fromString(Server::$url)->setPath(null)->setFragment('foo'); |
$client = new Client(); |
$client->get($url); |
} |
} |
/vendor/guzzlehttp/guzzle/tests/Adapter/Curl/BatchContextTest.php |
@@ -0,0 +1,111 @@ |
<?php |
|
namespace GuzzleHttp\Tests\Adapter\Curl; |
|
use GuzzleHttp\Adapter\Curl\BatchContext; |
use GuzzleHttp\Adapter\Transaction; |
use GuzzleHttp\Client; |
use GuzzleHttp\Message\Request; |
|
/** |
* @covers GuzzleHttp\Adapter\Curl\BatchContext |
*/ |
class BatchContextTest extends \PHPUnit_Framework_TestCase |
{ |
public function testProvidesGetters() |
{ |
$m = curl_multi_init(); |
$b = new BatchContext($m, true); |
$this->assertTrue($b->throwsExceptions()); |
$this->assertSame($m, $b->getMultiHandle()); |
$this->assertFalse($b->hasPending()); |
curl_multi_close($m); |
} |
|
public function testValidatesTransactionsAreNotAddedTwice() |
{ |
$m = curl_multi_init(); |
$b = new BatchContext($m, true); |
$h = curl_init(); |
$t = new Transaction( |
new Client(), |
new Request('GET', 'http://httbin.org') |
); |
$b->addTransaction($t, $h); |
try { |
$b->addTransaction($t, $h); |
$this->fail('Did not throw'); |
} catch (\RuntimeException $e) { |
curl_close($h); |
curl_multi_close($m); |
} |
} |
|
public function testManagesHandles() |
{ |
$m = curl_multi_init(); |
$b = new BatchContext($m, true); |
$h = curl_init(); |
$t = new Transaction( |
new Client(), |
new Request('GET', 'http://httbin.org') |
); |
$b->addTransaction($t, $h); |
$this->assertTrue($b->isActive()); |
$this->assertSame($t, $b->findTransaction($h)); |
$b->removeTransaction($t); |
$this->assertFalse($b->isActive()); |
try { |
$this->assertEquals([], $b->findTransaction($h)); |
$this->fail('Did not throw'); |
} catch (\RuntimeException $e) {} |
curl_multi_close($m); |
} |
|
/** |
* @expectedException \RuntimeException |
* @expectedExceptionMessage Transaction not registered |
*/ |
public function testThrowsWhenRemovingNonExistentTransaction() |
{ |
$b = new BatchContext('foo', false); |
$t = new Transaction( |
new Client(), |
new Request('GET', 'http://httbin.org') |
); |
$b->removeTransaction($t); |
} |
|
public function testReturnsPendingAsIteratorTypeObject() |
{ |
$t1 = new Transaction(new Client(), new Request('GET', 'http://t.com')); |
$t2 = new Transaction(new Client(), new Request('GET', 'http://t.com')); |
$t3 = new Transaction(new Client(), new Request('GET', 'http://t.com')); |
$iter = new \ArrayIterator([$t1, $t2, $t3]); |
$b = new BatchContext('foo', false, $iter); |
$this->assertTrue($b->hasPending()); |
$this->assertSame($t1, $b->nextPending()); |
$this->assertTrue($b->hasPending()); |
$this->assertSame($t2, $b->nextPending()); |
$this->assertTrue($b->hasPending()); |
$this->assertSame($t3, $b->nextPending()); |
$this->assertFalse($b->hasPending()); |
$this->assertNull($b->nextPending()); |
} |
|
public function testCanCloseAll() |
{ |
$m = curl_multi_init(); |
$b = new BatchContext($m, true); |
$h = curl_init(); |
$t = new Transaction( |
new Client(), |
new Request('GET', 'http://httbin.org') |
); |
$b->addTransaction($t, $h); |
$b->removeAll(); |
$this->assertFalse($b->isActive()); |
$this->assertEquals(0, count($this->readAttribute($b, 'handles'))); |
curl_multi_close($m); |
} |
} |
/vendor/guzzlehttp/guzzle/tests/Adapter/Curl/CurlAdapterTest.php |
@@ -0,0 +1,149 @@ |
<?php |
|
namespace GuzzleHttp\Tests\Adapter\Curl; |
|
require_once __DIR__ . '/AbstractCurl.php'; |
|
use GuzzleHttp\Adapter\Curl\CurlAdapter; |
use GuzzleHttp\Adapter\Transaction; |
use GuzzleHttp\Client; |
use GuzzleHttp\Event\BeforeEvent; |
use GuzzleHttp\Event\ErrorEvent; |
use GuzzleHttp\Event\HeadersEvent; |
use GuzzleHttp\Exception\ServerException; |
use GuzzleHttp\Message\MessageFactory; |
use GuzzleHttp\Message\Request; |
use GuzzleHttp\Message\Response; |
use GuzzleHttp\Tests\Server; |
|
/** |
* @covers GuzzleHttp\Adapter\Curl\CurlAdapter |
*/ |
class CurlAdapterTest extends AbstractCurl |
{ |
protected function setUp() |
{ |
if (!function_exists('curl_reset')) { |
$this->markTestSkipped('curl_reset() is not available'); |
} |
} |
|
protected function getAdapter($factory = null, $options = []) |
{ |
return new CurlAdapter($factory ?: new MessageFactory(), $options); |
} |
|
public function testCanSetMaxHandles() |
{ |
$a = new CurlAdapter(new MessageFactory(), ['max_handles' => 10]); |
$this->assertEquals(10, $this->readAttribute($a, 'maxHandles')); |
} |
|
public function testCanInterceptBeforeSending() |
{ |
$client = new Client(); |
$request = new Request('GET', 'http://httpbin.org/get'); |
$response = new Response(200); |
$request->getEmitter()->on( |
'before', |
function (BeforeEvent $e) use ($response) { |
$e->intercept($response); |
} |
); |
$transaction = new Transaction($client, $request); |
$f = 'does_not_work'; |
$a = new CurlAdapter(new MessageFactory(), ['handle_factory' => $f]); |
$a->send($transaction); |
$this->assertSame($response, $transaction->getResponse()); |
} |
|
/** |
* @expectedException \GuzzleHttp\Exception\RequestException |
* @expectedExceptionMessage cURL error |
*/ |
public function testThrowsCurlErrors() |
{ |
$client = new Client(); |
$request = $client->createRequest('GET', 'http://localhost:123', [ |
'connect_timeout' => 0.001, |
'timeout' => 0.001, |
]); |
$transaction = new Transaction($client, $request); |
$a = new CurlAdapter(new MessageFactory()); |
$a->send($transaction); |
} |
|
public function testHandlesCurlErrors() |
{ |
$client = new Client(); |
$request = $client->createRequest('GET', 'http://localhost:123', [ |
'connect_timeout' => 0.001, |
'timeout' => 0.001, |
]); |
$r = new Response(200); |
$request->getEmitter()->on('error', function (ErrorEvent $e) use ($r) { |
$e->intercept($r); |
}); |
$transaction = new Transaction($client, $request); |
$a = new CurlAdapter(new MessageFactory()); |
$a->send($transaction); |
$this->assertSame($r, $transaction->getResponse()); |
} |
|
public function testReleasesAdditionalEasyHandles() |
{ |
Server::flush(); |
Server::enqueue([ |
"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", |
"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" |
]); |
$a = new CurlAdapter(new MessageFactory(), ['max_handles' => 2]); |
$client = new Client(['base_url' => Server::$url, 'adapter' => $a]); |
$request = $client->createRequest('GET', '/', [ |
'events' => [ |
'headers' => function (HeadersEvent $e) use ($client) { |
$client->get('/', [ |
'events' => [ |
'headers' => function (HeadersEvent $e) { |
$e->getClient()->get('/'); |
} |
] |
]); |
} |
] |
]); |
$transaction = new Transaction($client, $request); |
$a->send($transaction); |
$this->assertCount(2, $this->readAttribute($a, 'handles')); |
} |
|
public function testDoesNotSaveToWhenFailed() |
{ |
Server::flush(); |
Server::enqueue([ |
"HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n" |
]); |
|
$tmp = tempnam('/tmp', 'test_save_to'); |
unlink($tmp); |
$a = new CurlAdapter(new MessageFactory()); |
$client = new Client(['base_url' => Server::$url, 'adapter' => $a]); |
try { |
$client->get('/', ['save_to' => $tmp]); |
} catch (ServerException $e) { |
$this->assertFileNotExists($tmp); |
} |
} |
|
public function testRewindsStreamOnComplete() |
{ |
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\nFoo: bar\r\nContent-Length: 4\r\n\r\ntest"); |
$t = new Transaction(new Client(), new Request('GET', Server::$url)); |
$a = new CurlAdapter(new MessageFactory()); |
$response = $a->send($t); |
$this->assertEquals('test', $response->getBody()->read(4)); |
} |
} |
/vendor/guzzlehttp/guzzle/tests/Adapter/Curl/CurlFactoryTest.php |
@@ -0,0 +1,392 @@ |
<?php |
|
// Override curl_setopt_array() to get the last set curl options |
namespace GuzzleHttp\Adapter\Curl |
{ |
function curl_setopt_array($handle, array $options) |
{ |
if (array_values($options) != [null, null, null, null]) { |
$_SERVER['last_curl'] = $options; |
} |
\curl_setopt_array($handle, $options); |
} |
} |
|
namespace GuzzleHttp\Tests\Adapter\Curl { |
|
use GuzzleHttp\Adapter\Curl\CurlFactory; |
use GuzzleHttp\Adapter\Curl\MultiAdapter; |
use GuzzleHttp\Adapter\Transaction; |
use GuzzleHttp\Client; |
use GuzzleHttp\Event\BeforeEvent; |
use GuzzleHttp\Exception\ServerException; |
use GuzzleHttp\Message\MessageFactory; |
use GuzzleHttp\Message\Request; |
use GuzzleHttp\Message\RequestInterface; |
use GuzzleHttp\Stream\Stream; |
use GuzzleHttp\Tests\Server; |
|
/** |
* @covers GuzzleHttp\Adapter\Curl\CurlFactory |
*/ |
class CurlFactoryTest extends \PHPUnit_Framework_TestCase |
{ |
/** @var \GuzzleHttp\Tests\Server */ |
static $server; |
|
public static function setUpBeforeClass() |
{ |
unset($_SERVER['last_curl']); |
} |
|
public static function tearDownAfterClass() |
{ |
unset($_SERVER['last_curl']); |
} |
|
public function testCreatesCurlHandle() |
{ |
Server::flush(); |
Server::enqueue(["HTTP/1.1 200 OK\r\nFoo: Bar\r\n Baz: bam\r\nContent-Length: 2\r\n\r\nhi"]); |
$request = new Request( |
'PUT', |
Server::$url . 'haha', |
['Hi' => ' 123'], |
Stream::factory('testing') |
); |
$stream = Stream::factory(); |
$request->getConfig()->set('save_to', $stream); |
$request->getConfig()->set('verify', true); |
$this->emit($request); |
|
$t = new Transaction(new Client(), $request); |
$f = new CurlFactory(); |
$h = $f($t, new MessageFactory()); |
$this->assertInternalType('resource', $h); |
curl_exec($h); |
$response = $t->getResponse(); |
$this->assertInstanceOf('GuzzleHttp\Message\ResponseInterface', $response); |
$this->assertEquals('hi', $response->getBody()); |
$this->assertEquals('Bar', $response->getHeader('Foo')); |
$this->assertEquals('bam', $response->getHeader('Baz')); |
curl_close($h); |
|
$sent = Server::received(true)[0]; |
$this->assertEquals('PUT', $sent->getMethod()); |
$this->assertEquals('/haha', $sent->getPath()); |
$this->assertEquals('123', $sent->getHeader('Hi')); |
$this->assertEquals('7', $sent->getHeader('Content-Length')); |
$this->assertEquals('testing', $sent->getBody()); |
$this->assertEquals('1.1', $sent->getProtocolVersion()); |
$this->assertEquals('hi', (string) $stream); |
|
$this->assertEquals(true, $_SERVER['last_curl'][CURLOPT_SSL_VERIFYPEER]); |
$this->assertEquals(2, $_SERVER['last_curl'][CURLOPT_SSL_VERIFYHOST]); |
} |
|
public function testSendsHeadRequests() |
{ |
Server::flush(); |
Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n"]); |
$request = new Request('HEAD', Server::$url); |
$this->emit($request); |
|
$t = new Transaction(new Client(), $request); |
$f = new CurlFactory(); |
$h = $f($t, new MessageFactory()); |
curl_exec($h); |
curl_close($h); |
$response = $t->getResponse(); |
$this->assertEquals('2', $response->getHeader('Content-Length')); |
$this->assertEquals('', $response->getBody()); |
|
$sent = Server::received(true)[0]; |
$this->assertEquals('HEAD', $sent->getMethod()); |
$this->assertEquals('/', $sent->getPath()); |
} |
|
public function testSendsPostRequestWithNoBody() |
{ |
Server::flush(); |
Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"]); |
$request = new Request('POST', Server::$url); |
$this->emit($request); |
$t = new Transaction(new Client(), $request); |
$f = new CurlFactory(); |
$h = $f($t, new MessageFactory()); |
curl_exec($h); |
curl_close($h); |
$sent = Server::received(true)[0]; |
$this->assertEquals('POST', $sent->getMethod()); |
$this->assertEquals('', $sent->getBody()); |
} |
|
public function testSendsChunkedRequests() |
{ |
$stream = $this->getMockBuilder('GuzzleHttp\Stream\Stream') |
->setConstructorArgs([fopen('php://temp', 'r+')]) |
->setMethods(['getSize']) |
->getMock(); |
$stream->expects($this->any()) |
->method('getSize') |
->will($this->returnValue(null)); |
$stream->write('foo'); |
$stream->seek(0); |
|
Server::flush(); |
Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"]); |
$request = new Request('PUT', Server::$url, [], $stream); |
$this->emit($request); |
$this->assertNull($request->getBody()->getSize()); |
$t = new Transaction(new Client(), $request); |
$f = new CurlFactory(); |
$h = $f($t, new MessageFactory()); |
curl_exec($h); |
curl_close($h); |
$sent = Server::received(false)[0]; |
$this->assertContains('PUT / HTTP/1.1', $sent); |
$this->assertContains('transfer-encoding: chunked', strtolower($sent)); |
$this->assertContains("\r\n\r\nfoo", $sent); |
} |
|
public function testDecodesGzippedResponses() |
{ |
Server::flush(); |
$content = gzencode('test'); |
$message = "HTTP/1.1 200 OK\r\n" |
. "Content-Encoding: gzip\r\n" |
. "Content-Length: " . strlen($content) . "\r\n\r\n" |
. $content; |
Server::enqueue($message); |
$client = new Client(); |
$request = $client->createRequest('GET', Server::$url); |
$this->emit($request); |
$t = new Transaction($client, $request); |
$f = new CurlFactory(); |
$h = $f($t, new MessageFactory()); |
curl_exec($h); |
curl_close($h); |
$sent = Server::received(true)[0]; |
$this->assertSame('', $sent->getHeader('Accept-Encoding')); |
$this->assertEquals('test', (string) $t->getResponse()->getBody()); |
} |
|
public function testDecodesWithCustomAcceptHeader() |
{ |
Server::flush(); |
$content = gzencode('test'); |
$message = "HTTP/1.1 200 OK\r\n" |
. "Content-Encoding: gzip\r\n" |
. "Content-Length: " . strlen($content) . "\r\n\r\n" |
. $content; |
Server::enqueue($message); |
$client = new Client(); |
$request = $client->createRequest('GET', Server::$url, [ |
'decode_content' => 'gzip' |
]); |
$this->emit($request); |
$t = new Transaction($client, $request); |
$f = new CurlFactory(); |
$h = $f($t, new MessageFactory()); |
curl_exec($h); |
curl_close($h); |
$sent = Server::received(true)[0]; |
$this->assertSame('gzip', $sent->getHeader('Accept-Encoding')); |
$this->assertEquals('test', (string) $t->getResponse()->getBody()); |
} |
|
public function testDoesNotForceDecode() |
{ |
Server::flush(); |
$content = gzencode('test'); |
$message = "HTTP/1.1 200 OK\r\n" |
. "Content-Encoding: gzip\r\n" |
. "Content-Length: " . strlen($content) . "\r\n\r\n" |
. $content; |
Server::enqueue($message); |
$client = new Client(); |
$request = $client->createRequest('GET', Server::$url, [ |
'headers' => ['Accept-Encoding' => 'gzip'], |
'decode_content' => false |
]); |
$this->emit($request); |
$t = new Transaction($client, $request); |
$f = new CurlFactory(); |
$h = $f($t, new MessageFactory()); |
curl_exec($h); |
curl_close($h); |
$sent = Server::received(true)[0]; |
$this->assertSame('gzip', $sent->getHeader('Accept-Encoding')); |
$this->assertSame($content, (string) $t->getResponse()->getBody()); |
} |
|
public function testAddsDebugInfoToBuffer() |
{ |
$r = fopen('php://temp', 'r+'); |
Server::flush(); |
Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nfoo"]); |
$request = new Request('GET', Server::$url); |
$request->getConfig()->set('debug', $r); |
$this->emit($request); |
$t = new Transaction(new Client(), $request); |
$f = new CurlFactory(); |
$h = $f($t, new MessageFactory()); |
curl_exec($h); |
curl_close($h); |
rewind($r); |
$this->assertNotEmpty(stream_get_contents($r)); |
} |
|
public function testAddsProxyOptions() |
{ |
$request = new Request('GET', Server::$url); |
$this->emit($request); |
$request->getConfig()->set('proxy', '123'); |
$request->getConfig()->set('connect_timeout', 1); |
$request->getConfig()->set('timeout', 2); |
$request->getConfig()->set('cert', __FILE__); |
$request->getConfig()->set('ssl_key', [__FILE__, '123']); |
$request->getConfig()->set('verify', false); |
$t = new Transaction(new Client(), $request); |
$f = new CurlFactory(); |
curl_close($f($t, new MessageFactory())); |
$this->assertEquals('123', $_SERVER['last_curl'][CURLOPT_PROXY]); |
$this->assertEquals(1000, $_SERVER['last_curl'][CURLOPT_CONNECTTIMEOUT_MS]); |
$this->assertEquals(2000, $_SERVER['last_curl'][CURLOPT_TIMEOUT_MS]); |
$this->assertEquals(__FILE__, $_SERVER['last_curl'][CURLOPT_SSLCERT]); |
$this->assertEquals(__FILE__, $_SERVER['last_curl'][CURLOPT_SSLKEY]); |
$this->assertEquals('123', $_SERVER['last_curl'][CURLOPT_SSLKEYPASSWD]); |
$this->assertEquals(0, $_SERVER['last_curl'][CURLOPT_SSL_VERIFYHOST]); |
$this->assertEquals(false, $_SERVER['last_curl'][CURLOPT_SSL_VERIFYPEER]); |
} |
|
/** |
* @expectedException \RuntimeException |
*/ |
public function testEnsuresCertExists() |
{ |
$request = new Request('GET', Server::$url); |
$this->emit($request); |
$request->getConfig()->set('cert', __FILE__ . 'ewfwef'); |
$f = new CurlFactory(); |
$f(new Transaction(new Client(), $request), new MessageFactory()); |
} |
|
/** |
* @expectedException \RuntimeException |
*/ |
public function testEnsuresKeyExists() |
{ |
$request = new Request('GET', Server::$url); |
$this->emit($request); |
$request->getConfig()->set('ssl_key', __FILE__ . 'ewfwef'); |
$f = new CurlFactory(); |
$f(new Transaction(new Client(), $request), new MessageFactory()); |
} |
|
/** |
* @expectedException \RuntimeException |
*/ |
public function testEnsuresCacertExists() |
{ |
$request = new Request('GET', Server::$url); |
$this->emit($request); |
$request->getConfig()->set('verify', __FILE__ . 'ewfwef'); |
$f = new CurlFactory(); |
$f(new Transaction(new Client(), $request), new MessageFactory()); |
} |
|
public function testClientUsesSslByDefault() |
{ |
Server::flush(); |
Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nfoo"]); |
$f = new CurlFactory(); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new MultiAdapter(new MessageFactory(), ['handle_factory' => $f]) |
]); |
$client->get(); |
$this->assertEquals(2, $_SERVER['last_curl'][CURLOPT_SSL_VERIFYHOST]); |
$this->assertEquals(true, $_SERVER['last_curl'][CURLOPT_SSL_VERIFYPEER]); |
$this->assertFileExists($_SERVER['last_curl'][CURLOPT_CAINFO]); |
} |
|
public function testConvertsConstantNameKeysToValues() |
{ |
$request = new Request('GET', Server::$url); |
$request->getConfig()->set('curl', ['CURLOPT_USERAGENT' => 'foo']); |
$this->emit($request); |
$f = new CurlFactory(); |
curl_close($f(new Transaction(new Client(), $request), new MessageFactory())); |
$this->assertEquals('foo', $_SERVER['last_curl'][CURLOPT_USERAGENT]); |
} |
|
public function testStripsFragment() |
{ |
$request = new Request('GET', Server::$url . '#foo'); |
$this->emit($request); |
$f = new CurlFactory(); |
curl_close($f(new Transaction(new Client(), $request), new MessageFactory())); |
$this->assertEquals(Server::$url, $_SERVER['last_curl'][CURLOPT_URL]); |
} |
|
public function testDoesNotSendSizeTwice() |
{ |
$request = new Request('PUT', Server::$url, [], Stream::factory(str_repeat('a', 32769))); |
$this->emit($request); |
$f = new CurlFactory(); |
curl_close($f(new Transaction(new Client(), $request), new MessageFactory())); |
$this->assertEquals(32769, $_SERVER['last_curl'][CURLOPT_INFILESIZE]); |
$this->assertNotContains('Content-Length', implode(' ', $_SERVER['last_curl'][CURLOPT_HTTPHEADER])); |
} |
|
public function testCanSendPayloadWithGet() |
{ |
Server::flush(); |
Server::enqueue(["HTTP/1.1 200 OK\r\n\r\n"]); |
$request = new Request( |
'GET', |
Server::$url, |
[], |
Stream::factory('foo') |
); |
$this->emit($request); |
$t = new Transaction(new Client(), $request); |
$f = new CurlFactory(); |
$h = $f($t, new MessageFactory()); |
curl_exec($h); |
curl_close($h); |
$sent = Server::received(true)[0]; |
$this->assertEquals('foo', (string) $sent->getBody()); |
$this->assertEquals(3, (string) $sent->getHeader('Content-Length')); |
} |
|
private function emit(RequestInterface $request) |
{ |
$event = new BeforeEvent(new Transaction(new Client(), $request)); |
$request->getEmitter()->emit('before', $event); |
} |
|
public function testDoesNotAlwaysAddContentType() |
{ |
Server::flush(); |
Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"]); |
$client = new Client(); |
$client->put(Server::$url . '/foo', ['body' => 'foo']); |
$request = Server::received(true)[0]; |
$this->assertEquals('', $request->getHeader('Content-Type')); |
} |
|
/** |
* @expectedException \GuzzleHttp\Exception\AdapterException |
*/ |
public function testThrowsForStreamOption() |
{ |
$request = new Request('GET', Server::$url . 'haha'); |
$request->getConfig()->set('stream', true); |
$t = new Transaction(new Client(), $request); |
$f = new CurlFactory(); |
$f($t, new MessageFactory()); |
} |
} |
} |
/vendor/guzzlehttp/guzzle/tests/Adapter/Curl/MultiAdapterTest.php |
@@ -0,0 +1,362 @@ |
<?php |
|
namespace GuzzleHttp\Tests\Adapter\Curl; |
|
require_once __DIR__ . '/AbstractCurl.php'; |
|
use GuzzleHttp\Adapter\Curl\MultiAdapter; |
use GuzzleHttp\Adapter\Transaction; |
use GuzzleHttp\Client; |
use GuzzleHttp\Event\CompleteEvent; |
use GuzzleHttp\Event\ErrorEvent; |
use GuzzleHttp\Exception\RequestException; |
use GuzzleHttp\Message\MessageFactory; |
use GuzzleHttp\Message\Request; |
use GuzzleHttp\Message\Response; |
use GuzzleHttp\Stream\NoSeekStream; |
use GuzzleHttp\Stream\Stream; |
use GuzzleHttp\Tests\Server; |
|
/** |
* @covers GuzzleHttp\Adapter\Curl\MultiAdapter |
*/ |
class MultiAdapterTest extends AbstractCurl |
{ |
protected function getAdapter($factory = null, $options = []) |
{ |
return new MultiAdapter($factory ?: new MessageFactory(), $options); |
} |
|
public function testSendsSingleRequest() |
{ |
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\nFoo: bar\r\nContent-Length: 0\r\n\r\n"); |
$t = new Transaction(new Client(), new Request('GET', Server::$url)); |
$a = new MultiAdapter(new MessageFactory()); |
$response = $a->send($t); |
$this->assertEquals(200, $response->getStatusCode()); |
$this->assertEquals('bar', $response->getHeader('Foo')); |
} |
|
public function testCanSetSelectTimeout() |
{ |
$current = isset($_SERVER[MultiAdapter::ENV_SELECT_TIMEOUT]) |
? $_SERVER[MultiAdapter::ENV_SELECT_TIMEOUT]: null; |
unset($_SERVER[MultiAdapter::ENV_SELECT_TIMEOUT]); |
$a = new MultiAdapter(new MessageFactory()); |
$this->assertEquals(1, $this->readAttribute($a, 'selectTimeout')); |
$a = new MultiAdapter(new MessageFactory(), ['select_timeout' => 10]); |
$this->assertEquals(10, $this->readAttribute($a, 'selectTimeout')); |
$_SERVER[MultiAdapter::ENV_SELECT_TIMEOUT] = 2; |
$a = new MultiAdapter(new MessageFactory()); |
$this->assertEquals(2, $this->readAttribute($a, 'selectTimeout')); |
$_SERVER[MultiAdapter::ENV_SELECT_TIMEOUT] = $current; |
} |
|
public function testCanSetMaxHandles() |
{ |
$a = new MultiAdapter(new MessageFactory()); |
$this->assertEquals(3, $this->readAttribute($a, 'maxHandles')); |
$a = new MultiAdapter(new MessageFactory(), ['max_handles' => 10]); |
$this->assertEquals(10, $this->readAttribute($a, 'maxHandles')); |
} |
|
/** |
* @expectedException \GuzzleHttp\Exception\AdapterException |
* @expectedExceptionMessage cURL error -2: |
*/ |
public function testChecksCurlMultiResult() |
{ |
MultiAdapter::throwMultiError(-2); |
} |
|
public function testChecksForCurlException() |
{ |
$mh = curl_multi_init(); |
$request = new Request('GET', 'http://httbin.org'); |
$transaction = $this->getMockBuilder('GuzzleHttp\Adapter\Transaction') |
->setMethods(['getRequest']) |
->disableOriginalConstructor() |
->getMock(); |
$transaction->expects($this->exactly(2)) |
->method('getRequest') |
->will($this->returnValue($request)); |
$context = $this->getMockBuilder('GuzzleHttp\Adapter\Curl\BatchContext') |
->setMethods(['throwsExceptions']) |
->setConstructorArgs([$mh, true]) |
->getMock(); |
$context->expects($this->once()) |
->method('throwsExceptions') |
->will($this->returnValue(true)); |
$a = new MultiAdapter(new MessageFactory()); |
$r = new \ReflectionMethod($a, 'isCurlException'); |
$r->setAccessible(true); |
try { |
$r->invoke($a, $transaction, ['result' => -10], $context, []); |
curl_multi_close($mh); |
$this->fail('Did not throw'); |
} catch (RequestException $e) { |
curl_multi_close($mh); |
$this->assertSame($request, $e->getRequest()); |
$this->assertContains('[curl] (#-10) ', $e->getMessage()); |
$this->assertContains($request->getUrl(), $e->getMessage()); |
} |
} |
|
public function testSendsParallelRequestsFromQueue() |
{ |
$c = new Client(); |
Server::flush(); |
Server::enqueue([ |
"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", |
"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" |
]); |
$transactions = [ |
new Transaction($c, new Request('GET', Server::$url)), |
new Transaction($c, new Request('PUT', Server::$url)), |
new Transaction($c, new Request('HEAD', Server::$url)), |
new Transaction($c, new Request('GET', Server::$url)) |
]; |
$a = new MultiAdapter(new MessageFactory()); |
$a->sendAll(new \ArrayIterator($transactions), 2); |
foreach ($transactions as $t) { |
$response = $t->getResponse(); |
$this->assertNotNull($response); |
$this->assertEquals(200, $response->getStatusCode()); |
} |
} |
|
public function testCreatesAndReleasesHandlesWhenNeeded() |
{ |
$a = new MultiAdapter(new MessageFactory()); |
$c = new Client([ |
'adapter' => $a, |
'base_url' => Server::$url |
]); |
|
Server::flush(); |
Server::enqueue([ |
"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", |
"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", |
"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", |
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
]); |
|
$ef = function (ErrorEvent $e) { throw $e->getException(); }; |
|
$request1 = $c->createRequest('GET', '/'); |
$request1->getEmitter()->on('headers', function () use ($a, $c, $ef) { |
$a->send(new Transaction($c, $c->createRequest('GET', '/', [ |
'events' => [ |
'headers' => function () use ($a, $c, $ef) { |
$r = $c->createRequest('GET', '/', [ |
'events' => ['error' => ['fn' => $ef, 'priority' => 9999]] |
]); |
$r->getEmitter()->once('headers', function () use ($a, $c, $r) { |
$a->send(new Transaction($c, $r)); |
}); |
$a->send(new Transaction($c, $r)); |
// Now, reuse an existing handle |
$a->send(new Transaction($c, $r)); |
}, |
'error' => ['fn' => $ef, 'priority' => 9999] |
] |
]))); |
}); |
|
$request1->getEmitter()->on('error', $ef); |
|
$transactions = [ |
new Transaction($c, $request1), |
new Transaction($c, $c->createRequest('PUT')), |
new Transaction($c, $c->createRequest('HEAD')) |
]; |
|
$a->sendAll(new \ArrayIterator($transactions), 2); |
|
foreach ($transactions as $index => $t) { |
$response = $t->getResponse(); |
$this->assertInstanceOf( |
'GuzzleHttp\\Message\\ResponseInterface', |
$response, |
'Transaction at index ' . $index . ' did not populate response' |
); |
$this->assertEquals(200, $response->getStatusCode()); |
} |
} |
|
public function testThrowsAndReleasesWhenErrorDuringCompleteEvent() |
{ |
Server::flush(); |
Server::enqueue("HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n"); |
$request = new Request('GET', Server::$url); |
$request->getEmitter()->on('complete', function (CompleteEvent $e) { |
throw new RequestException('foo', $e->getRequest()); |
}); |
$t = new Transaction(new Client(), $request); |
$a = new MultiAdapter(new MessageFactory()); |
try { |
$a->send($t); |
$this->fail('Did not throw'); |
} catch (RequestException $e) { |
$this->assertSame($request, $e->getRequest()); |
} |
} |
|
public function testEnsuresResponseWasSetForGet() |
{ |
$client = new Client(); |
$request = $client->createRequest('GET', Server::$url); |
$response = new Response(200, []); |
$er = null; |
|
$request->getEmitter()->on( |
'error', |
function (ErrorEvent $e) use (&$er, $response) { |
$er = $e; |
} |
); |
|
$transaction = $this->getMockBuilder('GuzzleHttp\Adapter\Transaction') |
->setMethods(['getResponse', 'setResponse']) |
->setConstructorArgs([$client, $request]) |
->getMock(); |
$transaction->expects($this->any())->method('setResponse'); |
$transaction->expects($this->any()) |
->method('getResponse') |
->will($this->returnCallback(function () use ($response) { |
$caller = debug_backtrace()[6]['function']; |
return $caller == 'addHandle' || |
$caller == 'validateResponseWasSet' |
? null |
: $response; |
})); |
|
$a = new MultiAdapter(new MessageFactory()); |
Server::flush(); |
Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"]); |
$a->sendAll(new \ArrayIterator([$transaction]), 10); |
$this->assertNotNull($er); |
|
$this->assertContains( |
'No response was received', |
$er->getException()->getMessage() |
); |
} |
|
private function runConnectionTest( |
$queue, |
$stream, |
$msg, |
$statusCode = null |
) { |
$obj = new \stdClass(); |
$er = null; |
$client = new Client(); |
$request = $client->createRequest('PUT', Server::$url, [ |
'body' => $stream |
]); |
|
$request->getEmitter()->on( |
'error', |
function (ErrorEvent $e) use (&$er) { |
$er = $e; |
} |
); |
|
$transaction = $this->getMockBuilder('GuzzleHttp\Adapter\Transaction') |
->setMethods(['getResponse', 'setResponse']) |
->setConstructorArgs([$client, $request]) |
->getMock(); |
|
$transaction->expects($this->any()) |
->method('setResponse') |
->will($this->returnCallback(function ($r) use (&$obj) { |
$obj->res = $r; |
})); |
|
$transaction->expects($this->any()) |
->method('getResponse') |
->will($this->returnCallback(function () use ($obj, &$called) { |
$caller = debug_backtrace()[6]['function']; |
if ($caller == 'addHandle') { |
return null; |
} elseif ($caller == 'validateResponseWasSet') { |
return ++$called == 2 ? $obj->res : null; |
} else { |
return $obj->res; |
} |
})); |
|
$a = new MultiAdapter(new MessageFactory()); |
Server::flush(); |
Server::enqueue($queue); |
$a->sendAll(new \ArrayIterator([$transaction]), 10); |
|
if ($msg) { |
$this->assertNotNull($er); |
$this->assertContains($msg, $er->getException()->getMessage()); |
} else { |
$this->assertEquals( |
$statusCode, |
$transaction->getResponse()->getStatusCode() |
); |
} |
} |
|
public function testThrowsWhenTheBodyCannotBeRewound() |
{ |
$this->runConnectionTest( |
["HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"], |
new NoSeekStream(Stream::factory('foo')), |
'attempting to rewind the request body failed' |
); |
} |
|
public function testRetriesRewindableStreamsWhenClosedConnectionErrors() |
{ |
$this->runConnectionTest( |
[ |
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", |
"HTTP/1.1 201 OK\r\nContent-Length: 0\r\n\r\n", |
], |
Stream::factory('foo'), |
false, |
201 |
); |
} |
|
public function testThrowsImmediatelyWhenInstructed() |
{ |
Server::flush(); |
Server::enqueue(["HTTP/1.1 501\r\nContent-Length: 0\r\n\r\n"]); |
$c = new Client(['base_url' => Server::$url]); |
$request = $c->createRequest('GET', '/'); |
$request->getEmitter()->on('error', function (ErrorEvent $e) { |
$e->throwImmediately(true); |
}); |
$transactions = [new Transaction($c, $request)]; |
$a = new MultiAdapter(new MessageFactory()); |
try { |
$a->sendAll(new \ArrayIterator($transactions), 1); |
$this->fail('Did not throw'); |
} catch (RequestException $e) { |
$this->assertSame($request, $e->getRequest()); |
} |
} |
|
public function testRewindsStreamOnComplete() |
{ |
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\nFoo: bar\r\nContent-Length: 4\r\n\r\ntest"); |
$t = new Transaction(new Client(), new Request('GET', Server::$url)); |
$a = new MultiAdapter(new MessageFactory()); |
$response = $a->send($t); |
$this->assertEquals('test', $response->getBody()->read(4)); |
} |
} |
/vendor/guzzlehttp/guzzle/tests/Adapter/Curl/RequestMediatorTest.php |
@@ -0,0 +1,113 @@ |
<?php |
|
namespace GuzzleHttp\Tests\Adapter\Curl; |
|
use GuzzleHttp\Adapter\Curl\MultiAdapter; |
use GuzzleHttp\Adapter\Curl\RequestMediator; |
use GuzzleHttp\Adapter\Transaction; |
use GuzzleHttp\Client; |
use GuzzleHttp\Event\HeadersEvent; |
use GuzzleHttp\Message\MessageFactory; |
use GuzzleHttp\Message\Request; |
use GuzzleHttp\Message\Response; |
use GuzzleHttp\Stream\Stream; |
use GuzzleHttp\Tests\Server; |
|
/** |
* @covers GuzzleHttp\Adapter\Curl\RequestMediator |
*/ |
class RequestMediatorTest extends \PHPUnit_Framework_TestCase |
{ |
public function testSetsResponseBodyForDownload() |
{ |
$body = Stream::factory(); |
$request = new Request('GET', 'http://httbin.org'); |
$ee = null; |
$request->getEmitter()->on( |
'headers', |
function (HeadersEvent $e) use (&$ee) { |
$ee = $e; |
} |
); |
$t = new Transaction(new Client(), $request); |
$m = new RequestMediator($t, new MessageFactory()); |
$m->setResponseBody($body); |
$this->assertEquals(18, $m->receiveResponseHeader(null, "HTTP/1.1 202 FOO\r\n")); |
$this->assertEquals(10, $m->receiveResponseHeader(null, "Foo: Bar\r\n")); |
$this->assertEquals(11, $m->receiveResponseHeader(null, "Baz : Bam\r\n")); |
$this->assertEquals(19, $m->receiveResponseHeader(null, "Content-Length: 3\r\n")); |
$this->assertEquals(2, $m->receiveResponseHeader(null, "\r\n")); |
$this->assertNotNull($ee); |
$this->assertEquals(202, $t->getResponse()->getStatusCode()); |
$this->assertEquals('FOO', $t->getResponse()->getReasonPhrase()); |
$this->assertEquals('Bar', $t->getResponse()->getHeader('Foo')); |
$this->assertEquals('Bam', $t->getResponse()->getHeader('Baz')); |
$m->writeResponseBody(null, 'foo'); |
$this->assertEquals('foo', (string) $body); |
$this->assertEquals('3', $t->getResponse()->getHeader('Content-Length')); |
} |
|
public function testSendsToNewBodyWhenNot2xxResponse() |
{ |
$body = Stream::factory(); |
$request = new Request('GET', 'http://httbin.org'); |
$t = new Transaction(new Client(), $request); |
$m = new RequestMediator($t, new MessageFactory()); |
$m->setResponseBody($body); |
$this->assertEquals(27, $m->receiveResponseHeader(null, "HTTP/1.1 304 Not Modified\r\n")); |
$this->assertEquals(2, $m->receiveResponseHeader(null, "\r\n")); |
$this->assertEquals(304, $t->getResponse()->getStatusCode()); |
$m->writeResponseBody(null, 'foo'); |
$this->assertEquals('', (string) $body); |
$this->assertEquals('foo', (string) $t->getResponse()->getBody()); |
} |
|
public function testUsesDefaultBodyIfNoneSet() |
{ |
$t = new Transaction(new Client(), new Request('GET', 'http://httbin.org')); |
$t->setResponse(new Response(200)); |
$m = new RequestMediator($t, new MessageFactory()); |
$this->assertEquals(3, $m->writeResponseBody(null, 'foo')); |
$this->assertEquals('foo', (string) $t->getResponse()->getBody()); |
} |
|
public function testCanUseResponseBody() |
{ |
$body = Stream::factory(); |
$t = new Transaction(new Client(), new Request('GET', 'http://httbin.org')); |
$t->setResponse(new Response(200, [], $body)); |
$m = new RequestMediator($t, new MessageFactory()); |
$this->assertEquals(3, $m->writeResponseBody(null, 'foo')); |
$this->assertEquals('foo', (string) $body); |
} |
|
public function testHandlesTransactionWithNoResponseWhenWritingBody() |
{ |
$t = new Transaction(new Client(), new Request('GET', 'http://httbin.org')); |
$m = new RequestMediator($t, new MessageFactory()); |
$this->assertEquals(0, $m->writeResponseBody(null, 'test')); |
} |
|
public function testReadsFromRequestBody() |
{ |
$body = Stream::factory('foo'); |
$t = new Transaction(new Client(), new Request('PUT', 'http://httbin.org', [], $body)); |
$m = new RequestMediator($t, new MessageFactory()); |
$this->assertEquals('foo', $m->readRequestBody(null, null, 3)); |
} |
|
public function testEmitsHeadersEventForHeadRequest() |
{ |
Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK"]); |
$ee = null; |
$client = new Client(['adapter' => new MultiAdapter(new MessageFactory())]); |
$client->head(Server::$url, [ |
'events' => [ |
'headers' => function (HeadersEvent $e) use (&$ee) { |
$ee = $e; |
} |
] |
]); |
$this->assertInstanceOf('GuzzleHttp\\Event\\HeadersEvent', $ee); |
} |
} |