Skip to content

Commit 34f67ee

Browse files
committed
Handle null explicitly for max body length of FullHttpMessageFormatter
1 parent c1f637c commit 34f67ee

File tree

2 files changed

+238
-3
lines changed

2 files changed

+238
-3
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Formatter;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\StreamInterface;
9+
10+
class FullHttpMessageFormatterSpec extends ObjectBehavior
11+
{
12+
function let($maxBodyLength)
13+
{
14+
$this->beConstructedWith($maxBodyLength);
15+
}
16+
17+
function it_is_initializable()
18+
{
19+
$this->shouldHaveType('Http\Message\Formatter\FullHttpMessageFormatter');
20+
}
21+
22+
function it_is_a_formatter()
23+
{
24+
$this->shouldImplement('Http\Message\Formatter');
25+
}
26+
27+
function it_formats_the_request_with_size_limit(RequestInterface $request, StreamInterface $stream)
28+
{
29+
$this->beConstructedWith(18);
30+
31+
$stream->isSeekable()->willReturn(true);
32+
$stream->rewind()->shouldBeCalled();
33+
$stream->__toString()->willReturn('This is an HTML stream request content.');
34+
$request->getBody()->willReturn($stream);
35+
$request->getMethod()->willReturn('GET');
36+
$request->getRequestTarget()->willReturn('/foo');
37+
$request->getProtocolVersion()->willReturn('1.1');
38+
$request->getHeaders()->willReturn([
39+
'X-Param-Foo' => ['foo'],
40+
'X-Param-Bar' => ['bar'],
41+
]);
42+
43+
$expectedMessage = <<<STR
44+
GET /foo HTTP/1.1
45+
X-Param-Foo: foo
46+
X-Param-Bar: bar
47+
48+
This is an HTML st
49+
STR;
50+
$this->formatRequest($request)->shouldReturn($expectedMessage);
51+
}
52+
53+
function it_formats_the_request_without_size_limit(RequestInterface $request, StreamInterface $stream)
54+
{
55+
$this->beConstructedWith(null);
56+
57+
$stream->isSeekable()->willReturn(true);
58+
$stream->rewind()->shouldBeCalled();
59+
$stream->__toString()->willReturn('This is an HTML stream request content.');
60+
$request->getBody()->willReturn($stream);
61+
$request->getMethod()->willReturn('GET');
62+
$request->getRequestTarget()->willReturn('/foo');
63+
$request->getProtocolVersion()->willReturn('1.1');
64+
$request->getHeaders()->willReturn([
65+
'X-Param-Foo' => ['foo'],
66+
'X-Param-Bar' => ['bar'],
67+
]);
68+
69+
$expectedMessage = <<<STR
70+
GET /foo HTTP/1.1
71+
X-Param-Foo: foo
72+
X-Param-Bar: bar
73+
74+
This is an HTML stream request content.
75+
STR;
76+
$this->formatRequest($request)->shouldReturn($expectedMessage);
77+
}
78+
79+
function it_does_not_format_the_request(RequestInterface $request, StreamInterface $stream)
80+
{
81+
$this->beConstructedWith(0);
82+
83+
$stream->isSeekable()->willReturn(true);
84+
$stream->__toString()->willReturn('This is an HTML stream request content.');
85+
$request->getBody()->willReturn($stream);
86+
$request->getMethod()->willReturn('GET');
87+
$request->getRequestTarget()->willReturn('/foo');
88+
$request->getProtocolVersion()->willReturn('1.1');
89+
$request->getHeaders()->willReturn([
90+
'X-Param-Foo' => ['foo'],
91+
'X-Param-Bar' => ['bar'],
92+
]);
93+
94+
$expectedMessage = <<<STR
95+
GET /foo HTTP/1.1
96+
X-Param-Foo: foo
97+
X-Param-Bar: bar
98+
99+
100+
STR;
101+
$this->formatRequest($request)->shouldReturn($expectedMessage);
102+
}
103+
104+
function it_does_not_format_no_seekable_request(RequestInterface $request, StreamInterface $stream)
105+
{
106+
$this->beConstructedWith(1000);
107+
108+
$stream->isSeekable()->willReturn(false);
109+
$stream->__toString()->willReturn('This is an HTML stream request content.');
110+
$request->getBody()->willReturn($stream);
111+
$request->getMethod()->willReturn('GET');
112+
$request->getRequestTarget()->willReturn('/foo');
113+
$request->getProtocolVersion()->willReturn('1.1');
114+
$request->getHeaders()->willReturn([
115+
'X-Param-Foo' => ['foo'],
116+
'X-Param-Bar' => ['bar'],
117+
]);
118+
119+
$expectedMessage = <<<STR
120+
GET /foo HTTP/1.1
121+
X-Param-Foo: foo
122+
X-Param-Bar: bar
123+
124+
125+
STR;
126+
$this->formatRequest($request)->shouldReturn($expectedMessage);
127+
}
128+
129+
function it_formats_the_response_with_size_limit(ResponseInterface $response, StreamInterface $stream)
130+
{
131+
$this->beConstructedWith(18);
132+
133+
$stream->isSeekable()->willReturn(true);
134+
$stream->rewind()->shouldBeCalled();
135+
$stream->__toString()->willReturn('This is an HTML stream response content.');
136+
$response->getBody()->willReturn($stream);
137+
$response->getProtocolVersion()->willReturn('1.1');
138+
$response->getStatusCode()->willReturn(200);
139+
$response->getReasonPhrase()->willReturn('OK');
140+
$response->getHeaders()->willReturn([
141+
'X-Param-Foo' => ['foo'],
142+
'X-Param-Bar' => ['bar'],
143+
]);
144+
145+
$expectedMessage = <<<STR
146+
HTTP/1.1 200 OK
147+
X-Param-Foo: foo
148+
X-Param-Bar: bar
149+
150+
This is an HTML st
151+
STR;
152+
$this->formatResponse($response)->shouldReturn($expectedMessage);
153+
}
154+
155+
function it_formats_the_response_without_size_limit(ResponseInterface $response, StreamInterface $stream)
156+
{
157+
$this->beConstructedWith(null);
158+
159+
$stream->isSeekable()->willReturn(true);
160+
$stream->rewind()->shouldBeCalled();
161+
$stream->__toString()->willReturn('This is an HTML stream response content.');
162+
$response->getBody()->willReturn($stream);
163+
$response->getProtocolVersion()->willReturn('1.1');
164+
$response->getStatusCode()->willReturn(200);
165+
$response->getReasonPhrase()->willReturn('OK');
166+
$response->getHeaders()->willReturn([
167+
'X-Param-Foo' => ['foo'],
168+
'X-Param-Bar' => ['bar'],
169+
]);
170+
171+
$expectedMessage = <<<STR
172+
HTTP/1.1 200 OK
173+
X-Param-Foo: foo
174+
X-Param-Bar: bar
175+
176+
This is an HTML stream response content.
177+
STR;
178+
$this->formatResponse($response)->shouldReturn($expectedMessage);
179+
}
180+
181+
function it_does_not_format_the_response(ResponseInterface $response, StreamInterface $stream)
182+
{
183+
$this->beConstructedWith(0);
184+
185+
$stream->isSeekable()->willReturn(true);
186+
$stream->__toString()->willReturn('This is an HTML stream response content.');
187+
$response->getBody()->willReturn($stream);
188+
$response->getProtocolVersion()->willReturn('1.1');
189+
$response->getStatusCode()->willReturn(200);
190+
$response->getReasonPhrase()->willReturn('OK');
191+
$response->getHeaders()->willReturn([
192+
'X-Param-Foo' => ['foo'],
193+
'X-Param-Bar' => ['bar'],
194+
]);
195+
196+
$expectedMessage = <<<STR
197+
HTTP/1.1 200 OK
198+
X-Param-Foo: foo
199+
X-Param-Bar: bar
200+
201+
202+
STR;
203+
$this->formatResponse($response)->shouldReturn($expectedMessage);
204+
}
205+
206+
function it_does_not_format_no_seekable_response(ResponseInterface $response, StreamInterface $stream)
207+
{
208+
$this->beConstructedWith(1000);
209+
210+
$stream->isSeekable()->willReturn(false);
211+
$stream->__toString()->willReturn('This is an HTML stream response content.');
212+
$response->getBody()->willReturn($stream);
213+
$response->getProtocolVersion()->willReturn('1.1');
214+
$response->getStatusCode()->willReturn(200);
215+
$response->getReasonPhrase()->willReturn('OK');
216+
$response->getHeaders()->willReturn([
217+
'X-Param-Foo' => ['foo'],
218+
'X-Param-Bar' => ['bar'],
219+
]);
220+
221+
$expectedMessage = <<<STR
222+
HTTP/1.1 200 OK
223+
X-Param-Foo: foo
224+
X-Param-Bar: bar
225+
226+
227+
STR;
228+
$this->formatResponse($response)->shouldReturn($expectedMessage);
229+
}
230+
}

src/Formatter/FullHttpMessageFormatter.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class FullHttpMessageFormatter implements Formatter
1717
/**
1818
* The maximum length of the body.
1919
*
20-
* @var int
20+
* @var int|null
2121
*/
2222
private $maxBodyLength;
2323

2424
/**
25-
* @param int $maxBodyLength
25+
* @param int|null $maxBodyLength
2626
*/
2727
public function __construct($maxBodyLength = 1000)
2828
{
@@ -83,7 +83,12 @@ private function addBody(MessageInterface $request, $message)
8383
return $message."\n";
8484
}
8585

86-
$message .= "\n".mb_substr($stream->__toString(), 0, $this->maxBodyLength);
86+
if (null === $this->maxBodyLength) {
87+
$message .= "\n".$stream->__toString();
88+
} else {
89+
$message .= "\n".mb_substr($stream->__toString(), 0, $this->maxBodyLength);
90+
}
91+
8792
$stream->rewind();
8893

8994
return $message;

0 commit comments

Comments
 (0)