-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from hashbangcode/3_curl_output
3: Added curl output renderer and tests.
- Loading branch information
Showing
3 changed files
with
165 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Hashbangcode\CurlConverter\Output; | ||
|
||
use Hashbangcode\CurlConverter\CurlParametersInterface; | ||
|
||
/** | ||
* Class CurlOutput. | ||
* | ||
* @package Hashbangcode\CurlConverter\Output | ||
*/ | ||
class CurlOutput implements OutputInterface | ||
{ | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function render(CurlParametersInterface $curlParameters): string | ||
{ | ||
$output = 'curl '; | ||
|
||
if ($curlParameters->followRedirects()) { | ||
$output .= '--location '; | ||
} | ||
|
||
if ($curlParameters->getHttpVerb() !== 'GET') { | ||
$output .= '--request ' . $curlParameters->getHttpVerb() . ' '; | ||
} | ||
|
||
if ($curlParameters->isInsecure()) { | ||
$output .= '--insecure '; | ||
} | ||
|
||
if ($curlParameters->areCredentialsSet()) { | ||
$output .= '--user username:password '; | ||
} | ||
|
||
foreach ($curlParameters->getHeaders() as $header) { | ||
$output .= '-H \'' . $header . '\' '; | ||
} | ||
|
||
if ($curlParameters->hasData()) { | ||
$output .= '--data \'' . $curlParameters->getData() . '\' '; | ||
} | ||
|
||
$output .= $curlParameters->getUrl(); | ||
|
||
return $output; | ||
} | ||
|
||
} |
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,97 @@ | ||
<?php | ||
|
||
namespace Hashbangcode\CurlConverter\Output\Test; | ||
|
||
use Hashbangcode\CurlConverter\CurlParameters; | ||
use Hashbangcode\CurlConverter\Output\CurlOutput; | ||
use PHPUnit\Framework\TestCase; | ||
use Hashbangcode\CurlConverter\Output\PhpOutput; | ||
|
||
class CurlOutputTest extends TestCase | ||
{ | ||
public function testCurlOutput() | ||
{ | ||
$curlParameters = new CurlParameters(); | ||
$curlParameters->setUrl('https://www.hashbangcode.com/'); | ||
|
||
$phpOutput = new CurlOutput(); | ||
$output = $phpOutput->render($curlParameters); | ||
|
||
$this->assertEquals('curl https://www.hashbangcode.com/', $output); | ||
} | ||
|
||
public function testCurlFollowRedirectsOutput() | ||
{ | ||
$curlParameters = new CurlParameters(); | ||
$curlParameters->setUrl('https://www.hashbangcode.com/'); | ||
$curlParameters->setFollowRedirects(true); | ||
|
||
$curlOutput = new CurlOutput(); | ||
$output = $curlOutput->render($curlParameters); | ||
|
||
$this->assertEquals('curl --location https://www.hashbangcode.com/', $output); | ||
} | ||
|
||
public function testCurlHttpVerbOutput() { | ||
$curlParameters = new CurlParameters(); | ||
$curlParameters->setUrl('https://www.hashbangcode.com/'); | ||
$curlParameters->setFollowRedirects(true); | ||
$curlParameters->setHttpVerb('PUT'); | ||
|
||
$curlOutput = new CurlOutput(); | ||
$output = $curlOutput->render($curlParameters); | ||
|
||
$this->assertEquals('curl --location --request PUT https://www.hashbangcode.com/', $output); | ||
} | ||
|
||
public function testCurlHeadersOutput() { | ||
$curlParameters = new CurlParameters(); | ||
$curlParameters->setUrl('https://www.hashbangcode.com/'); | ||
$headers = [ | ||
'Cache-Control: max-age=0', | ||
'Accept-Language: en-GB', | ||
'Accept: text/html', | ||
]; | ||
$curlParameters->setHeaders($headers); | ||
|
||
$curlOutput = new CurlOutput(); | ||
$output = $curlOutput->render($curlParameters); | ||
|
||
$this->assertEquals('curl -H \'Cache-Control: max-age=0\' -H \'Accept-Language: en-GB\' -H \'Accept: text/html\' https://www.hashbangcode.com/', $output); | ||
} | ||
|
||
public function testCurlInsecureOutput() { | ||
$curlParameters = new CurlParameters(); | ||
$curlParameters->setUrl('https://www.hashbangcode.com/'); | ||
$curlParameters->setIsInsecure(true); | ||
|
||
$curlOutput = new CurlOutput(); | ||
$output = $curlOutput->render($curlParameters); | ||
|
||
$this->assertEquals('curl --insecure https://www.hashbangcode.com/', $output); | ||
} | ||
|
||
public function testCurlUsernameAndPasswordSetOutput() { | ||
$curlParameters = new CurlParameters(); | ||
$curlParameters->setUrl('https://www.hashbangcode.com/'); | ||
$curlParameters->setUsername('username'); | ||
$curlParameters->setPassword('password'); | ||
|
||
$curlOutput = new CurlOutput(); | ||
$output = $curlOutput->render($curlParameters); | ||
|
||
$this->assertEquals('curl --user username:password https://www.hashbangcode.com/', $output); | ||
} | ||
|
||
public function testCurlDataIsOutput() { | ||
$curlParameters = new CurlParameters(); | ||
$curlParameters->setUrl('https://www.hashbangcode.com/'); | ||
$curlParameters->setData('something=123'); | ||
$curlParameters->setHttpVerb('POST'); | ||
|
||
$curlOutput = new CurlOutput(); | ||
$output = $curlOutput->render($curlParameters); | ||
|
||
$this->assertEquals('curl --request POST --data \'something=123\' https://www.hashbangcode.com/', $output); | ||
} | ||
} |