Skip to content

Support -E, --cert and --key options #36

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Console/Commands/CurlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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'),
];
}
}
21 changes: 21 additions & 0 deletions src/Models/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Request

private ?int $connectTimeout = null;

private array $options = [];

private function __construct($url, $method)
{
$this->url = $url;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Support/HttpCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/Console/Commands/CurlCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/with-cert-and-key.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl --cert /path/to/cert --key /path/to/key https://example.com
5 changes: 5 additions & 0 deletions tests/fixtures/with-cert-and-key.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Http::withOptions([
'cert' => '/path/to/cert',
'ssl_key' => '/path/to/key',
])
->get('https://example.com');
1 change: 1 addition & 0 deletions tests/fixtures/with-cert-path-and-password.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -E /path/to/cert:password https://example.com
7 changes: 7 additions & 0 deletions tests/fixtures/with-cert-path-and-password.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Http::withOptions([
'cert' => [
'/path/to/cert',
'password',
],
])
->get('https://example.com');
1 change: 1 addition & 0 deletions tests/fixtures/with-cert-path-only.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl --cert /path/to/cert https://example.com
4 changes: 4 additions & 0 deletions tests/fixtures/with-cert-path-only.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Http::withOptions([
'cert' => '/path/to/cert',
])
->get('https://example.com');