forked from reactphp/reactphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerTest.php
More file actions
81 lines (60 loc) · 2.07 KB
/
Copy pathServerTest.php
File metadata and controls
81 lines (60 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace React\Tests\Http;
use React\Http\Server;
use React\Tests\Socket\TestCase;
use React\Tests\Socket\Stub\ServerStub;
use React\Tests\Socket\Stub\ConnectionStub;
class ServerTest extends TestCase
{
public function testRequestEventIsEmitted()
{
$io = new ServerStub();
$server = new Server($io);
$server->on('request', $this->expectCallableOnce());
$conn = new ConnectionStub();
$io->emit('connection', array($conn));
$data = $this->createGetRequest();
$conn->emit('data', array($data));
}
public function testRequestEvent()
{
$io = new ServerStub();
$i = 0;
$server = new Server($io);
$server->on('request', function ($request, $response) use (&$i) {
$i++;
$this->assertInstanceOf('React\Http\Request', $request);
$this->assertSame('/', $request->getPath());
$this->assertSame('GET', $request->getMethod());
$this->assertSame('127.0.0.1', $request->remoteAddress);
$this->assertInstanceOf('React\Http\Response', $response);
});
$conn = new ConnectionStub();
$io->emit('connection', array($conn));
$data = $this->createGetRequest();
$conn->emit('data', array($data));
$this->assertSame(1, $i);
}
public function testResponseContainsPoweredByHeader()
{
$io = new ServerStub();
$server = new Server($io);
$server->on('request', function ($request, $response) {
$response->writeHead();
$response->end();
});
$conn = new ConnectionStub();
$io->emit('connection', array($conn));
$data = $this->createGetRequest();
$conn->emit('data', array($data));
$this->assertContains("\r\nX-Powered-By: React/alpha\r\n", $conn->getData());
}
private function createGetRequest()
{
$data = "GET / HTTP/1.1\r\n";
$data .= "Host: example.com:80\r\n";
$data .= "Connection: close\r\n";
$data .= "\r\n";
return $data;
}
}