diff --git a/examples/01-hello-world.php b/examples/01-hello-world.php index d44634ed..49c12664 100644 --- a/examples/01-hello-world.php +++ b/examples/01-hello-world.php @@ -2,15 +2,15 @@ use React\EventLoop\Factory; use React\Socket\Server; -use React\Http\Request; use React\Http\Response; +use Psr\Http\Message\RequestInterface; require __DIR__ . '/../vendor/autoload.php'; $loop = Factory::create(); $socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop); -$server = new \React\Http\Server($socket, function (Request $request, Response $response) { +$server = new \React\Http\Server($socket, function (RequestInterface $request, Response $response) { $response->writeHead(200, array('Content-Type' => 'text/plain')); $response->end("Hello world!\n"); }); diff --git a/examples/02-hello-world-https.php b/examples/02-hello-world-https.php index 6ca7e809..29e5aab2 100644 --- a/examples/02-hello-world-https.php +++ b/examples/02-hello-world-https.php @@ -2,9 +2,9 @@ use React\EventLoop\Factory; use React\Socket\Server; -use React\Http\Request; use React\Http\Response; use React\Socket\SecureServer; +use Psr\Http\Message\RequestInterface; require __DIR__ . '/../vendor/autoload.php'; @@ -14,7 +14,7 @@ 'local_cert' => isset($argv[2]) ? $argv[2] : __DIR__ . '/localhost.pem' )); -$server = new \React\Http\Server($socket, function (Request $request, Response $response) { +$server = new \React\Http\Server($socket, function (RequestInterface $request, Response $response) { $response->writeHead(200, array('Content-Type' => 'text/plain')); $response->end("Hello world!\n"); }); diff --git a/examples/03-handling-body-data.php b/examples/03-handling-body-data.php index a016b66e..9bd26c1e 100644 --- a/examples/03-handling-body-data.php +++ b/examples/03-handling-body-data.php @@ -2,27 +2,27 @@ use React\EventLoop\Factory; use React\Socket\Server; -use React\Http\Request; use React\Http\Response; +use Psr\Http\Message\RequestInterface; require __DIR__ . '/../vendor/autoload.php'; $loop = Factory::create(); $socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop); -$server = new \React\Http\Server($socket, function (Request $request, Response $response) { +$server = new \React\Http\Server($socket, function (RequestInterface $request, Response $response) { $contentLength = 0; - $request->on('data', function ($data) use (&$contentLength) { + $request->getBody()->on('data', function ($data) use (&$contentLength) { $contentLength += strlen($data); }); - $request->on('end', function () use ($response, &$contentLength){ + $request->getBody()->on('end', function () use ($response, &$contentLength){ $response->writeHead(200, array('Content-Type' => 'text/plain')); $response->end("The length of the submitted request body is: " . $contentLength); }); // an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event - $request->on('error', function (\Exception $exception) use ($response, &$contentLength) { + $request->getBody()->on('error', function (\Exception $exception) use ($response, &$contentLength) { $response->writeHead(400, array('Content-Type' => 'text/plain')); $response->end("An error occured while reading at length: " . $contentLength); }); diff --git a/src/HttpBodyStream.php b/src/HttpBodyStream.php new file mode 100644 index 00000000..83dde2cf --- /dev/null +++ b/src/HttpBodyStream.php @@ -0,0 +1,173 @@ +input = $input; + $this->size = $size; + + $this->input->on('data', array($this, 'handleData')); + $this->input->on('end', array($this, 'handleEnd')); + $this->input->on('error', array($this, 'handleError')); + $this->input->on('close', array($this, 'close')); + } + + public function isReadable() + { + return !$this->closed && $this->input->isReadable(); + } + + public function pause() + { + $this->input->pause(); + } + + public function resume() + { + $this->input->resume(); + } + + public function pipe(WritableStreamInterface $dest, array $options = array()) + { + Util::pipe($this, $dest, $options); + + return $dest; + } + + public function close() + { + if ($this->closed) { + return; + } + + $this->closed = true; + + $this->input->close(); + + $this->emit('close'); + $this->removeAllListeners(); + } + + public function getSize() + { + return $this->size; + } + + /** @ignore */ + public function __toString() + { + return ''; + } + + /** @ignore */ + public function detach() + { + return null; + } + + /** @ignore */ + public function tell() + { + throw new \BadMethodCallException(); + } + + /** @ignore */ + public function eof() + { + throw new \BadMethodCallException(); + } + + /** @ignore */ + public function isSeekable() + { + return false; + } + + /** @ignore */ + public function seek($offset, $whence = SEEK_SET) + { + throw new \BadMethodCallException(); + } + + /** @ignore */ + public function rewind() + { + throw new \BadMethodCallException(); + } + + /** @ignore */ + public function isWritable() + { + return false; + } + + /** @ignore */ + public function write($string) + { + throw new \BadMethodCallException(); + } + + /** @ignore */ + public function read($length) + { + throw new \BadMethodCallException(); + } + + /** @ignore */ + public function getContents() + { + return ''; + } + + /** @ignore */ + public function getMetadata($key = null) + { + return null; + } + + /** @internal */ + public function handleData($data) + { + $this->emit('data', array($data)); + } + + /** @internal */ + public function handleError(\Exception $e) + { + $this->emit('error', array($e)); + $this->close(); + } + + /** @internal */ + public function handleEnd() + { + if (!$this->closed) { + $this->emit('end'); + $this->close(); + } + } +} diff --git a/src/Request.php b/src/Request.php deleted file mode 100644 index 05df869e..00000000 --- a/src/Request.php +++ /dev/null @@ -1,194 +0,0 @@ -request = $request; - $this->stream = $stream; - - $that = $this; - // forward data and end events from body stream to request - $stream->on('data', function ($data) use ($that) { - $that->emit('data', array($data)); - }); - $stream->on('end', function () use ($that) { - $that->emit('end'); - }); - $stream->on('error', function ($error) use ($that) { - $that->emit('error', array($error)); - }); - $stream->on('close', array($this, 'close')); - } - - /** - * Returns the request method - * - * @return string - */ - public function getMethod() - { - return $this->request->getMethod(); - } - - /** - * Returns the request path - * - * @return string - */ - public function getPath() - { - return $this->request->getUri()->getPath(); - } - - /** - * Returns an array with all query parameters ($_GET) - * - * @return array - */ - public function getQueryParams() - { - $params = array(); - parse_str($this->request->getUri()->getQuery(), $params); - - return $params; - } - - /** - * Returns the HTTP protocol version (such as "1.0" or "1.1") - * - * @return string - */ - public function getProtocolVersion() - { - return $this->request->getProtocolVersion(); - } - - /** - * Returns an array with ALL headers - * - * The keys represent the header name in the exact case in which they were - * originally specified. The values will be an array of strings for each - * value for the respective header name. - * - * @return array - */ - public function getHeaders() - { - return $this->request->getHeaders(); - } - - /** - * Retrieves a message header value by the given case-insensitive name. - * - * @param string $name - * @return string[] a list of all values for this header name or an empty array if header was not found - */ - public function getHeader($name) - { - return $this->request->getHeader($name); - } - - /** - * Retrieves a comma-separated string of the values for a single header. - * - * @param string $name - * @return string a comma-separated list of all values for this header name or an empty string if header was not found - */ - public function getHeaderLine($name) - { - return $this->request->getHeaderLine($name); - } - - /** - * Checks if a header exists by the given case-insensitive name. - * - * @param string $name - * @return bool - */ - public function hasHeader($name) - { - return $this->request->hasHeader($name); - } - - public function isReadable() - { - return $this->readable; - } - - public function pause() - { - if (!$this->readable) { - return; - } - - $this->stream->pause(); - } - - public function resume() - { - if (!$this->readable) { - return; - } - - $this->stream->resume(); - } - - public function close() - { - if (!$this->readable) { - return; - } - - $this->readable = false; - $this->stream->close(); - - $this->emit('close'); - $this->removeAllListeners(); - } - - public function pipe(WritableStreamInterface $dest, array $options = array()) - { - Util::pipe($this, $dest, $options); - - return $dest; - } -} diff --git a/src/Server.php b/src/Server.php index e35f833b..bd3faebf 100644 --- a/src/Server.php +++ b/src/Server.php @@ -213,7 +213,7 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque $stream = new LengthLimitedStream($stream, $contentLength); } - $request = new Request($request, $stream); + $request = $request->withBody(new HttpBodyStream($stream, $contentLength)); if ($request->getProtocolVersion() !== '1.0' && '100-continue' === strtolower($request->getHeaderLine('Expect'))) { $conn->write("HTTP/1.1 100 Continue\r\n\r\n"); diff --git a/tests/HttpBodyStreamTest.php b/tests/HttpBodyStreamTest.php new file mode 100644 index 00000000..9817384a --- /dev/null +++ b/tests/HttpBodyStreamTest.php @@ -0,0 +1,187 @@ +input = new ReadableStream(); + $this->bodyStream = new HttpBodyStream($this->input, null); + } + + public function testDataEmit() + { + $this->bodyStream->on('data', $this->expectCallableOnce(array("hello"))); + $this->input->emit('data', array("hello")); + } + + public function testPauseStream() + { + $input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock(); + $input->expects($this->once())->method('pause'); + + $bodyStream = new HttpBodyStream($input, null); + $bodyStream->pause(); + } + + public function testResumeStream() + { + $input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock(); + $input->expects($this->once())->method('pause'); + + $bodyStream = new HttpBodyStream($input, null); + $bodyStream->pause(); + $bodyStream->resume(); + } + + public function testPipeStream() + { + $dest = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock(); + + $ret = $this->bodyStream->pipe($dest); + + $this->assertSame($dest, $ret); + } + + public function testHandleClose() + { + $this->bodyStream->on('close', $this->expectCallableOnce()); + + $this->input->close(); + $this->input->emit('end', array()); + + $this->assertFalse($this->bodyStream->isReadable()); + } + + public function testStopDataEmittingAfterClose() + { + $bodyStream = new HttpBodyStream($this->input, null); + $bodyStream->on('close', $this->expectCallableOnce()); + $this->bodyStream->on('data', $this->expectCallableOnce(array("hello"))); + + $this->input->emit('data', array("hello")); + $bodyStream->close(); + $this->input->emit('data', array("world")); + } + + public function testHandleError() + { + $this->bodyStream->on('error', $this->expectCallableOnce()); + $this->bodyStream->on('close', $this->expectCallableOnce()); + + $this->input->emit('error', array(new \RuntimeException())); + + $this->assertFalse($this->bodyStream->isReadable()); + } + + public function testToString() + { + $this->assertEquals('', $this->bodyStream->__toString()); + } + + public function testDetach() + { + $this->assertEquals(null, $this->bodyStream->detach()); + } + + public function testGetSizeDefault() + { + $this->assertEquals(null, $this->bodyStream->getSize()); + } + + public function testGetSizeCustom() + { + $stream = new HttpBodyStream($this->input, 5); + $this->assertEquals(5, $stream->getSize()); + } + + /** + * @expectedException BadMethodCallException + */ + public function testTell() + { + $this->bodyStream->tell(); + } + + /** + * @expectedException BadMethodCallException + */ + public function testEof() + { + $this->bodyStream->eof(); + } + + public function testIsSeekable() + { + $this->assertFalse($this->bodyStream->isSeekable()); + } + + /** + * @expectedException BadMethodCallException + */ + public function testWrite() + { + $this->bodyStream->write(''); + } + + /** + * @expectedException BadMethodCallException + */ + public function testRead() + { + $this->bodyStream->read(''); + } + + public function testGetContents() + { + $this->assertEquals('', $this->bodyStream->getContents()); + } + + public function testGetMetaData() + { + $this->assertEquals(null, $this->bodyStream->getMetadata()); + } + + public function testIsReadable() + { + $this->assertTrue($this->bodyStream->isReadable()); + } + + public function testPause() + { + $this->bodyStream->pause(); + } + + public function testResume() + { + $this->bodyStream->resume(); + } + + /** + * @expectedException BadMethodCallException + */ + public function testSeek() + { + $this->bodyStream->seek(''); + } + + /** + * @expectedException BadMethodCallException + */ + public function testRewind() + { + $this->bodyStream->rewind(); + } + + public function testIsWriteable() + { + $this->assertFalse($this->bodyStream->isWritable()); + } +} diff --git a/tests/RequestTest.php b/tests/RequestTest.php deleted file mode 100644 index bafb17ac..00000000 --- a/tests/RequestTest.php +++ /dev/null @@ -1,116 +0,0 @@ -stream = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock(); - } - - public function testEmptyHeader() - { - $request = new Request(new Psr('GET', '/', array()), $this->stream); - - $this->assertEquals(array(), $request->getHeaders()); - $this->assertFalse($request->hasHeader('Test')); - $this->assertEquals(array(), $request->getHeader('Test')); - $this->assertEquals('', $request->getHeaderLine('Test')); - } - - public function testHeaderIsCaseInsensitive() - { - $request = new Request(new Psr('GET', '/', array( - 'TEST' => array('Yes'), - )), $this->stream); - - $this->assertEquals(array('TEST' => array('Yes')), $request->getHeaders()); - $this->assertTrue($request->hasHeader('Test')); - $this->assertEquals(array('Yes'), $request->getHeader('Test')); - $this->assertEquals('Yes', $request->getHeaderLine('Test')); - } - - public function testHeaderWithMultipleValues() - { - $request = new Request(new Psr('GET', '/', array( - 'Test' => array('a', 'b'), - )), $this->stream); - - $this->assertEquals(array('Test' => array('a', 'b')), $request->getHeaders()); - $this->assertTrue($request->hasHeader('Test')); - $this->assertEquals(array('a', 'b'), $request->getHeader('Test')); - $this->assertEquals('a, b', $request->getHeaderLine('Test')); - } - - public function testCloseEmitsCloseEvent() - { - $request = new Request(new Psr('GET', '/'), $this->stream); - - $request->on('close', $this->expectCallableOnce()); - - $request->close(); - } - - public function testCloseMultipleTimesEmitsCloseEventOnce() - { - $request = new Request(new Psr('GET', '/'), $this->stream); - - $request->on('close', $this->expectCallableOnce()); - - $request->close(); - $request->close(); - } - - public function testCloseWillCloseUnderlyingStream() - { - $this->stream->expects($this->once())->method('close'); - - $request = new Request(new Psr('GET', '/'), $this->stream); - - $request->close(); - } - - public function testIsNotReadableAfterClose() - { - $request = new Request(new Psr('GET', '/'), $this->stream); - - $request->close(); - - $this->assertFalse($request->isReadable()); - } - - public function testPauseWillBeForwarded() - { - $this->stream->expects($this->once())->method('pause'); - - $request = new Request(new Psr('GET', '/'), $this->stream); - - $request->pause(); - } - - public function testResumeWillBeForwarded() - { - $this->stream->expects($this->once())->method('resume'); - - $request = new Request(new Psr('GET', '/'), $this->stream); - - $request->resume(); - } - - public function testPipeReturnsDest() - { - $dest = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock(); - - $request = new Request(new Psr('GET', '/'), $this->stream); - - $ret = $request->pipe($dest); - - $this->assertSame($dest, $ret); - } -} diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 72cdf5bc..40434f18 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -4,7 +4,7 @@ use React\Http\Server; use React\Http\Response; -use React\Http\Request; +use Psr\Http\Message\RequestInterface; class ServerTest extends TestCase { @@ -78,9 +78,8 @@ public function testRequestEvent() $this->connection->emit('data', array($data)); $this->assertSame(1, $i); - $this->assertInstanceOf('React\Http\Request', $requestAssertion); - $this->assertSame('/', $requestAssertion->getPath()); - $this->assertSame(array(), $requestAssertion->getQueryParams()); + $this->assertInstanceOf('RingCentral\Psr7\Request', $requestAssertion); + $this->assertSame('/', $requestAssertion->getUri()->getPath()); $this->assertSame('GET', $requestAssertion->getMethod()); $this->assertSame('127.0.0.1', $requestAssertion->remoteAddress); @@ -89,8 +88,8 @@ public function testRequestEvent() public function testRequestPauseWillbeForwardedToConnection() { - $server = new Server($this->socket, function (Request $request) { - $request->pause(); + $server = new Server($this->socket, function (RequestInterface $request) { + $request->getBody()->pause(); }); $this->connection->expects($this->once())->method('pause'); @@ -107,9 +106,8 @@ public function testRequestPauseWillbeForwardedToConnection() public function testRequestResumeWillbeForwardedToConnection() { - - $server = new Server($this->socket, function (Request $request) { - $request->resume(); + $server = new Server($this->socket, function (RequestInterface $request) { + $request->getBody()->resume(); }); $this->connection->expects($this->once())->method('resume'); @@ -121,8 +119,8 @@ public function testRequestResumeWillbeForwardedToConnection() public function testRequestCloseWillPauseConnection() { - $server = new Server($this->socket, function (Request $request) { - $request->close(); + $server = new Server($this->socket, function (RequestInterface $request) { + $request->getBody()->close(); }); $this->connection->expects($this->once())->method('pause'); @@ -134,9 +132,9 @@ public function testRequestCloseWillPauseConnection() public function testRequestPauseAfterCloseWillNotBeForwarded() { - $server = new Server($this->socket, function (Request $request) { - $request->close(); - $request->pause(); + $server = new Server($this->socket, function (RequestInterface $request) { + $request->getBody()->close(); + $request->getBody()->pause(); }); $this->connection->expects($this->once())->method('pause'); @@ -148,9 +146,9 @@ public function testRequestPauseAfterCloseWillNotBeForwarded() public function testRequestResumeAfterCloseWillNotBeForwarded() { - $server = new Server($this->socket, function (Request $request) { - $request->close(); - $request->resume(); + $server = new Server($this->socket, function (RequestInterface $request) { + $request->getBody()->close(); + $request->getBody()->resume(); }); $this->connection->expects($this->once())->method('pause'); @@ -165,8 +163,8 @@ public function testRequestEventWithoutBodyWillNotEmitData() { $never = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request) use ($never) { - $request->on('data', $never); + $server = new Server($this->socket, function (RequestInterface $request) use ($never) { + $request->getBody()->on('data', $never); }); $this->socket->emit('connection', array($this->connection)); @@ -179,8 +177,8 @@ public function testRequestEventWithSecondDataEventWillEmitBodyData() { $once = $this->expectCallableOnceWith('incomplete'); - $server = new Server($this->socket, function (Request $request) use ($once) { - $request->on('data', $once); + $server = new Server($this->socket, function (RequestInterface $request) use ($once) { + $request->getBody()->on('data', $once); }); $this->socket->emit('connection', array($this->connection)); @@ -198,8 +196,8 @@ public function testRequestEventWithPartialBodyWillEmitData() { $once = $this->expectCallableOnceWith('incomplete'); - $server = new Server($this->socket, function (Request $request) use ($once) { - $request->on('data', $once); + $server = new Server($this->socket, function (RequestInterface $request) use ($once) { + $request->getBody()->on('data', $once); }); $this->socket->emit('connection', array($this->connection)); @@ -218,7 +216,7 @@ public function testRequestEventWithPartialBodyWillEmitData() public function testResponseContainsPoweredByHeader() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->writeHead(); $response->end(); }); @@ -246,7 +244,7 @@ function ($data) use (&$buffer) { public function testClosingResponseDoesNotSendAnyData() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->close(); }); @@ -262,7 +260,7 @@ public function testClosingResponseDoesNotSendAnyData() public function testResponseContainsSameRequestProtocolVersionAndChunkedBodyForHttp11() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->writeHead(); $response->end('bye'); }); @@ -291,7 +289,7 @@ function ($data) use (&$buffer) { public function testResponseContainsSameRequestProtocolVersionAndRawBodyForHttp10() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->writeHead(); $response->end('bye'); }); @@ -422,11 +420,11 @@ public function testBodyDataWillBeSendViaRequestEvent() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); $this->socket->emit('connection', array($this->connection)); @@ -449,11 +447,11 @@ public function testChunkedEncodedRequestWillBeParsedForRequestEvent() $errorEvent = $this->expectCallableNever(); $requestValidation = null; - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); $requestValidation = $request; }); @@ -479,11 +477,11 @@ public function testChunkedEncodedRequestAdditionalDataWontBeEmitted() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); @@ -508,11 +506,11 @@ public function testEmptyChunkedEncodedRequest() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); $this->socket->emit('connection', array($this->connection)); @@ -534,11 +532,11 @@ public function testChunkedIsUpperCase() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); @@ -562,11 +560,11 @@ public function testChunkedIsMixedUpperAndLowerCase() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); @@ -696,11 +694,11 @@ public function testWontEmitFurtherDataWhenContentLengthIsReached() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); $this->socket->emit('connection', array($this->connection)); @@ -724,11 +722,11 @@ public function testWontEmitFurtherDataWhenContentLengthIsReachedSplitted() $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); @@ -756,11 +754,11 @@ public function testContentLengthContainsZeroWillEmitEndEvent() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); $this->socket->emit('connection', array($this->connection)); @@ -781,11 +779,11 @@ public function testContentLengthContainsZeroWillEmitEndEventAdditionalDataWillB $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); $this->socket->emit('connection', array($this->connection)); @@ -807,11 +805,11 @@ public function testContentLengthContainsZeroWillEmitEndEventAdditionalDataWillB $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); $this->socket->emit('connection', array($this->connection)); @@ -837,11 +835,11 @@ public function testContentLengthWillBeIgnoredIfTransferEncodingIsSet() $errorEvent = $this->expectCallableNever(); $requestValidation = null; - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); $requestValidation = $request; }); @@ -873,11 +871,11 @@ public function testInvalidContentLengthWillBeIgnoreddIfTransferEncodingIsSet() $errorEvent = $this->expectCallableNever(); $requestValidation = null; - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); $requestValidation = $request; }); @@ -978,7 +976,7 @@ public function testInvalidChunkHeaderResultsInErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); $server = new Server($this->socket, function ($request, $response) use ($errorEvent){ - $request->on('error', $errorEvent); + $request->getBody()->on('error', $errorEvent); }); $this->connection->expects($this->never())->method('close'); @@ -1000,7 +998,7 @@ public function testTooLongChunkHeaderResultsInErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); $server = new Server($this->socket, function ($request, $response) use ($errorEvent){ - $request->on('error', $errorEvent); + $request->getBody()->on('error', $errorEvent); }); $this->connection->expects($this->never())->method('close'); @@ -1024,7 +1022,7 @@ public function testTooLongChunkBodyResultsInErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); $server = new Server($this->socket, function ($request, $response) use ($errorEvent){ - $request->on('error', $errorEvent); + $request->getBody()->on('error', $errorEvent); }); $this->connection->expects($this->never())->method('close'); @@ -1046,7 +1044,7 @@ public function testUnexpectedEndOfConnectionWillResultsInErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); $server = new Server($this->socket, function ($request, $response) use ($errorEvent){ - $request->on('error', $errorEvent); + $request->getBody()->on('error', $errorEvent); }); $this->connection->expects($this->never())->method('close'); @@ -1107,7 +1105,7 @@ public function testErrorInLengthLimitedStreamNeverClosesConnection() public function testCloseRequestWillPauseConnection() { $server = new Server($this->socket, function ($request, $response) { - $request->close(); + $request->getBody()->close(); }); $this->connection->expects($this->never())->method('close'); @@ -1127,10 +1125,10 @@ public function testEndEventWillBeEmittedOnSimpleRequest() $errorEvent = $this->expectCallableNever(); $server = new Server($this->socket, function ($request, $response) use ($dataEvent, $closeEvent, $endEvent, $errorEvent){ - $request->on('data', $dataEvent); - $request->on('close', $closeEvent); - $request->on('end', $endEvent); - $request->on('error', $errorEvent); + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('error', $errorEvent); }); $this->connection->expects($this->once())->method('pause'); @@ -1150,11 +1148,11 @@ public function testRequestWithoutDefinedLengthWillIgnoreDataEvent() $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket, function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { - $request->on('data', $dataEvent); - $request->on('end', $endEvent); - $request->on('close', $closeEvent); - $request->on('error', $errorEvent); + $server = new Server($this->socket, function (RequestInterface $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $request->getBody()->on('data', $dataEvent); + $request->getBody()->on('end', $endEvent); + $request->getBody()->on('close', $closeEvent); + $request->getBody()->on('error', $errorEvent); }); $this->socket->emit('connection', array($this->connection)); @@ -1167,7 +1165,7 @@ public function testRequestWithoutDefinedLengthWillIgnoreDataEvent() public function testResponseWillBeChunkDecodedByDefault() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->writeHead(); $response->write('hello'); }); @@ -1189,7 +1187,7 @@ public function testResponseWillBeChunkDecodedByDefault() public function testContentLengthWillBeRemovedForResponseStream() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->writeHead( 200, array( @@ -1226,7 +1224,7 @@ function ($data) use (&$buffer) { public function testOnlyAllowChunkedEncoding() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->writeHead( 200, array( @@ -1262,7 +1260,7 @@ function ($data) use (&$buffer) { public function testDateHeaderWillBeAddedWhenNoneIsGiven() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->writeHead(200); }); @@ -1291,7 +1289,7 @@ function ($data) use (&$buffer) { public function testAddCustomDateHeader() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->writeHead(200, array("Date" => "Tue, 15 Nov 1994 08:12:31 GMT")); }); @@ -1320,7 +1318,7 @@ function ($data) use (&$buffer) { public function testRemoveDateHeader() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->writeHead(200, array('Date' => array())); }); @@ -1421,7 +1419,7 @@ public function testContinueWontBeSendForHttp10() public function testContinueWithLaterResponse() { - $server = new Server($this->socket, function (Request $request, Response $response) { + $server = new Server($this->socket, function (RequestInterface $request, Response $response) { $response->writeHead(); $response->end(); });