From 34f67eebf9bd202fdf176ee239de83e0a42ab362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Fri, 8 Mar 2019 07:20:02 +0100 Subject: [PATCH] Handle null explicitly for max body length of FullHttpMessageFormatter --- .../FullHttpMessageFormatterSpec.php | 230 ++++++++++++++++++ src/Formatter/FullHttpMessageFormatter.php | 11 +- 2 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 spec/Formatter/FullHttpMessageFormatterSpec.php diff --git a/spec/Formatter/FullHttpMessageFormatterSpec.php b/spec/Formatter/FullHttpMessageFormatterSpec.php new file mode 100644 index 0000000..a1c4588 --- /dev/null +++ b/spec/Formatter/FullHttpMessageFormatterSpec.php @@ -0,0 +1,230 @@ +beConstructedWith($maxBodyLength); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Message\Formatter\FullHttpMessageFormatter'); + } + + function it_is_a_formatter() + { + $this->shouldImplement('Http\Message\Formatter'); + } + + function it_formats_the_request_with_size_limit(RequestInterface $request, StreamInterface $stream) + { + $this->beConstructedWith(18); + + $stream->isSeekable()->willReturn(true); + $stream->rewind()->shouldBeCalled(); + $stream->__toString()->willReturn('This is an HTML stream request content.'); + $request->getBody()->willReturn($stream); + $request->getMethod()->willReturn('GET'); + $request->getRequestTarget()->willReturn('/foo'); + $request->getProtocolVersion()->willReturn('1.1'); + $request->getHeaders()->willReturn([ + 'X-Param-Foo' => ['foo'], + 'X-Param-Bar' => ['bar'], + ]); + + $expectedMessage = <<formatRequest($request)->shouldReturn($expectedMessage); + } + + function it_formats_the_request_without_size_limit(RequestInterface $request, StreamInterface $stream) + { + $this->beConstructedWith(null); + + $stream->isSeekable()->willReturn(true); + $stream->rewind()->shouldBeCalled(); + $stream->__toString()->willReturn('This is an HTML stream request content.'); + $request->getBody()->willReturn($stream); + $request->getMethod()->willReturn('GET'); + $request->getRequestTarget()->willReturn('/foo'); + $request->getProtocolVersion()->willReturn('1.1'); + $request->getHeaders()->willReturn([ + 'X-Param-Foo' => ['foo'], + 'X-Param-Bar' => ['bar'], + ]); + + $expectedMessage = <<formatRequest($request)->shouldReturn($expectedMessage); + } + + function it_does_not_format_the_request(RequestInterface $request, StreamInterface $stream) + { + $this->beConstructedWith(0); + + $stream->isSeekable()->willReturn(true); + $stream->__toString()->willReturn('This is an HTML stream request content.'); + $request->getBody()->willReturn($stream); + $request->getMethod()->willReturn('GET'); + $request->getRequestTarget()->willReturn('/foo'); + $request->getProtocolVersion()->willReturn('1.1'); + $request->getHeaders()->willReturn([ + 'X-Param-Foo' => ['foo'], + 'X-Param-Bar' => ['bar'], + ]); + + $expectedMessage = <<formatRequest($request)->shouldReturn($expectedMessage); + } + + function it_does_not_format_no_seekable_request(RequestInterface $request, StreamInterface $stream) + { + $this->beConstructedWith(1000); + + $stream->isSeekable()->willReturn(false); + $stream->__toString()->willReturn('This is an HTML stream request content.'); + $request->getBody()->willReturn($stream); + $request->getMethod()->willReturn('GET'); + $request->getRequestTarget()->willReturn('/foo'); + $request->getProtocolVersion()->willReturn('1.1'); + $request->getHeaders()->willReturn([ + 'X-Param-Foo' => ['foo'], + 'X-Param-Bar' => ['bar'], + ]); + + $expectedMessage = <<formatRequest($request)->shouldReturn($expectedMessage); + } + + function it_formats_the_response_with_size_limit(ResponseInterface $response, StreamInterface $stream) + { + $this->beConstructedWith(18); + + $stream->isSeekable()->willReturn(true); + $stream->rewind()->shouldBeCalled(); + $stream->__toString()->willReturn('This is an HTML stream response content.'); + $response->getBody()->willReturn($stream); + $response->getProtocolVersion()->willReturn('1.1'); + $response->getStatusCode()->willReturn(200); + $response->getReasonPhrase()->willReturn('OK'); + $response->getHeaders()->willReturn([ + 'X-Param-Foo' => ['foo'], + 'X-Param-Bar' => ['bar'], + ]); + + $expectedMessage = <<formatResponse($response)->shouldReturn($expectedMessage); + } + + function it_formats_the_response_without_size_limit(ResponseInterface $response, StreamInterface $stream) + { + $this->beConstructedWith(null); + + $stream->isSeekable()->willReturn(true); + $stream->rewind()->shouldBeCalled(); + $stream->__toString()->willReturn('This is an HTML stream response content.'); + $response->getBody()->willReturn($stream); + $response->getProtocolVersion()->willReturn('1.1'); + $response->getStatusCode()->willReturn(200); + $response->getReasonPhrase()->willReturn('OK'); + $response->getHeaders()->willReturn([ + 'X-Param-Foo' => ['foo'], + 'X-Param-Bar' => ['bar'], + ]); + + $expectedMessage = <<formatResponse($response)->shouldReturn($expectedMessage); + } + + function it_does_not_format_the_response(ResponseInterface $response, StreamInterface $stream) + { + $this->beConstructedWith(0); + + $stream->isSeekable()->willReturn(true); + $stream->__toString()->willReturn('This is an HTML stream response content.'); + $response->getBody()->willReturn($stream); + $response->getProtocolVersion()->willReturn('1.1'); + $response->getStatusCode()->willReturn(200); + $response->getReasonPhrase()->willReturn('OK'); + $response->getHeaders()->willReturn([ + 'X-Param-Foo' => ['foo'], + 'X-Param-Bar' => ['bar'], + ]); + + $expectedMessage = <<formatResponse($response)->shouldReturn($expectedMessage); + } + + function it_does_not_format_no_seekable_response(ResponseInterface $response, StreamInterface $stream) + { + $this->beConstructedWith(1000); + + $stream->isSeekable()->willReturn(false); + $stream->__toString()->willReturn('This is an HTML stream response content.'); + $response->getBody()->willReturn($stream); + $response->getProtocolVersion()->willReturn('1.1'); + $response->getStatusCode()->willReturn(200); + $response->getReasonPhrase()->willReturn('OK'); + $response->getHeaders()->willReturn([ + 'X-Param-Foo' => ['foo'], + 'X-Param-Bar' => ['bar'], + ]); + + $expectedMessage = <<formatResponse($response)->shouldReturn($expectedMessage); + } +} diff --git a/src/Formatter/FullHttpMessageFormatter.php b/src/Formatter/FullHttpMessageFormatter.php index c163a2d..d065f0e 100644 --- a/src/Formatter/FullHttpMessageFormatter.php +++ b/src/Formatter/FullHttpMessageFormatter.php @@ -17,12 +17,12 @@ class FullHttpMessageFormatter implements Formatter /** * The maximum length of the body. * - * @var int + * @var int|null */ private $maxBodyLength; /** - * @param int $maxBodyLength + * @param int|null $maxBodyLength */ public function __construct($maxBodyLength = 1000) { @@ -83,7 +83,12 @@ private function addBody(MessageInterface $request, $message) return $message."\n"; } - $message .= "\n".mb_substr($stream->__toString(), 0, $this->maxBodyLength); + if (null === $this->maxBodyLength) { + $message .= "\n".$stream->__toString(); + } else { + $message .= "\n".mb_substr($stream->__toString(), 0, $this->maxBodyLength); + } + $stream->rewind(); return $message;