Skip to content

Commit 53ec1f0

Browse files
authored
Merge pull request #108 from clue-labs/data-event
Do not emit empty data events
2 parents fd85e7c + 9c6dcdf commit 53ec1f0

4 files changed

Lines changed: 84 additions & 2 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"php": ">=5.3.0",
88
"ringcentral/psr7": "^1.0",
99
"react/socket": "^0.4",
10-
"react/stream": "^0.4",
10+
"react/stream": "^0.4.4",
1111
"evenement/evenement": "^2.0 || ^1.0"
1212
},
1313
"autoload": {

src/Server.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public function handleRequest(ConnectionInterface $conn, Request $request, $body
6363
}
6464

6565
$this->emit('request', array($request, $response));
66-
$request->emit('data', array($bodyBuffer));
66+
67+
if ($bodyBuffer !== '') {
68+
$request->emit('data', array($bodyBuffer));
69+
}
6770
}
6871
}

tests/ServerTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ public function setUp()
3636
->getMock();
3737
}
3838

39+
public function testRequestEventWillNotBeEmittedForIncompleteHeaders()
40+
{
41+
$server = new Server($this->socket);
42+
$server->on('request', $this->expectCallableNever());
43+
44+
$this->socket->emit('connection', array($this->connection));
45+
46+
$data = '';
47+
$data .= "GET / HTTP/1.1\r\n";
48+
$this->connection->emit('data', array($data));
49+
}
50+
3951
public function testRequestEventIsEmitted()
4052
{
4153
$server = new Server($this->socket);
@@ -107,6 +119,62 @@ public function testRequestResumeWillbeForwardedToConnection()
107119
$this->connection->emit('data', array($data));
108120
}
109121

122+
public function testRequestEventWithoutBodyWillNotEmitData()
123+
{
124+
$never = $this->expectCallableNever();
125+
126+
$server = new Server($this->socket);
127+
$server->on('request', function (Request $request) use ($never) {
128+
$request->on('data', $never);
129+
});
130+
131+
$this->socket->emit('connection', array($this->connection));
132+
133+
$data = $this->createGetRequest();
134+
$this->connection->emit('data', array($data));
135+
}
136+
137+
public function testRequestEventWithSecondDataEventWillEmitBodyData()
138+
{
139+
$once = $this->expectCallableOnceWith('incomplete');
140+
141+
$server = new Server($this->socket);
142+
$server->on('request', function (Request $request) use ($once) {
143+
$request->on('data', $once);
144+
});
145+
146+
$this->socket->emit('connection', array($this->connection));
147+
148+
$data = '';
149+
$data .= "POST / HTTP/1.1\r\n";
150+
$data .= "Content-Length: 100\r\n";
151+
$data .= "\r\n";
152+
$data .= "incomplete";
153+
$this->connection->emit('data', array($data));
154+
}
155+
156+
public function testRequestEventWithPartialBodyWillEmitData()
157+
{
158+
$once = $this->expectCallableOnceWith('incomplete');
159+
160+
$server = new Server($this->socket);
161+
$server->on('request', function (Request $request) use ($once) {
162+
$request->on('data', $once);
163+
});
164+
165+
$this->socket->emit('connection', array($this->connection));
166+
167+
$data = '';
168+
$data .= "POST / HTTP/1.1\r\n";
169+
$data .= "Content-Length: 100\r\n";
170+
$data .= "\r\n";
171+
$this->connection->emit('data', array($data));
172+
173+
$data = '';
174+
$data .= "incomplete";
175+
$this->connection->emit('data', array($data));
176+
}
177+
110178
public function testResponseContainsPoweredByHeader()
111179
{
112180
$server = new Server($this->socket);

tests/TestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ protected function expectCallableOnce()
2424
return $mock;
2525
}
2626

27+
protected function expectCallableOnceWith($value)
28+
{
29+
$mock = $this->createCallableMock();
30+
$mock
31+
->expects($this->once())
32+
->method('__invoke')
33+
->with($value);
34+
35+
return $mock;
36+
}
37+
2738
protected function expectCallableNever()
2839
{
2940
$mock = $this->createCallableMock();

0 commit comments

Comments
 (0)