Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php namespace GuzzleHttp\Test\Handler; use GuzzleHttp\Exception\ConnectException; use G..

Decoded Output download

<?php
 namespace GuzzleHttp\Test\Handler; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\RequestException; 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; class StreamHandlerTest extends TestCase { private function queueRes() { Server::flush(); Server::enqueue(array(new Response(200, array("Foo" => "Bar", "Content-Length" => 8), "hi there"))); } public function testReturnsResponseForSuccessfulRequest() { $this->queueRes(); $handler = new StreamHandler(); $response = $handler(new Request("GET", Server::$url, array("Foo" => "Bar")), array())->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")); } public function testAddsErrorToResponse() { $handler = new StreamHandler(); $this->expectException(ConnectException::class); $handler(new Request("GET", "http://localhost:123"), array("timeout" => 0.01))->wait(); } public function testStreamAttributeKeepsStreamOpen() { $this->queueRes(); $handler = new StreamHandler(); $request = new Request("PUT", Server::$url . "foo?baz=bar", array("Foo" => "Bar"), "test"); $response = $handler($request, array("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::assertIsResource($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, array())->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, array("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(\sys_get_temp_dir(), "save_to_path"); $this->queueRes(); $handler = new StreamHandler(); $request = new Request("GET", Server::$url); $response = $handler($request, array("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(\sys_get_temp_dir(), "save_to_path"); \unlink($tmpfname); $this->queueRes(); $handler = new StreamHandler(); $request = new Request("GET", Server::$url); $response = $handler($request, array("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(array(new Response(200, array("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, array())->wait(); $body = $response->getBody(); $stream = $body->detach(); self::assertSame("hi there", \stream_get_contents($stream)); \fclose($stream); } public function testDoesNotDrainWhenHeadRequest() { Server::flush(); Server::enqueue(array(new Response(200, array("Foo" => "Bar", "Content-Length" => 8), ''))); $handler = new StreamHandler(); $request = new Request("HEAD", Server::$url); $response = $handler($request, array())->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(array(new Response(200, array("Content-Encoding" => "gzip", "Content-Length" => \strlen($content)), $content))); $handler = new StreamHandler(); $request = new Request("GET", Server::$url); $response = $handler($request, array("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 testAutomaticallyDecompressGzipHead() { Server::flush(); $content = \gzencode("test"); Server::enqueue(array(new Response(200, array("Content-Encoding" => "gzip", "Content-Length" => \strlen($content)), $content))); $handler = new StreamHandler(); $request = new Request("HEAD", Server::$url); $response = $handler($request, array("decode_content" => true))->wait(); self::assertTrue(!$response->hasHeader("content-length") || $response->getHeaderLine("content-length") == \strlen($content)); } public function testReportsOriginalSizeAndContentEncodingAfterDecoding() { Server::flush(); $content = \gzencode("test"); Server::enqueue(array(new Response(200, array("Content-Encoding" => "gzip", "Content-Length" => \strlen($content)), $content))); $handler = new StreamHandler(); $request = new Request("GET", Server::$url); $response = $handler($request, array("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(array(new Response(200, array("Content-Encoding" => "gzip", "Content-Length" => \strlen($content)), $content))); $handler = new StreamHandler(); $request = new Request("GET", Server::$url); $response = $handler($request, array("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, array(), null, "1.0"); $handler($request, array()); 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(); } public function testAddsProxy() { $this->expectException(ConnectException::class); $this->expectExceptionMessage("Connection refused"); $this->getSendResult(array("proxy" => "127.0.0.1:8125")); } public function testAddsProxyByProtocol() { $url = Server::$url; $res = $this->getSendResult(array("proxy" => array("http" => $url))); $opts = \stream_context_get_options($res->getBody()->detach()); foreach (array(\PHP_URL_HOST, \PHP_URL_PORT) as $part) { self::assertSame(parse_url($url, $part), parse_url($opts["http"]["proxy"], $part)); } } public function testAddsProxyButHonorsNoProxy() { $url = Server::$url; $res = $this->getSendResult(array("proxy" => array("http" => $url, "no" => array("*")))); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertArrayNotHasKey("proxy", $opts["http"]); } public function testUsesProxy() { $this->queueRes(); $handler = new StreamHandler(); $request = new Request("GET", "http://www.example.com", array(), null, "1.0"); $response = $handler($request, array("proxy" => Server::$url))->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()); } public function testAddsTimeout() { $res = $this->getSendResult(array("stream" => true, "timeout" => 200)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertEquals(200, $opts["http"]["timeout"]); } public function testVerifiesVerifyIsValidIfPath() { $this->expectException(RequestException::class); $this->expectExceptionMessage("SSL CA bundle not found: /does/not/exist"); $this->getSendResult(array("verify" => "/does/not/exist")); } public function testVerifyCanBeDisabled() { $handler = $this->getSendResult(array("verify" => false)); self::assertInstanceOf(Response::class, $handler); } public function testVerifiesCertIfValidPath() { $this->expectException(RequestException::class); $this->expectExceptionMessage("SSL certificate not found: /does/not/exist"); $this->getSendResult(array("cert" => "/does/not/exist")); } public function testVerifyCanBeSetToPath() { $path = Utils::defaultCaBundle(); $res = $this->getSendResult(array("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() { $res = $this->getSendResult(array("verify" => true)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertArrayNotHasKey("cafile", $opts["ssl"]); } public function testEnsuresVerifyOptionIsValid() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage("Invalid verify request option"); $this->getSendResult(array("verify" => 10)); } public function testEnsuresCryptoMethodOptionIsValid() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage("Invalid crypto_method request option: unknown version provided"); $this->getSendResult(array("crypto_method" => 123)); } public function testSetsCryptoMethodTls10() { $res = $this->getSendResult(array("crypto_method" => \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertSame(\STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT, $opts["http"]["crypto_method"]); } public function testSetsCryptoMethodTls11() { $res = $this->getSendResult(array("crypto_method" => \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertSame(\STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT, $opts["http"]["crypto_method"]); } public function testSetsCryptoMethodTls12() { $res = $this->getSendResult(array("crypto_method" => \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertSame(\STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, $opts["http"]["crypto_method"]); } public function testSetsCryptoMethodTls13() { $res = $this->getSendResult(array("crypto_method" => \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertSame(\STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT, $opts["http"]["crypto_method"]); } public function testCanSetPasswordWhenSettingCert() { $path = __FILE__; $res = $this->getSendResult(array("cert" => array($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(array("debug" => $f)); \fseek($f, 0); $contents = \stream_get_contents($f); self::assertStringContainsString("<GET http://127.0.0.1:8126/> [CONNECT]", $contents); self::assertStringContainsString("<GET http://127.0.0.1:8126/> [FILE_SIZE_IS]", $contents); self::assertStringContainsString("<GET http://127.0.0.1:8126/> [PROGRESS]", $contents); } public function testDebugAttributeWritesStreamInfoToBuffer() { $called = false; $this->queueRes(); $buffer = \fopen("php://temp", "r+"); $this->getSendResult(array("progress" => static function () use(&$called) { $called = true; }, "debug" => $buffer)); \fseek($buffer, 0); $contents = \stream_get_contents($buffer); self::assertStringContainsString("<GET http://127.0.0.1:8126/> [CONNECT]", $contents); self::assertStringContainsString("<GET http://127.0.0.1:8126/> [FILE_SIZE_IS] message: "Content-Length: 8"", $contents); self::assertStringContainsString("<GET http://127.0.0.1:8126/> [PROGRESS] bytes_max: "8"", $contents); self::assertTrue($called); } public function testEmitsProgressInformation() { $called = array(); $this->queueRes(); $this->getSendResult(array("progress" => static function (...$args) use(&$called) { $called[] = $args; })); self::assertNotEmpty($called); self::assertEquals(8, $called[0][0]); self::assertEquals(0, $called[0][1]); } public function testEmitsProgressInformationAndDebugInformation() { $called = array(); $this->queueRes(); $buffer = \fopen("php://memory", "w+"); $this->getSendResult(array("debug" => $buffer, "progress" => static function (...$args) use(&$called) { $called[] = $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(array("stream_context" => array("http" => array("request_fulluri" => true, "method" => "HEAD"), "socket" => array("bindto" => "127.0.0.1:0"), "ssl" => array("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"]); } public function testEnsuresThatStreamContextIsAnArray() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage("stream_context must be an array"); $this->getSendResult(array("stream_context" => "foo")); } public function testDoesNotAddContentTypeByDefault() { $this->queueRes(); $handler = new StreamHandler(); $request = new Request("PUT", Server::$url, array("Content-Length" => 3), "foo"); $handler($request, array()); $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, array(), "foo"); $handler($request, array()); $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, array(), ''); $handler($request, array()); $req = Server::received()[0]; self::assertEquals(0, $req->getHeaderLine("Content-Length")); } public function testSupports100Continue() { Server::flush(); $response = new Response(200, array("Test" => "Hello", "Content-Length" => "4"), "test"); Server::enqueue(array($response)); $request = new Request("PUT", Server::$url, array("Expect" => "100-Continue"), "test"); $handler = new StreamHandler(); $response = $handler($request, array())->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(array($response)); $a = new StreamHandler(); $request = new Request("GET", Server::$url); $s = Utils::currentTime(); $a($request, array("delay" => 0.1))->wait(); self::assertGreaterThan(0.0001, Utils::currentTime() - $s); } public function testEnsuresOnHeadersIsCallable() { $req = new Request("GET", Server::$url); $handler = new StreamHandler(); $this->expectException(\InvalidArgumentException::class); $handler($req, array("on_headers" => "error!")); } public function testRejectsPromiseWhenOnHeadersFails() { Server::flush(); Server::enqueue(array(new Response(200, array("X-Foo" => "bar"), "abc 123"))); $req = new Request("GET", Server::$url); $handler = new StreamHandler(); $promise = $handler($req, array("on_headers" => static function () { throw new \Exception("test"); })); $this->expectException(RequestException::class); $this->expectExceptionMessage("An error was encountered during the on_headers event"); $promise->wait(); } public function testSuccessfullyCallsOnHeadersBeforeWritingToSink() { Server::flush(); Server::enqueue(array(new Response(200, array("X-Foo" => "bar"), "abc 123"))); $req = new Request("GET", Server::$url); $got = null; $stream = Psr7\Utils::streamFor(); $stream = FnStream::decorate($stream, array("write" => static function ($data) use($stream, &$got) { self::assertNotNull($got); return $stream->write($data); })); $handler = new StreamHandler(); $promise = $handler($req, array("sink" => $stream, "on_headers" => static 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(array(new Response(200))); $req = new Request("GET", Server::$url); $gotStats = null; $handler = new StreamHandler(); $promise = $handler($req, array("on_stats" => static 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 Request("GET", "http://127.0.0.1:123"); $gotStats = null; $handler = new StreamHandler(); $promise = $handler($req, array("connect_timeout" => 0.001, "timeout" => 0.001, "on_stats" => static 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::assertIsFloat($gotStats->getTransferTime()); self::assertInstanceOf(ConnectException::class, $gotStats->getHandlerErrorData()); } public function testStreamIgnoresZeroTimeout() { Server::flush(); Server::enqueue(array(new Response(200))); $req = new Request("GET", Server::$url); $gotStats = null; $handler = new StreamHandler(); $promise = $handler($req, array("connect_timeout" => 10, "timeout" => 0)); $response = $promise->wait(); self::assertSame(200, $response->getStatusCode()); } public function testDrainsResponseAndReadsAllContentWhenContentLengthIsZero() { Server::flush(); Server::enqueue(array(new Response(200, array("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, array())->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"), array(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 ...
", $line); $line = \fgets($body); self::assertFalse($line); self::assertTrue(\stream_get_meta_data($body)["timed_out"]); self::assertFalse(\feof($body)); } public function testHandlesGarbageHttpServerGracefully() { $handler = new StreamHandler(); $this->expectException(RequestException::class); $this->expectExceptionMessage("An error was encountered while creating the response"); $handler(new Request("GET", Server::$url . "guzzle-server/garbage"), array(RequestOptions::STREAM => true))->wait(); } public function testHandlesInvalidStatusCodeGracefully() { $handler = new StreamHandler(); $this->expectException(RequestException::class); $this->expectExceptionMessage("An error was encountered while creating the response"); $handler(new Request("GET", Server::$url . "guzzle-server/bad-status"), array(RequestOptions::STREAM => true))->wait(); } public function testRejectsNonHttpSchemes() { $handler = new StreamHandler(); $this->expectException(RequestException::class); $this->expectExceptionMessage("The scheme 'file' is not supported."); $handler(new Request("GET", "file:///etc/passwd"), array(RequestOptions::STREAM => true))->wait(); } } ?>

Did this file decode correctly?

Original Code

<?php
 namespace GuzzleHttp\Test\Handler; use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\RequestException; 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; class StreamHandlerTest extends TestCase { private function queueRes() { Server::flush(); Server::enqueue(array(new Response(200, array("\106\157\157" => "\102\x61\162", "\x43\157\156\x74\x65\x6e\164\x2d\114\x65\156\x67\164\x68" => 8), "\150\x69\40\164\x68\145\162\145"))); } public function testReturnsResponseForSuccessfulRequest() { $this->queueRes(); $handler = new StreamHandler(); $response = $handler(new Request("\x47\x45\x54", Server::$url, array("\x46\157\157" => "\102\141\x72")), array())->wait(); self::assertSame(200, $response->getStatusCode()); self::assertSame("\117\x4b", $response->getReasonPhrase()); self::assertSame("\102\x61\x72", $response->getHeaderLine("\106\x6f\x6f")); self::assertSame("\70", $response->getHeaderLine("\x43\157\x6e\x74\x65\156\164\55\114\145\x6e\x67\164\150")); self::assertSame("\x68\151\x20\164\x68\145\x72\145", (string) $response->getBody()); $sent = Server::received()[0]; self::assertSame("\107\105\x54", $sent->getMethod()); self::assertSame("\x2f", $sent->getUri()->getPath()); self::assertSame("\61\x32\x37\56\x30\56\60\x2e\x31\72\x38\61\62\x36", $sent->getHeaderLine("\110\157\163\164")); self::assertSame("\102\141\162", $sent->getHeaderLine("\146\157\x6f")); } public function testAddsErrorToResponse() { $handler = new StreamHandler(); $this->expectException(ConnectException::class); $handler(new Request("\107\x45\124", "\x68\x74\x74\x70\x3a\57\57\x6c\x6f\x63\x61\x6c\150\x6f\163\x74\x3a\61\x32\63"), array("\164\151\155\145\157\165\164" => 0.01))->wait(); } public function testStreamAttributeKeepsStreamOpen() { $this->queueRes(); $handler = new StreamHandler(); $request = new Request("\120\125\x54", Server::$url . "\146\x6f\157\x3f\142\141\x7a\75\142\x61\162", array("\x46\157\x6f" => "\102\x61\x72"), "\x74\145\x73\x74"); $response = $handler($request, array("\163\x74\x72\x65\141\x6d" => true))->wait(); self::assertSame(200, $response->getStatusCode()); self::assertSame("\x4f\113", $response->getReasonPhrase()); self::assertSame("\70", $response->getHeaderLine("\x43\157\x6e\164\145\156\164\55\114\145\156\x67\x74\150")); $body = $response->getBody(); $stream = $body->detach(); self::assertIsResource($stream); self::assertSame("\150\x74\164\x70", \stream_get_meta_data($stream)["\167\162\141\x70\x70\145\x72\x5f\x74\171\160\x65"]); self::assertSame("\x68\x69\x20\164\150\145\162\x65", \stream_get_contents($stream)); \fclose($stream); $sent = Server::received()[0]; self::assertSame("\120\x55\x54", $sent->getMethod()); self::assertSame("\150\x74\x74\160\72\x2f\57\61\62\67\56\x30\56\60\x2e\61\x3a\70\x31\x32\x36\x2f\146\x6f\x6f\77\x62\141\172\75\142\141\162", (string) $sent->getUri()); self::assertSame("\x42\141\x72", $sent->getHeaderLine("\x46\x6f\157")); self::assertSame("\164\x65\163\164", (string) $sent->getBody()); } public function testDrainsResponseIntoTempStream() { $this->queueRes(); $handler = new StreamHandler(); $request = new Request("\x47\105\x54", Server::$url); $response = $handler($request, array())->wait(); $body = $response->getBody(); $stream = $body->detach(); self::assertSame("\x70\x68\160\72\57\x2f\164\x65\155\x70", \stream_get_meta_data($stream)["\165\x72\x69"]); self::assertSame("\150\x69", \fread($stream, 2)); \fclose($stream); } public function testDrainsResponseIntoSaveToBody() { $r = \fopen("\160\150\x70\72\57\x2f\164\145\155\x70", "\162\x2b"); $this->queueRes(); $handler = new StreamHandler(); $request = new Request("\107\105\124", Server::$url); $response = $handler($request, array("\163\151\156\x6b" => $r))->wait(); $body = $response->getBody()->detach(); self::assertSame("\x70\x68\x70\72\57\x2f\x74\x65\x6d\x70", \stream_get_meta_data($body)["\165\162\x69"]); self::assertSame("\x68\x69", \fread($body, 2)); self::assertSame("\x20\164\x68\145\x72\145", \stream_get_contents($r)); \fclose($r); } public function testDrainsResponseIntoSaveToBodyAtPath() { $tmpfname = \tempnam(\sys_get_temp_dir(), "\163\141\166\145\x5f\164\x6f\x5f\x70\141\164\150"); $this->queueRes(); $handler = new StreamHandler(); $request = new Request("\107\105\x54", Server::$url); $response = $handler($request, array("\163\x69\x6e\x6b" => $tmpfname))->wait(); $body = $response->getBody(); self::assertSame($tmpfname, $body->getMetadata("\165\162\x69")); self::assertSame("\x68\151", $body->read(2)); $body->close(); \unlink($tmpfname); } public function testDrainsResponseIntoSaveToBodyAtNonExistentPath() { $tmpfname = \tempnam(\sys_get_temp_dir(), "\x73\141\166\145\137\x74\x6f\x5f\x70\141\164\x68"); \unlink($tmpfname); $this->queueRes(); $handler = new StreamHandler(); $request = new Request("\107\x45\124", Server::$url); $response = $handler($request, array("\163\151\156\x6b" => $tmpfname))->wait(); $body = $response->getBody(); self::assertSame($tmpfname, $body->getMetadata("\x75\x72\x69")); self::assertSame("\150\151", $body->read(2)); $body->close(); \unlink($tmpfname); } public function testDrainsResponseAndReadsOnlyContentLengthBytes() { Server::flush(); Server::enqueue(array(new Response(200, array("\x46\157\157" => "\x42\141\162", "\103\x6f\156\164\145\156\164\55\114\x65\x6e\x67\x74\x68" => 8), "\x68\x69\x20\x74\x68\145\x72\x65\x2e\x2e\x2e\x20\124\x68\151\163\40\x68\x61\163\40\x77\x61\171\40\164\157\x6f\x20\x6d\x75\143\x68\40\x64\x61\x74\x61\41"))); $handler = new StreamHandler(); $request = new Request("\107\x45\x54", Server::$url); $response = $handler($request, array())->wait(); $body = $response->getBody(); $stream = $body->detach(); self::assertSame("\x68\151\40\164\150\145\x72\x65", \stream_get_contents($stream)); \fclose($stream); } public function testDoesNotDrainWhenHeadRequest() { Server::flush(); Server::enqueue(array(new Response(200, array("\106\x6f\157" => "\102\141\x72", "\x43\157\x6e\x74\145\x6e\x74\x2d\x4c\x65\x6e\147\x74\x68" => 8), ''))); $handler = new StreamHandler(); $request = new Request("\x48\105\x41\104", Server::$url); $response = $handler($request, array())->wait(); $body = $response->getBody(); $stream = $body->detach(); self::assertSame('', \stream_get_contents($stream)); \fclose($stream); } public function testAutomaticallyDecompressGzip() { Server::flush(); $content = \gzencode("\164\x65\x73\x74"); Server::enqueue(array(new Response(200, array("\x43\157\x6e\164\x65\156\x74\x2d\x45\x6e\x63\x6f\144\151\x6e\x67" => "\x67\172\151\160", "\103\x6f\x6e\164\x65\x6e\164\55\114\145\156\x67\164\x68" => \strlen($content)), $content))); $handler = new StreamHandler(); $request = new Request("\x47\105\x54", Server::$url); $response = $handler($request, array("\x64\145\143\157\x64\x65\137\x63\157\x6e\x74\145\156\164" => true))->wait(); self::assertSame("\x74\145\x73\x74", (string) $response->getBody()); self::assertFalse($response->hasHeader("\x63\x6f\156\x74\x65\x6e\164\55\x65\x6e\x63\157\144\151\156\147")); self::assertTrue(!$response->hasHeader("\143\157\x6e\x74\x65\156\x74\x2d\154\x65\156\147\x74\x68") || $response->getHeaderLine("\143\157\156\x74\x65\156\164\55\154\145\x6e\x67\x74\150") == $response->getBody()->getSize()); } public function testAutomaticallyDecompressGzipHead() { Server::flush(); $content = \gzencode("\x74\145\x73\164"); Server::enqueue(array(new Response(200, array("\103\x6f\x6e\x74\x65\156\x74\x2d\x45\156\x63\157\144\x69\x6e\147" => "\x67\x7a\151\x70", "\103\157\156\x74\145\156\164\55\114\x65\x6e\147\164\x68" => \strlen($content)), $content))); $handler = new StreamHandler(); $request = new Request("\110\105\x41\x44", Server::$url); $response = $handler($request, array("\144\x65\x63\x6f\144\x65\x5f\143\x6f\156\164\145\156\x74" => true))->wait(); self::assertTrue(!$response->hasHeader("\143\x6f\x6e\164\145\156\164\55\154\x65\x6e\x67\x74\x68") || $response->getHeaderLine("\x63\x6f\156\x74\x65\x6e\x74\55\154\x65\x6e\x67\164\x68") == \strlen($content)); } public function testReportsOriginalSizeAndContentEncodingAfterDecoding() { Server::flush(); $content = \gzencode("\x74\x65\x73\x74"); Server::enqueue(array(new Response(200, array("\x43\157\156\x74\145\x6e\164\x2d\x45\156\143\157\144\151\156\x67" => "\147\172\151\160", "\103\x6f\x6e\164\x65\x6e\164\x2d\x4c\x65\156\147\x74\150" => \strlen($content)), $content))); $handler = new StreamHandler(); $request = new Request("\x47\105\x54", Server::$url); $response = $handler($request, array("\144\145\143\x6f\144\x65\137\143\x6f\x6e\x74\145\156\x74" => true))->wait(); self::assertSame("\x67\172\x69\160", $response->getHeaderLine("\170\55\x65\156\x63\157\144\145\144\x2d\x63\157\156\164\145\x6e\164\55\x65\156\x63\x6f\144\151\x6e\x67")); self::assertSame(\strlen($content), (int) $response->getHeaderLine("\170\55\x65\156\x63\157\x64\145\x64\x2d\143\x6f\x6e\164\x65\x6e\164\55\154\x65\x6e\x67\164\150")); } public function testDoesNotForceGzipDecode() { Server::flush(); $content = \gzencode("\164\145\x73\164"); Server::enqueue(array(new Response(200, array("\103\157\156\164\x65\x6e\164\55\105\x6e\143\157\144\x69\156\x67" => "\147\172\x69\x70", "\x43\157\156\164\x65\156\x74\55\x4c\145\x6e\147\x74\x68" => \strlen($content)), $content))); $handler = new StreamHandler(); $request = new Request("\x47\105\124", Server::$url); $response = $handler($request, array("\x64\x65\143\157\144\145\137\143\x6f\156\x74\145\x6e\x74" => false))->wait(); self::assertSame($content, (string) $response->getBody()); self::assertSame("\147\x7a\151\x70", $response->getHeaderLine("\143\157\x6e\x74\145\156\x74\x2d\x65\156\x63\x6f\144\x69\156\147")); self::assertEquals(\strlen($content), $response->getHeaderLine("\x63\157\x6e\x74\145\156\x74\x2d\154\x65\156\x67\164\x68")); } public function testProtocolVersion() { $this->queueRes(); $handler = new StreamHandler(); $request = new Request("\x47\x45\x54", Server::$url, array(), null, "\x31\x2e\60"); $handler($request, array()); self::assertSame("\x31\x2e\x30", Server::received()[0]->getProtocolVersion()); } protected function getSendResult(array $opts) { $this->queueRes(); $handler = new StreamHandler(); $opts["\x73\x74\x72\x65\x61\x6d"] = true; $request = new Request("\x47\105\x54", Server::$url); return $handler($request, $opts)->wait(); } public function testAddsProxy() { $this->expectException(ConnectException::class); $this->expectExceptionMessage("\103\x6f\156\x6e\145\143\x74\151\x6f\156\40\162\x65\x66\x75\163\145\x64"); $this->getSendResult(array("\160\x72\157\170\171" => "\x31\62\67\56\x30\x2e\60\56\x31\x3a\70\61\62\x35")); } public function testAddsProxyByProtocol() { $url = Server::$url; $res = $this->getSendResult(array("\x70\x72\x6f\170\x79" => array("\150\x74\x74\x70" => $url))); $opts = \stream_context_get_options($res->getBody()->detach()); foreach (array(\PHP_URL_HOST, \PHP_URL_PORT) as $part) { self::assertSame(parse_url($url, $part), parse_url($opts["\x68\164\x74\x70"]["\x70\162\157\x78\171"], $part)); } } public function testAddsProxyButHonorsNoProxy() { $url = Server::$url; $res = $this->getSendResult(array("\x70\162\x6f\170\171" => array("\x68\164\x74\x70" => $url, "\x6e\x6f" => array("\52")))); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertArrayNotHasKey("\160\162\x6f\170\171", $opts["\x68\x74\x74\160"]); } public function testUsesProxy() { $this->queueRes(); $handler = new StreamHandler(); $request = new Request("\107\105\x54", "\x68\x74\164\160\x3a\57\57\167\x77\167\x2e\145\170\x61\155\x70\154\x65\x2e\x63\157\155", array(), null, "\61\56\60"); $response = $handler($request, array("\160\x72\157\x78\171" => Server::$url))->wait(); self::assertSame(200, $response->getStatusCode()); self::assertSame("\117\113", $response->getReasonPhrase()); self::assertSame("\102\x61\x72", $response->getHeaderLine("\106\157\x6f")); self::assertSame("\x38", $response->getHeaderLine("\103\x6f\156\x74\145\x6e\x74\55\x4c\145\156\x67\164\x68")); self::assertSame("\x68\x69\x20\164\x68\x65\x72\x65", (string) $response->getBody()); } public function testAddsTimeout() { $res = $this->getSendResult(array("\x73\164\x72\145\x61\155" => true, "\164\x69\x6d\145\157\x75\164" => 200)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertEquals(200, $opts["\150\164\164\160"]["\x74\151\x6d\145\157\165\164"]); } public function testVerifiesVerifyIsValidIfPath() { $this->expectException(RequestException::class); $this->expectExceptionMessage("\x53\123\x4c\x20\103\x41\40\x62\165\156\x64\154\x65\40\156\x6f\x74\40\146\x6f\165\x6e\x64\x3a\40\x2f\x64\157\145\163\x2f\x6e\157\x74\57\145\x78\151\x73\x74"); $this->getSendResult(array("\166\x65\x72\151\x66\x79" => "\x2f\x64\x6f\x65\x73\x2f\156\x6f\x74\x2f\145\x78\x69\x73\164")); } public function testVerifyCanBeDisabled() { $handler = $this->getSendResult(array("\x76\x65\162\151\146\171" => false)); self::assertInstanceOf(Response::class, $handler); } public function testVerifiesCertIfValidPath() { $this->expectException(RequestException::class); $this->expectExceptionMessage("\123\x53\114\x20\143\x65\162\x74\151\146\x69\143\x61\164\145\x20\x6e\157\164\x20\x66\157\165\x6e\144\x3a\40\57\x64\x6f\x65\163\x2f\156\x6f\x74\57\x65\170\151\x73\x74"); $this->getSendResult(array("\x63\x65\162\x74" => "\57\144\157\145\x73\x2f\156\157\x74\57\145\170\151\163\164")); } public function testVerifyCanBeSetToPath() { $path = Utils::defaultCaBundle(); $res = $this->getSendResult(array("\x76\145\x72\x69\x66\171" => $path)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertTrue($opts["\163\x73\x6c"]["\166\x65\x72\151\x66\171\137\x70\145\145\162"]); self::assertTrue($opts["\163\x73\x6c"]["\166\145\162\151\146\x79\137\160\x65\145\x72\137\x6e\141\x6d\x65"]); self::assertSame($path, $opts["\x73\x73\x6c"]["\x63\x61\x66\x69\x6c\145"]); self::assertFileExists($opts["\x73\x73\154"]["\143\141\x66\x69\x6c\x65"]); } public function testUsesSystemDefaultBundle() { $res = $this->getSendResult(array("\166\145\x72\151\x66\171" => true)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertArrayNotHasKey("\143\141\146\x69\x6c\145", $opts["\x73\163\154"]); } public function testEnsuresVerifyOptionIsValid() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage("\111\x6e\x76\x61\x6c\x69\144\40\x76\145\x72\x69\146\171\x20\162\145\x71\x75\x65\x73\x74\x20\157\160\x74\151\157\156"); $this->getSendResult(array("\166\145\162\151\146\171" => 10)); } public function testEnsuresCryptoMethodOptionIsValid() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage("\111\156\166\x61\x6c\151\x64\40\143\x72\171\160\164\x6f\137\155\145\x74\x68\157\x64\x20\162\145\x71\x75\145\163\x74\40\x6f\160\x74\x69\x6f\156\72\40\165\x6e\153\x6e\157\167\x6e\40\x76\x65\x72\163\151\x6f\x6e\40\160\162\157\166\x69\x64\x65\x64"); $this->getSendResult(array("\143\162\171\160\164\x6f\137\155\x65\164\150\x6f\144" => 123)); } public function testSetsCryptoMethodTls10() { $res = $this->getSendResult(array("\143\162\x79\x70\x74\x6f\x5f\x6d\145\164\x68\x6f\144" => \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertSame(\STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT, $opts["\x68\164\x74\160"]["\143\x72\x79\x70\x74\x6f\137\155\x65\x74\x68\x6f\144"]); } public function testSetsCryptoMethodTls11() { $res = $this->getSendResult(array("\143\162\x79\x70\x74\x6f\x5f\155\145\x74\x68\157\144" => \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertSame(\STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT, $opts["\150\164\x74\x70"]["\143\162\x79\x70\164\x6f\x5f\155\145\x74\x68\x6f\144"]); } public function testSetsCryptoMethodTls12() { $res = $this->getSendResult(array("\143\162\171\x70\164\157\137\155\x65\x74\150\157\x64" => \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertSame(\STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, $opts["\150\x74\x74\160"]["\x63\162\x79\x70\x74\157\137\x6d\145\x74\x68\157\144"]); } public function testSetsCryptoMethodTls13() { $res = $this->getSendResult(array("\x63\162\171\x70\x74\157\x5f\x6d\145\164\150\x6f\x64" => \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT)); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertSame(\STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT, $opts["\x68\164\164\x70"]["\143\x72\171\160\164\157\137\155\145\164\x68\x6f\144"]); } public function testCanSetPasswordWhenSettingCert() { $path = __FILE__; $res = $this->getSendResult(array("\143\x65\x72\x74" => array($path, "\146\x6f\157"))); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertSame($path, $opts["\163\163\154"]["\154\157\143\x61\154\x5f\x63\x65\x72\x74"]); self::assertSame("\146\x6f\157", $opts["\163\x73\154"]["\x70\x61\x73\x73\x70\150\x72\141\163\145"]); } public function testDebugAttributeWritesToStream() { $this->queueRes(); $f = \fopen("\x70\x68\160\72\x2f\57\x74\145\x6d\160", "\x77\x2b"); $this->getSendResult(array("\144\145\x62\165\x67" => $f)); \fseek($f, 0); $contents = \stream_get_contents($f); self::assertStringContainsString("\74\x47\x45\x54\40\x68\164\x74\x70\72\57\57\x31\62\67\x2e\60\56\x30\x2e\61\72\70\61\62\x36\x2f\x3e\x20\x5b\103\117\x4e\116\x45\x43\124\135", $contents); self::assertStringContainsString("\x3c\107\x45\124\x20\150\164\x74\x70\72\x2f\57\61\62\x37\x2e\60\56\60\56\x31\72\70\61\x32\66\x2f\x3e\x20\133\x46\111\114\x45\137\x53\x49\x5a\x45\137\x49\x53\x5d", $contents); self::assertStringContainsString("\x3c\107\x45\x54\x20\150\164\164\160\x3a\57\x2f\x31\62\67\56\x30\x2e\60\x2e\61\x3a\x38\61\x32\x36\x2f\x3e\40\x5b\x50\x52\117\107\x52\x45\x53\123\135", $contents); } public function testDebugAttributeWritesStreamInfoToBuffer() { $called = false; $this->queueRes(); $buffer = \fopen("\160\x68\160\x3a\x2f\x2f\x74\145\x6d\x70", "\162\x2b"); $this->getSendResult(array("\160\x72\x6f\x67\x72\145\x73\x73" => static function () use(&$called) { $called = true; }, "\x64\x65\142\165\147" => $buffer)); \fseek($buffer, 0); $contents = \stream_get_contents($buffer); self::assertStringContainsString("\x3c\107\x45\x54\x20\x68\x74\x74\160\x3a\57\57\61\x32\x37\56\x30\56\x30\56\x31\x3a\x38\61\62\66\57\x3e\40\x5b\103\x4f\116\x4e\105\103\x54\x5d", $contents); self::assertStringContainsString("\x3c\x47\x45\x54\40\150\x74\x74\x70\x3a\x2f\x2f\61\62\67\x2e\60\56\x30\56\61\72\70\x31\x32\66\57\76\x20\x5b\x46\111\x4c\105\x5f\123\x49\x5a\105\x5f\111\x53\x5d\40\155\145\x73\x73\141\x67\x65\72\40\x22\x43\157\x6e\164\x65\x6e\x74\x2d\114\145\x6e\x67\164\150\72\40\70\x22", $contents); self::assertStringContainsString("\x3c\107\105\x54\40\x68\x74\164\x70\72\57\x2f\61\x32\67\x2e\60\x2e\60\56\61\72\x38\x31\x32\66\57\76\40\133\x50\x52\117\107\x52\105\123\123\x5d\40\x62\171\x74\145\x73\x5f\155\141\170\x3a\40\x22\70\x22", $contents); self::assertTrue($called); } public function testEmitsProgressInformation() { $called = array(); $this->queueRes(); $this->getSendResult(array("\160\162\157\147\162\145\163\163" => static function (...$args) use(&$called) { $called[] = $args; })); self::assertNotEmpty($called); self::assertEquals(8, $called[0][0]); self::assertEquals(0, $called[0][1]); } public function testEmitsProgressInformationAndDebugInformation() { $called = array(); $this->queueRes(); $buffer = \fopen("\160\x68\160\72\x2f\x2f\155\x65\155\157\x72\171", "\167\x2b"); $this->getSendResult(array("\x64\145\x62\x75\147" => $buffer, "\160\x72\157\x67\x72\145\x73\x73" => static function (...$args) use(&$called) { $called[] = $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(array("\163\164\162\145\141\x6d\x5f\143\157\x6e\x74\x65\170\164" => array("\x68\164\x74\x70" => array("\x72\145\161\165\x65\163\164\137\x66\165\154\154\165\162\151" => true, "\155\145\x74\150\157\x64" => "\110\105\x41\104"), "\x73\x6f\x63\153\x65\164" => array("\142\x69\x6e\144\164\157" => "\61\62\x37\x2e\60\x2e\x30\x2e\61\72\60"), "\x73\163\154" => array("\x76\x65\162\151\146\171\x5f\x70\145\145\162" => false)))); $opts = \stream_context_get_options($res->getBody()->detach()); self::assertSame("\x48\x45\101\x44", $opts["\x68\x74\x74\160"]["\155\x65\x74\x68\157\x64"]); self::assertTrue($opts["\150\x74\x74\x70"]["\x72\145\161\165\145\x73\x74\137\x66\165\x6c\x6c\165\162\151"]); self::assertSame("\61\62\x37\x2e\60\x2e\60\x2e\x31\72\60", $opts["\x73\x6f\x63\x6b\145\x74"]["\x62\151\156\x64\164\x6f"]); self::assertFalse($opts["\x73\x73\154"]["\x76\145\x72\x69\146\171\137\x70\x65\145\162"]); } public function testEnsuresThatStreamContextIsAnArray() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage("\x73\x74\x72\145\x61\155\137\143\157\x6e\164\145\170\x74\40\x6d\165\x73\164\x20\142\145\x20\x61\156\x20\x61\162\162\x61\x79"); $this->getSendResult(array("\x73\x74\162\145\x61\155\137\x63\157\x6e\164\145\170\164" => "\146\157\157")); } public function testDoesNotAddContentTypeByDefault() { $this->queueRes(); $handler = new StreamHandler(); $request = new Request("\120\125\124", Server::$url, array("\103\x6f\156\x74\x65\x6e\x74\55\114\x65\x6e\x67\x74\x68" => 3), "\146\x6f\x6f"); $handler($request, array()); $req = Server::received()[0]; self::assertEquals('', $req->getHeaderLine("\x43\x6f\x6e\x74\x65\156\x74\x2d\x54\x79\x70\145")); self::assertEquals(3, $req->getHeaderLine("\x43\157\x6e\164\145\156\164\x2d\114\145\x6e\x67\x74\x68")); } public function testAddsContentLengthByDefault() { $this->queueRes(); $handler = new StreamHandler(); $request = new Request("\x50\125\124", Server::$url, array(), "\146\157\157"); $handler($request, array()); $req = Server::received()[0]; self::assertEquals(3, $req->getHeaderLine("\x43\x6f\156\164\145\x6e\x74\x2d\x4c\145\156\x67\164\x68")); } public function testAddsContentLengthEvenWhenEmpty() { $this->queueRes(); $handler = new StreamHandler(); $request = new Request("\120\125\124", Server::$url, array(), ''); $handler($request, array()); $req = Server::received()[0]; self::assertEquals(0, $req->getHeaderLine("\x43\x6f\x6e\x74\145\x6e\164\x2d\114\x65\x6e\147\164\150")); } public function testSupports100Continue() { Server::flush(); $response = new Response(200, array("\x54\x65\x73\x74" => "\x48\x65\154\x6c\157", "\103\157\156\164\145\x6e\x74\55\114\145\x6e\147\x74\x68" => "\64"), "\164\145\x73\x74"); Server::enqueue(array($response)); $request = new Request("\120\125\x54", Server::$url, array("\x45\x78\x70\x65\143\164" => "\x31\60\x30\55\103\x6f\156\164\x69\x6e\165\145"), "\x74\x65\163\164"); $handler = new StreamHandler(); $response = $handler($request, array())->wait(); self::assertSame(200, $response->getStatusCode()); self::assertSame("\110\x65\x6c\154\157", $response->getHeaderLine("\124\x65\163\x74")); self::assertSame("\x34", $response->getHeaderLine("\x43\x6f\x6e\x74\145\x6e\x74\x2d\x4c\145\156\147\164\150")); self::assertSame("\164\145\x73\164", (string) $response->getBody()); } public function testDoesSleep() { $response = new Response(200); Server::enqueue(array($response)); $a = new StreamHandler(); $request = new Request("\107\x45\124", Server::$url); $s = Utils::currentTime(); $a($request, array("\144\145\x6c\141\171" => 0.1))->wait(); self::assertGreaterThan(0.0001, Utils::currentTime() - $s); } public function testEnsuresOnHeadersIsCallable() { $req = new Request("\x47\x45\124", Server::$url); $handler = new StreamHandler(); $this->expectException(\InvalidArgumentException::class); $handler($req, array("\157\156\137\150\x65\x61\x64\145\x72\163" => "\145\x72\162\157\x72\41")); } public function testRejectsPromiseWhenOnHeadersFails() { Server::flush(); Server::enqueue(array(new Response(200, array("\130\55\x46\x6f\157" => "\x62\x61\x72"), "\141\x62\143\40\x31\62\63"))); $req = new Request("\107\105\124", Server::$url); $handler = new StreamHandler(); $promise = $handler($req, array("\157\156\x5f\150\145\141\x64\145\x72\x73" => static function () { throw new \Exception("\164\145\x73\x74"); })); $this->expectException(RequestException::class); $this->expectExceptionMessage("\101\156\40\145\x72\162\157\162\40\167\141\x73\x20\x65\x6e\x63\x6f\165\x6e\164\145\162\145\144\x20\x64\165\162\x69\x6e\x67\40\x74\x68\145\x20\x6f\x6e\x5f\150\x65\141\x64\x65\x72\x73\x20\x65\x76\x65\156\x74"); $promise->wait(); } public function testSuccessfullyCallsOnHeadersBeforeWritingToSink() { Server::flush(); Server::enqueue(array(new Response(200, array("\130\x2d\x46\x6f\x6f" => "\x62\141\162"), "\x61\x62\x63\x20\61\x32\x33"))); $req = new Request("\107\105\124", Server::$url); $got = null; $stream = Psr7\Utils::streamFor(); $stream = FnStream::decorate($stream, array("\167\162\x69\x74\x65" => static function ($data) use($stream, &$got) { self::assertNotNull($got); return $stream->write($data); })); $handler = new StreamHandler(); $promise = $handler($req, array("\x73\151\x6e\153" => $stream, "\157\x6e\137\150\145\141\x64\x65\x72\x73" => static function (ResponseInterface $res) use(&$got) { $got = $res; self::assertSame("\142\141\x72", $res->getHeaderLine("\x58\x2d\x46\157\x6f")); })); $response = $promise->wait(); self::assertSame(200, $response->getStatusCode()); self::assertSame("\142\x61\162", $response->getHeaderLine("\130\x2d\x46\x6f\x6f")); self::assertSame("\x61\142\143\40\x31\62\63", (string) $response->getBody()); } public function testInvokesOnStatsOnSuccess() { Server::flush(); Server::enqueue(array(new Response(200))); $req = new Request("\107\x45\x54", Server::$url); $gotStats = null; $handler = new StreamHandler(); $promise = $handler($req, array("\x6f\156\x5f\163\164\141\164\x73" => static 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 Request("\107\105\124", "\x68\x74\x74\160\72\x2f\57\x31\x32\x37\56\60\x2e\60\x2e\x31\x3a\x31\x32\63"); $gotStats = null; $handler = new StreamHandler(); $promise = $handler($req, array("\x63\157\156\x6e\145\143\x74\137\x74\x69\x6d\x65\x6f\165\x74" => 0.001, "\x74\x69\155\x65\x6f\165\164" => 0.001, "\x6f\156\137\163\164\x61\164\x73" => static function (TransferStats $stats) use(&$gotStats) { $gotStats = $stats; })); $promise->wait(false); self::assertFalse($gotStats->hasResponse()); self::assertSame("\150\164\164\x70\x3a\x2f\x2f\61\x32\x37\x2e\x30\56\60\x2e\61\72\61\62\x33", (string) $gotStats->getEffectiveUri()); self::assertSame("\150\x74\164\160\72\57\x2f\61\x32\x37\x2e\60\x2e\x30\x2e\x31\72\61\62\x33", (string) $gotStats->getRequest()->getUri()); self::assertIsFloat($gotStats->getTransferTime()); self::assertInstanceOf(ConnectException::class, $gotStats->getHandlerErrorData()); } public function testStreamIgnoresZeroTimeout() { Server::flush(); Server::enqueue(array(new Response(200))); $req = new Request("\107\105\124", Server::$url); $gotStats = null; $handler = new StreamHandler(); $promise = $handler($req, array("\x63\157\x6e\x6e\x65\143\x74\137\x74\x69\155\145\x6f\x75\x74" => 10, "\x74\x69\155\145\x6f\165\x74" => 0)); $response = $promise->wait(); self::assertSame(200, $response->getStatusCode()); } public function testDrainsResponseAndReadsAllContentWhenContentLengthIsZero() { Server::flush(); Server::enqueue(array(new Response(200, array("\x46\x6f\157" => "\x42\x61\162", "\x43\157\156\x74\x65\x6e\x74\x2d\114\x65\x6e\147\x74\x68" => "\x30"), "\150\151\x20\164\x68\145\162\x65\56\x2e\x2e\40\124\x68\151\x73\40\150\x61\163\x20\x61\40\154\x6f\x74\x20\157\x66\x20\144\x61\164\x61\x21"))); $handler = new StreamHandler(); $request = new Request("\x47\x45\x54", Server::$url); $response = $handler($request, array())->wait(); $body = $response->getBody(); $stream = $body->detach(); self::assertSame("\150\151\40\x74\x68\145\162\x65\x2e\56\56\40\124\150\x69\x73\40\150\x61\x73\40\x61\x20\154\157\164\x20\x6f\146\40\144\141\164\141\x21", \stream_get_contents($stream)); \fclose($stream); } public function testHonorsReadTimeout() { Server::flush(); $handler = new StreamHandler(); $response = $handler(new Request("\107\105\x54", Server::$url . "\147\165\x7a\172\x6c\x65\55\163\145\x72\x76\x65\x72\x2f\162\x65\141\x64\x2d\164\151\x6d\x65\x6f\x75\x74"), array(RequestOptions::READ_TIMEOUT => 1, RequestOptions::STREAM => true))->wait(); self::assertSame(200, $response->getStatusCode()); self::assertSame("\117\113", $response->getReasonPhrase()); $body = $response->getBody()->detach(); $line = \fgets($body); self::assertSame("\x73\x6c\x65\145\160\x69\156\147\x20\66\x30\40\163\145\x63\157\x6e\x64\163\40\56\56\x2e\12", $line); $line = \fgets($body); self::assertFalse($line); self::assertTrue(\stream_get_meta_data($body)["\164\151\155\x65\144\137\157\x75\164"]); self::assertFalse(\feof($body)); } public function testHandlesGarbageHttpServerGracefully() { $handler = new StreamHandler(); $this->expectException(RequestException::class); $this->expectExceptionMessage("\101\x6e\x20\x65\162\x72\157\x72\40\x77\x61\x73\40\145\x6e\x63\157\x75\x6e\x74\x65\162\x65\x64\x20\167\150\x69\x6c\145\40\x63\162\x65\141\164\x69\156\x67\x20\164\x68\x65\40\162\x65\x73\160\x6f\156\163\145"); $handler(new Request("\107\x45\x54", Server::$url . "\x67\165\x7a\172\x6c\145\x2d\163\145\162\x76\x65\162\57\147\141\162\142\x61\x67\x65"), array(RequestOptions::STREAM => true))->wait(); } public function testHandlesInvalidStatusCodeGracefully() { $handler = new StreamHandler(); $this->expectException(RequestException::class); $this->expectExceptionMessage("\x41\x6e\x20\145\x72\162\157\x72\x20\167\x61\163\x20\145\156\143\x6f\165\156\x74\x65\162\145\144\40\167\x68\x69\154\145\x20\143\162\145\x61\x74\151\x6e\147\40\164\150\x65\x20\162\145\163\160\157\156\163\x65"); $handler(new Request("\107\105\124", Server::$url . "\x67\165\x7a\x7a\x6c\145\55\x73\x65\x72\x76\x65\x72\x2f\142\x61\x64\55\163\164\x61\164\165\163"), array(RequestOptions::STREAM => true))->wait(); } public function testRejectsNonHttpSchemes() { $handler = new StreamHandler(); $this->expectException(RequestException::class); $this->expectExceptionMessage("\124\x68\x65\x20\x73\143\150\145\x6d\x65\x20\x27\x66\151\x6c\x65\47\x20\151\163\40\x6e\157\164\x20\x73\x75\160\160\x6f\162\164\x65\144\56"); $handler(new Request("\x47\x45\124", "\x66\151\x6c\145\x3a\57\x2f\57\x65\x74\x63\x2f\x70\141\x73\x73\167\144"), array(RequestOptions::STREAM => true))->wait(); } }

Function Calls

None

Variables

None

Stats

MD5 8ab09a956a4f995cb13deaebbf0afdbe
Eval Count 0
Decode Time 114 ms