Skip to content
Merged
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|curl-silent} {--u|user=} {--L|location} {--compressed} {--k|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|curl-silent} {--u|user=} {--L|location} {--compressed} {--k|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 bool $insecure = false;

private function __construct($url, $method)
Expand Down Expand Up @@ -108,6 +110,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'];
}

if ($data['insecure']) {
$request->insecure = true;
}
Expand Down Expand Up @@ -180,6 +196,11 @@ public function connectTimeout(): int
return $this->connectTimeout;
}

public function options(): array
{
return $this->options;
}

public function isInsecure(): bool
{
return $this->insecure;
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 ($request->isInsecure()) {
$options[] = 'withoutVerifying()';
}
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 @@ -64,6 +64,9 @@ public static function curlCommandFixtures(): array
'GET request with short k flag' => ['with-insecure-k-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');
Loading