-
Notifications
You must be signed in to change notification settings - Fork 40
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
Added cURL formatter #50
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5d03103
Added cURL formatter
Nyholm 6056307
style fix
Nyholm 26687cc
Escape body
Nyholm 105d882
Added tests for escaped body
Nyholm ebe5202
Make sure to rewind stream.
Nyholm bd069f7
Quote everything properly
Nyholm 24f35df
Update tests
Nyholm df39129
Fixes borrowed from namshi/cuzzle
Nyholm 2aa729b
Added credits section
Nyholm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace spec\Http\Message\Formatter; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class CurlCommandFormatterSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Message\Formatter\CurlCommandFormatter'); | ||
} | ||
|
||
function it_is_a_formatter() | ||
{ | ||
$this->shouldImplement('Http\Message\Formatter'); | ||
} | ||
|
||
function it_formats_the_request(RequestInterface $request, UriInterface $uri, StreamInterface $body) | ||
{ | ||
$request->getUri()->willReturn($uri); | ||
$request->getBody()->willReturn($body); | ||
|
||
$uri->withFragment('')->shouldBeCalled()->willReturn('http://foo.com/bar'); | ||
$request->getMethod()->willReturn('GET'); | ||
$request->getProtocolVersion()->willReturn('1.1'); | ||
|
||
$request->getHeaders()->willReturn(['foo'=>['bar', 'baz']]); | ||
$request->getHeaderLine('foo')->willReturn('bar, baz'); | ||
|
||
$this->formatRequest($request)->shouldReturn('curl \'http://foo.com/bar\' -H \'foo: bar, baz\''); | ||
} | ||
|
||
function it_formats_post_request(RequestInterface $request, UriInterface $uri, StreamInterface $body) | ||
{ | ||
$request->getUri()->willReturn($uri); | ||
$request->getBody()->willReturn($body); | ||
|
||
$body->__toString()->willReturn('body " data'." test' bar"); | ||
$body->getSize()->willReturn(1); | ||
$body->isSeekable()->willReturn(true); | ||
$body->rewind()->willReturn(true); | ||
|
||
$uri->withFragment('')->shouldBeCalled()->willReturn('http://foo.com/bar'); | ||
$request->getMethod()->willReturn('POST'); | ||
$request->getProtocolVersion()->willReturn('2.0'); | ||
|
||
$request->getHeaders()->willReturn([]); | ||
|
||
$this->formatRequest($request)->shouldReturn("curl 'http://foo.com/bar' --http2 --request POST --data 'body \" data test'\'' bar'"); | ||
} | ||
|
||
function it_does_nothing_for_response(ResponseInterface $response) | ||
{ | ||
$this->formatResponse($response)->shouldReturn(''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Http\Message\Formatter; | ||
|
||
use Http\Message\Formatter; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* A formatter that prints a cURL command for HTTP requests. | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class CurlCommandFormatter implements Formatter | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function formatRequest(RequestInterface $request) | ||
{ | ||
$command = sprintf('curl %s', escapeshellarg((string) $request->getUri()->withFragment(''))); | ||
if ($request->getProtocolVersion() === '1.0') { | ||
$command .= ' --http1.0'; | ||
} elseif ($request->getProtocolVersion() === '2.0') { | ||
$command .= ' --http2'; | ||
} | ||
|
||
$method = strtoupper($request->getMethod()); | ||
if ('HEAD' === $method) { | ||
$command .= ' --head'; | ||
} elseif ('GET' !== $method) { | ||
$command .= ' --request '.$method; | ||
} | ||
|
||
$command .= $this->getHeadersAsCommandOptions($request); | ||
|
||
$body = $request->getBody(); | ||
if ($body->getSize() > 0) { | ||
if (!$body->isSeekable()) { | ||
return 'Cant format Request as cUrl command if body stream is not seekable.'; | ||
} | ||
$command .= sprintf(' --data %s', escapeshellarg($body->__toString())); | ||
$body->rewind(); | ||
} | ||
|
||
return $command; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function formatResponse(ResponseInterface $response) | ||
{ | ||
return ''; | ||
} | ||
|
||
/** | ||
* @param RequestInterface $request | ||
* | ||
* @return string | ||
*/ | ||
private function getHeadersAsCommandOptions(RequestInterface $request) | ||
{ | ||
$command = ''; | ||
foreach ($request->getHeaders() as $name => $values) { | ||
if ('host' === strtolower($name) && $values[0] === $request->getUri()->getHost()) { | ||
continue; | ||
} | ||
|
||
if ('user-agent' === strtolower($name)) { | ||
$command .= sprintf('-A %s', escapeshellarg($values[0])); | ||
continue; | ||
} | ||
|
||
$command .= sprintf(' -H %s', escapeshellarg($name.': '.$request->getHeaderLine($name))); | ||
} | ||
|
||
return $command; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder if we should have some sort of sanity check to not put huge files in here? say if the body size > 5000 we truncate or just do something like [too large body] in the command? if we use this formatter eg in the symfony debug toolbar, it could break on large files