From 1be36c719314b80baf0a07b37049fc959826696b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 9 Feb 2017 16:43:22 +0100 Subject: [PATCH] Do not emit empty data events --- composer.json | 2 +- src/Server.php | 5 +++- tests/ServerTest.php | 68 ++++++++++++++++++++++++++++++++++++++++++++ tests/TestCase.php | 11 +++++++ 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 3d431527..3cee9484 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "php": ">=5.3.0", "ringcentral/psr7": "^1.0", "react/socket": "^0.4", - "react/stream": "^0.4", + "react/stream": "^0.4.4", "evenement/evenement": "^2.0 || ^1.0" }, "autoload": { diff --git a/src/Server.php b/src/Server.php index 2f5f641a..2434ab6f 100644 --- a/src/Server.php +++ b/src/Server.php @@ -65,6 +65,9 @@ public function handleRequest(ConnectionInterface $conn, Request $request, $body } $this->emit('request', array($request, $response)); - $request->emit('data', array($bodyBuffer)); + + if ($bodyBuffer !== '') { + $request->emit('data', array($bodyBuffer)); + } } } diff --git a/tests/ServerTest.php b/tests/ServerTest.php index e79347e9..f2faf661 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -36,6 +36,18 @@ public function setUp() ->getMock(); } + public function testRequestEventWillNotBeEmittedForIncompleteHeaders() + { + $server = new Server($this->socket); + $server->on('request', $this->expectCallableNever()); + + $this->socket->emit('connection', array($this->connection)); + + $data = ''; + $data .= "GET / HTTP/1.1\r\n"; + $this->connection->emit('data', array($data)); + } + public function testRequestEventIsEmitted() { $server = new Server($this->socket); @@ -79,6 +91,62 @@ public function testRequestEvent() $this->assertInstanceOf('React\Http\Response', $responseAssertion); } + public function testRequestEventWithoutBodyWillNotEmitData() + { + $never = $this->expectCallableNever(); + + $server = new Server($this->socket); + $server->on('request', function (Request $request) use ($never) { + $request->on('data', $never); + }); + + $this->socket->emit('connection', array($this->connection)); + + $data = $this->createGetRequest(); + $this->connection->emit('data', array($data)); + } + + public function testRequestEventWithSecondDataEventWillEmitBodyData() + { + $once = $this->expectCallableOnceWith('incomplete'); + + $server = new Server($this->socket); + $server->on('request', function (Request $request) use ($once) { + $request->on('data', $once); + }); + + $this->socket->emit('connection', array($this->connection)); + + $data = ''; + $data .= "POST / HTTP/1.1\r\n"; + $data .= "Content-Length: 100\r\n"; + $data .= "\r\n"; + $data .= "incomplete"; + $this->connection->emit('data', array($data)); + } + + public function testRequestEventWithPartialBodyWillEmitData() + { + $once = $this->expectCallableOnceWith('incomplete'); + + $server = new Server($this->socket); + $server->on('request', function (Request $request) use ($once) { + $request->on('data', $once); + }); + + $this->socket->emit('connection', array($this->connection)); + + $data = ''; + $data .= "POST / HTTP/1.1\r\n"; + $data .= "Content-Length: 100\r\n"; + $data .= "\r\n"; + $this->connection->emit('data', array($data)); + + $data = ''; + $data .= "incomplete"; + $this->connection->emit('data', array($data)); + } + public function testResponseContainsPoweredByHeader() { $server = new Server($this->socket); diff --git a/tests/TestCase.php b/tests/TestCase.php index 24fe27f2..73bb401e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -24,6 +24,17 @@ protected function expectCallableOnce() return $mock; } + protected function expectCallableOnceWith($value) + { + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($value); + + return $mock; + } + protected function expectCallableNever() { $mock = $this->createCallableMock();