Skip to content

Commit

Permalink
Merge pull request #92 from z38/binary-body
Browse files Browse the repository at this point in the history
Improve formatting of requests with binary streams
  • Loading branch information
Nyholm committed Jan 22, 2018
2 parents 977edb5 + 3b410c5 commit c7f6a70
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Change Log


## Unreleased

### Fixed

- Fix CurlCommandFormatter for binary request payloads


## 1.6.0 - 2017-07-05

### Added
Expand Down
36 changes: 36 additions & 0 deletions spec/Formatter/CurlCommandFormatterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,40 @@ function it_formats_the_request_with_user_agent(RequestInterface $request, UriIn

$this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' -A 'foobar-browser'");
}

function it_formats_requests_with_null_bytes(RequestInterface $request, UriInterface $uri, StreamInterface $body)
{
$request->getUri()->willReturn($uri);
$request->getBody()->willReturn($body);

$body->__toString()->willReturn("\0");
$body->getSize()->willReturn(1);
$body->isSeekable()->willReturn(true);
$body->rewind()->willReturn(true);

$uri->withFragment('')->willReturn('http://foo.com/bar');
$request->getMethod()->willReturn('POST');
$request->getProtocolVersion()->willReturn('1.1');
$request->getHeaders()->willReturn([]);

$this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --request POST --data '[binary stream omitted]'");
}

function it_formats_requests_with_nonseekable_body(RequestInterface $request, UriInterface $uri, StreamInterface $body)
{
$request->getUri()->willReturn($uri);
$request->getBody()->willReturn($body);

$body->getSize()->willReturn(1);
$body->isSeekable()->willReturn(false);
$body->__toString()->shouldNotBeCalled();
$body->rewind()->shouldNotBeCalled();

$uri->withFragment('')->willReturn('http://foo.com/bar');
$request->getMethod()->willReturn('POST');
$request->getProtocolVersion()->willReturn('1.1');
$request->getHeaders()->willReturn([]);

$this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --request POST --data '[non-seekable stream omitted]'");
}
}
13 changes: 9 additions & 4 deletions src/Formatter/CurlCommandFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ public function formatRequest(RequestInterface $request)

$body = $request->getBody();
if ($body->getSize() > 0) {
if (!$body->isSeekable()) {
return 'Cant format Request as cUrl command if body stream is not seekable.';
if ($body->isSeekable()) {
$data = $body->__toString();
$body->rewind();
if (preg_match('/[\x00-\x1F\x7F]/', $data)) {
$data = '[binary stream omitted]';
}
} else {
$data = '[non-seekable stream omitted]';
}
$command .= sprintf(' --data %s', escapeshellarg($body->__toString()));
$body->rewind();
$command .= sprintf(' --data %s', escapeshellarg($data));
}

return $command;
Expand Down

0 comments on commit c7f6a70

Please sign in to comment.