updated plugin W3 Total Cache
version 2.3.3
This commit is contained in:
770
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/CurlFactoryTest.php
vendored
Normal file
770
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/CurlFactoryTest.php
vendored
Normal file
@ -0,0 +1,770 @@
|
||||
<?php
|
||||
namespace GuzzleHttp\Test\Handler;
|
||||
|
||||
use GuzzleHttp\Handler;
|
||||
use GuzzleHttp\Handler\CurlFactory;
|
||||
use GuzzleHttp\Handler\EasyHandle;
|
||||
use GuzzleHttp\Psr7;
|
||||
use GuzzleHttp\Tests\Server;
|
||||
use GuzzleHttp\TransferStats;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* @covers \GuzzleHttp\Handler\CurlFactory
|
||||
*/
|
||||
class CurlFactoryTest extends TestCase
|
||||
{
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
$_SERVER['curl_test'] = true;
|
||||
unset($_SERVER['_curl']);
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass()
|
||||
{
|
||||
unset($_SERVER['_curl'], $_SERVER['curl_test']);
|
||||
}
|
||||
|
||||
public function testCreatesCurlHandle()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([
|
||||
new Psr7\Response(200, [
|
||||
'Foo' => 'Bar',
|
||||
'Baz' => 'bam',
|
||||
'Content-Length' => 2,
|
||||
], 'hi')
|
||||
]);
|
||||
$stream = Psr7\stream_for();
|
||||
$request = new Psr7\Request('PUT', Server::$url, [
|
||||
'Hi' => ' 123',
|
||||
'Content-Length' => '7'
|
||||
], 'testing');
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$result = $f->create($request, ['sink' => $stream]);
|
||||
self::assertInstanceOf(EasyHandle::class, $result);
|
||||
self::assertInternalType('resource', $result->handle);
|
||||
self::assertInternalType('array', $result->headers);
|
||||
self::assertSame($stream, $result->sink);
|
||||
curl_close($result->handle);
|
||||
self::assertSame('PUT', $_SERVER['_curl'][CURLOPT_CUSTOMREQUEST]);
|
||||
self::assertSame(
|
||||
'http://127.0.0.1:8126/',
|
||||
$_SERVER['_curl'][CURLOPT_URL]
|
||||
);
|
||||
// Sends via post fields when the request is small enough
|
||||
self::assertSame('testing', $_SERVER['_curl'][CURLOPT_POSTFIELDS]);
|
||||
self::assertEquals(0, $_SERVER['_curl'][CURLOPT_RETURNTRANSFER]);
|
||||
self::assertEquals(0, $_SERVER['_curl'][CURLOPT_HEADER]);
|
||||
self::assertSame(150, $_SERVER['_curl'][CURLOPT_CONNECTTIMEOUT]);
|
||||
self::assertInstanceOf('Closure', $_SERVER['_curl'][CURLOPT_HEADERFUNCTION]);
|
||||
if (defined('CURLOPT_PROTOCOLS')) {
|
||||
self::assertSame(
|
||||
CURLPROTO_HTTP | CURLPROTO_HTTPS,
|
||||
$_SERVER['_curl'][CURLOPT_PROTOCOLS]
|
||||
);
|
||||
}
|
||||
self::assertContains('Expect:', $_SERVER['_curl'][CURLOPT_HTTPHEADER]);
|
||||
self::assertContains('Accept:', $_SERVER['_curl'][CURLOPT_HTTPHEADER]);
|
||||
self::assertContains('Content-Type:', $_SERVER['_curl'][CURLOPT_HTTPHEADER]);
|
||||
self::assertContains('Hi: 123', $_SERVER['_curl'][CURLOPT_HTTPHEADER]);
|
||||
self::assertContains('Host: 127.0.0.1:8126', $_SERVER['_curl'][CURLOPT_HTTPHEADER]);
|
||||
}
|
||||
|
||||
public function testSendsHeadRequests()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Psr7\Response()]);
|
||||
$a = new Handler\CurlMultiHandler();
|
||||
$response = $a(new Psr7\Request('HEAD', Server::$url), []);
|
||||
$response->wait();
|
||||
self::assertEquals(true, $_SERVER['_curl'][CURLOPT_NOBODY]);
|
||||
$checks = [CURLOPT_WRITEFUNCTION, CURLOPT_READFUNCTION, CURLOPT_INFILE];
|
||||
foreach ($checks as $check) {
|
||||
self::assertArrayNotHasKey($check, $_SERVER['_curl']);
|
||||
}
|
||||
self::assertEquals('HEAD', Server::received()[0]->getMethod());
|
||||
}
|
||||
|
||||
public function testCanAddCustomCurlOptions()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Psr7\Response()]);
|
||||
$a = new Handler\CurlMultiHandler();
|
||||
$req = new Psr7\Request('GET', Server::$url);
|
||||
$a($req, ['curl' => [CURLOPT_LOW_SPEED_LIMIT => 10]]);
|
||||
self::assertEquals(10, $_SERVER['_curl'][CURLOPT_LOW_SPEED_LIMIT]);
|
||||
}
|
||||
|
||||
public function testCanChangeCurlOptions()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Psr7\Response()]);
|
||||
$a = new Handler\CurlMultiHandler();
|
||||
$req = new Psr7\Request('GET', Server::$url);
|
||||
$a($req, ['curl' => [CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0]]);
|
||||
self::assertEquals(CURL_HTTP_VERSION_1_0, $_SERVER['_curl'][CURLOPT_HTTP_VERSION]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage SSL CA bundle not found: /does/not/exist
|
||||
*/
|
||||
public function testValidatesVerify()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['verify' => '/does/not/exist']);
|
||||
}
|
||||
|
||||
public function testCanSetVerifyToFile()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', 'http://foo.com'), ['verify' => __FILE__]);
|
||||
self::assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_CAINFO]);
|
||||
self::assertEquals(2, $_SERVER['_curl'][CURLOPT_SSL_VERIFYHOST]);
|
||||
self::assertEquals(true, $_SERVER['_curl'][CURLOPT_SSL_VERIFYPEER]);
|
||||
}
|
||||
|
||||
public function testCanSetVerifyToDir()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', 'http://foo.com'), ['verify' => __DIR__]);
|
||||
self::assertEquals(__DIR__, $_SERVER['_curl'][CURLOPT_CAPATH]);
|
||||
self::assertEquals(2, $_SERVER['_curl'][CURLOPT_SSL_VERIFYHOST]);
|
||||
self::assertEquals(true, $_SERVER['_curl'][CURLOPT_SSL_VERIFYPEER]);
|
||||
}
|
||||
|
||||
public function testAddsVerifyAsTrue()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['verify' => true]);
|
||||
self::assertEquals(2, $_SERVER['_curl'][CURLOPT_SSL_VERIFYHOST]);
|
||||
self::assertEquals(true, $_SERVER['_curl'][CURLOPT_SSL_VERIFYPEER]);
|
||||
self::assertArrayNotHasKey(CURLOPT_CAINFO, $_SERVER['_curl']);
|
||||
}
|
||||
|
||||
public function testCanDisableVerify()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['verify' => false]);
|
||||
self::assertEquals(0, $_SERVER['_curl'][CURLOPT_SSL_VERIFYHOST]);
|
||||
self::assertEquals(false, $_SERVER['_curl'][CURLOPT_SSL_VERIFYPEER]);
|
||||
}
|
||||
|
||||
public function testAddsProxy()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['proxy' => 'http://bar.com']);
|
||||
self::assertEquals('http://bar.com', $_SERVER['_curl'][CURLOPT_PROXY]);
|
||||
}
|
||||
|
||||
public function testAddsViaScheme()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), [
|
||||
'proxy' => ['http' => 'http://bar.com', 'https' => 'https://t'],
|
||||
]);
|
||||
self::assertEquals('http://bar.com', $_SERVER['_curl'][CURLOPT_PROXY]);
|
||||
$this->checkNoProxyForHost('http://test.test.com', ['test.test.com'], false);
|
||||
$this->checkNoProxyForHost('http://test.test.com', ['.test.com'], false);
|
||||
$this->checkNoProxyForHost('http://test.test.com', ['*.test.com'], true);
|
||||
$this->checkNoProxyForHost('http://test.test.com', ['*'], false);
|
||||
$this->checkNoProxyForHost('http://127.0.0.1', ['127.0.0.*'], true);
|
||||
}
|
||||
|
||||
private function checkNoProxyForHost($url, $noProxy, $assertUseProxy)
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', $url), [
|
||||
'proxy' => [
|
||||
'http' => 'http://bar.com',
|
||||
'https' => 'https://t',
|
||||
'no' => $noProxy
|
||||
],
|
||||
]);
|
||||
if ($assertUseProxy) {
|
||||
self::assertArrayHasKey(CURLOPT_PROXY, $_SERVER['_curl']);
|
||||
} else {
|
||||
self::assertArrayNotHasKey(CURLOPT_PROXY, $_SERVER['_curl']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage SSL private key not found: /does/not/exist
|
||||
*/
|
||||
public function testValidatesSslKey()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['ssl_key' => '/does/not/exist']);
|
||||
}
|
||||
|
||||
public function testAddsSslKey()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['ssl_key' => __FILE__]);
|
||||
self::assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_SSLKEY]);
|
||||
}
|
||||
|
||||
public function testAddsSslKeyWithPassword()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['ssl_key' => [__FILE__, 'test']]);
|
||||
self::assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_SSLKEY]);
|
||||
self::assertEquals('test', $_SERVER['_curl'][CURLOPT_SSLKEYPASSWD]);
|
||||
}
|
||||
|
||||
public function testAddsSslKeyWhenUsingArraySyntaxButNoPassword()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['ssl_key' => [__FILE__]]);
|
||||
|
||||
self::assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_SSLKEY]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage SSL certificate not found: /does/not/exist
|
||||
*/
|
||||
public function testValidatesCert()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['cert' => '/does/not/exist']);
|
||||
}
|
||||
|
||||
public function testAddsCert()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['cert' => __FILE__]);
|
||||
self::assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_SSLCERT]);
|
||||
}
|
||||
|
||||
public function testAddsCertWithPassword()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['cert' => [__FILE__, 'test']]);
|
||||
self::assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_SSLCERT]);
|
||||
self::assertEquals('test', $_SERVER['_curl'][CURLOPT_SSLCERTPASSWD]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage progress client option must be callable
|
||||
*/
|
||||
public function testValidatesProgress()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), ['progress' => 'foo']);
|
||||
}
|
||||
|
||||
public function testEmitsDebugInfoToStream()
|
||||
{
|
||||
$res = fopen('php://memory', 'r+');
|
||||
Server::flush();
|
||||
Server::enqueue([new Psr7\Response()]);
|
||||
$a = new Handler\CurlMultiHandler();
|
||||
$response = $a(new Psr7\Request('HEAD', Server::$url), ['debug' => $res]);
|
||||
$response->wait();
|
||||
rewind($res);
|
||||
$output = str_replace("\r", '', stream_get_contents($res));
|
||||
self::assertContains("> HEAD / HTTP/1.1", $output);
|
||||
self::assertContains("< HTTP/1.1 200", $output);
|
||||
fclose($res);
|
||||
}
|
||||
|
||||
public function testEmitsProgressToFunction()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Psr7\Response()]);
|
||||
$a = new Handler\CurlMultiHandler();
|
||||
$called = [];
|
||||
$request = new Psr7\Request('HEAD', Server::$url);
|
||||
$response = $a($request, [
|
||||
'progress' => function () use (&$called) {
|
||||
$called[] = func_get_args();
|
||||
},
|
||||
]);
|
||||
$response->wait();
|
||||
self::assertNotEmpty($called);
|
||||
foreach ($called as $call) {
|
||||
self::assertCount(4, $call);
|
||||
}
|
||||
}
|
||||
|
||||
private function addDecodeResponse($withEncoding = true)
|
||||
{
|
||||
$content = gzencode('test');
|
||||
$headers = ['Content-Length' => strlen($content)];
|
||||
if ($withEncoding) {
|
||||
$headers['Content-Encoding'] = 'gzip';
|
||||
}
|
||||
$response = new Psr7\Response(200, $headers, $content);
|
||||
Server::flush();
|
||||
Server::enqueue([$response]);
|
||||
return $content;
|
||||
}
|
||||
|
||||
public function testDecodesGzippedResponses()
|
||||
{
|
||||
$this->addDecodeResponse();
|
||||
$handler = new Handler\CurlMultiHandler();
|
||||
$request = new Psr7\Request('GET', Server::$url);
|
||||
$response = $handler($request, ['decode_content' => true]);
|
||||
$response = $response->wait();
|
||||
self::assertEquals('test', (string) $response->getBody());
|
||||
self::assertEquals('', $_SERVER['_curl'][CURLOPT_ENCODING]);
|
||||
$sent = Server::received()[0];
|
||||
self::assertFalse($sent->hasHeader('Accept-Encoding'));
|
||||
}
|
||||
|
||||
public function testReportsOriginalSizeAndContentEncodingAfterDecoding()
|
||||
{
|
||||
$this->addDecodeResponse();
|
||||
$handler = new Handler\CurlMultiHandler();
|
||||
$request = new Psr7\Request('GET', Server::$url);
|
||||
$response = $handler($request, ['decode_content' => true]);
|
||||
$response = $response->wait();
|
||||
self::assertSame(
|
||||
'gzip',
|
||||
$response->getHeaderLine('x-encoded-content-encoding')
|
||||
);
|
||||
self::assertSame(
|
||||
strlen(gzencode('test')),
|
||||
(int) $response->getHeaderLine('x-encoded-content-length')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDecodesGzippedResponsesWithHeader()
|
||||
{
|
||||
$this->addDecodeResponse();
|
||||
$handler = new Handler\CurlMultiHandler();
|
||||
$request = new Psr7\Request('GET', Server::$url, ['Accept-Encoding' => 'gzip']);
|
||||
$response = $handler($request, ['decode_content' => true]);
|
||||
$response = $response->wait();
|
||||
self::assertEquals('gzip', $_SERVER['_curl'][CURLOPT_ENCODING]);
|
||||
$sent = Server::received()[0];
|
||||
self::assertEquals('gzip', $sent->getHeaderLine('Accept-Encoding'));
|
||||
self::assertEquals('test', (string) $response->getBody());
|
||||
self::assertFalse($response->hasHeader('content-encoding'));
|
||||
self::assertTrue(
|
||||
!$response->hasHeader('content-length') ||
|
||||
$response->getHeaderLine('content-length') == $response->getBody()->getSize()
|
||||
);
|
||||
}
|
||||
|
||||
public function testDoesNotForceDecode()
|
||||
{
|
||||
$content = $this->addDecodeResponse();
|
||||
$handler = new Handler\CurlMultiHandler();
|
||||
$request = new Psr7\Request('GET', Server::$url);
|
||||
$response = $handler($request, ['decode_content' => false]);
|
||||
$response = $response->wait();
|
||||
$sent = Server::received()[0];
|
||||
self::assertFalse($sent->hasHeader('Accept-Encoding'));
|
||||
self::assertEquals($content, (string) $response->getBody());
|
||||
}
|
||||
|
||||
public function testProtocolVersion()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Psr7\Response()]);
|
||||
$a = new Handler\CurlMultiHandler();
|
||||
$request = new Psr7\Request('GET', Server::$url, [], null, '1.0');
|
||||
$a($request, []);
|
||||
self::assertEquals(CURL_HTTP_VERSION_1_0, $_SERVER['_curl'][CURLOPT_HTTP_VERSION]);
|
||||
}
|
||||
|
||||
public function testSavesToStream()
|
||||
{
|
||||
$stream = fopen('php://memory', 'r+');
|
||||
$this->addDecodeResponse();
|
||||
$handler = new Handler\CurlMultiHandler();
|
||||
$request = new Psr7\Request('GET', Server::$url);
|
||||
$response = $handler($request, [
|
||||
'decode_content' => true,
|
||||
'sink' => $stream,
|
||||
]);
|
||||
$response->wait();
|
||||
rewind($stream);
|
||||
self::assertEquals('test', stream_get_contents($stream));
|
||||
}
|
||||
|
||||
public function testSavesToGuzzleStream()
|
||||
{
|
||||
$stream = Psr7\stream_for();
|
||||
$this->addDecodeResponse();
|
||||
$handler = new Handler\CurlMultiHandler();
|
||||
$request = new Psr7\Request('GET', Server::$url);
|
||||
$response = $handler($request, [
|
||||
'decode_content' => true,
|
||||
'sink' => $stream,
|
||||
]);
|
||||
$response->wait();
|
||||
self::assertEquals('test', (string) $stream);
|
||||
}
|
||||
|
||||
public function testSavesToFileOnDisk()
|
||||
{
|
||||
$tmpfile = tempnam(sys_get_temp_dir(), 'testfile');
|
||||
$this->addDecodeResponse();
|
||||
$handler = new Handler\CurlMultiHandler();
|
||||
$request = new Psr7\Request('GET', Server::$url);
|
||||
$response = $handler($request, [
|
||||
'decode_content' => true,
|
||||
'sink' => $tmpfile,
|
||||
]);
|
||||
$response->wait();
|
||||
self::assertStringEqualsFile($tmpfile, 'test');
|
||||
unlink($tmpfile);
|
||||
}
|
||||
|
||||
public function testDoesNotAddMultipleContentLengthHeaders()
|
||||
{
|
||||
$this->addDecodeResponse();
|
||||
$handler = new Handler\CurlMultiHandler();
|
||||
$request = new Psr7\Request('PUT', Server::$url, ['Content-Length' => 3], 'foo');
|
||||
$response = $handler($request, []);
|
||||
$response->wait();
|
||||
$sent = Server::received()[0];
|
||||
self::assertEquals(3, $sent->getHeaderLine('Content-Length'));
|
||||
self::assertFalse($sent->hasHeader('Transfer-Encoding'));
|
||||
self::assertEquals('foo', (string) $sent->getBody());
|
||||
}
|
||||
|
||||
public function testSendsPostWithNoBodyOrDefaultContentType()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Psr7\Response()]);
|
||||
$handler = new Handler\CurlMultiHandler();
|
||||
$request = new Psr7\Request('POST', Server::$url);
|
||||
$response = $handler($request, []);
|
||||
$response->wait();
|
||||
$received = Server::received()[0];
|
||||
self::assertEquals('POST', $received->getMethod());
|
||||
self::assertFalse($received->hasHeader('content-type'));
|
||||
self::assertSame('0', $received->getHeaderLine('content-length'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\RequestException
|
||||
* @expectedExceptionMessage but attempting to rewind the request body failed
|
||||
*/
|
||||
public function testFailsWhenCannotRewindRetryAfterNoResponse()
|
||||
{
|
||||
$factory = new Handler\CurlFactory(1);
|
||||
$stream = Psr7\stream_for('abc');
|
||||
$stream->read(1);
|
||||
$stream = new Psr7\NoSeekStream($stream);
|
||||
$request = new Psr7\Request('PUT', Server::$url, [], $stream);
|
||||
$fn = function ($request, $options) use (&$fn, $factory) {
|
||||
$easy = $factory->create($request, $options);
|
||||
return Handler\CurlFactory::finish($fn, $easy, $factory);
|
||||
};
|
||||
$fn($request, [])->wait();
|
||||
}
|
||||
|
||||
public function testRetriesWhenBodyCanBeRewound()
|
||||
{
|
||||
$callHandler = $called = false;
|
||||
|
||||
$fn = function ($r, $options) use (&$callHandler) {
|
||||
$callHandler = true;
|
||||
return \GuzzleHttp\Promise\promise_for(new Psr7\Response());
|
||||
};
|
||||
|
||||
$bd = Psr7\FnStream::decorate(Psr7\stream_for('test'), [
|
||||
'tell' => function () {
|
||||
return 1;
|
||||
},
|
||||
'rewind' => function () use (&$called) {
|
||||
$called = true;
|
||||
}
|
||||
]);
|
||||
|
||||
$factory = new Handler\CurlFactory(1);
|
||||
$req = new Psr7\Request('PUT', Server::$url, [], $bd);
|
||||
$easy = $factory->create($req, []);
|
||||
$res = Handler\CurlFactory::finish($fn, $easy, $factory);
|
||||
$res = $res->wait();
|
||||
self::assertTrue($callHandler);
|
||||
self::assertTrue($called);
|
||||
self::assertEquals('200', $res->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\RequestException
|
||||
* @expectedExceptionMessage The cURL request was retried 3 times
|
||||
*/
|
||||
public function testFailsWhenRetryMoreThanThreeTimes()
|
||||
{
|
||||
$factory = new Handler\CurlFactory(1);
|
||||
$call = 0;
|
||||
$fn = function ($request, $options) use (&$mock, &$call, $factory) {
|
||||
$call++;
|
||||
$easy = $factory->create($request, $options);
|
||||
return Handler\CurlFactory::finish($mock, $easy, $factory);
|
||||
};
|
||||
$mock = new Handler\MockHandler([$fn, $fn, $fn]);
|
||||
$p = $mock(new Psr7\Request('PUT', Server::$url, [], 'test'), []);
|
||||
$p->wait(false);
|
||||
self::assertEquals(3, $call);
|
||||
$p->wait(true);
|
||||
}
|
||||
|
||||
public function testHandles100Continue()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([
|
||||
new Psr7\Response(200, ['Test' => 'Hello', 'Content-Length' => 4], 'test'),
|
||||
]);
|
||||
$request = new Psr7\Request('PUT', Server::$url, [
|
||||
'Expect' => '100-Continue'
|
||||
], 'test');
|
||||
$handler = new Handler\CurlMultiHandler();
|
||||
$response = $handler($request, [])->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
self::assertSame('OK', $response->getReasonPhrase());
|
||||
self::assertSame('Hello', $response->getHeaderLine('Test'));
|
||||
self::assertSame('4', $response->getHeaderLine('Content-Length'));
|
||||
self::assertSame('test', (string) $response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\ConnectException
|
||||
*/
|
||||
public function testCreatesConnectException()
|
||||
{
|
||||
$m = new \ReflectionMethod(CurlFactory::class, 'finishError');
|
||||
$m->setAccessible(true);
|
||||
$factory = new Handler\CurlFactory(1);
|
||||
$easy = $factory->create(new Psr7\Request('GET', Server::$url), []);
|
||||
$easy->errno = CURLE_COULDNT_CONNECT;
|
||||
$response = $m->invoke(
|
||||
null,
|
||||
function () {
|
||||
},
|
||||
$easy,
|
||||
$factory
|
||||
);
|
||||
$response->wait();
|
||||
}
|
||||
|
||||
public function testAddsTimeouts()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), [
|
||||
'timeout' => 0.1,
|
||||
'connect_timeout' => 0.2
|
||||
]);
|
||||
self::assertEquals(100, $_SERVER['_curl'][CURLOPT_TIMEOUT_MS]);
|
||||
self::assertEquals(200, $_SERVER['_curl'][CURLOPT_CONNECTTIMEOUT_MS]);
|
||||
}
|
||||
|
||||
public function testAddsStreamingBody()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$bd = Psr7\FnStream::decorate(Psr7\stream_for('foo'), [
|
||||
'getSize' => function () {
|
||||
return null;
|
||||
}
|
||||
]);
|
||||
$request = new Psr7\Request('PUT', Server::$url, [], $bd);
|
||||
$f->create($request, []);
|
||||
self::assertEquals(1, $_SERVER['_curl'][CURLOPT_UPLOAD]);
|
||||
self::assertInternalType('callable', $_SERVER['_curl'][CURLOPT_READFUNCTION]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
* @expectedExceptionMessage Directory /does/not/exist/so does not exist for sink value of /does/not/exist/so/error.txt
|
||||
*/
|
||||
public function testEnsuresDirExistsBeforeThrowingWarning()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$f->create(new Psr7\Request('GET', Server::$url), [
|
||||
'sink' => '/does/not/exist/so/error.txt'
|
||||
]);
|
||||
}
|
||||
|
||||
public function testClosesIdleHandles()
|
||||
{
|
||||
$f = new Handler\CurlFactory(3);
|
||||
$req = new Psr7\Request('GET', Server::$url);
|
||||
$easy = $f->create($req, []);
|
||||
$h1 = $easy->handle;
|
||||
$f->release($easy);
|
||||
self::assertCount(1, self::readAttribute($f, 'handles'));
|
||||
$easy = $f->create($req, []);
|
||||
self::assertSame($easy->handle, $h1);
|
||||
$easy2 = $f->create($req, []);
|
||||
$easy3 = $f->create($req, []);
|
||||
$easy4 = $f->create($req, []);
|
||||
$f->release($easy);
|
||||
self::assertCount(1, self::readAttribute($f, 'handles'));
|
||||
$f->release($easy2);
|
||||
self::assertCount(2, self::readAttribute($f, 'handles'));
|
||||
$f->release($easy3);
|
||||
self::assertCount(3, self::readAttribute($f, 'handles'));
|
||||
$f->release($easy4);
|
||||
self::assertCount(3, self::readAttribute($f, 'handles'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testEnsuresOnHeadersIsCallable()
|
||||
{
|
||||
$req = new Psr7\Request('GET', Server::$url);
|
||||
$handler = new Handler\CurlHandler();
|
||||
$handler($req, ['on_headers' => 'error!']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\RequestException
|
||||
* @expectedExceptionMessage An error was encountered during the on_headers event
|
||||
* @expectedExceptionMessage test
|
||||
*/
|
||||
public function testRejectsPromiseWhenOnHeadersFails()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([
|
||||
new Psr7\Response(200, ['X-Foo' => 'bar'], 'abc 123')
|
||||
]);
|
||||
$req = new Psr7\Request('GET', Server::$url);
|
||||
$handler = new Handler\CurlHandler();
|
||||
$promise = $handler($req, [
|
||||
'on_headers' => function () {
|
||||
throw new \Exception('test');
|
||||
}
|
||||
]);
|
||||
$promise->wait();
|
||||
}
|
||||
|
||||
public function testSuccessfullyCallsOnHeadersBeforeWritingToSink()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([
|
||||
new Psr7\Response(200, ['X-Foo' => 'bar'], 'abc 123')
|
||||
]);
|
||||
$req = new Psr7\Request('GET', Server::$url);
|
||||
$got = null;
|
||||
|
||||
$stream = Psr7\stream_for();
|
||||
$stream = Psr7\FnStream::decorate($stream, [
|
||||
'write' => function ($data) use ($stream, &$got) {
|
||||
self::assertNotNull($got);
|
||||
return $stream->write($data);
|
||||
}
|
||||
]);
|
||||
|
||||
$handler = new Handler\CurlHandler();
|
||||
$promise = $handler($req, [
|
||||
'sink' => $stream,
|
||||
'on_headers' => function (ResponseInterface $res) use (&$got) {
|
||||
$got = $res;
|
||||
self::assertEquals('bar', $res->getHeaderLine('X-Foo'));
|
||||
}
|
||||
]);
|
||||
|
||||
$response = $promise->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
self::assertSame('bar', $response->getHeaderLine('X-Foo'));
|
||||
self::assertSame('abc 123', (string) $response->getBody());
|
||||
}
|
||||
|
||||
public function testInvokesOnStatsOnSuccess()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Psr7\Response(200)]);
|
||||
$req = new Psr7\Request('GET', Server::$url);
|
||||
$gotStats = null;
|
||||
$handler = new Handler\CurlHandler();
|
||||
$promise = $handler($req, [
|
||||
'on_stats' => function (TransferStats $stats) use (&$gotStats) {
|
||||
$gotStats = $stats;
|
||||
}
|
||||
]);
|
||||
$response = $promise->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
self::assertSame(200, $gotStats->getResponse()->getStatusCode());
|
||||
self::assertSame(
|
||||
Server::$url,
|
||||
(string) $gotStats->getEffectiveUri()
|
||||
);
|
||||
self::assertSame(
|
||||
Server::$url,
|
||||
(string) $gotStats->getRequest()->getUri()
|
||||
);
|
||||
self::assertGreaterThan(0, $gotStats->getTransferTime());
|
||||
self::assertArrayHasKey('appconnect_time', $gotStats->getHandlerStats());
|
||||
}
|
||||
|
||||
public function testInvokesOnStatsOnError()
|
||||
{
|
||||
$req = new Psr7\Request('GET', 'http://127.0.0.1:123');
|
||||
$gotStats = null;
|
||||
$handler = new Handler\CurlHandler();
|
||||
$promise = $handler($req, [
|
||||
'connect_timeout' => 0.001,
|
||||
'timeout' => 0.001,
|
||||
'on_stats' => function (TransferStats $stats) use (&$gotStats) {
|
||||
$gotStats = $stats;
|
||||
}
|
||||
]);
|
||||
$promise->wait(false);
|
||||
self::assertFalse($gotStats->hasResponse());
|
||||
self::assertSame(
|
||||
'http://127.0.0.1:123',
|
||||
(string) $gotStats->getEffectiveUri()
|
||||
);
|
||||
self::assertSame(
|
||||
'http://127.0.0.1:123',
|
||||
(string) $gotStats->getRequest()->getUri()
|
||||
);
|
||||
self::assertInternalType('float', $gotStats->getTransferTime());
|
||||
self::assertInternalType('int', $gotStats->getHandlerErrorData());
|
||||
self::assertArrayHasKey('appconnect_time', $gotStats->getHandlerStats());
|
||||
}
|
||||
|
||||
public function testRewindsBodyIfPossible()
|
||||
{
|
||||
$body = Psr7\stream_for(str_repeat('x', 1024 * 1024 * 2));
|
||||
$body->seek(1024 * 1024);
|
||||
self::assertSame(1024 * 1024, $body->tell());
|
||||
|
||||
$req = new Psr7\Request('POST', 'https://www.example.com', [
|
||||
'Content-Length' => 1024 * 1024 * 2,
|
||||
], $body);
|
||||
$factory = new CurlFactory(1);
|
||||
$factory->create($req, []);
|
||||
|
||||
self::assertSame(0, $body->tell());
|
||||
}
|
||||
|
||||
public function testDoesNotRewindUnseekableBody()
|
||||
{
|
||||
$body = Psr7\stream_for(str_repeat('x', 1024 * 1024 * 2));
|
||||
$body->seek(1024 * 1024);
|
||||
$body = new Psr7\NoSeekStream($body);
|
||||
self::assertSame(1024 * 1024, $body->tell());
|
||||
|
||||
$req = new Psr7\Request('POST', 'https://www.example.com', [
|
||||
'Content-Length' => 1024 * 1024,
|
||||
], $body);
|
||||
$factory = new CurlFactory(1);
|
||||
$factory->create($req, []);
|
||||
|
||||
self::assertSame(1024 * 1024, $body->tell());
|
||||
}
|
||||
|
||||
public function testRelease()
|
||||
{
|
||||
$factory = new CurlFactory(1);
|
||||
$easyHandle = new EasyHandle();
|
||||
$easyHandle->handle = curl_init();
|
||||
|
||||
self::assertEmpty($factory->release($easyHandle));
|
||||
}
|
||||
}
|
87
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/CurlHandlerTest.php
vendored
Normal file
87
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/CurlHandlerTest.php
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
namespace GuzzleHttp\Test\Handler;
|
||||
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use GuzzleHttp\Handler\CurlHandler;
|
||||
use GuzzleHttp\Psr7;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use GuzzleHttp\Tests\Server;
|
||||
use GuzzleHttp\Utils;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \GuzzleHttp\Handler\CurlHandler
|
||||
*/
|
||||
class CurlHandlerTest extends TestCase
|
||||
{
|
||||
protected function getHandler($options = [])
|
||||
{
|
||||
return new CurlHandler($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\ConnectException
|
||||
* @expectedExceptionMessage cURL
|
||||
*/
|
||||
public function testCreatesCurlErrors()
|
||||
{
|
||||
$handler = new CurlHandler();
|
||||
$request = new Request('GET', 'http://localhost:123');
|
||||
$handler($request, ['timeout' => 0.001, 'connect_timeout' => 0.001])->wait();
|
||||
}
|
||||
|
||||
public function testReusesHandles()
|
||||
{
|
||||
Server::flush();
|
||||
$response = new response(200);
|
||||
Server::enqueue([$response, $response]);
|
||||
$a = new CurlHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
self::assertInstanceOf('GuzzleHttp\Promise\FulfilledPromise', $a($request, []));
|
||||
self::assertInstanceOf('GuzzleHttp\Promise\FulfilledPromise', $a($request, []));
|
||||
}
|
||||
|
||||
public function testDoesSleep()
|
||||
{
|
||||
$response = new response(200);
|
||||
Server::enqueue([$response]);
|
||||
$a = new CurlHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$s = Utils::currentTime();
|
||||
$a($request, ['delay' => 0.1])->wait();
|
||||
self::assertGreaterThan(0.0001, Utils::currentTime() - $s);
|
||||
}
|
||||
|
||||
public function testCreatesCurlErrorsWithContext()
|
||||
{
|
||||
$handler = new CurlHandler();
|
||||
$request = new Request('GET', 'http://localhost:123');
|
||||
$called = false;
|
||||
$p = $handler($request, ['timeout' => 0.001, 'connect_timeout' => 0.001])
|
||||
->otherwise(function (ConnectException $e) use (&$called) {
|
||||
$called = true;
|
||||
self::assertArrayHasKey('errno', $e->getHandlerContext());
|
||||
});
|
||||
$p->wait();
|
||||
self::assertTrue($called);
|
||||
}
|
||||
|
||||
public function testUsesContentLengthWhenOverInMemorySize()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Response()]);
|
||||
$stream = Psr7\stream_for(str_repeat('.', 1000000));
|
||||
$handler = new CurlHandler();
|
||||
$request = new Request(
|
||||
'PUT',
|
||||
Server::$url,
|
||||
['Content-Length' => 1000000],
|
||||
$stream
|
||||
);
|
||||
$handler($request, [])->wait();
|
||||
$received = Server::received()[0];
|
||||
self::assertEquals(1000000, $received->getHeaderLine('Content-Length'));
|
||||
self::assertFalse($received->hasHeader('Transfer-Encoding'));
|
||||
}
|
||||
}
|
123
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/CurlMultiHandlerTest.php
vendored
Normal file
123
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/CurlMultiHandlerTest.php
vendored
Normal file
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
namespace GuzzleHttp\Tests\Handler;
|
||||
|
||||
use GuzzleHttp\Handler\CurlMultiHandler;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use GuzzleHttp\Tests\Server;
|
||||
use GuzzleHttp\Utils;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CurlMultiHandlerTest extends TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
$_SERVER['curl_test'] = true;
|
||||
unset($_SERVER['_curl_multi']);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
unset($_SERVER['_curl_multi'], $_SERVER['curl_test']);
|
||||
}
|
||||
|
||||
public function testCanAddCustomCurlOptions()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Response()]);
|
||||
$a = new CurlMultiHandler(['options' => [
|
||||
CURLMOPT_MAXCONNECTS => 5,
|
||||
]]);
|
||||
$request = new Request('GET', Server::$url);
|
||||
$a($request, []);
|
||||
self::assertEquals(5, $_SERVER['_curl_multi'][CURLMOPT_MAXCONNECTS]);
|
||||
}
|
||||
|
||||
public function testSendsRequest()
|
||||
{
|
||||
Server::enqueue([new Response()]);
|
||||
$a = new CurlMultiHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$response = $a($request, [])->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\ConnectException
|
||||
* @expectedExceptionMessage cURL error
|
||||
*/
|
||||
public function testCreatesExceptions()
|
||||
{
|
||||
$a = new CurlMultiHandler();
|
||||
$a(new Request('GET', 'http://localhost:123'), [])->wait();
|
||||
}
|
||||
|
||||
public function testCanSetSelectTimeout()
|
||||
{
|
||||
$a = new CurlMultiHandler(['select_timeout' => 2]);
|
||||
self::assertEquals(2, self::readAttribute($a, 'selectTimeout'));
|
||||
}
|
||||
|
||||
public function testCanCancel()
|
||||
{
|
||||
Server::flush();
|
||||
$response = new Response(200);
|
||||
Server::enqueue(array_fill_keys(range(0, 10), $response));
|
||||
$a = new CurlMultiHandler();
|
||||
$responses = [];
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$response = $a(new Request('GET', Server::$url), []);
|
||||
$response->cancel();
|
||||
$responses[] = $response;
|
||||
}
|
||||
|
||||
foreach ($responses as $r) {
|
||||
self::assertSame('rejected', $response->getState());
|
||||
}
|
||||
}
|
||||
|
||||
public function testCannotCancelFinished()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Response(200)]);
|
||||
$a = new CurlMultiHandler();
|
||||
$response = $a(new Request('GET', Server::$url), []);
|
||||
$response->wait();
|
||||
$response->cancel();
|
||||
self::assertSame('fulfilled', $response->getState());
|
||||
}
|
||||
|
||||
public function testDelaysConcurrently()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Response()]);
|
||||
$a = new CurlMultiHandler();
|
||||
$expected = Utils::currentTime() + (100 / 1000);
|
||||
$response = $a(new Request('GET', Server::$url), ['delay' => 100]);
|
||||
$response->wait();
|
||||
self::assertGreaterThanOrEqual($expected, Utils::currentTime());
|
||||
}
|
||||
|
||||
public function testUsesTimeoutEnvironmentVariables()
|
||||
{
|
||||
$a = new CurlMultiHandler();
|
||||
|
||||
//default if no options are given and no environment variable is set
|
||||
self::assertEquals(1, self::readAttribute($a, 'selectTimeout'));
|
||||
|
||||
putenv("GUZZLE_CURL_SELECT_TIMEOUT=3");
|
||||
$a = new CurlMultiHandler();
|
||||
$selectTimeout = getenv('GUZZLE_CURL_SELECT_TIMEOUT');
|
||||
//Handler reads from the environment if no options are given
|
||||
self::assertEquals($selectTimeout, self::readAttribute($a, 'selectTimeout'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
*/
|
||||
public function throwsWhenAccessingInvalidProperty()
|
||||
{
|
||||
$h = new CurlMultiHandler();
|
||||
$h->foo;
|
||||
}
|
||||
}
|
23
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/EasyHandleTest.php
vendored
Normal file
23
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/EasyHandleTest.php
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace GuzzleHttp\Test\Handler;
|
||||
|
||||
use GuzzleHttp\Handler\EasyHandle;
|
||||
use GuzzleHttp\Psr7;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \GuzzleHttp\Handler\EasyHandle
|
||||
*/
|
||||
class EasyHandleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \BadMethodCallException
|
||||
* @expectedExceptionMessage The EasyHandle has been released
|
||||
*/
|
||||
public function testEnsuresHandleExists()
|
||||
{
|
||||
$easy = new EasyHandle;
|
||||
unset($easy->handle);
|
||||
$easy->handle;
|
||||
}
|
||||
}
|
261
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/MockHandlerTest.php
vendored
Normal file
261
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/MockHandlerTest.php
vendored
Normal file
@ -0,0 +1,261 @@
|
||||
<?php
|
||||
namespace GuzzleHttp\Test\Handler;
|
||||
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\Promise\PromiseInterface;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use GuzzleHttp\TransferStats;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \GuzzleHttp\Handler\MockHandler
|
||||
*/
|
||||
class MockHandlerTest extends TestCase
|
||||
{
|
||||
public function testReturnsMockResponse()
|
||||
{
|
||||
$res = new Response();
|
||||
$mock = new MockHandler([$res]);
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$p = $mock($request, []);
|
||||
self::assertSame($res, $p->wait());
|
||||
}
|
||||
|
||||
public function testIsCountable()
|
||||
{
|
||||
$res = new Response();
|
||||
$mock = new MockHandler([$res, $res]);
|
||||
self::assertCount(2, $mock);
|
||||
}
|
||||
|
||||
public function testEmptyHandlerIsCountable()
|
||||
{
|
||||
self::assertCount(0, new MockHandler());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testEnsuresEachAppendIsValid()
|
||||
{
|
||||
$mock = new MockHandler(['a']);
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$mock($request, []);
|
||||
}
|
||||
|
||||
public function testCanQueueExceptions()
|
||||
{
|
||||
$e = new \Exception('a');
|
||||
$mock = new MockHandler([$e]);
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$p = $mock($request, []);
|
||||
try {
|
||||
$p->wait();
|
||||
self::fail();
|
||||
} catch (\Exception $e2) {
|
||||
self::assertSame($e, $e2);
|
||||
}
|
||||
}
|
||||
|
||||
public function testCanGetLastRequestAndOptions()
|
||||
{
|
||||
$res = new Response();
|
||||
$mock = new MockHandler([$res]);
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$mock($request, ['foo' => 'bar']);
|
||||
self::assertSame($request, $mock->getLastRequest());
|
||||
self::assertSame(['foo' => 'bar'], $mock->getLastOptions());
|
||||
}
|
||||
|
||||
public function testSinkFilename()
|
||||
{
|
||||
$filename = sys_get_temp_dir() . '/mock_test_' . uniqid();
|
||||
$res = new Response(200, [], 'TEST CONTENT');
|
||||
$mock = new MockHandler([$res]);
|
||||
$request = new Request('GET', '/');
|
||||
$p = $mock($request, ['sink' => $filename]);
|
||||
$p->wait();
|
||||
|
||||
self::assertFileExists($filename);
|
||||
self::assertStringEqualsFile($filename, 'TEST CONTENT');
|
||||
|
||||
unlink($filename);
|
||||
}
|
||||
|
||||
public function testSinkResource()
|
||||
{
|
||||
$file = tmpfile();
|
||||
$meta = stream_get_meta_data($file);
|
||||
$res = new Response(200, [], 'TEST CONTENT');
|
||||
$mock = new MockHandler([$res]);
|
||||
$request = new Request('GET', '/');
|
||||
$p = $mock($request, ['sink' => $file]);
|
||||
$p->wait();
|
||||
|
||||
self::assertFileExists($meta['uri']);
|
||||
self::assertStringEqualsFile($meta['uri'], 'TEST CONTENT');
|
||||
}
|
||||
|
||||
public function testSinkStream()
|
||||
{
|
||||
$stream = new \GuzzleHttp\Psr7\Stream(tmpfile());
|
||||
$res = new Response(200, [], 'TEST CONTENT');
|
||||
$mock = new MockHandler([$res]);
|
||||
$request = new Request('GET', '/');
|
||||
$p = $mock($request, ['sink' => $stream]);
|
||||
$p->wait();
|
||||
|
||||
self::assertFileExists($stream->getMetadata('uri'));
|
||||
self::assertStringEqualsFile($stream->getMetadata('uri'), 'TEST CONTENT');
|
||||
}
|
||||
|
||||
public function testCanEnqueueCallables()
|
||||
{
|
||||
$r = new Response();
|
||||
$fn = function ($req, $o) use ($r) {
|
||||
return $r;
|
||||
};
|
||||
$mock = new MockHandler([$fn]);
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$p = $mock($request, ['foo' => 'bar']);
|
||||
self::assertSame($r, $p->wait());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testEnsuresOnHeadersIsCallable()
|
||||
{
|
||||
$res = new Response();
|
||||
$mock = new MockHandler([$res]);
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$mock($request, ['on_headers' => 'error!']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\RequestException
|
||||
* @expectedExceptionMessage An error was encountered during the on_headers event
|
||||
* @expectedExceptionMessage test
|
||||
*/
|
||||
public function testRejectsPromiseWhenOnHeadersFails()
|
||||
{
|
||||
$res = new Response();
|
||||
$mock = new MockHandler([$res]);
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$promise = $mock($request, [
|
||||
'on_headers' => function () {
|
||||
throw new \Exception('test');
|
||||
}
|
||||
]);
|
||||
|
||||
$promise->wait();
|
||||
}
|
||||
public function testInvokesOnFulfilled()
|
||||
{
|
||||
$res = new Response();
|
||||
$mock = new MockHandler([$res], function ($v) use (&$c) {
|
||||
$c = $v;
|
||||
});
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$mock($request, [])->wait();
|
||||
self::assertSame($res, $c);
|
||||
}
|
||||
|
||||
public function testInvokesOnRejected()
|
||||
{
|
||||
$e = new \Exception('a');
|
||||
$c = null;
|
||||
$mock = new MockHandler([$e], null, function ($v) use (&$c) {
|
||||
$c = $v;
|
||||
});
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$mock($request, [])->wait(false);
|
||||
self::assertSame($e, $c);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \OutOfBoundsException
|
||||
*/
|
||||
public function testThrowsWhenNoMoreResponses()
|
||||
{
|
||||
$mock = new MockHandler();
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$mock($request, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\BadResponseException
|
||||
*/
|
||||
public function testCanCreateWithDefaultMiddleware()
|
||||
{
|
||||
$r = new Response(500);
|
||||
$mock = MockHandler::createWithMiddleware([$r]);
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$mock($request, ['http_errors' => true])->wait();
|
||||
}
|
||||
|
||||
public function testInvokesOnStatsFunctionForResponse()
|
||||
{
|
||||
$res = new Response();
|
||||
$mock = new MockHandler([$res]);
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
/** @var TransferStats|null $stats */
|
||||
$stats = null;
|
||||
$onStats = function (TransferStats $s) use (&$stats) {
|
||||
$stats = $s;
|
||||
};
|
||||
$p = $mock($request, ['on_stats' => $onStats]);
|
||||
$p->wait();
|
||||
self::assertSame($res, $stats->getResponse());
|
||||
self::assertSame($request, $stats->getRequest());
|
||||
}
|
||||
|
||||
public function testInvokesOnStatsFunctionForError()
|
||||
{
|
||||
$e = new \Exception('a');
|
||||
$c = null;
|
||||
$mock = new MockHandler([$e], null, function ($v) use (&$c) {
|
||||
$c = $v;
|
||||
});
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
|
||||
/** @var TransferStats|null $stats */
|
||||
$stats = null;
|
||||
$onStats = function (TransferStats $s) use (&$stats) {
|
||||
$stats = $s;
|
||||
};
|
||||
$mock($request, ['on_stats' => $onStats])->wait(false);
|
||||
self::assertSame($e, $stats->getHandlerErrorData());
|
||||
self::assertNull($stats->getResponse());
|
||||
self::assertSame($request, $stats->getRequest());
|
||||
}
|
||||
|
||||
public function testTransferTime()
|
||||
{
|
||||
$e = new \Exception('a');
|
||||
$c = null;
|
||||
$mock = new MockHandler([$e], null, function ($v) use (&$c) {
|
||||
$c = $v;
|
||||
});
|
||||
$request = new Request('GET', 'http://example.com');
|
||||
$stats = null;
|
||||
$onStats = function (TransferStats $s) use (&$stats) {
|
||||
$stats = $s;
|
||||
};
|
||||
$mock($request, [ 'on_stats' => $onStats, 'transfer_time' => 0.4 ])->wait(false);
|
||||
self::assertEquals(0.4, $stats->getTransferTime());
|
||||
}
|
||||
|
||||
public function testResetQueue()
|
||||
{
|
||||
$mock = new MockHandler([new Response(200), new Response(204)]);
|
||||
self::assertCount(2, $mock);
|
||||
|
||||
$mock->reset();
|
||||
self::assertEmpty($mock);
|
||||
|
||||
$mock->append(new Response(500));
|
||||
self::assertCount(1, $mock);
|
||||
}
|
||||
}
|
74
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/ProxyTest.php
vendored
Normal file
74
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/ProxyTest.php
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
namespace GuzzleHttp\Test\Handler;
|
||||
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\Handler\Proxy;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \GuzzleHttp\Handler\Proxy
|
||||
*/
|
||||
class ProxyTest extends TestCase
|
||||
{
|
||||
public function testSendsToNonSync()
|
||||
{
|
||||
$a = $b = null;
|
||||
$m1 = new MockHandler([function ($v) use (&$a) {
|
||||
$a = $v;
|
||||
}]);
|
||||
$m2 = new MockHandler([function ($v) use (&$b) {
|
||||
$b = $v;
|
||||
}]);
|
||||
$h = Proxy::wrapSync($m1, $m2);
|
||||
$h(new Request('GET', 'http://foo.com'), []);
|
||||
self::assertNotNull($a);
|
||||
self::assertNull($b);
|
||||
}
|
||||
|
||||
public function testSendsToSync()
|
||||
{
|
||||
$a = $b = null;
|
||||
$m1 = new MockHandler([function ($v) use (&$a) {
|
||||
$a = $v;
|
||||
}]);
|
||||
$m2 = new MockHandler([function ($v) use (&$b) {
|
||||
$b = $v;
|
||||
}]);
|
||||
$h = Proxy::wrapSync($m1, $m2);
|
||||
$h(new Request('GET', 'http://foo.com'), [RequestOptions::SYNCHRONOUS => true]);
|
||||
self::assertNull($a);
|
||||
self::assertNotNull($b);
|
||||
}
|
||||
|
||||
public function testSendsToStreaming()
|
||||
{
|
||||
$a = $b = null;
|
||||
$m1 = new MockHandler([function ($v) use (&$a) {
|
||||
$a = $v;
|
||||
}]);
|
||||
$m2 = new MockHandler([function ($v) use (&$b) {
|
||||
$b = $v;
|
||||
}]);
|
||||
$h = Proxy::wrapStreaming($m1, $m2);
|
||||
$h(new Request('GET', 'http://foo.com'), []);
|
||||
self::assertNotNull($a);
|
||||
self::assertNull($b);
|
||||
}
|
||||
|
||||
public function testSendsToNonStreaming()
|
||||
{
|
||||
$a = $b = null;
|
||||
$m1 = new MockHandler([function ($v) use (&$a) {
|
||||
$a = $v;
|
||||
}]);
|
||||
$m2 = new MockHandler([function ($v) use (&$b) {
|
||||
$b = $v;
|
||||
}]);
|
||||
$h = Proxy::wrapStreaming($m1, $m2);
|
||||
$h(new Request('GET', 'http://foo.com'), ['stream' => true]);
|
||||
self::assertNull($a);
|
||||
self::assertNotNull($b);
|
||||
}
|
||||
}
|
689
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/StreamHandlerTest.php
vendored
Normal file
689
wp-content/plugins/w3-total-cache/vendor/guzzlehttp/guzzle/tests/Handler/StreamHandlerTest.php
vendored
Normal file
@ -0,0 +1,689 @@
|
||||
<?php
|
||||
namespace GuzzleHttp\Test\Handler;
|
||||
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use GuzzleHttp\Handler\StreamHandler;
|
||||
use GuzzleHttp\Psr7;
|
||||
use GuzzleHttp\Psr7\FnStream;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use GuzzleHttp\Tests\Server;
|
||||
use GuzzleHttp\TransferStats;
|
||||
use GuzzleHttp\Utils;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* @covers \GuzzleHttp\Handler\StreamHandler
|
||||
*/
|
||||
class StreamHandlerTest extends TestCase
|
||||
{
|
||||
private function queueRes()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([
|
||||
new Response(200, [
|
||||
'Foo' => 'Bar',
|
||||
'Content-Length' => 8,
|
||||
], 'hi there')
|
||||
]);
|
||||
}
|
||||
|
||||
public function testReturnsResponseForSuccessfulRequest()
|
||||
{
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$response = $handler(
|
||||
new Request('GET', Server::$url, ['Foo' => 'Bar']),
|
||||
[]
|
||||
)->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
self::assertSame('OK', $response->getReasonPhrase());
|
||||
self::assertSame('Bar', $response->getHeaderLine('Foo'));
|
||||
self::assertSame('8', $response->getHeaderLine('Content-Length'));
|
||||
self::assertSame('hi there', (string) $response->getBody());
|
||||
$sent = Server::received()[0];
|
||||
self::assertSame('GET', $sent->getMethod());
|
||||
self::assertSame('/', $sent->getUri()->getPath());
|
||||
self::assertSame('127.0.0.1:8126', $sent->getHeaderLine('Host'));
|
||||
self::assertSame('Bar', $sent->getHeaderLine('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\RequestException
|
||||
*/
|
||||
public function testAddsErrorToResponse()
|
||||
{
|
||||
$handler = new StreamHandler();
|
||||
$handler(
|
||||
new Request('GET', 'http://localhost:123'),
|
||||
['timeout' => 0.01]
|
||||
)->wait();
|
||||
}
|
||||
|
||||
public function testStreamAttributeKeepsStreamOpen()
|
||||
{
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request(
|
||||
'PUT',
|
||||
Server::$url . 'foo?baz=bar',
|
||||
['Foo' => 'Bar'],
|
||||
'test'
|
||||
);
|
||||
$response = $handler($request, ['stream' => true])->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
self::assertSame('OK', $response->getReasonPhrase());
|
||||
self::assertSame('8', $response->getHeaderLine('Content-Length'));
|
||||
$body = $response->getBody();
|
||||
$stream = $body->detach();
|
||||
self::assertInternalType('resource', $stream);
|
||||
self::assertSame('http', stream_get_meta_data($stream)['wrapper_type']);
|
||||
self::assertSame('hi there', stream_get_contents($stream));
|
||||
fclose($stream);
|
||||
$sent = Server::received()[0];
|
||||
self::assertSame('PUT', $sent->getMethod());
|
||||
self::assertSame('http://127.0.0.1:8126/foo?baz=bar', (string) $sent->getUri());
|
||||
self::assertSame('Bar', $sent->getHeaderLine('Foo'));
|
||||
self::assertSame('test', (string) $sent->getBody());
|
||||
}
|
||||
|
||||
public function testDrainsResponseIntoTempStream()
|
||||
{
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$response = $handler($request, [])->wait();
|
||||
$body = $response->getBody();
|
||||
$stream = $body->detach();
|
||||
self::assertSame('php://temp', stream_get_meta_data($stream)['uri']);
|
||||
self::assertSame('hi', fread($stream, 2));
|
||||
fclose($stream);
|
||||
}
|
||||
|
||||
public function testDrainsResponseIntoSaveToBody()
|
||||
{
|
||||
$r = fopen('php://temp', 'r+');
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$response = $handler($request, ['sink' => $r])->wait();
|
||||
$body = $response->getBody()->detach();
|
||||
self::assertSame('php://temp', stream_get_meta_data($body)['uri']);
|
||||
self::assertSame('hi', fread($body, 2));
|
||||
self::assertSame(' there', stream_get_contents($r));
|
||||
fclose($r);
|
||||
}
|
||||
|
||||
public function testDrainsResponseIntoSaveToBodyAtPath()
|
||||
{
|
||||
$tmpfname = tempnam('/tmp', 'save_to_path');
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$response = $handler($request, ['sink' => $tmpfname])->wait();
|
||||
$body = $response->getBody();
|
||||
self::assertSame($tmpfname, $body->getMetadata('uri'));
|
||||
self::assertSame('hi', $body->read(2));
|
||||
$body->close();
|
||||
unlink($tmpfname);
|
||||
}
|
||||
|
||||
public function testDrainsResponseIntoSaveToBodyAtNonExistentPath()
|
||||
{
|
||||
$tmpfname = tempnam('/tmp', 'save_to_path');
|
||||
unlink($tmpfname);
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$response = $handler($request, ['sink' => $tmpfname])->wait();
|
||||
$body = $response->getBody();
|
||||
self::assertSame($tmpfname, $body->getMetadata('uri'));
|
||||
self::assertSame('hi', $body->read(2));
|
||||
$body->close();
|
||||
unlink($tmpfname);
|
||||
}
|
||||
|
||||
public function testDrainsResponseAndReadsOnlyContentLengthBytes()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([
|
||||
new Response(200, [
|
||||
'Foo' => 'Bar',
|
||||
'Content-Length' => 8,
|
||||
], 'hi there... This has way too much data!')
|
||||
]);
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$response = $handler($request, [])->wait();
|
||||
$body = $response->getBody();
|
||||
$stream = $body->detach();
|
||||
self::assertSame('hi there', stream_get_contents($stream));
|
||||
fclose($stream);
|
||||
}
|
||||
|
||||
public function testDoesNotDrainWhenHeadRequest()
|
||||
{
|
||||
Server::flush();
|
||||
// Say the content-length is 8, but return no response.
|
||||
Server::enqueue([
|
||||
new Response(200, [
|
||||
'Foo' => 'Bar',
|
||||
'Content-Length' => 8,
|
||||
], '')
|
||||
]);
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('HEAD', Server::$url);
|
||||
$response = $handler($request, [])->wait();
|
||||
$body = $response->getBody();
|
||||
$stream = $body->detach();
|
||||
self::assertSame('', stream_get_contents($stream));
|
||||
fclose($stream);
|
||||
}
|
||||
|
||||
public function testAutomaticallyDecompressGzip()
|
||||
{
|
||||
Server::flush();
|
||||
$content = gzencode('test');
|
||||
Server::enqueue([
|
||||
new Response(200, [
|
||||
'Content-Encoding' => 'gzip',
|
||||
'Content-Length' => strlen($content),
|
||||
], $content)
|
||||
]);
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$response = $handler($request, ['decode_content' => true])->wait();
|
||||
self::assertSame('test', (string) $response->getBody());
|
||||
self::assertFalse($response->hasHeader('content-encoding'));
|
||||
self::assertTrue(!$response->hasHeader('content-length') || $response->getHeaderLine('content-length') == $response->getBody()->getSize());
|
||||
}
|
||||
|
||||
public function testReportsOriginalSizeAndContentEncodingAfterDecoding()
|
||||
{
|
||||
Server::flush();
|
||||
$content = gzencode('test');
|
||||
Server::enqueue([
|
||||
new Response(200, [
|
||||
'Content-Encoding' => 'gzip',
|
||||
'Content-Length' => strlen($content),
|
||||
], $content)
|
||||
]);
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$response = $handler($request, ['decode_content' => true])->wait();
|
||||
|
||||
self::assertSame(
|
||||
'gzip',
|
||||
$response->getHeaderLine('x-encoded-content-encoding')
|
||||
);
|
||||
self::assertSame(
|
||||
strlen($content),
|
||||
(int) $response->getHeaderLine('x-encoded-content-length')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDoesNotForceGzipDecode()
|
||||
{
|
||||
Server::flush();
|
||||
$content = gzencode('test');
|
||||
Server::enqueue([
|
||||
new Response(200, [
|
||||
'Content-Encoding' => 'gzip',
|
||||
'Content-Length' => strlen($content),
|
||||
], $content)
|
||||
]);
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$response = $handler($request, ['decode_content' => false])->wait();
|
||||
self::assertSame($content, (string) $response->getBody());
|
||||
self::assertSame('gzip', $response->getHeaderLine('content-encoding'));
|
||||
self::assertEquals(strlen($content), $response->getHeaderLine('content-length'));
|
||||
}
|
||||
|
||||
public function testProtocolVersion()
|
||||
{
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url, [], null, '1.0');
|
||||
$handler($request, []);
|
||||
self::assertSame('1.0', Server::received()[0]->getProtocolVersion());
|
||||
}
|
||||
|
||||
protected function getSendResult(array $opts)
|
||||
{
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$opts['stream'] = true;
|
||||
$request = new Request('GET', Server::$url);
|
||||
return $handler($request, $opts)->wait();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\ConnectException
|
||||
* @expectedExceptionMessage Connection refused
|
||||
*/
|
||||
public function testAddsProxy()
|
||||
{
|
||||
$this->getSendResult(['proxy' => '127.0.0.1:8125']);
|
||||
}
|
||||
|
||||
public function testAddsProxyByProtocol()
|
||||
{
|
||||
$url = str_replace('http', 'tcp', Server::$url);
|
||||
// Workaround until #1823 is fixed properly
|
||||
$url = rtrim($url, '/');
|
||||
$res = $this->getSendResult(['proxy' => ['http' => $url]]);
|
||||
$opts = stream_context_get_options($res->getBody()->detach());
|
||||
self::assertSame($url, $opts['http']['proxy']);
|
||||
}
|
||||
|
||||
public function testAddsProxyButHonorsNoProxy()
|
||||
{
|
||||
$url = str_replace('http', 'tcp', Server::$url);
|
||||
$res = $this->getSendResult(['proxy' => [
|
||||
'http' => $url,
|
||||
'no' => ['*']
|
||||
]]);
|
||||
$opts = stream_context_get_options($res->getBody()->detach());
|
||||
self::assertArrayNotHasKey('proxy', $opts['http']);
|
||||
}
|
||||
|
||||
public function testAddsTimeout()
|
||||
{
|
||||
$res = $this->getSendResult(['stream' => true, 'timeout' => 200]);
|
||||
$opts = stream_context_get_options($res->getBody()->detach());
|
||||
self::assertEquals(200, $opts['http']['timeout']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\RequestException
|
||||
* @expectedExceptionMessage SSL CA bundle not found: /does/not/exist
|
||||
*/
|
||||
public function testVerifiesVerifyIsValidIfPath()
|
||||
{
|
||||
$this->getSendResult(['verify' => '/does/not/exist']);
|
||||
}
|
||||
|
||||
public function testVerifyCanBeDisabled()
|
||||
{
|
||||
$handler = $this->getSendResult(['verify' => false]);
|
||||
self::assertInstanceOf('GuzzleHttp\Psr7\Response', $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\RequestException
|
||||
* @expectedExceptionMessage SSL certificate not found: /does/not/exist
|
||||
*/
|
||||
public function testVerifiesCertIfValidPath()
|
||||
{
|
||||
$this->getSendResult(['cert' => '/does/not/exist']);
|
||||
}
|
||||
|
||||
public function testVerifyCanBeSetToPath()
|
||||
{
|
||||
$path = $path = \GuzzleHttp\default_ca_bundle();
|
||||
$res = $this->getSendResult(['verify' => $path]);
|
||||
$opts = stream_context_get_options($res->getBody()->detach());
|
||||
self::assertTrue($opts['ssl']['verify_peer']);
|
||||
self::assertTrue($opts['ssl']['verify_peer_name']);
|
||||
self::assertSame($path, $opts['ssl']['cafile']);
|
||||
self::assertFileExists($opts['ssl']['cafile']);
|
||||
}
|
||||
|
||||
public function testUsesSystemDefaultBundle()
|
||||
{
|
||||
$path = $path = \GuzzleHttp\default_ca_bundle();
|
||||
$res = $this->getSendResult(['verify' => true]);
|
||||
$opts = stream_context_get_options($res->getBody()->detach());
|
||||
if (PHP_VERSION_ID < 50600) {
|
||||
self::assertSame($path, $opts['ssl']['cafile']);
|
||||
} else {
|
||||
self::assertArrayNotHasKey('cafile', $opts['ssl']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid verify request option
|
||||
*/
|
||||
public function testEnsuresVerifyOptionIsValid()
|
||||
{
|
||||
$this->getSendResult(['verify' => 10]);
|
||||
}
|
||||
|
||||
public function testCanSetPasswordWhenSettingCert()
|
||||
{
|
||||
$path = __FILE__;
|
||||
$res = $this->getSendResult(['cert' => [$path, 'foo']]);
|
||||
$opts = stream_context_get_options($res->getBody()->detach());
|
||||
self::assertSame($path, $opts['ssl']['local_cert']);
|
||||
self::assertSame('foo', $opts['ssl']['passphrase']);
|
||||
}
|
||||
|
||||
public function testDebugAttributeWritesToStream()
|
||||
{
|
||||
$this->queueRes();
|
||||
$f = fopen('php://temp', 'w+');
|
||||
$this->getSendResult(['debug' => $f]);
|
||||
fseek($f, 0);
|
||||
$contents = stream_get_contents($f);
|
||||
self::assertContains('<GET http://127.0.0.1:8126/> [CONNECT]', $contents);
|
||||
self::assertContains('<GET http://127.0.0.1:8126/> [FILE_SIZE_IS]', $contents);
|
||||
self::assertContains('<GET http://127.0.0.1:8126/> [PROGRESS]', $contents);
|
||||
}
|
||||
|
||||
public function testDebugAttributeWritesStreamInfoToBuffer()
|
||||
{
|
||||
$called = false;
|
||||
$this->queueRes();
|
||||
$buffer = fopen('php://temp', 'r+');
|
||||
$this->getSendResult([
|
||||
'progress' => function () use (&$called) {
|
||||
$called = true;
|
||||
},
|
||||
'debug' => $buffer,
|
||||
]);
|
||||
fseek($buffer, 0);
|
||||
$contents = stream_get_contents($buffer);
|
||||
self::assertContains('<GET http://127.0.0.1:8126/> [CONNECT]', $contents);
|
||||
self::assertContains('<GET http://127.0.0.1:8126/> [FILE_SIZE_IS] message: "Content-Length: 8"', $contents);
|
||||
self::assertContains('<GET http://127.0.0.1:8126/> [PROGRESS] bytes_max: "8"', $contents);
|
||||
self::assertTrue($called);
|
||||
}
|
||||
|
||||
public function testEmitsProgressInformation()
|
||||
{
|
||||
$called = [];
|
||||
$this->queueRes();
|
||||
$this->getSendResult([
|
||||
'progress' => function () use (&$called) {
|
||||
$called[] = func_get_args();
|
||||
},
|
||||
]);
|
||||
self::assertNotEmpty($called);
|
||||
self::assertEquals(8, $called[0][0]);
|
||||
self::assertEquals(0, $called[0][1]);
|
||||
}
|
||||
|
||||
public function testEmitsProgressInformationAndDebugInformation()
|
||||
{
|
||||
$called = [];
|
||||
$this->queueRes();
|
||||
$buffer = fopen('php://memory', 'w+');
|
||||
$this->getSendResult([
|
||||
'debug' => $buffer,
|
||||
'progress' => function () use (&$called) {
|
||||
$called[] = func_get_args();
|
||||
},
|
||||
]);
|
||||
self::assertNotEmpty($called);
|
||||
self::assertEquals(8, $called[0][0]);
|
||||
self::assertEquals(0, $called[0][1]);
|
||||
rewind($buffer);
|
||||
self::assertNotEmpty(stream_get_contents($buffer));
|
||||
fclose($buffer);
|
||||
}
|
||||
|
||||
public function testPerformsShallowMergeOfCustomContextOptions()
|
||||
{
|
||||
$res = $this->getSendResult([
|
||||
'stream_context' => [
|
||||
'http' => [
|
||||
'request_fulluri' => true,
|
||||
'method' => 'HEAD',
|
||||
],
|
||||
'socket' => [
|
||||
'bindto' => '127.0.0.1:0',
|
||||
],
|
||||
'ssl' => [
|
||||
'verify_peer' => false,
|
||||
],
|
||||
],
|
||||
]);
|
||||
$opts = stream_context_get_options($res->getBody()->detach());
|
||||
self::assertSame('HEAD', $opts['http']['method']);
|
||||
self::assertTrue($opts['http']['request_fulluri']);
|
||||
self::assertSame('127.0.0.1:0', $opts['socket']['bindto']);
|
||||
self::assertFalse($opts['ssl']['verify_peer']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage stream_context must be an array
|
||||
*/
|
||||
public function testEnsuresThatStreamContextIsAnArray()
|
||||
{
|
||||
$this->getSendResult(['stream_context' => 'foo']);
|
||||
}
|
||||
|
||||
public function testDoesNotAddContentTypeByDefault()
|
||||
{
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('PUT', Server::$url, ['Content-Length' => 3], 'foo');
|
||||
$handler($request, []);
|
||||
$req = Server::received()[0];
|
||||
self::assertEquals('', $req->getHeaderLine('Content-Type'));
|
||||
self::assertEquals(3, $req->getHeaderLine('Content-Length'));
|
||||
}
|
||||
|
||||
public function testAddsContentLengthByDefault()
|
||||
{
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('PUT', Server::$url, [], 'foo');
|
||||
$handler($request, []);
|
||||
$req = Server::received()[0];
|
||||
self::assertEquals(3, $req->getHeaderLine('Content-Length'));
|
||||
}
|
||||
|
||||
public function testAddsContentLengthEvenWhenEmpty()
|
||||
{
|
||||
$this->queueRes();
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('PUT', Server::$url, [], '');
|
||||
$handler($request, []);
|
||||
$req = Server::received()[0];
|
||||
self::assertEquals(0, $req->getHeaderLine('Content-Length'));
|
||||
}
|
||||
|
||||
public function testSupports100Continue()
|
||||
{
|
||||
Server::flush();
|
||||
$response = new Response(200, ['Test' => 'Hello', 'Content-Length' => '4'], 'test');
|
||||
Server::enqueue([$response]);
|
||||
$request = new Request('PUT', Server::$url, ['Expect' => '100-Continue'], 'test');
|
||||
$handler = new StreamHandler();
|
||||
$response = $handler($request, [])->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
self::assertSame('Hello', $response->getHeaderLine('Test'));
|
||||
self::assertSame('4', $response->getHeaderLine('Content-Length'));
|
||||
self::assertSame('test', (string) $response->getBody());
|
||||
}
|
||||
|
||||
public function testDoesSleep()
|
||||
{
|
||||
$response = new response(200);
|
||||
Server::enqueue([$response]);
|
||||
$a = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$s = Utils::currentTime();
|
||||
$a($request, ['delay' => 0.1])->wait();
|
||||
self::assertGreaterThan(0.0001, Utils::currentTime() - $s);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testEnsuresOnHeadersIsCallable()
|
||||
{
|
||||
$req = new Request('GET', Server::$url);
|
||||
$handler = new StreamHandler();
|
||||
$handler($req, ['on_headers' => 'error!']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \GuzzleHttp\Exception\RequestException
|
||||
* @expectedExceptionMessage An error was encountered during the on_headers event
|
||||
* @expectedExceptionMessage test
|
||||
*/
|
||||
public function testRejectsPromiseWhenOnHeadersFails()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([
|
||||
new Response(200, ['X-Foo' => 'bar'], 'abc 123')
|
||||
]);
|
||||
$req = new Request('GET', Server::$url);
|
||||
$handler = new StreamHandler();
|
||||
$promise = $handler($req, [
|
||||
'on_headers' => function () {
|
||||
throw new \Exception('test');
|
||||
}
|
||||
]);
|
||||
$promise->wait();
|
||||
}
|
||||
|
||||
public function testSuccessfullyCallsOnHeadersBeforeWritingToSink()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([
|
||||
new Response(200, ['X-Foo' => 'bar'], 'abc 123')
|
||||
]);
|
||||
$req = new Request('GET', Server::$url);
|
||||
$got = null;
|
||||
|
||||
$stream = Psr7\stream_for();
|
||||
$stream = FnStream::decorate($stream, [
|
||||
'write' => function ($data) use ($stream, &$got) {
|
||||
self::assertNotNull($got);
|
||||
return $stream->write($data);
|
||||
}
|
||||
]);
|
||||
|
||||
$handler = new StreamHandler();
|
||||
$promise = $handler($req, [
|
||||
'sink' => $stream,
|
||||
'on_headers' => function (ResponseInterface $res) use (&$got) {
|
||||
$got = $res;
|
||||
self::assertSame('bar', $res->getHeaderLine('X-Foo'));
|
||||
}
|
||||
]);
|
||||
|
||||
$response = $promise->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
self::assertSame('bar', $response->getHeaderLine('X-Foo'));
|
||||
self::assertSame('abc 123', (string) $response->getBody());
|
||||
}
|
||||
|
||||
public function testInvokesOnStatsOnSuccess()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Psr7\Response(200)]);
|
||||
$req = new Psr7\Request('GET', Server::$url);
|
||||
$gotStats = null;
|
||||
$handler = new StreamHandler();
|
||||
$promise = $handler($req, [
|
||||
'on_stats' => function (TransferStats $stats) use (&$gotStats) {
|
||||
$gotStats = $stats;
|
||||
}
|
||||
]);
|
||||
$response = $promise->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
self::assertSame(200, $gotStats->getResponse()->getStatusCode());
|
||||
self::assertSame(
|
||||
Server::$url,
|
||||
(string) $gotStats->getEffectiveUri()
|
||||
);
|
||||
self::assertSame(
|
||||
Server::$url,
|
||||
(string) $gotStats->getRequest()->getUri()
|
||||
);
|
||||
self::assertGreaterThan(0, $gotStats->getTransferTime());
|
||||
}
|
||||
|
||||
public function testInvokesOnStatsOnError()
|
||||
{
|
||||
$req = new Psr7\Request('GET', 'http://127.0.0.1:123');
|
||||
$gotStats = null;
|
||||
$handler = new StreamHandler();
|
||||
$promise = $handler($req, [
|
||||
'connect_timeout' => 0.001,
|
||||
'timeout' => 0.001,
|
||||
'on_stats' => function (TransferStats $stats) use (&$gotStats) {
|
||||
$gotStats = $stats;
|
||||
}
|
||||
]);
|
||||
$promise->wait(false);
|
||||
self::assertFalse($gotStats->hasResponse());
|
||||
self::assertSame(
|
||||
'http://127.0.0.1:123',
|
||||
(string) $gotStats->getEffectiveUri()
|
||||
);
|
||||
self::assertSame(
|
||||
'http://127.0.0.1:123',
|
||||
(string) $gotStats->getRequest()->getUri()
|
||||
);
|
||||
self::assertInternalType('float', $gotStats->getTransferTime());
|
||||
self::assertInstanceOf(
|
||||
ConnectException::class,
|
||||
$gotStats->getHandlerErrorData()
|
||||
);
|
||||
}
|
||||
|
||||
public function testStreamIgnoresZeroTimeout()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([new Psr7\Response(200)]);
|
||||
$req = new Psr7\Request('GET', Server::$url);
|
||||
$gotStats = null;
|
||||
$handler = new StreamHandler();
|
||||
$promise = $handler($req, [
|
||||
'connect_timeout' => 10,
|
||||
'timeout' => 0
|
||||
]);
|
||||
$response = $promise->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function testDrainsResponseAndReadsAllContentWhenContentLengthIsZero()
|
||||
{
|
||||
Server::flush();
|
||||
Server::enqueue([
|
||||
new Response(200, [
|
||||
'Foo' => 'Bar',
|
||||
'Content-Length' => '0',
|
||||
], 'hi there... This has a lot of data!')
|
||||
]);
|
||||
$handler = new StreamHandler();
|
||||
$request = new Request('GET', Server::$url);
|
||||
$response = $handler($request, [])->wait();
|
||||
$body = $response->getBody();
|
||||
$stream = $body->detach();
|
||||
self::assertSame('hi there... This has a lot of data!', stream_get_contents($stream));
|
||||
fclose($stream);
|
||||
}
|
||||
|
||||
public function testHonorsReadTimeout()
|
||||
{
|
||||
Server::flush();
|
||||
$handler = new StreamHandler();
|
||||
$response = $handler(
|
||||
new Request('GET', Server::$url . 'guzzle-server/read-timeout'),
|
||||
[
|
||||
RequestOptions::READ_TIMEOUT => 1,
|
||||
RequestOptions::STREAM => true,
|
||||
]
|
||||
)->wait();
|
||||
self::assertSame(200, $response->getStatusCode());
|
||||
self::assertSame('OK', $response->getReasonPhrase());
|
||||
$body = $response->getBody()->detach();
|
||||
$line = fgets($body);
|
||||
self::assertSame("sleeping 60 seconds ...\n", $line);
|
||||
$line = fgets($body);
|
||||
self::assertFalse($line);
|
||||
self::assertTrue(stream_get_meta_data($body)['timed_out']);
|
||||
self::assertFalse(feof($body));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user