Skip to content

Handle null explicitly for max body length of FullHttpMessageFormatter #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 230 additions & 0 deletions spec/Formatter/FullHttpMessageFormatterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php

namespace spec\Http\Message\Formatter;

use PhpSpec\ObjectBehavior;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class FullHttpMessageFormatterSpec extends ObjectBehavior
{
function let($maxBodyLength)
{
$this->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 = <<<STR
GET /foo HTTP/1.1
X-Param-Foo: foo
X-Param-Bar: bar

This is an HTML st
STR;
$this->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 = <<<STR
GET /foo HTTP/1.1
X-Param-Foo: foo
X-Param-Bar: bar

This is an HTML stream request content.
STR;
$this->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 = <<<STR
GET /foo HTTP/1.1
X-Param-Foo: foo
X-Param-Bar: bar


STR;
$this->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 = <<<STR
GET /foo HTTP/1.1
X-Param-Foo: foo
X-Param-Bar: bar


STR;
$this->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 = <<<STR
HTTP/1.1 200 OK
X-Param-Foo: foo
X-Param-Bar: bar

This is an HTML st
STR;
$this->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 = <<<STR
HTTP/1.1 200 OK
X-Param-Foo: foo
X-Param-Bar: bar

This is an HTML stream response content.
STR;
$this->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 = <<<STR
HTTP/1.1 200 OK
X-Param-Foo: foo
X-Param-Bar: bar


STR;
$this->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 = <<<STR
HTTP/1.1 200 OK
X-Param-Foo: foo
X-Param-Bar: bar


STR;
$this->formatResponse($response)->shouldReturn($expectedMessage);
}
}
11 changes: 8 additions & 3 deletions src/Formatter/FullHttpMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down