diff --git a/src/Console/Commands/CurlCommand.php b/src/Console/Commands/CurlCommand.php index 8ea27cd..f93787a 100644 --- a/src/Console/Commands/CurlCommand.php +++ b/src/Console/Commands/CurlCommand.php @@ -7,7 +7,7 @@ class CurlCommand extends Command { - protected $signature = 'shift:curl {--X|request=} {--G|get} {--H|header=*} {--d|data=*} {--data-urlencode=*} {--data-raw=*} {--F|form=*} {--digest} {--basic} {--connect-timeout=} {--max-timeout=} {--retry=} {--s|silent} {--u|user=} {--L|location} {--compressed} {--insecure} {url}'; + protected $signature = 'shift:curl {--X|request=} {--G|get} {--H|header=*} {--d|data=*} {--data-urlencode=*} {--data-raw=*} {--F|form=*} {--digest} {--basic} {--connect-timeout=} {--max-timeout=} {--retry=} {--s|silent} {--u|user=} {--L|location} {--compressed} {--insecure} {--E|cert=} {--key=} {url}'; protected $description = 'Convert a UNIX curl request to an HTTP Client request'; @@ -42,6 +42,8 @@ private function gatherOptions() 'user' => $this->option('user'), 'compressed' => $this->option('compressed'), 'insecure' => $this->option('insecure'), + 'cert' => $this->option('cert'), + 'key' => $this->option('key'), ]; } } diff --git a/src/Models/Request.php b/src/Models/Request.php index 8670e70..fbb0488 100644 --- a/src/Models/Request.php +++ b/src/Models/Request.php @@ -27,6 +27,8 @@ class Request private ?int $connectTimeout = null; + private array $options = []; + private function __construct($url, $method) { $this->url = $url; @@ -106,6 +108,20 @@ public static function create(array $data): self $request->connectTimeout = $data['connectTimeout']; } + if (isset($data['cert'])) { + @[$certificate, $password] = explode(':', $data['cert'], 2); + + if (isset($password)) { + $request->options['cert'] = [$certificate, $password]; + } else { + $request->options['cert'] = $certificate; + } + } + + if (isset($data['key'])) { + $request->options['ssl_key'] = $data['key']; + } + return $request; } @@ -174,6 +190,11 @@ public function connectTimeout(): int return $this->connectTimeout; } + public function options(): array + { + return $this->options; + } + private static function convertDataType(string $value) { return preg_match('/^[1-9]\d*$/', $value) ? intval($value) : $value; diff --git a/src/Support/HttpCall.php b/src/Support/HttpCall.php index 5873237..1a89315 100644 --- a/src/Support/HttpCall.php +++ b/src/Support/HttpCall.php @@ -93,6 +93,10 @@ private static function generateOptions(Request $request): string $options[] = 'connectTimeout(' . $request->connectTimeout() . ')'; } + if (!empty($request->options())) { + $options[] = 'withOptions(' . self::prettyPrintArray($request->options()) . ')'; + } + if (empty($options)) { return ''; } diff --git a/tests/Feature/Console/Commands/CurlCommandTest.php b/tests/Feature/Console/Commands/CurlCommandTest.php index 83c74d3..2470b0f 100644 --- a/tests/Feature/Console/Commands/CurlCommandTest.php +++ b/tests/Feature/Console/Commands/CurlCommandTest.php @@ -63,6 +63,9 @@ public function curlCommandFixtures() 'GET request with insecure flag' => ['with-insecure-option'], 'Request with raw data' => ['with-raw-data'], 'POST request with mixed data' => ['raw-data-mixed'], + 'Request with cert (path only)' => ['with-cert-path-only'], + 'Request with cert (path and password)' => ['with-cert-path-and-password'], + 'Request with cert and key' => ['with-cert-and-key'], ]; } diff --git a/tests/fixtures/with-cert-and-key.in b/tests/fixtures/with-cert-and-key.in new file mode 100644 index 0000000..c303e16 --- /dev/null +++ b/tests/fixtures/with-cert-and-key.in @@ -0,0 +1 @@ +curl --cert /path/to/cert --key /path/to/key https://example.com diff --git a/tests/fixtures/with-cert-and-key.out b/tests/fixtures/with-cert-and-key.out new file mode 100644 index 0000000..3a9bbd4 --- /dev/null +++ b/tests/fixtures/with-cert-and-key.out @@ -0,0 +1,5 @@ +Http::withOptions([ + 'cert' => '/path/to/cert', + 'ssl_key' => '/path/to/key', + ]) + ->get('https://example.com'); diff --git a/tests/fixtures/with-cert-path-and-password.in b/tests/fixtures/with-cert-path-and-password.in new file mode 100644 index 0000000..99bdbdf --- /dev/null +++ b/tests/fixtures/with-cert-path-and-password.in @@ -0,0 +1 @@ +curl -E /path/to/cert:password https://example.com diff --git a/tests/fixtures/with-cert-path-and-password.out b/tests/fixtures/with-cert-path-and-password.out new file mode 100644 index 0000000..635128c --- /dev/null +++ b/tests/fixtures/with-cert-path-and-password.out @@ -0,0 +1,7 @@ +Http::withOptions([ + 'cert' => [ + '/path/to/cert', + 'password', + ], + ]) + ->get('https://example.com'); diff --git a/tests/fixtures/with-cert-path-only.in b/tests/fixtures/with-cert-path-only.in new file mode 100644 index 0000000..fd0d0f9 --- /dev/null +++ b/tests/fixtures/with-cert-path-only.in @@ -0,0 +1 @@ +curl --cert /path/to/cert https://example.com diff --git a/tests/fixtures/with-cert-path-only.out b/tests/fixtures/with-cert-path-only.out new file mode 100644 index 0000000..234d91d --- /dev/null +++ b/tests/fixtures/with-cert-path-only.out @@ -0,0 +1,4 @@ +Http::withOptions([ + 'cert' => '/path/to/cert', + ]) + ->get('https://example.com');