Skip to content

Commit

Permalink
Integrate callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
legionth committed Feb 8, 2017
1 parent 945ca81 commit 78c0865
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
7 changes: 4 additions & 3 deletions examples/01-hello-world.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
$loop = Factory::create();
$socket = new Server($loop);

$server = new \React\Http\Server($socket);
$server->on('request', function (Request $request, Response $response) {
$callback = function (Request $request, Response $response) {
$response->writeHead(200, array('Content-Type' => 'text/plain'));
$response->end("Hello world!\n");
});
};

$server = new \React\Http\Server($socket, $callback);

$socket->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0');

Expand Down
14 changes: 8 additions & 6 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
class Server extends EventEmitter implements ServerInterface
{
private $io;
private $callback;

public function __construct(SocketServerInterface $io)
public function __construct(SocketServerInterface $io, callable $callback)
{
$this->io = $io;
$this->callback = $callback;

$this->io->on('connection', function (ConnectionInterface $conn) {
// TODO: http 1.1 keep-alive
Expand Down Expand Up @@ -57,13 +59,13 @@ public function handleRequest(ConnectionInterface $conn, Request $request, $body
$response = new Response($conn);
$response->on('close', array($request, 'close'));

if (!$this->listeners('request')) {
$response->end();

return;
$callback = $this->callback;
try {
$callback($request, $response);
} catch (\Exception $ex) {
$this->emit('error', array());
}

$this->emit('request', array($request, $response));
$request->emit('data', array($bodyBuffer));
}
}
57 changes: 47 additions & 10 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ public function testRequestEventIsEmitted()
{
$io = new ServerStub();

$server = new Server($io);
$server->on('request', $this->expectCallableOnce());
$responseAssertion = null;

$callback = function (Request $request, Response $response) use (&$responseAssertion) {
$responseAssertion = $response;
};

$server = new Server($io, $callback);

$conn = new ConnectionStub();
$io->emit('connection', array($conn));
$server->on('error', $this->expectCallableNever());


$data = $this->createGetRequest();
$conn->emit('data', array($data));

$this->assertInstanceOf('React\Http\Response', $responseAssertion);
}

public function testRequestEvent()
Expand All @@ -30,19 +39,21 @@ public function testRequestEvent()
$requestAssertion = null;
$responseAssertion = null;

$server = new Server($io);
$server->on('request', function ($request, $response) use (&$i, &$requestAssertion, &$responseAssertion) {
$callback = function ($request, $response) use (&$i, &$requestAssertion , &$responseAssertion) {
$i++;
$requestAssertion = $request;
$responseAssertion = $response;
});
};

$server = new Server($io, $callback);

$conn = new ConnectionStub();
$io->emit('connection', array($conn));

$data = $this->createGetRequest();
$conn->emit('data', array($data));

$server->on('error', $this->expectCallableNever());

$this->assertSame(1, $i);
$this->assertInstanceOf('React\Http\Request', $requestAssertion);
$this->assertSame('/', $requestAssertion->getPath());
Expand All @@ -56,11 +67,12 @@ public function testResponseContainsPoweredByHeader()
{
$io = new ServerStub();

$server = new Server($io);
$server->on('request', function (Request $request, Response $response) {
$callback = function (Request $request, Response $response) {
$response->writeHead();
$response->end();
});
};

$server = new Server($io, $callback);

$conn = new ConnectionStub();
$io->emit('connection', array($conn));
Expand All @@ -75,8 +87,13 @@ public function testParserErrorEmitted()
{
$io = new ServerStub();

$assertionResponse = null;
$callback = function (Request $request, Response $response) use (&$assertionResponse){
$assertionResponse = $response;
};

$error = null;
$server = new Server($io);
$server = new Server($io, $callback);
$server->on('headers', $this->expectCallableNever());
$server->on('error', function ($message) use (&$error) {
$error = $message;
Expand All @@ -91,6 +108,26 @@ public function testParserErrorEmitted()

$this->assertInstanceOf('OverflowException', $error);
$this->assertEquals('', $conn->getData());

$this->assertEquals(null, $assertionResponse);
}

public function testExceptionWillEmitErrorEvent()
{
$io = new ServerStub();

$callback = function (Request $request, Response $response) {
throw new \Exception();
};

$server = new Server($io, $callback);

$conn = new ConnectionStub();
$io->emit('connection', array($conn));
$server->on('error', $this->expectCallableOnce());

$data = $this->createGetRequest();
$conn->emit('data', array($data));
}

private function createGetRequest()
Expand Down

0 comments on commit 78c0865

Please sign in to comment.