Skip to content

Commit

Permalink
Do not emit empty data events
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Feb 10, 2017
1 parent 04794ae commit 1be36c7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
5 changes: 4 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
68 changes: 68 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1be36c7

Please sign in to comment.