Skip to content

Commit 0e50877

Browse files
committed
Reworked tests
1 parent dec2291 commit 0e50877

1 file changed

Lines changed: 102 additions & 38 deletions

File tree

tests/ServerTest.php

Lines changed: 102 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ public function testBodyDataWillBeSendViaRequestEvent()
159159
{
160160
$server = new Server($this->socket);
161161

162-
$buffer = '';
163-
$server->on('request', function (Request $request, Response $response) use (&$buffer) {
164-
$request->on('data', function ($data) use (&$buffer) {
165-
$buffer .= $data;
166-
});
162+
$dataEvent = $this->expectCallableOnceWith('hello');
163+
$endEvent = $this->expectCallableNever();
164+
165+
$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent) {
166+
$request->on('data', $dataEvent);
167+
$request->on('end', $endEvent);
167168
});
168169

169170
$this->socket->emit('connection', array($this->connection));
@@ -176,19 +177,18 @@ public function testBodyDataWillBeSendViaRequestEvent()
176177
$data .= "hello";
177178

178179
$this->connection->emit('data', array($data));
179-
180-
$this->assertEquals('hello', $buffer);
181180
}
182181

183182
public function testChunkedEncodedRequestWillBeParsedForRequestEvent()
184183
{
185184
$server = new Server($this->socket);
186185

187-
$buffer = '';
188-
$server->on('request', function (Request $request, Response $response) use (&$buffer) {
189-
$request->on('data', function ($data) use (&$buffer) {
190-
$buffer .= $data;
191-
});
186+
$dataEvent = $this->expectCallableOnceWith('hello');
187+
$endEvent = $this->expectCallableOnce();
188+
189+
$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent) {
190+
$request->on('data', $dataEvent);
191+
$request->on('end', $endEvent);
192192
});
193193

194194
$this->socket->emit('connection', array($this->connection));
@@ -202,19 +202,18 @@ public function testChunkedEncodedRequestWillBeParsedForRequestEvent()
202202
$data .= "0\r\n\r\n";
203203

204204
$this->connection->emit('data', array($data));
205-
206-
$this->assertEquals('hello', $buffer);
207205
}
208206

209207
public function testChunkedEncodedRequestAdditionalDataWontBeEmitted()
210208
{
211209
$server = new Server($this->socket);
212210

213-
$buffer = '';
214-
$server->on('request', function (Request $request, Response $response) use (&$buffer) {
215-
$request->on('data', function ($data) use (&$buffer) {
216-
$buffer .= $data;
217-
});
211+
$dataEvent = $this->expectCallableOnceWith('hello');
212+
$endEvent = $this->expectCallableOnce();
213+
214+
$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent) {
215+
$request->on('data', $dataEvent);
216+
$request->on('end', $endEvent);
218217
});
219218

220219
$this->socket->emit('connection', array($this->connection));
@@ -229,18 +228,71 @@ public function testChunkedEncodedRequestAdditionalDataWontBeEmitted()
229228
$data .= "2\r\nhi\r\n";
230229

231230
$this->connection->emit('data', array($data));
232-
$this->assertEquals('hello', $buffer);
233231
}
234232

235233
public function testEmptyChunkedEncodedRequest()
236234
{
237235
$server = new Server($this->socket);
238236

239-
$buffer = '';
240-
$server->on('request', function (Request $request, Response $response) use (&$buffer) {
241-
$request->on('data', function ($data) use (&$buffer) {
242-
$buffer .= $data;
243-
});
237+
$dataEvent = $this->expectCallableNever();
238+
$endEvent = $this->expectCallableOnce();
239+
240+
$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent) {
241+
$request->on('data', $dataEvent);
242+
$request->on('end', $endEvent);
243+
});
244+
245+
$this->socket->emit('connection', array($this->connection));
246+
247+
$data = "GET / HTTP/1.1\r\n";
248+
$data .= "Host: example.com:80\r\n";
249+
$data .= "Connection: close\r\n";
250+
$data .= "Transfer-Encoding: chunked\r\n";
251+
$data .= "\r\n";
252+
$data .= "0\r\n\r\n";
253+
254+
$this->connection->emit('data', array($data));
255+
}
256+
257+
public function testOneChunkWillBeEmittedDelayed()
258+
{
259+
$server = new Server($this->socket);
260+
261+
$dataEvent = $this->expectCallableOnceWith('hello');
262+
$endEvent = $this->expectCallableOnce();
263+
264+
$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent) {
265+
$request->on('data', $dataEvent);
266+
$request->on('end', $endEvent);
267+
});
268+
269+
$this->socket->emit('connection', array($this->connection));
270+
271+
$data = "GET / HTTP/1.1\r\n";
272+
$data .= "Host: example.com:80\r\n";
273+
$data .= "Connection: close\r\n";
274+
$data .= "Transfer-Encoding: chunked\r\n";
275+
$data .= "\r\n";
276+
$data .= "5\r\nhel";
277+
278+
$this->connection->emit('data', array($data));
279+
280+
$data = "lo\r\n";
281+
$data .= "0\r\n\r\n";
282+
283+
$this->connection->emit('data', array($data));
284+
}
285+
286+
public function testEmitTwoChunksDelayed()
287+
{
288+
$server = new Server($this->socket);
289+
290+
$dataEvent = $this->expectCallableConsecutive(2, array('hello', 'world'));
291+
$endEvent = $this->expectCallableOnce();
292+
293+
$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent) {
294+
$request->on('data', $dataEvent);
295+
$request->on('end', $endEvent);
244296
});
245297

246298
$this->socket->emit('connection', array($this->connection));
@@ -250,21 +302,30 @@ public function testEmptyChunkedEncodedRequest()
250302
$data .= "Connection: close\r\n";
251303
$data .= "Transfer-Encoding: chunked\r\n";
252304
$data .= "\r\n";
305+
$data .= "5\r\nhello\r\n";
306+
307+
$this->connection->emit('data', array($data));
308+
309+
$data = "5\r\nworld\r\n";
253310
$data .= "0\r\n\r\n";
254311

255312
$this->connection->emit('data', array($data));
256-
$this->assertEquals('', $buffer);
257313
}
258314

315+
/**
316+
* All transfer-coding names are case-insensitive according to:
317+
* https://tools.ietf.org/html/rfc7230#section-4
318+
*/
259319
public function testChunkedIsUpperCase()
260320
{
261321
$server = new Server($this->socket);
262322

263-
$buffer = '';
264-
$server->on('request', function (Request $request, Response $response) use (&$buffer) {
265-
$request->on('data', function ($data) use (&$buffer) {
266-
$buffer .= $data;
267-
});
323+
$dataEvent = $this->expectCallableOnceWith('hello');
324+
$endEvent = $this->expectCallableOnce();
325+
326+
$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent) {
327+
$request->on('data', $dataEvent);
328+
$request->on('end', $endEvent);
268329
});
269330

270331
$this->socket->emit('connection', array($this->connection));
@@ -278,18 +339,22 @@ public function testChunkedIsUpperCase()
278339
$data .= "0\r\n\r\n";
279340

280341
$this->connection->emit('data', array($data));
281-
$this->assertEquals('hello', $buffer);
282342
}
283343

344+
/**
345+
* All transfer-coding names are case-insensitive according to:
346+
* https://tools.ietf.org/html/rfc7230#section-4
347+
*/
284348
public function testChunkedIsMixedUpperAndLowerCase()
285349
{
286350
$server = new Server($this->socket);
287351

288-
$buffer = '';
289-
$server->on('request', function (Request $request, Response $response) use (&$buffer) {
290-
$request->on('data', function ($data) use (&$buffer) {
291-
$buffer .= $data;
292-
});
352+
$dataEvent = $this->expectCallableOnceWith('hello');
353+
$endEvent = $this->expectCallableOnce();
354+
355+
$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent) {
356+
$request->on('data', $dataEvent);
357+
$request->on('end', $endEvent);
293358
});
294359

295360
$this->socket->emit('connection', array($this->connection));
@@ -303,7 +368,6 @@ public function testChunkedIsMixedUpperAndLowerCase()
303368
$data .= "0\r\n\r\n";
304369

305370
$this->connection->emit('data', array($data));
306-
$this->assertEquals('hello', $buffer);
307371
}
308372

309373
private function createGetRequest()

0 commit comments

Comments
 (0)