forked from reactphp/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerTest.php
More file actions
167 lines (134 loc) · 4.96 KB
/
Copy pathServerTest.php
File metadata and controls
167 lines (134 loc) · 4.96 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace React\Tests\Http;
use React\Http\Server;
use React\Http\Response;
use React\Http\Request;
class ServerTest extends TestCase
{
private $connection;
private $socket;
public function setUp()
{
$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();
}
public function testRequestEventIsEmitted()
{
$server = new Server($this->socket);
$server->on('request', $this->expectCallableOnce());
$this->socket->emit('connection', array($this->connection));
$data = $this->createGetRequest();
$this->connection->emit('data', array($data));
}
public function testRequestEvent()
{
$i = 0;
$requestAssertion = null;
$responseAssertion = null;
$server = new Server($this->socket);
$server->on('request', function ($request, $response) use (&$i, &$requestAssertion, &$responseAssertion) {
$i++;
$requestAssertion = $request;
$responseAssertion = $response;
});
$this->connection
->expects($this->once())
->method('getRemoteAddress')
->willReturn('127.0.0.1');
$this->socket->emit('connection', array($this->connection));
$data = $this->createGetRequest();
$this->connection->emit('data', array($data));
$this->assertSame(1, $i);
$this->assertInstanceOf('React\Http\Request', $requestAssertion);
$this->assertSame('/', $requestAssertion->getPath());
$this->assertSame('GET', $requestAssertion->getMethod());
$this->assertSame('127.0.0.1', $requestAssertion->remoteAddress);
$this->assertInstanceOf('React\Http\Response', $responseAssertion);
}
public function testRequestPauseWillbeForwardedToConnection()
{
$server = new Server($this->socket);
$server->on('request', function (Request $request) {
$request->pause();
});
$this->connection->expects($this->once())->method('pause');
$this->socket->emit('connection', array($this->connection));
$data = $this->createGetRequest();
$this->connection->emit('data', array($data));
}
public function testRequestResumeWillbeForwardedToConnection()
{
$server = new Server($this->socket);
$server->on('request', function (Request $request) {
$request->resume();
});
$this->connection->expects($this->once())->method('resume');
$this->socket->emit('connection', array($this->connection));
$data = $this->createGetRequest();
$this->connection->emit('data', array($data));
}
public function testResponseContainsPoweredByHeader()
{
$server = new Server($this->socket);
$server->on('request', function (Request $request, Response $response) {
$response->writeHead();
$response->end();
});
$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();
$this->connection->emit('data', array($data));
$this->assertContains("\r\nX-Powered-By: React/alpha\r\n", $buffer);
}
public function testParserErrorEmitted()
{
$error = null;
$server = new Server($this->socket);
$server->on('headers', $this->expectCallableNever());
$server->on('error', function ($message) use (&$error) {
$error = $message;
});
$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";
$this->connection->emit('data', array($data));
$this->assertInstanceOf('OverflowException', $error);
$this->connection->expects($this->never())->method('write');
}
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;
}
}