Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not emit empty data events #108

Merged
merged 1 commit into from
Feb 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -63,6 +63,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 @@ -107,6 +119,62 @@ public function testRequestResumeWillbeForwardedToConnection()
$this->connection->emit('data', array($data));
}

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