/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); |
} |
} |
/vendor/guzzlehttp/guzzle/tests/Adapter/MockAdapterTest.php |
@@ -0,0 +1,108 @@ |
<?php |
|
namespace GuzzleHttp\Tests\Adapter; |
|
use GuzzleHttp\Adapter\MockAdapter; |
use GuzzleHttp\Adapter\Transaction; |
use GuzzleHttp\Adapter\TransactionInterface; |
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\Stream\Stream; |
|
/** |
* @covers GuzzleHttp\Adapter\MockAdapter |
*/ |
class MockAdapterTest extends \PHPUnit_Framework_TestCase |
{ |
public function testYieldsMockResponse() |
{ |
$response = new Response(200); |
$m = new MockAdapter(); |
$m->setResponse($response); |
$this->assertSame($response, $m->send(new Transaction(new Client(), new Request('GET', 'http://httbin.org')))); |
} |
|
public function testMocksWithCallable() |
{ |
$response = new Response(200); |
$r = function (TransactionInterface $trans) use ($response) { |
$this->assertEquals(0, $trans->getRequest()->getBody()->tell()); |
return $response; |
}; |
$m = new MockAdapter($r); |
$body = Stream::factory('foo'); |
$request = new Request('GET', 'http://httbin.org', [], $body); |
$trans = new Transaction(new Client(), $request); |
$this->assertSame($response, $m->send($trans)); |
$this->assertEquals(3, $body->tell()); |
} |
|
/** |
* @expectedException \RuntimeException |
*/ |
public function testValidatesResponses() |
{ |
$m = new MockAdapter(); |
$m->setResponse('foo'); |
$m->send(new Transaction(new Client(), new Request('GET', 'http://httbin.org'))); |
} |
|
public function testHandlesErrors() |
{ |
$m = new MockAdapter(); |
$m->setResponse(new Response(404)); |
$request = new Request('GET', 'http://httbin.org'); |
$c = false; |
$request->getEmitter()->once('complete', function (CompleteEvent $e) use (&$c) { |
$c = true; |
throw new RequestException('foo', $e->getRequest()); |
}); |
$request->getEmitter()->on('error', function (ErrorEvent $e) { |
$e->intercept(new Response(201)); |
}); |
$r = $m->send(new Transaction(new Client(), $request)); |
$this->assertTrue($c); |
$this->assertEquals(201, $r->getStatusCode()); |
} |
|
/** |
* @expectedException \GuzzleHttp\Exception\RequestException |
*/ |
public function testThrowsUnhandledErrors() |
{ |
$m = new MockAdapter(); |
$m->setResponse(new Response(404)); |
$request = new Request('GET', 'http://httbin.org'); |
$request->getEmitter()->once('complete', function (CompleteEvent $e) { |
throw new RequestException('foo', $e->getRequest()); |
}); |
$m->send(new Transaction(new Client(), $request)); |
} |
|
public function testReadsRequestBody() |
{ |
$response = new Response(200); |
$m = new MockAdapter($response); |
$m->setResponse($response); |
$body = Stream::factory('foo'); |
$request = new Request('PUT', 'http://httpbin.org/put', [], $body); |
$this->assertSame($response, $m->send(new Transaction(new Client(), $request))); |
$this->assertEquals(3, $body->tell()); |
} |
|
public function testEmitsHeadersEvent() |
{ |
$m = new MockAdapter(new Response(404)); |
$request = new Request('GET', 'http://httbin.org'); |
$called = false; |
$request->getEmitter()->once('headers', function () use (&$called) { |
$called = true; |
}); |
$m->send(new Transaction(new Client(), $request)); |
$this->assertTrue($called); |
} |
} |
/vendor/guzzlehttp/guzzle/tests/Adapter/StreamAdapterTest.php |
@@ -0,0 +1,440 @@ |
<?php |
namespace GuzzleHttp\Tests\Adapter; |
|
use GuzzleHttp\Adapter\StreamAdapter; |
use GuzzleHttp\Client; |
use GuzzleHttp\Event\ErrorEvent; |
use GuzzleHttp\Message\MessageFactory; |
use GuzzleHttp\Message\Response; |
use GuzzleHttp\Stream\Stream; |
use GuzzleHttp\Tests\Server; |
|
/** |
* @covers GuzzleHttp\Adapter\StreamAdapter |
*/ |
class StreamAdapterTest extends \PHPUnit_Framework_TestCase |
{ |
public function testReturnsResponseForSuccessfulRequest() |
{ |
Server::flush(); |
Server::enqueue( |
"HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 2\r\n\r\nhi" |
); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$response = $client->get('/', ['headers' => ['Foo' => 'Bar']]); |
$this->assertEquals(200, $response->getStatusCode()); |
$this->assertEquals('OK', $response->getReasonPhrase()); |
$this->assertEquals('Bar', $response->getHeader('Foo')); |
$this->assertEquals('2', $response->getHeader('Content-Length')); |
$this->assertEquals('hi', $response->getBody()); |
$sent = Server::received(true)[0]; |
$this->assertEquals('GET', $sent->getMethod()); |
$this->assertEquals('/', $sent->getResource()); |
$this->assertEquals('127.0.0.1:8125', $sent->getHeader('host')); |
$this->assertEquals('Bar', $sent->getHeader('foo')); |
$this->assertTrue($sent->hasHeader('user-agent')); |
} |
|
/** |
* @expectedException \GuzzleHttp\Exception\RequestException |
* @expectedExceptionMessage Error creating resource. [url] http://localhost:123 [proxy] tcp://localhost:1234 |
*/ |
public function testThrowsExceptionsCaughtDuringTransfer() |
{ |
Server::flush(); |
$client = new Client([ |
'adapter' => new StreamAdapter(new MessageFactory()), |
]); |
$client->get('http://localhost:123', [ |
'timeout' => 0.01, |
'proxy' => 'tcp://localhost:1234' |
]); |
} |
|
/** |
* @expectedException \GuzzleHttp\Exception\RequestException |
* @expectedExceptionMessage URL is invalid: ftp://localhost:123 |
*/ |
public function testEnsuresTheHttpProtocol() |
{ |
Server::flush(); |
$client = new Client([ |
'adapter' => new StreamAdapter(new MessageFactory()), |
]); |
$client->get('ftp://localhost:123'); |
} |
|
public function testCanHandleExceptionsUsingEvents() |
{ |
Server::flush(); |
$client = new Client([ |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$request = $client->createRequest('GET', Server::$url); |
$mockResponse = new Response(200); |
$request->getEmitter()->on( |
'error', |
function (ErrorEvent $e) use ($mockResponse) { |
$e->intercept($mockResponse); |
} |
); |
$this->assertSame($mockResponse, $client->send($request)); |
} |
|
public function testEmitsAfterSendEvent() |
{ |
$ee = null; |
Server::flush(); |
Server::enqueue( |
"HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 8\r\n\r\nhi there" |
); |
$client = new Client(['adapter' => new StreamAdapter(new MessageFactory())]); |
$request = $client->createRequest('GET', Server::$url); |
$request->getEmitter()->on('complete', function ($e) use (&$ee) { |
$ee = $e; |
}); |
$client->send($request); |
$this->assertInstanceOf('GuzzleHttp\Event\CompleteEvent', $ee); |
$this->assertSame($request, $ee->getRequest()); |
$this->assertEquals(200, $ee->getResponse()->getStatusCode()); |
} |
|
public function testStreamAttributeKeepsStreamOpen() |
{ |
Server::flush(); |
Server::enqueue( |
"HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 8\r\n\r\nhi there" |
); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$response = $client->put('/foo', [ |
'headers' => ['Foo' => 'Bar'], |
'body' => 'test', |
'stream' => true |
]); |
$this->assertEquals(200, $response->getStatusCode()); |
$this->assertEquals('OK', $response->getReasonPhrase()); |
$this->assertEquals('8', $response->getHeader('Content-Length')); |
$body = $response->getBody(); |
if (defined('HHVM_VERSION')) { |
$this->markTestIncomplete('HHVM has not implemented this?'); |
} |
$this->assertEquals('http', $body->getMetadata()['wrapper_type']); |
$this->assertEquals(Server::$url . 'foo', $body->getMetadata()['uri']); |
$this->assertEquals('hi', $body->read(2)); |
$body->close(); |
|
$sent = Server::received(true)[0]; |
$this->assertEquals('PUT', $sent->getMethod()); |
$this->assertEquals('/foo', $sent->getResource()); |
$this->assertEquals('127.0.0.1:8125', $sent->getHeader('host')); |
$this->assertEquals('Bar', $sent->getHeader('foo')); |
$this->assertTrue($sent->hasHeader('user-agent')); |
} |
|
public function testDrainsResponseIntoTempStream() |
{ |
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 8\r\n\r\nhi there"); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$response = $client->get('/'); |
$body = $response->getBody(); |
$this->assertEquals('php://temp', $body->getMetadata()['uri']); |
$this->assertEquals('hi', $body->read(2)); |
$body->close(); |
} |
|
public function testDrainsResponseIntoSaveToBody() |
{ |
$r = fopen('php://temp', 'r+'); |
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 8\r\n\r\nhi there"); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$response = $client->get('/', ['save_to' => $r]); |
$body = $response->getBody(); |
$this->assertEquals('php://temp', $body->getMetadata()['uri']); |
$this->assertEquals('hi', $body->read(2)); |
$this->assertEquals(' there', stream_get_contents($r)); |
$body->close(); |
} |
|
public function testDrainsResponseIntoSaveToBodyAtPath() |
{ |
$tmpfname = tempnam('/tmp', 'save_to_path'); |
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 8\r\n\r\nhi there"); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$response = $client->get('/', ['save_to' => $tmpfname]); |
$body = $response->getBody(); |
$this->assertEquals($tmpfname, $body->getMetadata()['uri']); |
$this->assertEquals('hi', $body->read(2)); |
$body->close(); |
unlink($tmpfname); |
} |
|
public function testAutomaticallyDecompressGzip() |
{ |
Server::flush(); |
$content = gzencode('test'); |
$message = "HTTP/1.1 200 OK\r\n" |
. "Foo: Bar\r\n" |
. "Content-Encoding: gzip\r\n" |
. "Content-Length: " . strlen($content) . "\r\n\r\n" |
. $content; |
Server::enqueue($message); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$response = $client->get('/', ['stream' => true]); |
$body = $response->getBody(); |
$this->assertEquals('guzzle://stream', $body->getMetadata()['uri']); |
$this->assertEquals('test', (string) $body); |
} |
|
public function testDoesNotForceDecode() |
{ |
Server::flush(); |
$content = gzencode('test'); |
$message = "HTTP/1.1 200 OK\r\n" |
. "Foo: Bar\r\n" |
. "Content-Encoding: gzip\r\n" |
. "Content-Length: " . strlen($content) . "\r\n\r\n" |
. $content; |
Server::enqueue($message); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$response = $client->get('/', [ |
'decode_content' => false, |
'stream' => true |
]); |
$body = $response->getBody(); |
$this->assertSame($content, (string) $body); |
} |
|
protected function getStreamFromBody(Stream $body) |
{ |
$r = new \ReflectionProperty($body, 'stream'); |
$r->setAccessible(true); |
|
return $r->getValue($body); |
} |
|
protected function getSendResult(array $opts) |
{ |
Server::enqueue("HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 8\r\n\r\nhi there"); |
$client = new Client(['adapter' => new StreamAdapter(new MessageFactory())]); |
|
return $client->get(Server::$url, $opts); |
} |
|
public function testAddsProxy() |
{ |
$body = $this->getSendResult(['stream' => true, 'proxy' => '127.0.0.1:8125'])->getBody(); |
$opts = stream_context_get_options($this->getStreamFromBody($body)); |
$this->assertEquals('127.0.0.1:8125', $opts['http']['proxy']); |
} |
|
public function testAddsTimeout() |
{ |
$body = $this->getSendResult(['stream' => true, 'timeout' => 200])->getBody(); |
$opts = stream_context_get_options($this->getStreamFromBody($body)); |
$this->assertEquals(200, $opts['http']['timeout']); |
} |
|
/** |
* @expectedException \RuntimeException |
* @expectedExceptionMessage SSL certificate authority file not found: /does/not/exist |
*/ |
public function testVerifiesVerifyIsValidIfPath() |
{ |
(new Client([ |
'adapter' => new StreamAdapter(new MessageFactory()), |
'base_url' => Server::$url, |
'defaults' => ['verify' => '/does/not/exist'] |
]))->get('/'); |
} |
|
public function testVerifyCanBeDisabled() |
{ |
Server::enqueue("HTTP/1.1 200\r\nContent-Length: 0\r\n\r\n"); |
(new Client([ |
'adapter' => new StreamAdapter(new MessageFactory()), |
'base_url' => Server::$url, |
'defaults' => ['verify' => false] |
]))->get('/'); |
} |
|
public function testVerifyCanBeSetToPath() |
{ |
$path = __DIR__ . '/../../src/cacert.pem'; |
$this->assertFileExists($path); |
$body = $this->getSendResult(['stream' => true, 'verify' => $path])->getBody(); |
$opts = stream_context_get_options($this->getStreamFromBody($body)); |
$this->assertEquals(true, $opts['http']['verify_peer']); |
$this->assertEquals($path, $opts['http']['cafile']); |
$this->assertTrue(file_exists($opts['http']['cafile'])); |
} |
|
/** |
* @expectedException \RuntimeException |
* @expectedExceptionMessage SSL certificate not found: /does/not/exist |
*/ |
public function testVerifiesCertIfValidPath() |
{ |
(new Client([ |
'adapter' => new StreamAdapter(new MessageFactory()), |
'base_url' => Server::$url, |
'defaults' => ['cert' => '/does/not/exist'] |
]))->get('/'); |
} |
|
public function testCanSetPasswordWhenSettingCert() |
{ |
$path = __DIR__ . '/../../src/cacert.pem'; |
$body = $this->getSendResult(['stream' => true, 'cert' => [$path, 'foo']])->getBody(); |
$opts = stream_context_get_options($this->getStreamFromBody($body)); |
$this->assertEquals($path, $opts['http']['local_cert']); |
$this->assertEquals('foo', $opts['http']['passphrase']); |
} |
|
public function testDebugAttributeWritesStreamInfoToTempBufferByDefault() |
{ |
if (defined('HHVM_VERSION')) { |
$this->markTestSkipped('HHVM has not implemented this?'); |
return; |
} |
|
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\nFoo: Bar\r\nContent-Length: 8\r\n\r\nhi there"); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$fp = fopen('php://temp', 'w'); |
$client->get('/', ['debug' => $fp]); |
fseek($fp, 0); |
$contents = stream_get_contents($fp); |
$this->assertContains('<http://127.0.0.1:8125/> [CONNECT]', $contents); |
$this->assertContains('<http://127.0.0.1:8125/> [FILE_SIZE_IS]', $contents); |
$this->assertContains('<http://127.0.0.1:8125/> [PROGRESS]', $contents); |
} |
|
public function testDebugAttributeWritesStreamInfoToBuffer() |
{ |
if (defined('HHVM_VERSION')) { |
$this->markTestSkipped('HHVM has not implemented this?'); |
return; |
} |
|
$buffer = fopen('php://temp', 'r+'); |
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\nContent-Length: 8\r\nContent-Type: text/plain\r\n\r\nhi there"); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$client->get('/', ['debug' => $buffer]); |
fseek($buffer, 0); |
$contents = stream_get_contents($buffer); |
$this->assertContains('<http://127.0.0.1:8125/> [CONNECT]', $contents); |
$this->assertContains('<http://127.0.0.1:8125/> [FILE_SIZE_IS] message: "Content-Length: 8"', $contents); |
$this->assertContains('<http://127.0.0.1:8125/> [PROGRESS] bytes_max: "8"', $contents); |
$this->assertContains('<http://127.0.0.1:8125/> [MIME_TYPE_IS] message: "text/plain"', $contents); |
} |
|
public function testAddsProxyByProtocol() |
{ |
$url = str_replace('http', 'tcp', Server::$url); |
$body = $this->getSendResult(['stream' => true, 'proxy' => ['http' => $url]])->getBody(); |
$opts = stream_context_get_options($this->getStreamFromBody($body)); |
$this->assertEquals($url, $opts['http']['proxy']); |
} |
|
public function testPerformsShallowMergeOfCustomContextOptions() |
{ |
$body = $this->getSendResult([ |
'stream' => true, |
'config' => [ |
'stream_context' => [ |
'http' => [ |
'request_fulluri' => true, |
'method' => 'HEAD' |
], |
'socket' => [ |
'bindto' => '127.0.0.1:0' |
], |
'ssl' => [ |
'verify_peer' => false |
] |
] |
] |
])->getBody(); |
|
$opts = stream_context_get_options($this->getStreamFromBody($body)); |
$this->assertEquals('HEAD', $opts['http']['method']); |
$this->assertTrue($opts['http']['request_fulluri']); |
$this->assertFalse($opts['ssl']['verify_peer']); |
$this->assertEquals('127.0.0.1:0', $opts['socket']['bindto']); |
} |
|
/** |
* @expectedException \GuzzleHttp\Exception\RequestException |
* @expectedExceptionMessage stream_context must be an array |
*/ |
public function testEnsuresThatStreamContextIsAnArray() |
{ |
$this->getSendResult([ |
'stream' => true, |
'config' => ['stream_context' => 'foo'] |
]); |
} |
|
/** |
* @ticket https://github.com/guzzle/guzzle/issues/725 |
*/ |
public function testHandlesMultipleHeadersOfSameName() |
{ |
$a = new StreamAdapter(new MessageFactory()); |
$ref = new \ReflectionMethod($a, 'headersFromLines'); |
$ref->setAccessible(true); |
$this->assertEquals([ |
'foo' => ['bar', 'bam'], |
'abc' => ['123'] |
], $ref->invoke($a, [ |
'foo: bar', |
'foo: bam', |
'abc: 123' |
])); |
} |
|
public function testDoesNotAddContentTypeByDefault() |
{ |
Server::flush(); |
Server::enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); |
$client = new Client([ |
'base_url' => Server::$url, |
'adapter' => new StreamAdapter(new MessageFactory()) |
]); |
$client->put('/', ['body' => 'foo']); |
$requests = Server::received(true); |
$this->assertEquals('', $requests[0]->getHeader('Content-Type')); |
$this->assertEquals(3, $requests[0]->getHeader('Content-Length')); |
} |
} |