Skip to content

Commit 1be36c7

Browse files
committed
Do not emit empty data events
1 parent 04794ae commit 1be36c7

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

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
@@ -65,6 +65,9 @@ public function handleRequest(ConnectionInterface $conn, Request $request, $body
6565
}
6666

6767
$this->emit('request', array($request, $response));
68-
$request->emit('data', array($bodyBuffer));
68+
69+
if ($bodyBuffer !== '') {
70+
$request->emit('data', array($bodyBuffer));
71+
}
6972
}
7073
}

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);
@@ -79,6 +91,62 @@ public function testRequestEvent()
7991
$this->assertInstanceOf('React\Http\Response', $responseAssertion);
8092
}
8193

94+
public function testRequestEventWithoutBodyWillNotEmitData()
95+
{
96+
$never = $this->expectCallableNever();
97+
98+
$server = new Server($this->socket);
99+
$server->on('request', function (Request $request) use ($never) {
100+
$request->on('data', $never);
101+
});
102+
103+
$this->socket->emit('connection', array($this->connection));
104+
105+
$data = $this->createGetRequest();
106+
$this->connection->emit('data', array($data));
107+
}
108+
109+
public function testRequestEventWithSecondDataEventWillEmitBodyData()
110+
{
111+
$once = $this->expectCallableOnceWith('incomplete');
112+
113+
$server = new Server($this->socket);
114+
$server->on('request', function (Request $request) use ($once) {
115+
$request->on('data', $once);
116+
});
117+
118+
$this->socket->emit('connection', array($this->connection));
119+
120+
$data = '';
121+
$data .= "POST / HTTP/1.1\r\n";
122+
$data .= "Content-Length: 100\r\n";
123+
$data .= "\r\n";
124+
$data .= "incomplete";
125+
$this->connection->emit('data', array($data));
126+
}
127+
128+
public function testRequestEventWithPartialBodyWillEmitData()
129+
{
130+
$once = $this->expectCallableOnceWith('incomplete');
131+
132+
$server = new Server($this->socket);
133+
$server->on('request', function (Request $request) use ($once) {
134+
$request->on('data', $once);
135+
});
136+
137+
$this->socket->emit('connection', array($this->connection));
138+
139+
$data = '';
140+
$data .= "POST / HTTP/1.1\r\n";
141+
$data .= "Content-Length: 100\r\n";
142+
$data .= "\r\n";
143+
$this->connection->emit('data', array($data));
144+
145+
$data = '';
146+
$data .= "incomplete";
147+
$this->connection->emit('data', array($data));
148+
}
149+
82150
public function testResponseContainsPoweredByHeader()
83151
{
84152
$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)