From 5d37d3769b8c35ce1ad67f29ceb496607dc9b1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 23 Jan 2022 11:13:33 +0100 Subject: [PATCH 1/2] Update `Host` header tests requiring root access --- phpunit.xml.dist | 2 +- tests/FunctionalHttpServerTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fd6a9234..93a36f6b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -4,8 +4,8 @@ diff --git a/tests/FunctionalHttpServerTest.php b/tests/FunctionalHttpServerTest.php index fe0e1936..a5274533 100644 --- a/tests/FunctionalHttpServerTest.php +++ b/tests/FunctionalHttpServerTest.php @@ -302,7 +302,7 @@ public function testSecureHttpsOnStandardPortReturnsUriWithNoPort() $loop = Factory::create(); try { - $socket = new SocketServer('127.0.0.1:443', array('tls' => array( + $socket = new SocketServer('tls://127.0.0.1:443', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )), $loop); } catch (\RuntimeException $e) { @@ -341,7 +341,7 @@ public function testSecureHttpsOnStandardPortWithoutHostHeaderUsesSocketUri() $loop = Factory::create(); try { - $socket = new SocketServer('127.0.0.1:443', array('tls' => array( + $socket = new SocketServer('tls://127.0.0.1:443', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )), $loop); } catch (\RuntimeException $e) { @@ -410,7 +410,7 @@ public function testSecureHttpsOnHttpStandardPortReturnsUriWithPort() $loop = Factory::create(); try { - $socket = new SocketServer('127.0.0.1:80', array('tls' => array( + $socket = new SocketServer('tls://127.0.0.1:80', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )), $loop); } catch (\RuntimeException $e) { From 5d3013560286abdb8bb5f30072e6c8c4b662698f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 23 Jan 2022 11:38:37 +0100 Subject: [PATCH 2/2] Update test suite to use default loop --- composer.json | 2 +- tests/Client/FunctionalIntegrationTest.php | 36 ++- tests/FunctionalBrowserTest.php | 142 +++++------ tests/FunctionalHttpServerTest.php | 236 ++++++++---------- tests/HttpServerTest.php | 21 +- tests/Io/MiddlewareRunnerTest.php | 5 +- tests/Io/StreamingServerTest.php | 232 ++++++++--------- tests/Io/TransactionTest.php | 22 +- .../RequestBodyBufferMiddlewareTest.php | 20 +- 9 files changed, 329 insertions(+), 387 deletions(-) diff --git a/composer.json b/composer.json index 8a5c7df6..4c9a0383 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,7 @@ "ringcentral/psr7": "^1.2" }, "require-dev": { - "clue/block-react": "^1.1", + "clue/block-react": "^1.5", "clue/http-proxy-react": "^1.7", "clue/reactphp-ssh-proxy": "^1.3", "clue/socks-react": "^1.3", diff --git a/tests/Client/FunctionalIntegrationTest.php b/tests/Client/FunctionalIntegrationTest.php index 6a62be93..64a3ea8a 100644 --- a/tests/Client/FunctionalIntegrationTest.php +++ b/tests/Client/FunctionalIntegrationTest.php @@ -4,7 +4,7 @@ use Clue\React\Block; use Psr\Http\Message\ResponseInterface; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Http\Client\Client; use React\Promise\Deferred; use React\Promise\Stream; @@ -37,9 +37,7 @@ class FunctionalIntegrationTest extends TestCase public function testRequestToLocalhostEmitsSingleRemoteConnection() { - $loop = Factory::create(); - - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $socket->on('connection', $this->expectCallableOnce()); $socket->on('connection', function (ConnectionInterface $conn) use ($socket) { $conn->end("HTTP/1.1 200 OK\r\n\r\nOk"); @@ -47,26 +45,24 @@ public function testRequestToLocalhostEmitsSingleRemoteConnection() }); $port = parse_url($socket->getAddress(), PHP_URL_PORT); - $client = new Client($loop); + $client = new Client(Loop::get()); $request = $client->request('GET', 'http://localhost:' . $port); $promise = Stream\first($request, 'close'); $request->end(); - Block\await($promise, $loop, self::TIMEOUT_LOCAL); + Block\await($promise, null, self::TIMEOUT_LOCAL); } public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResponse() { - $loop = Factory::create(); - - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $socket->on('connection', function (ConnectionInterface $conn) use ($socket) { $conn->end("HTTP/1.0 200 OK\n\nbody"); $socket->close(); }); - $client = new Client($loop); + $client = new Client(Loop::get()); $request = $client->request('GET', str_replace('tcp:', 'http:', $socket->getAddress())); $once = $this->expectCallableOnceWith('body'); @@ -77,7 +73,7 @@ public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResp $promise = Stream\first($request, 'close'); $request->end(); - Block\await($promise, $loop, self::TIMEOUT_LOCAL); + Block\await($promise, null, self::TIMEOUT_LOCAL); } /** @group internet */ @@ -86,8 +82,7 @@ public function testSuccessfulResponseEmitsEnd() // max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP ini_set('xdebug.max_nesting_level', 256); - $loop = Factory::create(); - $client = new Client($loop); + $client = new Client(Loop::get()); $request = $client->request('GET', 'http://www.google.com/'); @@ -99,7 +94,7 @@ public function testSuccessfulResponseEmitsEnd() $promise = Stream\first($request, 'close'); $request->end(); - Block\await($promise, $loop, self::TIMEOUT_REMOTE); + Block\await($promise, null, self::TIMEOUT_REMOTE); } /** @group internet */ @@ -112,8 +107,7 @@ public function testPostDataReturnsData() // max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP ini_set('xdebug.max_nesting_level', 256); - $loop = Factory::create(); - $client = new Client($loop); + $client = new Client(Loop::get()); $data = str_repeat('.', 33000); $request = $client->request('POST', 'https://' . (mt_rand(0, 1) === 0 ? 'eu.' : '') . 'httpbin.org/post', array('Content-Length' => strlen($data))); @@ -128,7 +122,7 @@ public function testPostDataReturnsData() $request->end($data); - $buffer = Block\await($deferred->promise(), $loop, self::TIMEOUT_REMOTE); + $buffer = Block\await($deferred->promise(), null, self::TIMEOUT_REMOTE); $this->assertNotEquals('', $buffer); @@ -145,8 +139,7 @@ public function testPostJsonReturnsData() $this->markTestSkipped('Not supported on HHVM'); } - $loop = Factory::create(); - $client = new Client($loop); + $client = new Client(Loop::get()); $data = json_encode(array('numbers' => range(1, 50))); $request = $client->request('POST', 'https://httpbin.org/post', array('Content-Length' => strlen($data), 'Content-Type' => 'application/json')); @@ -161,7 +154,7 @@ public function testPostJsonReturnsData() $request->end($data); - $buffer = Block\await($deferred->promise(), $loop, self::TIMEOUT_REMOTE); + $buffer = Block\await($deferred->promise(), null, self::TIMEOUT_REMOTE); $this->assertNotEquals('', $buffer); @@ -176,8 +169,7 @@ public function testCancelPendingConnectionEmitsClose() // max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP ini_set('xdebug.max_nesting_level', 256); - $loop = Factory::create(); - $client = new Client($loop); + $client = new Client(Loop::get()); $request = $client->request('GET', 'http://www.google.com/'); $request->on('error', $this->expectCallableNever()); diff --git a/tests/FunctionalBrowserTest.php b/tests/FunctionalBrowserTest.php index 5db62003..2b7bd58c 100644 --- a/tests/FunctionalBrowserTest.php +++ b/tests/FunctionalBrowserTest.php @@ -5,7 +5,7 @@ use Clue\React\Block; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Http\Browser; use React\Http\HttpServer; use React\Http\Message\ResponseException; @@ -21,7 +21,6 @@ class FunctionalBrowserTest extends TestCase { - private $loop; private $browser; private $base; @@ -30,10 +29,9 @@ class FunctionalBrowserTest extends TestCase */ public function setUpBrowserAndServer() { - $this->loop = $loop = Factory::create(); - $this->browser = new Browser(null, $this->loop); + $this->browser = new Browser(); - $http = new HttpServer($this->loop, new StreamingRequestMiddleware(), function (ServerRequestInterface $request) use ($loop) { + $http = new HttpServer(new StreamingRequestMiddleware(), function (ServerRequestInterface $request) { $path = $request->getUri()->getPath(); $headers = array(); @@ -90,8 +88,8 @@ public function setUpBrowserAndServer() } if ($path === '/delay/10') { - return new Promise(function ($resolve) use ($loop) { - $loop->addTimer(10, function () use ($resolve) { + return new Promise(function ($resolve) { + Loop::addTimer(10, function () use ($resolve) { $resolve(new Response( 200, array(), @@ -127,7 +125,7 @@ public function setUpBrowserAndServer() if ($path === '/stream/1') { $stream = new ThroughStream(); - $loop->futureTick(function () use ($stream, $headers) { + Loop::futureTick(function () use ($stream, $headers) { $stream->end(json_encode(array( 'headers' => $headers ))); @@ -142,7 +140,7 @@ public function setUpBrowserAndServer() var_dump($path); }); - $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; @@ -153,7 +151,7 @@ public function setUpBrowserAndServer() */ public function testSimpleRequest() { - Block\await($this->browser->get($this->base . 'get'), $this->loop); + Block\await($this->browser->get($this->base . 'get')); } public function testGetRequestWithRelativeAddressRejects() @@ -161,7 +159,7 @@ public function testGetRequestWithRelativeAddressRejects() $promise = $this->browser->get('delay'); $this->setExpectedException('InvalidArgumentException', 'Invalid request URL given'); - Block\await($promise, $this->loop); + Block\await($promise); } /** @@ -169,7 +167,7 @@ public function testGetRequestWithRelativeAddressRejects() */ public function testGetRequestWithBaseAndRelativeAddressResolves() { - Block\await($this->browser->withBase($this->base)->get('get'), $this->loop); + Block\await($this->browser->withBase($this->base)->get('get')); } /** @@ -177,7 +175,7 @@ public function testGetRequestWithBaseAndRelativeAddressResolves() */ public function testGetRequestWithBaseAndFullAddressResolves() { - Block\await($this->browser->withBase('http://example.com/')->get($this->base . 'get'), $this->loop); + Block\await($this->browser->withBase('http://example.com/')->get($this->base . 'get')); } public function testCancelGetRequestWillRejectRequest() @@ -186,7 +184,7 @@ public function testCancelGetRequestWillRejectRequest() $promise->cancel(); $this->setExpectedException('RuntimeException'); - Block\await($promise, $this->loop); + Block\await($promise); } public function testCancelRequestWithPromiseFollowerWillRejectRequest() @@ -197,13 +195,13 @@ public function testCancelRequestWithPromiseFollowerWillRejectRequest() $promise->cancel(); $this->setExpectedException('RuntimeException'); - Block\await($promise, $this->loop); + Block\await($promise); } public function testRequestWithoutAuthenticationFails() { $this->setExpectedException('RuntimeException'); - Block\await($this->browser->get($this->base . 'basic-auth/user/pass'), $this->loop); + Block\await($this->browser->get($this->base . 'basic-auth/user/pass')); } /** @@ -213,7 +211,7 @@ public function testRequestWithAuthenticationSucceeds() { $base = str_replace('://', '://user:pass@', $this->base); - Block\await($this->browser->get($base . 'basic-auth/user/pass'), $this->loop); + Block\await($this->browser->get($base . 'basic-auth/user/pass')); } /** @@ -227,7 +225,7 @@ public function testRedirectToPageWithAuthenticationSendsAuthenticationFromLocat { $target = str_replace('://', '://user:pass@', $this->base) . 'basic-auth/user/pass'; - Block\await($this->browser->get($this->base . 'redirect-to?url=' . urlencode($target)), $this->loop); + Block\await($this->browser->get($this->base . 'redirect-to?url=' . urlencode($target))); } /** @@ -242,19 +240,19 @@ public function testRedirectFromPageWithInvalidAuthToPageWithCorrectAuthenticati $base = str_replace('://', '://unknown:invalid@', $this->base); $target = str_replace('://', '://user:pass@', $this->base) . 'basic-auth/user/pass'; - Block\await($this->browser->get($base . 'redirect-to?url=' . urlencode($target)), $this->loop); + Block\await($this->browser->get($base . 'redirect-to?url=' . urlencode($target))); } public function testCancelRedirectedRequestShouldReject() { $promise = $this->browser->get($this->base . 'redirect-to?url=delay%2F10'); - $this->loop->addTimer(0.1, function () use ($promise) { + Loop::addTimer(0.1, function () use ($promise) { $promise->cancel(); }); $this->setExpectedException('RuntimeException', 'Request cancelled'); - Block\await($promise, $this->loop); + Block\await($promise); } public function testTimeoutDelayedResponseShouldReject() @@ -262,7 +260,7 @@ public function testTimeoutDelayedResponseShouldReject() $promise = $this->browser->withTimeout(0.1)->get($this->base . 'delay/10'); $this->setExpectedException('RuntimeException', 'Request timed out after 0.1 seconds'); - Block\await($promise, $this->loop); + Block\await($promise); } public function testTimeoutDelayedResponseAfterStreamingRequestShouldReject() @@ -272,7 +270,7 @@ public function testTimeoutDelayedResponseAfterStreamingRequestShouldReject() $stream->end(); $this->setExpectedException('RuntimeException', 'Request timed out after 0.1 seconds'); - Block\await($promise, $this->loop); + Block\await($promise); } /** @@ -280,7 +278,7 @@ public function testTimeoutDelayedResponseAfterStreamingRequestShouldReject() */ public function testTimeoutFalseShouldResolveSuccessfully() { - Block\await($this->browser->withTimeout(false)->get($this->base . 'get'), $this->loop); + Block\await($this->browser->withTimeout(false)->get($this->base . 'get')); } /** @@ -288,7 +286,7 @@ public function testTimeoutFalseShouldResolveSuccessfully() */ public function testRedirectRequestRelative() { - Block\await($this->browser->get($this->base . 'redirect-to?url=get'), $this->loop); + Block\await($this->browser->get($this->base . 'redirect-to?url=get')); } /** @@ -296,7 +294,7 @@ public function testRedirectRequestRelative() */ public function testRedirectRequestAbsolute() { - Block\await($this->browser->get($this->base . 'redirect-to?url=' . urlencode($this->base . 'get')), $this->loop); + Block\await($this->browser->get($this->base . 'redirect-to?url=' . urlencode($this->base . 'get'))); } /** @@ -306,7 +304,7 @@ public function testFollowingRedirectsFalseResolvesWithRedirectResult() { $browser = $this->browser->withFollowRedirects(false); - Block\await($browser->get($this->base . 'redirect-to?url=get'), $this->loop); + Block\await($browser->get($this->base . 'redirect-to?url=get')); } public function testFollowRedirectsZeroRejectsOnRedirect() @@ -314,12 +312,12 @@ public function testFollowRedirectsZeroRejectsOnRedirect() $browser = $this->browser->withFollowRedirects(0); $this->setExpectedException('RuntimeException'); - Block\await($browser->get($this->base . 'redirect-to?url=get'), $this->loop); + Block\await($browser->get($this->base . 'redirect-to?url=get')); } public function testResponseStatus204ShouldResolveWithEmptyBody() { - $response = Block\await($this->browser->get($this->base . 'status/204'), $this->loop); + $response = Block\await($this->browser->get($this->base . 'status/204')); $this->assertFalse($response->hasHeader('Content-Length')); $body = $response->getBody(); @@ -329,7 +327,7 @@ public function testResponseStatus204ShouldResolveWithEmptyBody() public function testResponseStatus304ShouldResolveWithEmptyBodyButContentLengthResponseHeader() { - $response = Block\await($this->browser->get($this->base . 'status/304'), $this->loop); + $response = Block\await($this->browser->get($this->base . 'status/304')); $this->assertEquals('12', $response->getHeaderLine('Content-Length')); $body = $response->getBody(); @@ -344,7 +342,7 @@ public function testGetRequestWithResponseBufferMatchedExactlyResolves() { $promise = $this->browser->withResponseBuffer(5)->get($this->base . 'get'); - Block\await($promise, $this->loop); + Block\await($promise); } public function testGetRequestWithResponseBufferExceededRejects() @@ -356,7 +354,7 @@ public function testGetRequestWithResponseBufferExceededRejects() 'Response body size of 5 bytes exceeds maximum of 4 bytes', defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 0 ); - Block\await($promise, $this->loop); + Block\await($promise); } public function testGetRequestWithResponseBufferExceededDuringStreamingRejects() @@ -368,7 +366,7 @@ public function testGetRequestWithResponseBufferExceededDuringStreamingRejects() 'Response body size exceeds maximum of 4 bytes', defined('SOCKET_EMSGSIZE') ? SOCKET_EMSGSIZE : 0 ); - Block\await($promise, $this->loop); + Block\await($promise); } /** @@ -381,7 +379,7 @@ public function testCanAccessHttps() $this->markTestSkipped('Not supported on HHVM'); } - Block\await($this->browser->get('https://www.google.com/'), $this->loop); + Block\await($this->browser->get('https://www.google.com/')); } /** @@ -397,12 +395,12 @@ public function testVerifyPeerEnabledForBadSslRejects() 'tls' => array( 'verify_peer' => true ) - ), $this->loop); + )); - $browser = new Browser($connector, $this->loop); + $browser = new Browser($connector); $this->setExpectedException('RuntimeException'); - Block\await($browser->get('https://self-signed.badssl.com/'), $this->loop); + Block\await($browser->get('https://self-signed.badssl.com/')); } /** @@ -419,11 +417,11 @@ public function testVerifyPeerDisabledForBadSslResolves() 'tls' => array( 'verify_peer' => false ) - ), $this->loop); + )); - $browser = new Browser($connector, $this->loop); + $browser = new Browser($connector); - Block\await($browser->get('https://self-signed.badssl.com/'), $this->loop); + Block\await($browser->get('https://self-signed.badssl.com/')); } /** @@ -432,13 +430,13 @@ public function testVerifyPeerDisabledForBadSslResolves() public function testInvalidPort() { $this->setExpectedException('RuntimeException'); - Block\await($this->browser->get('http://www.google.com:443/'), $this->loop); + Block\await($this->browser->get('http://www.google.com:443/')); } public function testErrorStatusCodeRejectsWithResponseException() { try { - Block\await($this->browser->get($this->base . 'status/404'), $this->loop); + Block\await($this->browser->get($this->base . 'status/404')); $this->fail(); } catch (ResponseException $e) { $this->assertEquals(404, $e->getCode()); @@ -450,14 +448,14 @@ public function testErrorStatusCodeRejectsWithResponseException() public function testErrorStatusCodeDoesNotRejectWithRejectErrorResponseFalse() { - $response = Block\await($this->browser->withRejectErrorResponse(false)->get($this->base . 'status/404'), $this->loop); + $response = Block\await($this->browser->withRejectErrorResponse(false)->get($this->base . 'status/404')); $this->assertEquals(404, $response->getStatusCode()); } public function testPostString() { - $response = Block\await($this->browser->post($this->base . 'post', array(), 'hello world'), $this->loop); + $response = Block\await($this->browser->post($this->base . 'post', array(), 'hello world')); $data = json_decode((string)$response->getBody(), true); $this->assertEquals('hello world', $data['data']); @@ -465,7 +463,7 @@ public function testPostString() public function testRequestStreamReturnsResponseBodyUntilConnectionsEndsForHttp10() { - $response = Block\await($this->browser->withProtocolVersion('1.0')->get($this->base . 'stream/1'), $this->loop); + $response = Block\await($this->browser->withProtocolVersion('1.0')->get($this->base . 'stream/1')); $this->assertEquals('1.0', $response->getProtocolVersion()); $this->assertFalse($response->hasHeader('Transfer-Encoding')); @@ -476,7 +474,7 @@ public function testRequestStreamReturnsResponseBodyUntilConnectionsEndsForHttp1 public function testRequestStreamReturnsResponseWithTransferEncodingChunkedAndResponseBodyDecodedForHttp11() { - $response = Block\await($this->browser->get($this->base . 'stream/1'), $this->loop); + $response = Block\await($this->browser->get($this->base . 'stream/1')); $this->assertEquals('1.1', $response->getProtocolVersion()); @@ -488,7 +486,7 @@ public function testRequestStreamReturnsResponseWithTransferEncodingChunkedAndRe public function testRequestStreamWithHeadRequestReturnsEmptyResponseBodWithTransferEncodingChunkedForHttp11() { - $response = Block\await($this->browser->head($this->base . 'stream/1'), $this->loop); + $response = Block\await($this->browser->head($this->base . 'stream/1')); $this->assertEquals('1.1', $response->getProtocolVersion()); @@ -498,7 +496,7 @@ public function testRequestStreamWithHeadRequestReturnsEmptyResponseBodWithTrans public function testRequestStreamReturnsResponseWithResponseBodyUndecodedWhenResponseHasDoubleTransferEncoding() { - $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); + $socket = new SocketServer('127.0.0.1:0'); $socket->on('connection', function (\React\Socket\ConnectionInterface $connection) { $connection->on('data', function () use ($connection) { $connection->end("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked, chunked\r\nConnection: close\r\n\r\nhello"); @@ -507,7 +505,7 @@ public function testRequestStreamReturnsResponseWithResponseBodyUndecodedWhenRes $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; - $response = Block\await($this->browser->get($this->base . 'stream/1'), $this->loop); + $response = Block\await($this->browser->get($this->base . 'stream/1')); $this->assertEquals('1.1', $response->getProtocolVersion()); @@ -518,7 +516,7 @@ public function testRequestStreamReturnsResponseWithResponseBodyUndecodedWhenRes public function testReceiveStreamAndExplicitlyCloseConnectionEvenWhenServerKeepsConnectionOpen() { $closed = new \React\Promise\Deferred(); - $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); + $socket = new SocketServer('127.0.0.1:0'); $socket->on('connection', function (\React\Socket\ConnectionInterface $connection) use ($closed) { $connection->on('data', function () use ($connection) { $connection->write("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello"); @@ -530,10 +528,10 @@ public function testReceiveStreamAndExplicitlyCloseConnectionEvenWhenServerKeeps $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; - $response = Block\await($this->browser->get($this->base . 'get', array()), $this->loop); + $response = Block\await($this->browser->get($this->base . 'get', array())); $this->assertEquals('hello', (string)$response->getBody()); - $ret = Block\await($closed->promise(), $this->loop, 0.1); + $ret = Block\await($closed->promise(), null, 0.1); $this->assertTrue($ret); $socket->close(); @@ -543,11 +541,11 @@ public function testPostStreamChunked() { $stream = new ThroughStream(); - $this->loop->addTimer(0.001, function () use ($stream) { + Loop::addTimer(0.001, function () use ($stream) { $stream->end('hello world'); }); - $response = Block\await($this->browser->post($this->base . 'post', array(), $stream), $this->loop); + $response = Block\await($this->browser->post($this->base . 'post', array(), $stream)); $data = json_decode((string)$response->getBody(), true); $this->assertEquals('hello world', $data['data']); @@ -559,11 +557,11 @@ public function testPostStreamKnownLength() { $stream = new ThroughStream(); - $this->loop->addTimer(0.001, function () use ($stream) { + Loop::addTimer(0.001, function () use ($stream) { $stream->end('hello world'); }); - $response = Block\await($this->browser->post($this->base . 'post', array('Content-Length' => 11), $stream), $this->loop); + $response = Block\await($this->browser->post($this->base . 'post', array('Content-Length' => 11), $stream)); $data = json_decode((string)$response->getBody(), true); $this->assertEquals('hello world', $data['data']); @@ -574,16 +572,16 @@ public function testPostStreamKnownLength() */ public function testPostStreamWillStartSendingRequestEvenWhenBodyDoesNotEmitData() { - $http = new HttpServer($this->loop, new StreamingRequestMiddleware(), function (ServerRequestInterface $request) { + $http = new HttpServer(new StreamingRequestMiddleware(), function (ServerRequestInterface $request) { return new Response(200); }); - $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; $stream = new ThroughStream(); - Block\await($this->browser->post($this->base . 'post', array(), $stream), $this->loop); + Block\await($this->browser->post($this->base . 'post', array(), $stream)); $socket->close(); } @@ -593,7 +591,7 @@ public function testPostStreamClosed() $stream = new ThroughStream(); $stream->close(); - $response = Block\await($this->browser->post($this->base . 'post', array(), $stream), $this->loop); + $response = Block\await($this->browser->post($this->base . 'post', array(), $stream)); $data = json_decode((string)$response->getBody(), true); $this->assertEquals('', $data['data']); @@ -601,19 +599,19 @@ public function testPostStreamClosed() public function testSendsHttp11ByDefault() { - $http = new HttpServer($this->loop, function (ServerRequestInterface $request) { + $http = new HttpServer(function (ServerRequestInterface $request) { return new Response( 200, array(), $request->getProtocolVersion() ); }); - $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; - $response = Block\await($this->browser->get($this->base), $this->loop); + $response = Block\await($this->browser->get($this->base)); $this->assertEquals('1.1', (string)$response->getBody()); $socket->close(); @@ -621,19 +619,19 @@ public function testSendsHttp11ByDefault() public function testSendsExplicitHttp10Request() { - $http = new HttpServer($this->loop, function (ServerRequestInterface $request) { + $http = new HttpServer(function (ServerRequestInterface $request) { return new Response( 200, array(), $request->getProtocolVersion() ); }); - $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; - $response = Block\await($this->browser->withProtocolVersion('1.0')->get($this->base), $this->loop); + $response = Block\await($this->browser->withProtocolVersion('1.0')->get($this->base)); $this->assertEquals('1.0', (string)$response->getBody()); $socket->close(); @@ -641,7 +639,7 @@ public function testSendsExplicitHttp10Request() public function testHeadRequestReceivesResponseWithEmptyBodyButWithContentLengthResponseHeader() { - $response = Block\await($this->browser->head($this->base . 'get'), $this->loop); + $response = Block\await($this->browser->head($this->base . 'get')); $this->assertEquals('5', $response->getHeaderLine('Content-Length')); $body = $response->getBody(); @@ -651,7 +649,7 @@ public function testHeadRequestReceivesResponseWithEmptyBodyButWithContentLength public function testRequestStreamingGetReceivesResponseWithStreamingBodyAndKnownSize() { - $response = Block\await($this->browser->requestStreaming('GET', $this->base . 'get'), $this->loop); + $response = Block\await($this->browser->requestStreaming('GET', $this->base . 'get')); $this->assertEquals('5', $response->getHeaderLine('Content-Length')); $body = $response->getBody(); @@ -662,7 +660,7 @@ public function testRequestStreamingGetReceivesResponseWithStreamingBodyAndKnown public function testRequestStreamingGetReceivesResponseWithStreamingBodyAndUnknownSizeFromStreamingEndpoint() { - $response = Block\await($this->browser->requestStreaming('GET', $this->base . 'stream/1'), $this->loop); + $response = Block\await($this->browser->requestStreaming('GET', $this->base . 'stream/1')); $this->assertFalse($response->hasHeader('Content-Length')); $body = $response->getBody(); @@ -676,8 +674,7 @@ public function testRequestStreamingGetReceivesStreamingResponseBody() $buffer = Block\await( $this->browser->requestStreaming('GET', $this->base . 'get')->then(function (ResponseInterface $response) { return Stream\buffer($response->getBody()); - }), - $this->loop + }) ); $this->assertEquals('hello', $buffer); @@ -688,8 +685,7 @@ public function testRequestStreamingGetReceivesStreamingResponseBodyEvenWhenResp $buffer = Block\await( $this->browser->withResponseBuffer(4)->requestStreaming('GET', $this->base . 'get')->then(function (ResponseInterface $response) { return Stream\buffer($response->getBody()); - }), - $this->loop + }) ); $this->assertEquals('hello', $buffer); diff --git a/tests/FunctionalHttpServerTest.php b/tests/FunctionalHttpServerTest.php index a5274533..6fa85903 100644 --- a/tests/FunctionalHttpServerTest.php +++ b/tests/FunctionalHttpServerTest.php @@ -5,7 +5,7 @@ use Clue\React\Block; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ServerRequestInterface; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Http\HttpServer; use React\Http\Message\Response; use React\Http\Middleware\LimitConcurrentRequestsMiddleware; @@ -22,14 +22,13 @@ class FunctionalHttpServerTest extends TestCase { public function testPlainHttpOnRandomPort() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -38,7 +37,7 @@ public function testPlainHttpOnRandomPort() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('http://' . noScheme($socket->getAddress()) . '/', $response); @@ -48,17 +47,15 @@ public function testPlainHttpOnRandomPort() public function testPlainHttpOnRandomPortWithSingleRequestHandlerArray() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); $http = new HttpServer( - $loop, function () { return new Response(404); } ); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -67,7 +64,7 @@ function () { return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 404 Not Found", $response); @@ -76,14 +73,13 @@ function () { public function testPlainHttpOnRandomPortWithoutHostHeaderUsesSocketUri() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -92,7 +88,7 @@ public function testPlainHttpOnRandomPortWithoutHostHeaderUsesSocketUri() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('http://' . noScheme($socket->getAddress()) . '/', $response); @@ -102,14 +98,13 @@ public function testPlainHttpOnRandomPortWithoutHostHeaderUsesSocketUri() public function testPlainHttpOnRandomPortWithOtherHostHeaderTakesPrecedence() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -118,7 +113,7 @@ public function testPlainHttpOnRandomPortWithOtherHostHeaderTakesPrecedence() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('http://localhost:1000/', $response); @@ -132,18 +127,17 @@ public function testSecureHttpsOnRandomPort() $this->markTestSkipped('Not supported on HHVM'); } - $loop = Factory::create(); $connector = new Connector(array( 'tls' => array('verify_peer' => false) - ), $loop); + )); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); $socket = new SocketServer('tls://127.0.0.1:0', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )), $loop); + ))); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -152,7 +146,7 @@ public function testSecureHttpsOnRandomPort() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('https://' . noScheme($socket->getAddress()) . '/', $response); @@ -166,9 +160,7 @@ public function testSecureHttpsReturnsData() $this->markTestSkipped('Not supported on HHVM'); } - $loop = Factory::create(); - - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response( 200, array(), @@ -178,12 +170,12 @@ public function testSecureHttpsReturnsData() $socket = new SocketServer('tls://127.0.0.1:0', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )), $loop); + ))); $http->listen($socket); $connector = new Connector(array( 'tls' => array('verify_peer' => false) - ), $loop); + )); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); @@ -191,7 +183,7 @@ public function testSecureHttpsReturnsData() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString("\r\nContent-Length: 33000\r\n", $response); @@ -206,18 +198,17 @@ public function testSecureHttpsOnRandomPortWithoutHostHeaderUsesSocketUri() $this->markTestSkipped('Not supported on HHVM'); } - $loop = Factory::create(); $connector = new Connector(array( 'tls' => array('verify_peer' => false) - ), $loop); + )); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); $socket = new SocketServer('tls://127.0.0.1:0', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )), $loop); + ))); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -226,7 +217,7 @@ public function testSecureHttpsOnRandomPortWithoutHostHeaderUsesSocketUri() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('https://' . noScheme($socket->getAddress()) . '/', $response); @@ -236,15 +227,14 @@ public function testSecureHttpsOnRandomPortWithoutHostHeaderUsesSocketUri() public function testPlainHttpOnStandardPortReturnsUriWithNoPort() { - $loop = Factory::create(); try { - $socket = new SocketServer('127.0.0.1:80', array(), $loop); + $socket = new SocketServer('127.0.0.1:80'); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 80 failed (root and unused?)'); } - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); @@ -256,7 +246,7 @@ public function testPlainHttpOnStandardPortReturnsUriWithNoPort() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('http://127.0.0.1/', $response); @@ -266,15 +256,14 @@ public function testPlainHttpOnStandardPortReturnsUriWithNoPort() public function testPlainHttpOnStandardPortWithoutHostHeaderReturnsUriWithNoPort() { - $loop = Factory::create(); try { - $socket = new SocketServer('127.0.0.1:80', array(), $loop); + $socket = new SocketServer('127.0.0.1:80'); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 80 failed (root and unused?)'); } - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); @@ -286,7 +275,7 @@ public function testPlainHttpOnStandardPortWithoutHostHeaderReturnsUriWithNoPort return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('http://127.0.0.1/', $response); @@ -300,20 +289,19 @@ public function testSecureHttpsOnStandardPortReturnsUriWithNoPort() $this->markTestSkipped('Not supported on HHVM'); } - $loop = Factory::create(); try { $socket = new SocketServer('tls://127.0.0.1:443', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )), $loop); + ))); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 443 failed (root and unused?)'); } $connector = new Connector(array( 'tls' => array('verify_peer' => false) - ), $loop); + )); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); @@ -325,7 +313,7 @@ public function testSecureHttpsOnStandardPortReturnsUriWithNoPort() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('https://127.0.0.1/', $response); @@ -339,20 +327,19 @@ public function testSecureHttpsOnStandardPortWithoutHostHeaderUsesSocketUri() $this->markTestSkipped('Not supported on HHVM'); } - $loop = Factory::create(); try { $socket = new SocketServer('tls://127.0.0.1:443', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )), $loop); + ))); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 443 failed (root and unused?)'); } $connector = new Connector(array( 'tls' => array('verify_peer' => false) - ), $loop); + )); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); @@ -364,7 +351,7 @@ public function testSecureHttpsOnStandardPortWithoutHostHeaderUsesSocketUri() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('https://127.0.0.1/', $response); @@ -374,15 +361,14 @@ public function testSecureHttpsOnStandardPortWithoutHostHeaderUsesSocketUri() public function testPlainHttpOnHttpsStandardPortReturnsUriWithPort() { - $loop = Factory::create(); try { - $socket = new SocketServer('127.0.0.1:443', array(), $loop); + $socket = new SocketServer('127.0.0.1:443'); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 443 failed (root and unused?)'); } - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); @@ -394,7 +380,7 @@ public function testPlainHttpOnHttpsStandardPortReturnsUriWithPort() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('http://127.0.0.1:443/', $response); @@ -408,20 +394,19 @@ public function testSecureHttpsOnHttpStandardPortReturnsUriWithPort() $this->markTestSkipped('Not supported on HHVM'); } - $loop = Factory::create(); try { $socket = new SocketServer('tls://127.0.0.1:80', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )), $loop); + ))); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 80 failed (root and unused?)'); } $connector = new Connector(array( 'tls' => array('verify_peer' => false) - ), $loop); + )); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri() . 'x' . $request->getHeaderLine('Host')); }); @@ -433,7 +418,7 @@ public function testSecureHttpsOnHttpStandardPortReturnsUriWithPort() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertContainsString("HTTP/1.0 200 OK", $response); $this->assertContainsString('https://127.0.0.1:80/', $response); @@ -443,17 +428,16 @@ public function testSecureHttpsOnHttpStandardPortReturnsUriWithPort() public function testClosedStreamFromRequestHandlerWillSendEmptyBody() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); $stream = new ThroughStream(); $stream->close(); - $http = new HttpServer($loop, function (RequestInterface $request) use ($stream) { + $http = new HttpServer(function (RequestInterface $request) use ($stream) { return new Response(200, array(), $stream); }); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -462,7 +446,7 @@ public function testClosedStreamFromRequestHandlerWillSendEmptyBody() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertStringStartsWith("HTTP/1.0 200 OK", $response); $this->assertStringEndsWith("\r\n\r\n", $response); @@ -472,62 +456,58 @@ public function testClosedStreamFromRequestHandlerWillSendEmptyBody() public function testRequestHandlerWithStreamingRequestWillReceiveCloseEventIfConnectionClosesWhileSendingBody() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); $once = $this->expectCallableOnce(); $http = new HttpServer( - $loop, new StreamingRequestMiddleware(), function (RequestInterface $request) use ($once) { $request->getBody()->on('close', $once); } ); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); - $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) { + $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nContent-Length: 100\r\n\r\n"); - $loop->addTimer(0.001, function() use ($conn) { + Loop::addTimer(0.001, function() use ($conn) { $conn->end(); }); }); - Block\sleep(0.1, $loop); + Block\sleep(0.1); $socket->close(); } public function testStreamFromRequestHandlerWillBeClosedIfConnectionClosesWhileSendingStreamingRequestBody() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); $stream = new ThroughStream(); $http = new HttpServer( - $loop, new StreamingRequestMiddleware(), function (RequestInterface $request) use ($stream) { return new Response(200, array(), $stream); } ); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); - $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) { + $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nContent-Length: 100\r\n\r\n"); - $loop->addTimer(0.001, function() use ($conn) { + Loop::addTimer(0.001, function() use ($conn) { $conn->end(); }); }); // stream will be closed within 0.1s - $ret = Block\await(Stream\first($stream, 'close'), $loop, 0.1); + $ret = Block\await(Stream\first($stream, 'close'), null, 0.1); $socket->close(); @@ -536,28 +516,27 @@ function (RequestInterface $request) use ($stream) { public function testStreamFromRequestHandlerWillBeClosedIfConnectionCloses() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); $stream = new ThroughStream(); - $http = new HttpServer($loop, function (RequestInterface $request) use ($stream) { + $http = new HttpServer(function (RequestInterface $request) use ($stream) { return new Response(200, array(), $stream); }); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); - $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) { + $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\n\r\n"); - $loop->addTimer(0.1, function () use ($conn) { + Loop::addTimer(0.1, function () use ($conn) { $conn->close(); }); }); // await response stream to be closed - $ret = Block\await(Stream\first($stream, 'close'), $loop, 1.0); + $ret = Block\await(Stream\first($stream, 'close'), null, 1.0); $socket->close(); @@ -566,20 +545,19 @@ public function testStreamFromRequestHandlerWillBeClosedIfConnectionCloses() public function testUpgradeWithThroughStreamReturnsDataAsGiven() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { + $http = new HttpServer(function (RequestInterface $request) { $stream = new ThroughStream(); - $loop->addTimer(0.1, function () use ($stream) { + Loop::addTimer(0.1, function () use ($stream) { $stream->end(); }); return new Response(101, array('Upgrade' => 'echo'), $stream); }); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -593,7 +571,7 @@ public function testUpgradeWithThroughStreamReturnsDataAsGiven() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertStringStartsWith("HTTP/1.1 101 Switching Protocols\r\n", $response); $this->assertStringEndsWith("\r\n\r\nhelloworld", $response); @@ -603,20 +581,19 @@ public function testUpgradeWithThroughStreamReturnsDataAsGiven() public function testUpgradeWithRequestBodyAndThroughStreamReturnsDataAsGiven() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { + $http = new HttpServer(function (RequestInterface $request) { $stream = new ThroughStream(); - $loop->addTimer(0.1, function () use ($stream) { + Loop::addTimer(0.1, function () use ($stream) { $stream->end(); }); return new Response(101, array('Upgrade' => 'echo'), $stream); }); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -631,7 +608,7 @@ public function testUpgradeWithRequestBodyAndThroughStreamReturnsDataAsGiven() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertStringStartsWith("HTTP/1.1 101 Switching Protocols\r\n", $response); $this->assertStringEndsWith("\r\n\r\nhelloworld", $response); @@ -641,20 +618,19 @@ public function testUpgradeWithRequestBodyAndThroughStreamReturnsDataAsGiven() public function testConnectWithThroughStreamReturnsDataAsGiven() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { + $http = new HttpServer(function (RequestInterface $request) { $stream = new ThroughStream(); - $loop->addTimer(0.1, function () use ($stream) { + Loop::addTimer(0.1, function () use ($stream) { $stream->end(); }); return new Response(200, array(), $stream); }); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -668,7 +644,7 @@ public function testConnectWithThroughStreamReturnsDataAsGiven() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertStringStartsWith("HTTP/1.1 200 OK\r\n", $response); $this->assertStringEndsWith("\r\n\r\nhelloworld", $response); @@ -678,24 +654,23 @@ public function testConnectWithThroughStreamReturnsDataAsGiven() public function testConnectWithThroughStreamReturnedFromPromiseReturnsDataAsGiven() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { + $http = new HttpServer(function (RequestInterface $request) { $stream = new ThroughStream(); - $loop->addTimer(0.1, function () use ($stream) { + Loop::addTimer(0.1, function () use ($stream) { $stream->end(); }); - return new Promise\Promise(function ($resolve) use ($loop, $stream) { - $loop->addTimer(0.001, function () use ($resolve, $stream) { + return new Promise\Promise(function ($resolve) use ($stream) { + Loop::addTimer(0.001, function () use ($resolve, $stream) { $resolve(new Response(200, array(), $stream)); }); }); }); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -709,7 +684,7 @@ public function testConnectWithThroughStreamReturnedFromPromiseReturnsDataAsGive return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertStringStartsWith("HTTP/1.1 200 OK\r\n", $response); $this->assertStringEndsWith("\r\n\r\nhelloworld", $response); @@ -719,17 +694,16 @@ public function testConnectWithThroughStreamReturnedFromPromiseReturnsDataAsGive public function testConnectWithClosedThroughStreamReturnsNoData() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); - $http = new HttpServer($loop, function (RequestInterface $request) { + $http = new HttpServer(function (RequestInterface $request) { $stream = new ThroughStream(); $stream->close(); return new Response(200, array(), $stream); }); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -743,7 +717,7 @@ public function testConnectWithClosedThroughStreamReturnsNoData() return Stream\buffer($conn); }); - $response = Block\await($result, $loop, 1.0); + $response = Block\await($result, null, 1.0); $this->assertStringStartsWith("HTTP/1.1 200 OK\r\n", $response); $this->assertStringEndsWith("\r\n\r\n", $response); @@ -753,16 +727,14 @@ public function testConnectWithClosedThroughStreamReturnsNoData() public function testLimitConcurrentRequestsMiddlewareRequestStreamPausing() { - $loop = Factory::create(); - $connector = new Connector(array(), $loop); + $connector = new Connector(); $http = new HttpServer( - $loop, new LimitConcurrentRequestsMiddleware(5), new RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB - function (ServerRequestInterface $request, $next) use ($loop) { - return new Promise\Promise(function ($resolve) use ($request, $loop, $next) { - $loop->addTimer(0.1, function () use ($request, $resolve, $next) { + function (ServerRequestInterface $request, $next) { + return new Promise\Promise(function ($resolve) use ($request, $next) { + Loop::addTimer(0.1, function () use ($request, $resolve, $next) { $resolve($next($request)); }); }); @@ -772,7 +744,7 @@ function (ServerRequestInterface $request) { } ); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0'); $http->listen($socket); $result = array(); @@ -788,7 +760,7 @@ function (ServerRequestInterface $request) { }); } - $responses = Block\await(Promise\all($result), $loop, 1.0); + $responses = Block\await(Promise\all($result), null, 1.0); foreach ($responses as $response) { $this->assertContainsString("HTTP/1.0 200 OK", $response, $response); diff --git a/tests/HttpServerTest.php b/tests/HttpServerTest.php index 9200aa66..23dcc758 100644 --- a/tests/HttpServerTest.php +++ b/tests/HttpServerTest.php @@ -4,7 +4,7 @@ use Clue\React\Block; use Psr\Http\Message\ServerRequestInterface; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Http\HttpServer; use React\Http\Io\IniUtil; use React\Http\Middleware\StreamingRequestMiddleware; @@ -126,9 +126,8 @@ function (ServerRequestInterface $request) use (&$called) { public function testPostFormData() { - $loop = Factory::create(); $deferred = new Deferred(); - $http = new HttpServer($loop, function (ServerRequestInterface $request) use ($deferred) { + $http = new HttpServer(function (ServerRequestInterface $request) use ($deferred) { $deferred->resolve($request); }); @@ -136,7 +135,7 @@ public function testPostFormData() $this->socket->emit('connection', array($this->connection)); $this->connection->emit('data', array("POST / HTTP/1.0\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 7\r\n\r\nfoo=bar")); - $request = Block\await($deferred->promise(), $loop); + $request = Block\await($deferred->promise()); assert($request instanceof ServerRequestInterface); $form = $request->getParsedBody(); @@ -155,9 +154,8 @@ public function testPostFormData() public function testPostFileUpload() { - $loop = Factory::create(); $deferred = new Deferred(); - $http = new HttpServer($loop, function (ServerRequestInterface $request) use ($deferred) { + $http = new HttpServer(function (ServerRequestInterface $request) use ($deferred) { $deferred->resolve($request); }); @@ -166,16 +164,16 @@ public function testPostFileUpload() $connection = $this->connection; $data = $this->createPostFileUploadRequest(); - $loop->addPeriodicTimer(0.01, function ($timer) use ($loop, &$data, $connection) { + Loop::addPeriodicTimer(0.01, function ($timer) use (&$data, $connection) { $line = array_shift($data); $connection->emit('data', array($line)); if (count($data) === 0) { - $loop->cancelTimer($timer); + Loop::cancelTimer($timer); } }); - $request = Block\await($deferred->promise(), $loop); + $request = Block\await($deferred->promise()); assert($request instanceof ServerRequestInterface); $this->assertEmpty($request->getParsedBody()); @@ -199,9 +197,8 @@ public function testPostFileUpload() public function testPostJsonWillNotBeParsedByDefault() { - $loop = Factory::create(); $deferred = new Deferred(); - $http = new HttpServer($loop, function (ServerRequestInterface $request) use ($deferred) { + $http = new HttpServer(function (ServerRequestInterface $request) use ($deferred) { $deferred->resolve($request); }); @@ -209,7 +206,7 @@ public function testPostJsonWillNotBeParsedByDefault() $this->socket->emit('connection', array($this->connection)); $this->connection->emit('data', array("POST / HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: 6\r\n\r\n[true]")); - $request = Block\await($deferred->promise(), $loop); + $request = Block\await($deferred->promise()); assert($request instanceof ServerRequestInterface); $this->assertNull($request->getParsedBody()); diff --git a/tests/Io/MiddlewareRunnerTest.php b/tests/Io/MiddlewareRunnerTest.php index eda61012..d8f5f232 100644 --- a/tests/Io/MiddlewareRunnerTest.php +++ b/tests/Io/MiddlewareRunnerTest.php @@ -6,7 +6,6 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use React\EventLoop\Factory; use React\Http\Io\MiddlewareRunner; use React\Http\Message\ServerRequest; use React\Promise; @@ -162,7 +161,7 @@ public function testProcessStack(array $middlewares, $expectedCallCount) $response = $middlewareStack($request); $this->assertTrue($response instanceof PromiseInterface); - $response = Block\await($response, Factory::create()); + $response = Block\await($response); $this->assertTrue($response instanceof ResponseInterface); $this->assertSame(200, $response->getStatusCode()); @@ -229,7 +228,7 @@ function () use ($errorHandler, &$called, $response, $exception) { $request = new ServerRequest('GET', 'https://example.com/'); - $this->assertSame($response, Block\await($runner($request), Factory::create())); + $this->assertSame($response, Block\await($runner($request))); $this->assertSame(1, $retryCalled); $this->assertSame(2, $called); $this->assertSame($exception, $error); diff --git a/tests/Io/StreamingServerTest.php b/tests/Io/StreamingServerTest.php index ccafe338..59cfd118 100644 --- a/tests/Io/StreamingServerTest.php +++ b/tests/Io/StreamingServerTest.php @@ -3,7 +3,7 @@ namespace React\Tests\Http\Io; use Psr\Http\Message\ServerRequestInterface; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Http\Io\StreamingServer; use React\Http\Message\Response; use React\Http\Message\ServerRequest; @@ -48,7 +48,7 @@ public function setUpConnectionMockAndSocket() public function testRequestEventWillNotBeEmittedForIncompleteHeaders() { - $server = new StreamingServer(Factory::create(), $this->expectCallableNever()); + $server = new StreamingServer(Loop::get(), $this->expectCallableNever()); $server->listen($this->socket); $this->socket->emit('connection', array($this->connection)); @@ -60,7 +60,7 @@ public function testRequestEventWillNotBeEmittedForIncompleteHeaders() public function testRequestEventIsEmitted() { - $server = new StreamingServer(Factory::create(), $this->expectCallableOnce()); + $server = new StreamingServer(Loop::get(), $this->expectCallableOnce()); $server->listen($this->socket); $this->socket->emit('connection', array($this->connection)); @@ -75,7 +75,7 @@ public function testRequestEventIsEmitted() public function testRequestEventIsEmittedForArrayCallable() { $this->called = null; - $server = new StreamingServer(Factory::create(), array($this, 'helperCallableOnce')); + $server = new StreamingServer(Loop::get(), array($this, 'helperCallableOnce')); $server->listen($this->socket); $this->socket->emit('connection', array($this->connection)); @@ -95,7 +95,7 @@ public function testRequestEvent() { $i = 0; $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$i, &$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$i, &$requestAssertion) { $i++; $requestAssertion = $request; }); @@ -128,7 +128,7 @@ public function testRequestEventWithSingleRequestHandlerArray() { $i = 0; $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$i, &$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$i, &$requestAssertion) { $i++; $requestAssertion = $request; }); @@ -160,7 +160,7 @@ public function testRequestEventWithSingleRequestHandlerArray() public function testRequestGetWithHostAndCustomPort() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -182,7 +182,7 @@ public function testRequestGetWithHostAndCustomPort() public function testRequestGetWithHostAndHttpsPort() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -204,7 +204,7 @@ public function testRequestGetWithHostAndHttpsPort() public function testRequestGetWithHostAndDefaultPortWillBeIgnored() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -226,7 +226,7 @@ public function testRequestGetWithHostAndDefaultPortWillBeIgnored() public function testRequestGetHttp10WithoutHostWillBeIgnored() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -248,7 +248,7 @@ public function testRequestGetHttp10WithoutHostWillBeIgnored() public function testRequestGetHttp11WithoutHostWillReject() { - $server = new StreamingServer(Factory::create(), 'var_dump'); + $server = new StreamingServer(Loop::get(), 'var_dump'); $server->on('error', $this->expectCallableOnce()); $server->listen($this->socket); @@ -261,7 +261,7 @@ public function testRequestGetHttp11WithoutHostWillReject() public function testRequestOptionsAsterisk() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -281,7 +281,7 @@ public function testRequestOptionsAsterisk() public function testRequestNonOptionsWithAsteriskRequestTargetWillReject() { - $server = new StreamingServer(Factory::create(), $this->expectCallableNever()); + $server = new StreamingServer(Loop::get(), $this->expectCallableNever()); $server->on('error', $this->expectCallableOnce()); $server->listen($this->socket); @@ -294,7 +294,7 @@ public function testRequestNonOptionsWithAsteriskRequestTargetWillReject() public function testRequestConnectAuthorityForm() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -316,7 +316,7 @@ public function testRequestConnectAuthorityForm() public function testRequestConnectWithoutHostWillBePassesAsIs() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -338,7 +338,7 @@ public function testRequestConnectWithoutHostWillBePassesAsIs() public function testRequestConnectAuthorityFormWithDefaultPortWillBePassedAsIs() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -360,7 +360,7 @@ public function testRequestConnectAuthorityFormWithDefaultPortWillBePassedAsIs() public function testRequestConnectAuthorityFormNonMatchingHostWillBePassedAsIs() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -381,7 +381,7 @@ public function testRequestConnectAuthorityFormNonMatchingHostWillBePassedAsIs() public function testRequestConnectOriginFormRequestTargetWillReject() { - $server = new StreamingServer(Factory::create(), $this->expectCallableNever()); + $server = new StreamingServer(Loop::get(), $this->expectCallableNever()); $server->on('error', $this->expectCallableOnce()); $server->listen($this->socket); @@ -393,7 +393,7 @@ public function testRequestConnectOriginFormRequestTargetWillReject() public function testRequestNonConnectWithAuthorityRequestTargetWillReject() { - $server = new StreamingServer(Factory::create(), $this->expectCallableNever()); + $server = new StreamingServer(Loop::get(), $this->expectCallableNever()); $server->on('error', $this->expectCallableOnce()); $server->listen($this->socket); @@ -407,7 +407,7 @@ public function testRequestWithoutHostEventUsesSocketAddress() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -433,7 +433,7 @@ public function testRequestAbsoluteEvent() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -455,7 +455,7 @@ public function testRequestAbsoluteNonMatchingHostWillBePassedAsIs() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -475,7 +475,7 @@ public function testRequestAbsoluteNonMatchingHostWillBePassedAsIs() public function testRequestAbsoluteWithoutHostWillReject() { - $server = new StreamingServer(Factory::create(), $this->expectCallableNever()); + $server = new StreamingServer(Loop::get(), $this->expectCallableNever()); $server->on('error', $this->expectCallableOnce()); $server->listen($this->socket); @@ -489,7 +489,7 @@ public function testRequestOptionsAsteriskEvent() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -511,7 +511,7 @@ public function testRequestOptionsAbsoluteEvent() { $requestAssertion = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestAssertion) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestAssertion) { $requestAssertion = $request; }); @@ -531,7 +531,7 @@ public function testRequestOptionsAbsoluteEvent() public function testRequestPauseWillBeForwardedToConnection() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { $request->getBody()->pause(); }); @@ -551,7 +551,7 @@ public function testRequestPauseWillBeForwardedToConnection() public function testRequestResumeWillBeForwardedToConnection() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { $request->getBody()->resume(); }); @@ -571,7 +571,7 @@ public function testRequestResumeWillBeForwardedToConnection() public function testRequestCloseWillNotCloseConnection() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { $request->getBody()->close(); }); @@ -586,7 +586,7 @@ public function testRequestCloseWillNotCloseConnection() public function testRequestPauseAfterCloseWillNotBeForwarded() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { $request->getBody()->close(); $request->getBody()->pause(); }); @@ -603,7 +603,7 @@ public function testRequestPauseAfterCloseWillNotBeForwarded() public function testRequestResumeAfterCloseWillNotBeForwarded() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { $request->getBody()->close(); $request->getBody()->resume(); }); @@ -622,7 +622,7 @@ public function testRequestEventWithoutBodyWillNotEmitData() { $never = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($never) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($never) { $request->getBody()->on('data', $never); }); @@ -637,7 +637,7 @@ public function testRequestEventWithSecondDataEventWillEmitBodyData() { $once = $this->expectCallableOnceWith('incomplete'); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($once) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($once) { $request->getBody()->on('data', $once); }); @@ -657,7 +657,7 @@ public function testRequestEventWithPartialBodyWillEmitData() { $once = $this->expectCallableOnceWith('incomplete'); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($once) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($once) { $request->getBody()->on('data', $once); }); @@ -678,7 +678,7 @@ public function testRequestEventWithPartialBodyWillEmitData() public function testResponseContainsServerHeader() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response(); }); @@ -708,7 +708,7 @@ public function testResponsePendingPromiseWillNotSendAnything() { $never = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($never) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($never) { return new Promise(function () { }, $never); }); @@ -738,7 +738,7 @@ public function testResponsePendingPromiseWillBeCancelledIfConnectionCloses() { $once = $this->expectCallableOnce(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($once) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($once) { return new Promise(function () { }, $once); }); @@ -770,7 +770,7 @@ public function testResponseBodyStreamAlreadyClosedWillSendEmptyBodyChunkedEncod $stream = new ThroughStream(); $stream->close(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 200, array(), @@ -805,7 +805,7 @@ public function testResponseBodyStreamEndingWillSendEmptyBodyChunkedEncoded() { $stream = new ThroughStream(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 200, array(), @@ -843,7 +843,7 @@ public function testResponseBodyStreamAlreadyClosedWillSendEmptyBodyPlainHttp10( $stream = new ThroughStream(); $stream->close(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 200, array(), @@ -879,7 +879,7 @@ public function testResponseStreamWillBeClosedIfConnectionIsAlreadyClosed() $stream = new ThroughStream(); $stream->on('close', $this->expectCallableOnce()); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 200, array(), @@ -934,7 +934,7 @@ public function testResponseBodyStreamWillBeClosedIfConnectionEmitsCloseEvent() $stream = new ThroughStream(); $stream->on('close', $this->expectCallableOnce()); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 200, array(), @@ -952,7 +952,7 @@ public function testResponseBodyStreamWillBeClosedIfConnectionEmitsCloseEvent() public function testResponseUpgradeInResponseCanBeUsedToAdvertisePossibleUpgrade() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 200, array( @@ -988,7 +988,7 @@ function ($data) use (&$buffer) { public function testResponseUpgradeWishInRequestCanBeIgnoredByReturningNormalResponse() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 200, array( @@ -1023,7 +1023,7 @@ function ($data) use (&$buffer) { public function testResponseUpgradeSwitchingProtocolIncludesConnectionUpgradeHeaderWithoutContentLength() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 101, array( @@ -1063,7 +1063,7 @@ public function testResponseUpgradeSwitchingProtocolWithStreamWillPipeDataToConn { $stream = new ThroughStream(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 101, array( @@ -1104,7 +1104,7 @@ public function testResponseConnectMethodStreamWillPipeDataToConnection() { $stream = new ThroughStream(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 200, array(), @@ -1142,7 +1142,7 @@ public function testResponseConnectMethodStreamWillPipeDataFromConnection() { $stream = new ThroughStream(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 200, array(), @@ -1161,7 +1161,7 @@ public function testResponseConnectMethodStreamWillPipeDataFromConnection() public function testResponseContainsSameRequestProtocolVersionAndChunkedBodyForHttp11() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 200, array(), @@ -1194,7 +1194,7 @@ function ($data) use (&$buffer) { public function testResponseContainsSameRequestProtocolVersionAndRawBodyForHttp10() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 200, array(), @@ -1228,7 +1228,7 @@ function ($data) use (&$buffer) { public function testResponseContainsNoResponseBodyForHeadRequest() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 200, array(), @@ -1264,7 +1264,7 @@ public function testResponseContainsNoResponseBodyForHeadRequestWithStreamingRes $stream = new ThroughStream(); $stream->on('close', $this->expectCallableOnce()); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 200, array('Content-Length' => '3'), @@ -1296,7 +1296,7 @@ function ($data) use (&$buffer) { public function testResponseContainsNoResponseBodyAndNoContentLengthForNoContentStatus() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 204, array(), @@ -1332,7 +1332,7 @@ public function testResponseContainsNoResponseBodyAndNoContentLengthForNoContent $stream = new ThroughStream(); $stream->on('close', $this->expectCallableOnce()); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 204, array('Content-Length' => '3'), @@ -1364,7 +1364,7 @@ function ($data) use (&$buffer) { public function testResponseContainsNoContentLengthHeaderForNotModifiedStatus() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 304, array(), @@ -1396,7 +1396,7 @@ function ($data) use (&$buffer) { public function testResponseContainsExplicitContentLengthHeaderForNotModifiedStatus() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 304, array('Content-Length' => 3), @@ -1428,7 +1428,7 @@ function ($data) use (&$buffer) { public function testResponseContainsNoResponseBodyForNotModifiedStatus() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 304, array(), @@ -1464,7 +1464,7 @@ public function testResponseContainsNoResponseBodyForNotModifiedStatusWithStream $stream = new ThroughStream(); $stream->on('close', $this->expectCallableOnce()); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 304, array('Content-Length' => '3'), @@ -1497,7 +1497,7 @@ function ($data) use (&$buffer) { public function testRequestInvalidHttpProtocolVersionWillEmitErrorAndSendErrorResponse() { $error = null; - $server = new StreamingServer(Factory::create(), $this->expectCallableNever()); + $server = new StreamingServer(Loop::get(), $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -1531,7 +1531,7 @@ function ($data) use (&$buffer) { public function testRequestOverflowWillEmitErrorAndSendErrorResponse() { $error = null; - $server = new StreamingServer(Factory::create(), $this->expectCallableNever()); + $server = new StreamingServer(Loop::get(), $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -1565,7 +1565,7 @@ function ($data) use (&$buffer) { public function testRequestInvalidWillEmitErrorAndSendErrorResponse() { $error = null; - $server = new StreamingServer(Factory::create(), $this->expectCallableNever()); + $server = new StreamingServer(Loop::get(), $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -1602,7 +1602,7 @@ public function testRequestContentLengthBodyDataWillEmitDataEventOnRequestStream $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1630,7 +1630,7 @@ public function testRequestChunkedTransferEncodingRequestWillEmitDecodedDataEven $errorEvent = $this->expectCallableNever(); $requestValidation = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1661,7 +1661,7 @@ public function testRequestChunkedTransferEncodingWithAdditionalDataWontBeEmitte $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1690,7 +1690,7 @@ public function testRequestChunkedTransferEncodingEmpty() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1718,7 +1718,7 @@ public function testRequestChunkedTransferEncodingHeaderCanBeUpperCase() $errorEvent = $this->expectCallableNever(); $requestValidation = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1748,7 +1748,7 @@ public function testRequestChunkedTransferEncodingCanBeMixedUpperAndLowerCase() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1775,7 +1775,7 @@ public function testRequestContentLengthWillEmitDataEventAndEndEventAndAdditiona $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1806,7 +1806,7 @@ public function testRequestContentLengthWillEmitDataEventAndEndEventAndAdditiona $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1838,7 +1838,7 @@ public function testRequestZeroContentLengthWillEmitEndEvent() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1864,7 +1864,7 @@ public function testRequestZeroContentLengthWillEmitEndAndAdditionalDataWillBeIg $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1891,7 +1891,7 @@ public function testRequestZeroContentLengthWillEmitEndAndAdditionalDataWillBeIg $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -1917,7 +1917,7 @@ public function testRequestZeroContentLengthWillEmitEndAndAdditionalDataWillBeIg public function testRequestInvalidChunkHeaderTooLongWillEmitErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); - $server = new StreamingServer(Factory::create(), function ($request) use ($errorEvent){ + $server = new StreamingServer(Loop::get(), function ($request) use ($errorEvent){ $request->getBody()->on('error', $errorEvent); return \React\Promise\resolve(new Response()); }); @@ -1942,7 +1942,7 @@ public function testRequestInvalidChunkHeaderTooLongWillEmitErrorOnRequestStream public function testRequestInvalidChunkBodyTooLongWillEmitErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); - $server = new StreamingServer(Factory::create(), function ($request) use ($errorEvent){ + $server = new StreamingServer(Loop::get(), function ($request) use ($errorEvent){ $request->getBody()->on('error', $errorEvent); }); @@ -1964,7 +1964,7 @@ public function testRequestInvalidChunkBodyTooLongWillEmitErrorOnRequestStream() public function testRequestUnexpectedEndOfRequestWithChunkedTransferConnectionWillEmitErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); - $server = new StreamingServer(Factory::create(), function ($request) use ($errorEvent){ + $server = new StreamingServer(Loop::get(), function ($request) use ($errorEvent){ $request->getBody()->on('error', $errorEvent); }); @@ -1987,7 +1987,7 @@ public function testRequestUnexpectedEndOfRequestWithChunkedTransferConnectionWi public function testRequestInvalidChunkHeaderWillEmitErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); - $server = new StreamingServer(Factory::create(), function ($request) use ($errorEvent){ + $server = new StreamingServer(Loop::get(), function ($request) use ($errorEvent){ $request->getBody()->on('error', $errorEvent); }); @@ -2009,7 +2009,7 @@ public function testRequestInvalidChunkHeaderWillEmitErrorOnRequestStream() public function testRequestUnexpectedEndOfRequestWithContentLengthWillEmitErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); - $server = new StreamingServer(Factory::create(), function ($request) use ($errorEvent){ + $server = new StreamingServer(Loop::get(), function ($request) use ($errorEvent){ $request->getBody()->on('error', $errorEvent); }); @@ -2036,7 +2036,7 @@ public function testRequestWithoutBodyWillEmitEndOnRequestStream() $endEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function ($request) use ($dataEvent, $closeEvent, $endEvent, $errorEvent){ + $server = new StreamingServer(Loop::get(), function ($request) use ($dataEvent, $closeEvent, $endEvent, $errorEvent){ $request->getBody()->on('data', $dataEvent); $request->getBody()->on('close', $closeEvent); $request->getBody()->on('end', $endEvent); @@ -2060,7 +2060,7 @@ public function testRequestWithoutDefinedLengthWillIgnoreDataEvent() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { $request->getBody()->on('data', $dataEvent); $request->getBody()->on('end', $endEvent); $request->getBody()->on('close', $closeEvent); @@ -2079,7 +2079,7 @@ public function testRequestWithoutDefinedLengthWillIgnoreDataEvent() public function testResponseWithBodyStreamWillUseChunkedTransferEncodingByDefault() { $stream = new ThroughStream(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 200, array(), @@ -2113,7 +2113,7 @@ function ($data) use (&$buffer) { public function testResponseWithBodyStringWillOverwriteExplicitContentLengthAndTransferEncoding() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 200, array( @@ -2154,7 +2154,7 @@ public function testResponseContainsResponseBodyWithTransferEncodingChunkedForBo $body->expects($this->once())->method('getSize')->willReturn(null); $body->expects($this->once())->method('__toString')->willReturn('body'); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($body) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($body) { return new Response( 200, array(), @@ -2191,7 +2191,7 @@ public function testResponseContainsResponseBodyWithPlainBodyWithUnknownSizeForL $body->expects($this->once())->method('getSize')->willReturn(null); $body->expects($this->once())->method('__toString')->willReturn('body'); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($body) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($body) { return new Response( 200, array(), @@ -2225,7 +2225,7 @@ function ($data) use (&$buffer) { public function testResponseWithCustomTransferEncodingWillBeIgnoredAndUseChunkedTransferEncodingInstead() { $stream = new ThroughStream(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($stream) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($stream) { return new Response( 200, array( @@ -2262,7 +2262,7 @@ function ($data) use (&$buffer) { public function testResponseWithoutExplicitDateHeaderWillAddCurrentDate() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response(); }); @@ -2292,7 +2292,7 @@ function ($data) use (&$buffer) { public function testResponseWIthCustomDateHeaderOverwritesDefault() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 200, array("Date" => "Tue, 15 Nov 1994 08:12:31 GMT") @@ -2325,7 +2325,7 @@ function ($data) use (&$buffer) { public function testResponseWithEmptyDateHeaderRemovesDateHeader() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 200, array('Date' => '') @@ -2358,7 +2358,7 @@ function ($data) use (&$buffer) { public function testResponseCanContainMultipleCookieHeaders() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 200, array( @@ -2396,7 +2396,7 @@ function ($data) use (&$buffer) { public function testReponseWithExpectContinueRequestContainsContinueWithLaterResponse() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response(); }); @@ -2428,7 +2428,7 @@ function ($data) use (&$buffer) { public function testResponseWithExpectContinueRequestWontSendContinueForHttp10() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response(); }); @@ -2459,14 +2459,14 @@ function ($data) use (&$buffer) { public function testInvalidCallbackFunctionLeadsToException() { $this->setExpectedException('InvalidArgumentException'); - $server = new StreamingServer(Factory::create(), 'invalid'); + $server = new StreamingServer(Loop::get(), 'invalid'); } public function testResponseBodyStreamWillStreamDataWithChunkedTransferEncoding() { $input = new ThroughStream(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($input) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($input) { return new Response( 200, array(), @@ -2505,7 +2505,7 @@ public function testResponseBodyStreamWithContentLengthWillStreamTillLengthWitho { $input = new ThroughStream(); - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use ($input) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use ($input) { return new Response( 200, array('Content-Length' => 5), @@ -2543,7 +2543,7 @@ function ($data) use (&$buffer) { public function testResponseWithResponsePromise() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return \React\Promise\resolve(new Response()); }); @@ -2571,7 +2571,7 @@ function ($data) use (&$buffer) { public function testResponseReturnInvalidTypeWillResultInError() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return "invalid"; }); @@ -2605,7 +2605,7 @@ function ($data) use (&$buffer) { public function testResponseResolveWrongTypeInPromiseWillResultInError() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return \React\Promise\resolve("invalid"); }); @@ -2633,7 +2633,7 @@ function ($data) use (&$buffer) { public function testResponseRejectedPromiseWillResultInErrorMessage() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Promise(function ($resolve, $reject) { $reject(new \Exception()); }); @@ -2664,7 +2664,7 @@ function ($data) use (&$buffer) { public function testResponseExceptionInCallbackWillResultInErrorMessage() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Promise(function ($resolve, $reject) { throw new \Exception('Bad call'); }); @@ -2695,7 +2695,7 @@ function ($data) use (&$buffer) { public function testResponseWithContentLengthHeaderForStringBodyOverwritesTransferEncoding() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response( 200, array('Transfer-Encoding' => 'chunked'), @@ -2731,7 +2731,7 @@ function ($data) use (&$buffer) { public function testResponseWillBeHandled() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Response(); }); @@ -2759,7 +2759,7 @@ function ($data) use (&$buffer) { public function testResponseExceptionThrowInCallBackFunctionWillResultInErrorMessage() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { throw new \Exception('hello'); }); @@ -2797,7 +2797,7 @@ function ($data) use (&$buffer) { */ public function testResponseThrowableThrowInCallBackFunctionWillResultInErrorMessage() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { throw new \Error('hello'); }); @@ -2840,7 +2840,7 @@ function ($data) use (&$buffer) { public function testResponseRejectOfNonExceptionWillResultInErrorMessage() { - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) { return new Promise(function ($resolve, $reject) { $reject('Invalid type'); }); @@ -2877,7 +2877,7 @@ function ($data) use (&$buffer) { public function testRequestServerRequestParams() { $requestValidation = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestValidation) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestValidation) { $requestValidation = $request; }); @@ -2911,7 +2911,7 @@ public function testRequestServerRequestParams() public function testRequestQueryParametersWillBeAddedToRequest() { $requestValidation = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestValidation) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestValidation) { $requestValidation = $request; }); @@ -2931,7 +2931,7 @@ public function testRequestQueryParametersWillBeAddedToRequest() public function testRequestCookieWillBeAddedToServerRequest() { $requestValidation = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestValidation) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestValidation) { $requestValidation = $request; }); @@ -2952,7 +2952,7 @@ public function testRequestCookieWillBeAddedToServerRequest() public function testRequestInvalidMultipleCookiesWontBeAddedToServerRequest() { $requestValidation = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestValidation) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestValidation) { $requestValidation = $request; }); @@ -2973,7 +2973,7 @@ public function testRequestInvalidMultipleCookiesWontBeAddedToServerRequest() public function testRequestCookieWithSeparatorWillBeAddedToServerRequest() { $requestValidation = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestValidation) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestValidation) { $requestValidation = $request; }); @@ -2992,7 +2992,7 @@ public function testRequestCookieWithSeparatorWillBeAddedToServerRequest() public function testRequestCookieWithCommaValueWillBeAddedToServerRequest() { $requestValidation = null; - $server = new StreamingServer(Factory::create(), function (ServerRequestInterface $request) use (&$requestValidation) { + $server = new StreamingServer(Loop::get(), function (ServerRequestInterface $request) use (&$requestValidation) { $requestValidation = $request; }); @@ -3011,7 +3011,7 @@ public function testRequestCookieWithCommaValueWillBeAddedToServerRequest() { public function testNewConnectionWillInvokeParserOnce() { - $server = new StreamingServer(Factory::create(), $this->expectCallableNever()); + $server = new StreamingServer(Loop::get(), $this->expectCallableNever()); $parser = $this->getMockBuilder('React\Http\Io\RequestHeaderParser')->getMock(); $parser->expects($this->once())->method('handle'); @@ -3028,7 +3028,7 @@ public function testNewConnectionWillInvokeParserOnceAndInvokeRequestHandlerWhen { $request = new ServerRequest('GET', 'http://localhost/', array(), '', '1.0'); - $server = new StreamingServer(Factory::create(), $this->expectCallableOnceWith($request)); + $server = new StreamingServer(Loop::get(), $this->expectCallableOnceWith($request)); $parser = $this->getMockBuilder('React\Http\Io\RequestHeaderParser')->getMock(); $parser->expects($this->once())->method('handle'); @@ -3051,7 +3051,7 @@ public function testNewConnectionWillInvokeParserOnceAndInvokeRequestHandlerWhen { $request = new ServerRequest('GET', 'http://localhost/', array('Connection' => 'close')); - $server = new StreamingServer(Factory::create(), $this->expectCallableOnceWith($request)); + $server = new StreamingServer(Loop::get(), $this->expectCallableOnceWith($request)); $parser = $this->getMockBuilder('React\Http\Io\RequestHeaderParser')->getMock(); $parser->expects($this->once())->method('handle'); @@ -3074,7 +3074,7 @@ public function testNewConnectionWillInvokeParserOnceAndInvokeRequestHandlerWhen { $request = new ServerRequest('GET', 'http://localhost/'); - $server = new StreamingServer(Factory::create(), function () { + $server = new StreamingServer(Loop::get(), function () { return new Response(200, array('Connection' => 'close')); }); @@ -3099,7 +3099,7 @@ public function testNewConnectionWillInvokeParserTwiceAfterInvokingRequestHandle { $request = new ServerRequest('GET', 'http://localhost/'); - $server = new StreamingServer(Factory::create(), function () { + $server = new StreamingServer(Loop::get(), function () { return new Response(); }); @@ -3124,7 +3124,7 @@ public function testNewConnectionWillInvokeParserTwiceAfterInvokingRequestHandle { $request = new ServerRequest('GET', 'http://localhost/', array('Connection' => 'keep-alive'), '', '1.0'); - $server = new StreamingServer(Factory::create(), function () { + $server = new StreamingServer(Loop::get(), function () { return new Response(); }); @@ -3150,7 +3150,7 @@ public function testNewConnectionWillInvokeParserOnceAfterInvokingRequestHandler $request = new ServerRequest('GET', 'http://localhost/'); $body = new ThroughStream(); - $server = new StreamingServer(Factory::create(), function () use ($body) { + $server = new StreamingServer(Loop::get(), function () use ($body) { return new Response(200, array(), $body); }); @@ -3176,7 +3176,7 @@ public function testNewConnectionWillInvokeParserTwiceAfterInvokingRequestHandle $request = new ServerRequest('GET', 'http://localhost/'); $body = new ThroughStream(); - $server = new StreamingServer(Factory::create(), function () use ($body) { + $server = new StreamingServer(Loop::get(), function () use ($body) { return new Response(200, array(), $body); }); diff --git a/tests/Io/TransactionTest.php b/tests/Io/TransactionTest.php index 12e128cc..d62147b5 100644 --- a/tests/Io/TransactionTest.php +++ b/tests/Io/TransactionTest.php @@ -8,7 +8,7 @@ use RingCentral\Psr7\Response; use React\Http\Io\Transaction; use React\Http\Message\ResponseException; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Promise; use React\Promise\Deferred; use React\Stream\ThroughStream; @@ -383,10 +383,8 @@ public function testReceivingErrorResponseWillRejectWithResponseException() public function testReceivingStreamingBodyWillResolveWithBufferedResponseByDefault() { - $loop = Factory::create(); - $stream = new ThroughStream(); - $loop->addTimer(0.001, function () use ($stream) { + Loop::addTimer(0.001, function () use ($stream) { $stream->emit('data', array('hello world')); $stream->close(); }); @@ -398,10 +396,10 @@ public function testReceivingStreamingBodyWillResolveWithBufferedResponseByDefau $sender = $this->makeSenderMock(); $sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn(Promise\resolve($response)); - $transaction = new Transaction($sender, $loop); + $transaction = new Transaction($sender, Loop::get()); $promise = $transaction->send($request); - $response = Block\await($promise, $loop); + $response = Block\await($promise); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('hello world', (string)$response->getBody()); @@ -409,8 +407,6 @@ public function testReceivingStreamingBodyWillResolveWithBufferedResponseByDefau public function testReceivingStreamingBodyWithSizeExceedingMaximumResponseBufferWillRejectAndCloseResponseStream() { - $loop = Factory::create(); - $stream = new ThroughStream(); $stream->on('close', $this->expectCallableOnce()); @@ -422,17 +418,15 @@ public function testReceivingStreamingBodyWithSizeExceedingMaximumResponseBuffer $sender = $this->makeSenderMock(); $sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn(Promise\resolve($response)); - $transaction = new Transaction($sender, $loop); + $transaction = new Transaction($sender, Loop::get()); $promise = $transaction->send($request); $this->setExpectedException('OverflowException'); - Block\await($promise, $loop, 0.001); + Block\await($promise, null, 0.001); } public function testCancelBufferingResponseWillCloseStreamAndReject() { - $loop = Factory::create(); - $stream = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock(); $stream->expects($this->any())->method('isReadable')->willReturn(true); $stream->expects($this->once())->method('close'); @@ -444,12 +438,12 @@ public function testCancelBufferingResponseWillCloseStreamAndReject() $sender = $this->makeSenderMock(); $sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn(Promise\resolve($response)); - $transaction = new Transaction($sender, $loop); + $transaction = new Transaction($sender, Loop::get()); $promise = $transaction->send($request); $promise->cancel(); $this->setExpectedException('RuntimeException'); - Block\await($promise, $loop, 0.001); + Block\await($promise, null, 0.001); } public function testReceivingStreamingBodyWillResolveWithStreamingResponseIfStreamingIsEnabled() diff --git a/tests/Middleware/RequestBodyBufferMiddlewareTest.php b/tests/Middleware/RequestBodyBufferMiddlewareTest.php index 9889f501..e073e1f0 100644 --- a/tests/Middleware/RequestBodyBufferMiddlewareTest.php +++ b/tests/Middleware/RequestBodyBufferMiddlewareTest.php @@ -4,7 +4,7 @@ use Clue\React\Block; use Psr\Http\Message\ServerRequestInterface; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Http\Io\HttpBodyStream; use React\Http\Message\Response; use React\Http\Message\ServerRequest; @@ -118,8 +118,6 @@ function (ServerRequestInterface $request) use (&$exposedRequest) { public function testKnownExcessiveSizedBodyIsDisgardedTheRequestIsPassedDownToTheNextMiddleware() { - $loop = Factory::create(); - $stream = new ThroughStream(); $stream->end('aa'); $serverRequest = new ServerRequest( @@ -135,7 +133,7 @@ public function testKnownExcessiveSizedBodyIsDisgardedTheRequestIsPassedDownToTh function (ServerRequestInterface $request) { return new Response(200, array(), $request->getBody()->getContents()); } - ), $loop); + )); $this->assertSame(200, $response->getStatusCode()); $this->assertSame('', $response->getBody()->getContents()); @@ -143,10 +141,8 @@ function (ServerRequestInterface $request) { public function testKnownExcessiveSizedWithIniLikeSize() { - $loop = Factory::create(); - $stream = new ThroughStream(); - $loop->addTimer(0.001, function () use ($stream) { + Loop::addTimer(0.001, function () use ($stream) { $stream->end(str_repeat('a', 2048)); }); $serverRequest = new ServerRequest( @@ -162,7 +158,7 @@ public function testKnownExcessiveSizedWithIniLikeSize() function (ServerRequestInterface $request) { return new Response(200, array(), $request->getBody()->getContents()); } - ), $loop); + )); $this->assertSame(200, $response->getStatusCode()); $this->assertSame('', $response->getBody()->getContents()); @@ -192,8 +188,6 @@ function (ServerRequestInterface $request) use (&$exposedRequest) { public function testExcessiveSizeBodyIsDiscardedAndTheRequestIsPassedDownToTheNextMiddleware() { - $loop = Factory::create(); - $stream = new ThroughStream(); $serverRequest = new ServerRequest( 'GET', @@ -215,7 +209,7 @@ function (ServerRequestInterface $request) { $exposedResponse = Block\await($promise->then( null, $this->expectCallableNever() - ), $loop); + )); $this->assertSame(200, $exposedResponse->getStatusCode()); $this->assertSame('', $exposedResponse->getBody()->getContents()); @@ -223,8 +217,6 @@ function (ServerRequestInterface $request) { public function testBufferingErrorThrows() { - $loop = Factory::create(); - $stream = new ThroughStream(); $serverRequest = new ServerRequest( 'GET', @@ -244,7 +236,7 @@ function (ServerRequestInterface $request) { $stream->emit('error', array(new \RuntimeException())); $this->setExpectedException('RuntimeException'); - Block\await($promise, $loop); + Block\await($promise); } public function testFullBodyStreamedBeforeCallingNextMiddleware()