Skip to content

Commit 7488f49

Browse files
authored
Merge pull request #243 from bowphp/5.x-refactoring-for-perf
[5.x] Fixes testcase service errors
2 parents 5cb3710 + 7f6f223 commit 7488f49

4 files changed

Lines changed: 107 additions & 8 deletions

File tree

src/Http/Client/HttpClient.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ class HttpClient
1515
*/
1616
private $attach = [];
1717

18+
/**
19+
* The headers collection
20+
*
21+
* @var array
22+
*/
23+
private $headers = [];
24+
1825
/**
1926
* The curl instance
2027
*
@@ -146,6 +153,7 @@ public function delete(string $url, array $data = []): Response
146153
$this->applyCommonOptions();
147154

148155
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "DELETE");
156+
149157
$content = $this->execute();
150158

151159
return new Response($this->ch, $content);
@@ -170,14 +178,10 @@ public function addAttach(string|array $attach): array
170178
*/
171179
public function addHeaders(array $headers): HttpClient
172180
{
173-
$data = [];
174-
175181
foreach ($headers as $key => $value) {
176-
$data[] = $key . ': ' . $value;
182+
$this->headers[] = $key . ': ' . $value;
177183
}
178184

179-
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $data);
180-
181185
return $this;
182186
}
183187

@@ -189,7 +193,7 @@ public function addHeaders(array $headers): HttpClient
189193
*/
190194
private function init(string $url): void
191195
{
192-
if (is_null($this->base_url)) {
196+
if (!is_null($this->base_url)) {
193197
$url = $this->base_url . "/" . trim($url, "/");
194198
}
195199

@@ -227,6 +231,10 @@ private function close(): void
227231
*/
228232
private function execute(): string
229233
{
234+
if ($this->headers) {
235+
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
236+
}
237+
230238
$content = curl_exec($this->ch);
231239
$errno = curl_errno($this->ch);
232240

src/Testing/TestCase.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,31 @@ public function attach(array $attach): TestCase
5858
}
5959

6060
/**
61-
* Specify the additionnal who are use in the request
61+
* Specify the additionnal headers
6262
*
6363
* @param array $headers
6464
* @return TestCase
6565
*/
66-
public function withHeader(array $headers): TestCase
66+
public function withHeaders(array $headers): TestCase
6767
{
6868
$this->headers = $headers;
6969

7070
return $this;
7171
}
7272

73+
/**
74+
* Specify the additionnal header
75+
*
76+
* @param array $headers
77+
* @return TestCase
78+
*/
79+
public function withHeader(string $key, string $value): TestCase
80+
{
81+
$this->headers[$key] = $value;
82+
83+
return $this;
84+
}
85+
7386
/**
7487
* Get request
7588
*

tests/Support/HttpClientTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Bow\Tests\Support;
4+
5+
use Bow\Http\Client\HttpClient;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class HttpClientTest extends TestCase
9+
{
10+
public function test_get_method()
11+
{
12+
$http = new HttpClient();
13+
14+
$response = $http->get("https://google.com");
15+
16+
$this->assertEquals($response->statusCode(), 200);
17+
}
18+
19+
public function test_get_method_with_custom_headers()
20+
{
21+
$http = new HttpClient();
22+
23+
$http->addHeaders(["X-Api-Key" => "Fake-Key"]);
24+
$response = $http->get("https://google.com");
25+
26+
$this->assertEquals($response->statusCode(), 200);
27+
}
28+
29+
public function test_should_be_fail_with_get_method()
30+
{
31+
$http = new HttpClient("https://google.com");
32+
33+
$http->addHeaders(["X-Api-Key" => "Fake-Key"]);
34+
$response = $http->get("/the-fake-url");
35+
36+
$this->assertEquals($response->statusCode(), 404);
37+
}
38+
}

tests/Support/TestingTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Bow\Tests\Support;
4+
5+
use Bow\Testing\TestCase;
6+
7+
class TestingTest extends TestCase
8+
{
9+
/**
10+
* The base url
11+
*
12+
* @var string
13+
*/
14+
protected ?string $url = "https://google.com";
15+
16+
public function test_get_method()
17+
{
18+
$response = $this->get("/");
19+
20+
$response->assertStatus(200);
21+
}
22+
23+
public function test_get_method_with_custom_headers()
24+
{
25+
$this->withHeaders(["X-Api-Key" => "Fake-Key"]);
26+
27+
$response = $this->get("/");
28+
29+
$response->assertStatus(200);
30+
}
31+
32+
public function test_should_be_fail_with_get_method()
33+
{
34+
$this->withHeaders(["X-Api-Key" => "Fake-Key"]);
35+
36+
$response = $this->get("/the-fake-url-for-my-testing-please-do-not-block-this");
37+
38+
$response->assertStatus(404);
39+
}
40+
}

0 commit comments

Comments
 (0)