Skip to content
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
63 changes: 0 additions & 63 deletions tests/ConnectionStub.php

This file was deleted.

22 changes: 0 additions & 22 deletions tests/ServerStub.php

This file was deleted.

86 changes: 60 additions & 26 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,67 @@

class ServerTest extends TestCase
{
public function testRequestEventIsEmitted()
private $connection;
private $socket;

public function setUp()
{
$io = new ServerStub();
$this->connection = $this->getMockBuilder('React\Socket\Connection')
->disableOriginalConstructor()
->setMethods(
array(
'write',
'end',
'close',
'pause',
'resume',
'isReadable',
'isWritable',
'getRemoteAddress',
'pipe'
)
)
->getMock();

$this->socket = $this->getMockBuilder('React\Socket\Server')
->disableOriginalConstructor()
->setMethods(null)
->getMock();
}

$server = new Server($io);
public function testRequestEventIsEmitted()
{
$server = new Server($this->socket);
$server->on('request', $this->expectCallableOnce());

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

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

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

$i = 0;
$requestAssertion = null;
$responseAssertion = null;

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

$conn = new ConnectionStub();
$io->emit('connection', array($conn));
$this->connection
->expects($this->once())
->method('getRemoteAddress')
->willReturn('127.0.0.1');

$this->socket->emit('connection', array($this->connection));

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

$this->assertSame(1, $i);
$this->assertInstanceOf('React\Http\Request', $requestAssertion);
Expand All @@ -54,43 +81,50 @@ public function testRequestEvent()

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

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

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

$this->connection
->expects($this->any())
->method('write')
->will(
$this->returnCallback(
function ($data) use (&$buffer) {
$buffer .= $data;
}
)
);

$this->socket->emit('connection', array($this->connection));

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

$this->assertContains("\r\nX-Powered-By: React/alpha\r\n", $conn->getData());
$this->assertContains("\r\nX-Powered-By: React/alpha\r\n", $buffer);
}

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

$error = null;
$server = new Server($io);
$server = new Server($this->socket);
$server->on('headers', $this->expectCallableNever());
$server->on('error', function ($message) use (&$error) {
$error = $message;
});

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

$data = "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\nX-DATA: ";
$data .= str_repeat('A', 4097 - strlen($data)) . "\r\n\r\n";
$conn->emit('data', [$data]);
$this->connection->emit('data', [$data]);

$this->assertInstanceOf('OverflowException', $error);
$this->assertEquals('', $conn->getData());
$this->connection->expects($this->never())->method('write');
}

private function createGetRequest()
Expand Down