diff --git a/README.md b/README.md index 5a58c02b..5369bd52 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,7 @@ This is an HTTP server which responds with `Hello World` to every request. $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server(8080, $loop); -$http = new React\Http\Server($socket); -$http->on('request', function (Request $request, Response $response) { +$http = new React\Http\Server($socket, function (Request $request, Response $response) { $response->writeHead(200, array('Content-Type' => 'text/plain')); $response->end("Hello World!\n"); }); @@ -51,16 +50,22 @@ See also the [examples](examples). ### Server The `Server` class is responsible for handling incoming connections and then -emit a `request` event for each incoming HTTP request. +execute the execute the callback function passed to the constructor. It attaches itself to an instance of `React\Socket\ServerInterface` which emits underlying streaming connections in order to then parse incoming data -as HTTP: +as HTTP. + +For each incoming connection, it executes the callback function with the respective +[`Request`](#request) and [`Response`](#response) objects: ```php $socket = new React\Socket\Server(8080, $loop); -$http = new React\Http\Server($socket); +$http = new React\Http\Server($socket, function (Request $request, Response $response) { + $response->writeHead(200, array('Content-Type' => 'text/plain')); + $response->end("Hello World!\n"); +}); ``` Similarly, you can also attach this to a @@ -73,26 +78,13 @@ $socket = new SecureServer($socket, $loop, array( 'local_cert' => __DIR__ . '/localhost.pem' )); -$http = new React\Http\Server($socket); -``` - -For each incoming connection, it emits a `request` event with the respective -[`Request`](#request) and [`Response`](#response) objects: - -```php -$http->on('request', function (Request $request, Response $response) { +$http = new React\Http\Server($socket, function (Request $request, Response $response) { $response->writeHead(200, array('Content-Type' => 'text/plain')); $response->end("Hello World!\n"); }); ``` -See also [`Request`](#request) and [`Response`](#response) for more details. - -> Note that you SHOULD always listen for the `request` event. -Failing to do so will result in the server parsing the incoming request, -but never sending a response back to the client. - -Checkout [Request](#request) for details about the request data body. +See also [`Request`](#request) and [`Response`](#response) for more details(e.g. the request data body). The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages. If a client sends an invalid request message, uses an invalid HTTP protocol @@ -120,7 +112,7 @@ Listen on the `data` event and the `end` event of the [Request](#request) to evaluate the data of the request body: ```php -$http->on('request', function (Request $request, Response $response) { +$http = new React\Http\Server($socket, function (RequestInterface $request, Response $response) { $contentLength = 0; $request->on('data', function ($data) use (&$contentLength) { $contentLength += strlen($data); @@ -247,7 +239,7 @@ This method is mostly useful in combination with the [`expectsContinue()`](#expectscontinue) method like this: ```php -$http->on('request', function (Request $request, Response $response) { +$http = new React\Http\Server($socket, function (Request $request, Response $response) { if ($request->expectsContinue()) { $response->writeContinue(); } diff --git a/examples/01-hello-world.php b/examples/01-hello-world.php index 424a9c1e..d44634ed 100644 --- a/examples/01-hello-world.php +++ b/examples/01-hello-world.php @@ -10,8 +10,7 @@ $loop = Factory::create(); $socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop); -$server = new \React\Http\Server($socket); -$server->on('request', function (Request $request, Response $response) { +$server = new \React\Http\Server($socket, function (Request $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 c017a196..6ca7e809 100644 --- a/examples/02-hello-world-https.php +++ b/examples/02-hello-world-https.php @@ -14,8 +14,7 @@ 'local_cert' => isset($argv[2]) ? $argv[2] : __DIR__ . '/localhost.pem' )); -$server = new \React\Http\Server($socket); -$server->on('request', function (Request $reques, Response $response) { +$server = new \React\Http\Server($socket, function (Request $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 98b474f7..a016b66e 100644 --- a/examples/03-handling-body-data.php +++ b/examples/03-handling-body-data.php @@ -10,8 +10,7 @@ $loop = Factory::create(); $socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop); -$server = new \React\Http\Server($socket); -$server->on('request', function (Request $request, Response $response) { +$server = new \React\Http\Server($socket, function (Request $request, Response $response) { $contentLength = 0; $request->on('data', function ($data) use (&$contentLength) { $contentLength += strlen($data); diff --git a/src/Server.php b/src/Server.php index eb64e934..e3824846 100644 --- a/src/Server.php +++ b/src/Server.php @@ -6,6 +6,7 @@ use React\Socket\ServerInterface as SocketServerInterface; use React\Socket\ConnectionInterface; use Psr\Http\Message\RequestInterface; +use Doctrine\Instantiator\Exception\InvalidArgumentException; /** * The `Server` class is responsible for handling incoming connections and then @@ -21,7 +22,7 @@ * [`Request`](#request) and [`Response`](#response) objects: * * ```php - * $http->on('request', function (Request $request, Response $response) { + * $http = new React\Http\Server($socket, function (Request $request, Response $response) { * $response->writeHead(200, array('Content-Type' => 'text/plain')); * $response->end("Hello World!\n"); * }); @@ -29,10 +30,6 @@ * * See also [`Request`](#request) and [`Response`](#response) for more details. * - * > Note that you SHOULD always listen for the `request` event. - * Failing to do so will result in the server parsing the incoming request, - * but never sending a response back to the client. - * * The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages. * If a client sends an invalid request message or uses an invalid HTTP protocol * version, it will emit an `error` event, send an HTTP error response to the @@ -49,17 +46,25 @@ */ class Server extends EventEmitter { + private $callback; + /** * Creates a HTTP server that accepts connections from the given socket. * * It attaches itself to an instance of `React\Socket\ServerInterface` which * emits underlying streaming connections in order to then parse incoming data - * as HTTP: + * as HTTP. + * + * For each incoming connection, it executes the callback function with the respective + * [`Request`](#request) and [`Response`](#response) objects: * * ```php * $socket = new React\Socket\Server(8080, $loop); * - * $http = new React\Http\Server($socket); + * $http = new React\Http\Server($socket, function (Request $request, Response $response) { + * $response->writeHead(200, array('Content-Type' => 'text/plain')); + * $response->end("Hello World!\n"); + * }); * ``` * * Similarly, you can also attach this to a @@ -72,14 +77,23 @@ class Server extends EventEmitter * 'local_cert' => __DIR__ . '/localhost.pem' * )); * - * $http = new React\Http\Server($socket); - * ``` + * $http = new React\Http\Server($socket, function (Request $request, Response $response) { + * $response->writeHead(200, array('Content-Type' => 'text/plain')); + * $response->end("Hello World!\n"); + * }); + *``` * * @param \React\Socket\ServerInterface $io + * @param callable $callback */ - public function __construct(SocketServerInterface $io) + public function __construct(SocketServerInterface $io, $callback) { + if (!is_callable($callback)) { + throw new InvalidArgumentException(); + } + $io->on('connection', array($this, 'handleConnection')); + $this->callback = $callback; } /** @internal */ @@ -176,7 +190,8 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque '[]' ); - $this->emit('request', array($request, $response)); + $callback = $this->callback; + $callback($request, $response); if ($contentLength === 0) { // If Body is empty or Content-Length is 0 and won't emit further data, diff --git a/tests/ServerTest.php b/tests/ServerTest.php index dac9297b..47085e62 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -36,8 +36,7 @@ public function setUp() public function testRequestEventWillNotBeEmittedForIncompleteHeaders() { - $server = new Server($this->socket); - $server->on('request', $this->expectCallableNever()); + $server = new Server($this->socket, $this->expectCallableNever()); $this->socket->emit('connection', array($this->connection)); @@ -48,8 +47,7 @@ public function testRequestEventWillNotBeEmittedForIncompleteHeaders() public function testRequestEventIsEmitted() { - $server = new Server($this->socket); - $server->on('request', $this->expectCallableOnce()); + $server = new Server($this->socket, $this->expectCallableOnce()); $this->socket->emit('connection', array($this->connection)); @@ -63,8 +61,7 @@ public function testRequestEvent() $requestAssertion = null; $responseAssertion = null; - $server = new Server($this->socket); - $server->on('request', function ($request, $response) use (&$i, &$requestAssertion, &$responseAssertion) { + $server = new Server($this->socket, function ($request, $response) use (&$i, &$requestAssertion, &$responseAssertion) { $i++; $requestAssertion = $request; $responseAssertion = $response; @@ -92,8 +89,7 @@ public function testRequestEvent() public function testRequestPauseWillbeForwardedToConnection() { - $server = new Server($this->socket); - $server->on('request', function (Request $request) { + $server = new Server($this->socket, function (Request $request) { $request->pause(); }); @@ -111,8 +107,8 @@ public function testRequestPauseWillbeForwardedToConnection() public function testRequestResumeWillbeForwardedToConnection() { - $server = new Server($this->socket); - $server->on('request', function (Request $request) { + + $server = new Server($this->socket, function (Request $request) { $request->resume(); }); @@ -125,8 +121,7 @@ public function testRequestResumeWillbeForwardedToConnection() public function testRequestCloseWillPauseConnection() { - $server = new Server($this->socket); - $server->on('request', function (Request $request) { + $server = new Server($this->socket, function (Request $request) { $request->close(); }); @@ -139,8 +134,7 @@ public function testRequestCloseWillPauseConnection() public function testRequestPauseAfterCloseWillNotBeForwarded() { - $server = new Server($this->socket); - $server->on('request', function (Request $request) { + $server = new Server($this->socket, function (Request $request) { $request->close(); $request->pause(); }); @@ -154,8 +148,7 @@ public function testRequestPauseAfterCloseWillNotBeForwarded() public function testRequestResumeAfterCloseWillNotBeForwarded() { - $server = new Server($this->socket); - $server->on('request', function (Request $request) { + $server = new Server($this->socket, function (Request $request) { $request->close(); $request->resume(); }); @@ -172,8 +165,7 @@ public function testRequestEventWithoutBodyWillNotEmitData() { $never = $this->expectCallableNever(); - $server = new Server($this->socket); - $server->on('request', function (Request $request) use ($never) { + $server = new Server($this->socket, function (Request $request) use ($never) { $request->on('data', $never); }); @@ -187,8 +179,7 @@ public function testRequestEventWithSecondDataEventWillEmitBodyData() { $once = $this->expectCallableOnceWith('incomplete'); - $server = new Server($this->socket); - $server->on('request', function (Request $request) use ($once) { + $server = new Server($this->socket, function (Request $request) use ($once) { $request->on('data', $once); }); @@ -207,8 +198,7 @@ public function testRequestEventWithPartialBodyWillEmitData() { $once = $this->expectCallableOnceWith('incomplete'); - $server = new Server($this->socket); - $server->on('request', function (Request $request) use ($once) { + $server = new Server($this->socket, function (Request $request) use ($once) { $request->on('data', $once); }); @@ -228,8 +218,7 @@ public function testRequestEventWithPartialBodyWillEmitData() public function testResponseContainsPoweredByHeader() { - $server = new Server($this->socket); - $server->on('request', function (Request $request, Response $response) { + $server = new Server($this->socket, function (Request $request, Response $response) { $response->writeHead(); $response->end(); }); @@ -257,8 +246,7 @@ function ($data) use (&$buffer) { public function testClosingResponseDoesNotSendAnyData() { - $server = new Server($this->socket); - $server->on('request', function (Request $request, Response $response) { + $server = new Server($this->socket, function (Request $request, Response $response) { $response->close(); }); @@ -274,8 +262,7 @@ public function testClosingResponseDoesNotSendAnyData() public function testResponseContainsSameRequestProtocolVersionAndChunkedBodyForHttp11() { - $server = new Server($this->socket); - $server->on('request', function (Request $request, Response $response) { + $server = new Server($this->socket, function (Request $request, Response $response) { $response->writeHead(); $response->end('bye'); }); @@ -304,8 +291,7 @@ function ($data) use (&$buffer) { public function testResponseContainsSameRequestProtocolVersionAndRawBodyForHttp10() { - $server = new Server($this->socket); - $server->on('request', function (Request $request, Response $response) { + $server = new Server($this->socket, function (Request $request, Response $response) { $response->writeHead(); $response->end('bye'); }); @@ -335,7 +321,7 @@ function ($data) use (&$buffer) { public function testRequestInvalidHttpProtocolVersionWillEmitErrorAndSendErrorResponse() { $error = null; - $server = new Server($this->socket); + $server = new Server($this->socket, $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -364,32 +350,10 @@ function ($data) use (&$buffer) { $this->assertContains("\r\n\r\nError 505: HTTP Version Not Supported", $buffer); } - public function testServerWithNoRequestListenerDoesNotSendAnythingToConnection() - { - $server = new Server($this->socket); - - $this->connection - ->expects($this->never()) - ->method('write'); - - $this->connection - ->expects($this->never()) - ->method('end'); - - $this->connection - ->expects($this->never()) - ->method('close'); - - $this->socket->emit('connection', array($this->connection)); - - $data = $this->createGetRequest(); - $this->connection->emit('data', array($data)); - } - public function testRequestOverflowWillEmitErrorAndSendErrorResponse() { $error = null; - $server = new Server($this->socket); + $server = new Server($this->socket, $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -422,7 +386,7 @@ function ($data) use (&$buffer) { public function testRequestInvalidWillEmitErrorAndSendErrorResponse() { $error = null; - $server = new Server($this->socket); + $server = new Server($this->socket, $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -453,14 +417,12 @@ function ($data) use (&$buffer) { public function testBodyDataWillBeSendViaRequestEvent() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableOnceWith('hello'); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $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); @@ -481,15 +443,13 @@ public function testBodyDataWillBeSendViaRequestEvent() public function testChunkedEncodedRequestWillBeParsedForRequestEvent() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableOnceWith('hello'); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); $requestValidation = null; - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { + $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); @@ -514,20 +474,19 @@ public function testChunkedEncodedRequestWillBeParsedForRequestEvent() public function testChunkedEncodedRequestAdditionalDataWontBeEmitted() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableOnceWith('hello'); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $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); }); + $this->socket->emit('connection', array($this->connection)); $data = "GET / HTTP/1.1\r\n"; @@ -544,14 +503,12 @@ public function testChunkedEncodedRequestAdditionalDataWontBeEmitted() public function testEmptyChunkedEncodedRequest() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableNever(); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $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); @@ -572,20 +529,19 @@ public function testEmptyChunkedEncodedRequest() public function testChunkedIsUpperCase() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableOnceWith('hello'); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $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); }); + $this->socket->emit('connection', array($this->connection)); $data = "GET / HTTP/1.1\r\n"; @@ -601,20 +557,19 @@ public function testChunkedIsUpperCase() public function testChunkedIsMixedUpperAndLowerCase() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableOnceWith('hello'); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $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); }); + $this->socket->emit('connection', array($this->connection)); $data = "GET / HTTP/1.1\r\n"; @@ -630,7 +585,7 @@ public function testChunkedIsMixedUpperAndLowerCase() public function testRequestHttp11WithoutHostWillEmitErrorAndSendErrorResponse() { $error = null; - $server = new Server($this->socket); + $server = new Server($this->socket, $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -662,7 +617,7 @@ function ($data) use (&$buffer) { public function testRequestHttp11WithMalformedHostWillEmitErrorAndSendErrorResponse() { $error = null; - $server = new Server($this->socket); + $server = new Server($this->socket, $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -694,7 +649,7 @@ function ($data) use (&$buffer) { public function testRequestHttp11WithInvalidHostUriComponentsWillEmitErrorAndSendErrorResponse() { $error = null; - $server = new Server($this->socket); + $server = new Server($this->socket, $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -725,8 +680,7 @@ function ($data) use (&$buffer) { public function testRequestHttp10WithoutHostEmitsRequestWithNoError() { - $server = new Server($this->socket); - $server->on('request', $this->expectCallableOnce()); + $server = new Server($this->socket, $this->expectCallableOnce()); $server->on('error', $this->expectCallableNever()); $this->socket->emit('connection', array($this->connection)); @@ -737,14 +691,12 @@ public function testRequestHttp10WithoutHostEmitsRequestWithNoError() public function testWontEmitFurtherDataWhenContentLengthIsReached() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableOnceWith('hello'); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $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); @@ -766,20 +718,20 @@ public function testWontEmitFurtherDataWhenContentLengthIsReached() public function testWontEmitFurtherDataWhenContentLengthIsReachedSplitted() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableOnceWith('hello'); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + + $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); }); + $this->socket->emit('connection', array($this->connection)); $data = "GET / HTTP/1.1\r\n"; @@ -798,14 +750,13 @@ public function testWontEmitFurtherDataWhenContentLengthIsReachedSplitted() public function testContentLengthContainsZeroWillEmitEndEvent() { - $server = new Server($this->socket); $dataEvent = $this->expectCallableNever(); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $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); @@ -825,14 +776,12 @@ public function testContentLengthContainsZeroWillEmitEndEvent() public function testContentLengthContainsZeroWillEmitEndEventAdditionalDataWillBeIgnored() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableNever(); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $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); @@ -853,14 +802,12 @@ public function testContentLengthContainsZeroWillEmitEndEventAdditionalDataWillB public function testContentLengthContainsZeroWillEmitEndEventAdditionalDataWillBeIgnoredSplitted() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableNever(); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $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); @@ -884,15 +831,13 @@ public function testContentLengthContainsZeroWillEmitEndEventAdditionalDataWillB public function testContentLengthWillBeIgnoredIfTransferEncodingIsSet() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableOnceWith('hello'); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); $requestValidation = null; - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { + $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); @@ -922,15 +867,13 @@ public function testContentLengthWillBeIgnoredIfTransferEncodingIsSet() public function testInvalidContentLengthWillBeIgnoreddIfTransferEncodingIsSet() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableOnceWith('hello'); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); $requestValidation = null; - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) { + $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); @@ -962,8 +905,7 @@ public function testInvalidContentLengthWillBeIgnoreddIfTransferEncodingIsSet() public function testNonIntegerContentLengthValueWillLeadToError() { $error = null; - $server = new Server($this->socket); - $server->on('request', $this->expectCallableNever()); + $server = new Server($this->socket, $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -999,8 +941,7 @@ function ($data) use (&$buffer) { public function testMultipleIntegerInContentLengthWillLeadToError() { $error = null; - $server = new Server($this->socket); - $server->on('request', $this->expectCallableNever()); + $server = new Server($this->socket, $this->expectCallableNever()); $server->on('error', function ($message) use (&$error) { $error = $message; }); @@ -1036,8 +977,7 @@ function ($data) use (&$buffer) { public function testInvalidChunkHeaderResultsInErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); - $server = new Server($this->socket); - $server->on('request', function ($request, $response) use ($errorEvent){ + $server = new Server($this->socket, function ($request, $response) use ($errorEvent){ $request->on('error', $errorEvent); }); @@ -1059,8 +999,7 @@ public function testInvalidChunkHeaderResultsInErrorOnRequestStream() public function testTooLongChunkHeaderResultsInErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); - $server = new Server($this->socket); - $server->on('request', function ($request, $response) use ($errorEvent){ + $server = new Server($this->socket, function ($request, $response) use ($errorEvent){ $request->on('error', $errorEvent); }); @@ -1084,8 +1023,7 @@ public function testTooLongChunkHeaderResultsInErrorOnRequestStream() public function testTooLongChunkBodyResultsInErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); - $server = new Server($this->socket); - $server->on('request', function ($request, $response) use ($errorEvent){ + $server = new Server($this->socket, function ($request, $response) use ($errorEvent){ $request->on('error', $errorEvent); }); @@ -1107,8 +1045,7 @@ public function testTooLongChunkBodyResultsInErrorOnRequestStream() public function testUnexpectedEndOfConnectionWillResultsInErrorOnRequestStream() { $errorEvent = $this->expectCallableOnceWith($this->isInstanceOf('Exception')); - $server = new Server($this->socket); - $server->on('request', function ($request, $response) use ($errorEvent){ + $server = new Server($this->socket, function ($request, $response) use ($errorEvent){ $request->on('error', $errorEvent); }); @@ -1130,8 +1067,7 @@ public function testUnexpectedEndOfConnectionWillResultsInErrorOnRequestStream() public function testErrorInChunkedDecoderNeverClosesConnection() { - $server = new Server($this->socket); - $server->on('request', $this->expectCallableOnce()); + $server = new Server($this->socket, $this->expectCallableOnce()); $this->connection->expects($this->never())->method('close'); $this->connection->expects($this->once())->method('pause'); @@ -1150,8 +1086,7 @@ public function testErrorInChunkedDecoderNeverClosesConnection() public function testErrorInLengthLimitedStreamNeverClosesConnection() { - $server = new Server($this->socket); - $server->on('request', $this->expectCallableOnce()); + $server = new Server($this->socket, $this->expectCallableOnce()); $this->connection->expects($this->never())->method('close'); $this->connection->expects($this->once())->method('pause'); @@ -1171,8 +1106,7 @@ public function testErrorInLengthLimitedStreamNeverClosesConnection() public function testCloseRequestWillPauseConnection() { - $server = new Server($this->socket); - $server->on('request', function ($request, $response) { + $server = new Server($this->socket, function ($request, $response) { $request->close(); }); @@ -1192,8 +1126,7 @@ public function testEndEventWillBeEmittedOnSimpleRequest() $endEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server = new Server($this->socket); - $server->on('request', function ($request, $response) use ($dataEvent, $closeEvent, $endEvent, $errorEvent){ + $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); @@ -1212,14 +1145,12 @@ public function testEndEventWillBeEmittedOnSimpleRequest() public function testRequestWithoutDefinedLengthWillIgnoreDataEvent() { - $server = new Server($this->socket); - $dataEvent = $this->expectCallableNever(); $endEvent = $this->expectCallableOnce(); $closeEvent = $this->expectCallableOnce(); $errorEvent = $this->expectCallableNever(); - $server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) { + $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); @@ -1236,9 +1167,7 @@ public function testRequestWithoutDefinedLengthWillIgnoreDataEvent() public function testResponseWillBeChunkDecodedByDefault() { - $server = new Server($this->socket); - - $server->on('request', function (Request $request, Response $response) { + $server = new Server($this->socket, function (Request $request, Response $response) { $response->writeHead(); $response->write('hello'); }); @@ -1260,9 +1189,7 @@ public function testResponseWillBeChunkDecodedByDefault() public function testContentLengthWillBeRemovedForResponseStream() { - $server = new Server($this->socket); - - $server->on('request', function (Request $request, Response $response) { + $server = new Server($this->socket, function (Request $request, Response $response) { $response->writeHead( 200, array( @@ -1299,9 +1226,7 @@ function ($data) use (&$buffer) { public function testOnlyAllowChunkedEncoding() { - $server = new Server($this->socket); - - $server->on('request', function (Request $request, Response $response) { + $server = new Server($this->socket, function (Request $request, Response $response) { $response->writeHead( 200, array( @@ -1337,9 +1262,7 @@ function ($data) use (&$buffer) { public function testDateHeaderWillBeAddedWhenNoneIsGiven() { - $server = new Server($this->socket); - - $server->on('request', function (Request $request, Response $response) { + $server = new Server($this->socket, function (Request $request, Response $response) { $response->writeHead(200); }); @@ -1368,9 +1291,7 @@ function ($data) use (&$buffer) { public function testAddCustomDateHeader() { - $server = new Server($this->socket); - - $server->on('request', function (Request $request, Response $response) { + $server = new Server($this->socket, function (Request $request, Response $response) { $response->writeHead(200, array("Date" => "Tue, 15 Nov 1994 08:12:31 GMT")); }); @@ -1399,9 +1320,7 @@ function ($data) use (&$buffer) { public function testRemoveDateHeader() { - $server = new Server($this->socket); - - $server->on('request', function (Request $request, Response $response) { + $server = new Server($this->socket, function (Request $request, Response $response) { $response->writeHead(200, array('Date' => array())); }); @@ -1432,8 +1351,7 @@ public function testOnlyChunkedEncodingIsAllowedForTransferEncoding() { $error = null; - $server = new Server($this->socket); - $server->on('request', $this->expectCallableNever()); + $server = new Server($this->socket, $this->expectCallableNever()); $server->on('error', function ($exception) use (&$error) { $error = $exception; });