From 50296e8fe530675c4204fc7719ecd883b1e1d7ec Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Wed, 24 May 2023 17:30:37 +0000 Subject: [PATCH 1/2] Fixes http client errors from testcase service --- src/Http/Client/HttpClient.php | 20 ++++++++++++++------ src/Testing/TestCase.php | 17 +++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Http/Client/HttpClient.php b/src/Http/Client/HttpClient.php index ac0777d5..e05695fb 100644 --- a/src/Http/Client/HttpClient.php +++ b/src/Http/Client/HttpClient.php @@ -15,6 +15,13 @@ class HttpClient */ private $attach = []; + /** + * The headers collection + * + * @var array + */ + private $headers = []; + /** * The curl instance * @@ -146,6 +153,7 @@ public function delete(string $url, array $data = []): Response $this->applyCommonOptions(); curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "DELETE"); + $content = $this->execute(); return new Response($this->ch, $content); @@ -170,14 +178,10 @@ public function addAttach(string|array $attach): array */ public function addHeaders(array $headers): HttpClient { - $data = []; - foreach ($headers as $key => $value) { - $data[] = $key . ': ' . $value; + $this->headers[] = $key . ': ' . $value; } - curl_setopt($this->ch, CURLOPT_HTTPHEADER, $data); - return $this; } @@ -189,7 +193,7 @@ public function addHeaders(array $headers): HttpClient */ private function init(string $url): void { - if (is_null($this->base_url)) { + if (!is_null($this->base_url)) { $url = $this->base_url . "/" . trim($url, "/"); } @@ -227,6 +231,10 @@ private function close(): void */ private function execute(): string { + if ($this->headers) { + curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers); + } + $content = curl_exec($this->ch); $errno = curl_errno($this->ch); diff --git a/src/Testing/TestCase.php b/src/Testing/TestCase.php index 074d6351..4727c083 100644 --- a/src/Testing/TestCase.php +++ b/src/Testing/TestCase.php @@ -58,18 +58,31 @@ public function attach(array $attach): TestCase } /** - * Specify the additionnal who are use in the request + * Specify the additionnal headers * * @param array $headers * @return TestCase */ - public function withHeader(array $headers): TestCase + public function withHeaders(array $headers): TestCase { $this->headers = $headers; return $this; } + /** + * Specify the additionnal header + * + * @param array $headers + * @return TestCase + */ + public function withHeader(string $key, string $value): TestCase + { + $this->headers[$key] = $value; + + return $this; + } + /** * Get request * From f95e9807e11ca14e2f2bdee4231bd68d24f86e44 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Wed, 24 May 2023 17:30:50 +0000 Subject: [PATCH 2/2] Add unity tests --- tests/Support/HttpClientTest.php | 38 ++++++++++++++++++++++++++++++ tests/Support/TestingTest.php | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/Support/HttpClientTest.php create mode 100644 tests/Support/TestingTest.php diff --git a/tests/Support/HttpClientTest.php b/tests/Support/HttpClientTest.php new file mode 100644 index 00000000..13a0d3f4 --- /dev/null +++ b/tests/Support/HttpClientTest.php @@ -0,0 +1,38 @@ +get("https://google.com"); + + $this->assertEquals($response->statusCode(), 200); + } + + public function test_get_method_with_custom_headers() + { + $http = new HttpClient(); + + $http->addHeaders(["X-Api-Key" => "Fake-Key"]); + $response = $http->get("https://google.com"); + + $this->assertEquals($response->statusCode(), 200); + } + + public function test_should_be_fail_with_get_method() + { + $http = new HttpClient("https://google.com"); + + $http->addHeaders(["X-Api-Key" => "Fake-Key"]); + $response = $http->get("/the-fake-url"); + + $this->assertEquals($response->statusCode(), 404); + } +} diff --git a/tests/Support/TestingTest.php b/tests/Support/TestingTest.php new file mode 100644 index 00000000..5f10c275 --- /dev/null +++ b/tests/Support/TestingTest.php @@ -0,0 +1,40 @@ +get("/"); + + $response->assertStatus(200); + } + + public function test_get_method_with_custom_headers() + { + $this->withHeaders(["X-Api-Key" => "Fake-Key"]); + + $response = $this->get("/"); + + $response->assertStatus(200); + } + + public function test_should_be_fail_with_get_method() + { + $this->withHeaders(["X-Api-Key" => "Fake-Key"]); + + $response = $this->get("/the-fake-url-for-my-testing-please-do-not-block-this"); + + $response->assertStatus(404); + } +}