From 4b4d25b0e68c000fc202efe941284f760b6791db Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Thu, 13 Oct 2022 11:02:13 -0400 Subject: [PATCH 1/2] Support `--data-raw` --- src/Console/Commands/CurlCommand.php | 3 +- src/Models/Request.php | 29 ++++++++++++------- src/Support/HttpCall.php | 6 ++-- .../Console/Commands/CurlCommandTest.php | 2 ++ tests/fixtures/raw-data-mixed.in | 1 + tests/fixtures/raw-data-mixed.out | 6 ++++ tests/fixtures/with-raw-data.in | 7 +++++ tests/fixtures/with-raw-data.out | 9 ++++++ 8 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 tests/fixtures/raw-data-mixed.in create mode 100644 tests/fixtures/raw-data-mixed.out create mode 100644 tests/fixtures/with-raw-data.in create mode 100644 tests/fixtures/with-raw-data.out diff --git a/src/Console/Commands/CurlCommand.php b/src/Console/Commands/CurlCommand.php index aebaaf2..8ea27cd 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=*} {--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} {url}'; protected $description = 'Convert a UNIX curl request to an HTTP Client request'; @@ -30,6 +30,7 @@ private function gatherOptions() 'url' => $this->argument('url'), 'headers' => $this->option('header'), 'data' => $this->option('data'), + 'rawData' => $this->option('data-raw'), 'dataUrlEncode' => $this->option('data-urlencode'), 'fields' => $this->option('form'), 'digest' => $this->option('digest'), diff --git a/src/Models/Request.php b/src/Models/Request.php index d30a89a..fa0d556 100644 --- a/src/Models/Request.php +++ b/src/Models/Request.php @@ -14,7 +14,7 @@ class Request private array $data = []; - private bool $jsonData = false; + private bool $rawData = false; private bool $multipartFormData = false; @@ -52,12 +52,25 @@ public static function create(array $data): self ->all(); } + if (!empty($data['dataUrlEncode'])) { + $request->data = array_merge($request->data, self::parseData($data['dataUrlEncode'])); + } + + if (!empty($data['rawData'])) { + if (count($data['rawData']) === 1 && empty($data['data']) && empty($data['dataUrlEncode'])) { + $request->data = $data['rawData']; + $request->rawData = true; + } else { + $request->data = array_merge($request->data, self::parseData($data['rawData'])); + } + } + if (!empty($data['data'])) { if (count($data['data']) === 1 && Str::startsWith($data['data'][0], '{')) { $request->data = $data['data']; - $request->jsonData = true; + $request->rawData = true; } else { - $request->data = self::parseData($data['data']); + $request->data = array_merge($request->data, self::parseData($data['data'])); } } @@ -66,11 +79,7 @@ public static function create(array $data): self $request->multipartFormData = true; } - if (!empty($data['dataUrlEncode'])) { - $request->data = self::parseData($data['dataUrlEncode']); - } - - if (is_null($data['method']) && (!empty($data['data']) || !empty($data['fields']))) { + if (is_null($data['method']) && (!empty($data['rawData']) || !empty($data['data']) || !empty($data['fields'] ))) { $request->method = 'POST'; } @@ -114,9 +123,9 @@ public function headers(): array return $this->headers; } - public function isJsonData(): bool + public function isRawData(): bool { - return $this->jsonData; + return $this->rawData; } public function method(): string diff --git a/src/Support/HttpCall.php b/src/Support/HttpCall.php index ee13989..5873237 100644 --- a/src/Support/HttpCall.php +++ b/src/Support/HttpCall.php @@ -67,8 +67,8 @@ private static function generateOptions(Request $request): string if (!empty($request->data()) && $request->method() !== 'GET') { if ($request->isMultipartFormData()) { $options[] = 'asMultipart()'; - } elseif ($request->isJsonData()) { - $options[] = 'withBody(\'' . $request->data()[0] . '\')'; + } elseif ($request->isRawData()) { + $options[] = 'withBody(\'' . current($request->data()) . '\')'; } else { $options[] = 'asForm()'; } @@ -102,7 +102,7 @@ private static function generateOptions(Request $request): string private static function generateRequest(Request $request): string { - if (empty($request->data()) || $request->isJsonData()) { + if (empty($request->data()) || $request->isRawData()) { return "'" . $request->url() . "'"; } diff --git a/tests/Feature/Console/Commands/CurlCommandTest.php b/tests/Feature/Console/Commands/CurlCommandTest.php index db62d3d..9efb5a0 100644 --- a/tests/Feature/Console/Commands/CurlCommandTest.php +++ b/tests/Feature/Console/Commands/CurlCommandTest.php @@ -44,6 +44,8 @@ public function curlCommandFixtures() 'Missing URL scheme' => ['missing-url-scheme'], 'GET request with compressed flag' => ['with-compressed-option'], 'GET request with insecure flag' => ['with-insecure-option'], + 'Request with raw data' => ['with-raw-data'], + 'POST request with mixed data' => ['raw-data-mixed'], ]; } diff --git a/tests/fixtures/raw-data-mixed.in b/tests/fixtures/raw-data-mixed.in new file mode 100644 index 0000000..df6b709 --- /dev/null +++ b/tests/fixtures/raw-data-mixed.in @@ -0,0 +1 @@ +curl --request POST 'https://api.com' --header 'Accept: application/json' --data-raw 'some=data' -d foo=bar diff --git a/tests/fixtures/raw-data-mixed.out b/tests/fixtures/raw-data-mixed.out new file mode 100644 index 0000000..92841a3 --- /dev/null +++ b/tests/fixtures/raw-data-mixed.out @@ -0,0 +1,6 @@ +Http::asForm() + ->acceptJson() + ->post('https://api.com', [ + 'some' => 'data', + 'foo' => 'bar', + ]); diff --git a/tests/fixtures/with-raw-data.in b/tests/fixtures/with-raw-data.in new file mode 100644 index 0000000..026c832 --- /dev/null +++ b/tests/fixtures/with-raw-data.in @@ -0,0 +1,7 @@ +curl 'https://api.com' --header 'Accept: application/json' --header 'Content-Type: application/json' --data-raw '{ + "messages": [ + "a", + "b", + "c" + ] +}' diff --git a/tests/fixtures/with-raw-data.out b/tests/fixtures/with-raw-data.out new file mode 100644 index 0000000..2ec6a26 --- /dev/null +++ b/tests/fixtures/with-raw-data.out @@ -0,0 +1,9 @@ +Http::withBody('{ + "messages": [ + "a", + "b", + "c" + ] +}') + ->acceptJson() + ->post('https://api.com'); From c1e0bec6337b1f7a63fff1b5e8407a865ce1e292 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Thu, 13 Oct 2022 11:07:52 -0400 Subject: [PATCH 2/2] Fix lint --- src/Models/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/Request.php b/src/Models/Request.php index fa0d556..5338780 100644 --- a/src/Models/Request.php +++ b/src/Models/Request.php @@ -79,7 +79,7 @@ public static function create(array $data): self $request->multipartFormData = true; } - if (is_null($data['method']) && (!empty($data['rawData']) || !empty($data['data']) || !empty($data['fields'] ))) { + if (is_null($data['method']) && (!empty($data['rawData']) || !empty($data['data']) || !empty($data['fields']))) { $request->method = 'POST'; }