Skip to content

Commit 4e350a6

Browse files
committed
Validate Response body type
1 parent 19b03ff commit 4e350a6

2 files changed

Lines changed: 47 additions & 7 deletions

File tree

src/Message/Response.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,25 @@
3434
*/
3535
class Response extends Psr7Response
3636
{
37+
/**
38+
* @param int $status HTTP status code (e.g. 200/404)
39+
* @param array<string,string|string[]> $headers additional response headers
40+
* @param string|ReadableStreamInterface|StreamInterface $body response body
41+
* @param string $version HTTP protocol version (e.g. 1.1/1.0)
42+
* @param ?string $reason custom HTTP response phrase
43+
* @throws \InvalidArgumentException for an invalid body
44+
*/
3745
public function __construct(
3846
$status = 200,
3947
array $headers = array(),
40-
$body = null,
48+
$body = '',
4149
$version = '1.1',
4250
$reason = null
4351
) {
44-
if ($body instanceof ReadableStreamInterface) {
52+
if ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) {
4553
$body = new HttpBodyStream($body, null);
54+
} elseif (!\is_string($body) && !$body instanceof StreamInterface) {
55+
throw new \InvalidArgumentException('Invalid response body given');
4656
}
4757

4858
parent::__construct(

tests/Message/ResponseTest.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,51 @@
22

33
namespace React\Tests\Http\Message;
44

5+
use React\Http\Io\HttpBodyStream;
56
use React\Http\Message\Response;
67
use React\Stream\ThroughStream;
78
use React\Tests\Http\TestCase;
89

910
class ResponseTest extends TestCase
1011
{
11-
public function testResponseBodyWillBeHttpBodyStream()
12-
{
13-
$response = new Response(200, array(), new ThroughStream());
14-
$this->assertInstanceOf('React\Http\Io\HttpBodyStream', $response->getBody());
15-
}
1612

1713
public function testStringBodyWillBePsr7Stream()
1814
{
1915
$response = new Response(200, array(), 'hello');
2016
$this->assertInstanceOf('RingCentral\Psr7\Stream', $response->getBody());
2117
}
18+
19+
public function testConstructWithStreamingBodyWillReturnReadableBodyStream()
20+
{
21+
$response = new Response(200, array(), new ThroughStream());
22+
23+
$body = $response->getBody();
24+
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $body);
25+
$this->assertInstanceof('React\Stream\ReadableStreamInterface', $body);
26+
$this->assertInstanceOf('React\Http\Io\HttpBodyStream', $body);
27+
$this->assertNull($body->getSize());
28+
}
29+
30+
public function testConstructWithHttpBodyStreamReturnsBodyAsIs()
31+
{
32+
$response = new Response(
33+
200,
34+
array(),
35+
$body = new HttpBodyStream(new ThroughStream(), 100)
36+
);
37+
38+
$this->assertSame($body, $response->getBody());
39+
}
40+
41+
public function testFloatBodyWillThrow()
42+
{
43+
$this->setExpectedException('InvalidArgumentException');
44+
new Response(200, array(), 1.0);
45+
}
46+
47+
public function testResourceBodyWillThrow()
48+
{
49+
$this->setExpectedException('InvalidArgumentException');
50+
new Response(200, array(), tmpfile());
51+
}
2252
}

0 commit comments

Comments
 (0)