-
Notifications
You must be signed in to change notification settings - Fork 39
Added cURL formatter #50
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
Changes from 2 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 hidden or 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,58 @@ | ||
<?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->__toString()->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\' --request GET -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'); | ||
|
||
$uri->__toString()->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\''); | ||
} | ||
|
||
function it_does_nothing_for_response(ResponseInterface $response) | ||
{ | ||
$this->formatResponse($response)->shouldReturn(''); | ||
} | ||
} |
This file contains hidden or 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,49 @@ | ||
<?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\'', $request->getUri()); | ||
if ($request->getProtocolVersion() === '1.0') { | ||
$command .= ' --http1.0'; | ||
} elseif ($request->getProtocolVersion() === '2.0') { | ||
$command .= ' --http2'; | ||
} | ||
|
||
$command .= ' --request '.$request->getMethod(); | ||
|
||
foreach ($request->getHeaders() as $name => $values) { | ||
$command .= sprintf(' -H \'%s: %s\'', $name, $request->getHeaderLine($name)); | ||
} | ||
|
||
$body = $request->getBody()->__toString(); | ||
if (!empty($body)) { | ||
$command .= sprintf(' --data \'%s\'', $body); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should espace the body (what give a json string with ' or " inside ?, need a test for this) |
||
} | ||
|
||
return $command; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function formatResponse(ResponseInterface $response) | ||
{ | ||
return ''; | ||
} | ||
} |
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.
We should really cloned the stream before formatting IMO, i cannot imagine the number of issue and debugging nightmare for users not understanding how streams work.
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.
Hm, tell me why I should clone it. Shouldn't I just rewind it just like the
FullHttpMessageFormatter
?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.
Rewind is not possible everwhere, i.e. in the socket-client you cannot rewind a stream, but don't handle that here, that was just a reminder.
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.
Okey, So what do you propose?
$body = clone $request->getBody()?
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.
#49 :)
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 was not watching that repo until today. Sorry, I've missed that PR. ..
Thank you!