Skip to content

Commit 4fb561e

Browse files
committed
Update http client test
1 parent 28b8991 commit 4fb561e

File tree

4 files changed

+149
-10
lines changed

4 files changed

+149
-10
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ jobs:
6767
run: if [ ! -d /tmp/bowphp_testing ]; then mkdir -p /tmp/bowphp_testing; fi;
6868

6969
- name: Run test suite
70-
run: sudo composer run-script test
70+
run: sudo composer run-script test || sudo composer run-script testdox

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"scripts": {
6969
"phpcbf": "phpcbf --standard=psr12 --severity=4 --tab-width=4 src tests",
7070
"phpcs": "phpcs --standard=psr12 --severity=4 --tab-width=4 src",
71-
"test": "phpunit --configuration phpunit.dist.xml"
71+
"test": "phpunit --configuration phpunit.dist.xml",
72+
"testdox": "phpunit --configuration phpunit.dist.xml --testdox"
7273
}
7374
}

tests/Support/HttpClientTest.php

Lines changed: 143 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,167 @@
77

88
class HttpClientTest extends TestCase
99
{
10-
public function test_get_method()
10+
// ==================== GET Method Tests ====================
11+
12+
public function test_get_method_fails_with_invalid_domain()
1113
{
1214
$http = new HttpClient();
13-
1415
$response = $http->get("https://www.oogle.com");
1516

16-
$this->assertEquals($response->statusCode(), 503);
17+
$this->assertEquals(503, $response->statusCode());
1718
}
1819

19-
public function test_get_method_with_custom_headers()
20+
public function test_get_method_succeeds_with_valid_url()
2021
{
2122
$http = new HttpClient();
23+
$response = $http->get("https://www.google.com");
24+
25+
$this->assertEquals(200, $response->statusCode());
26+
}
2227

28+
public function test_get_method_with_custom_headers()
29+
{
30+
$http = new HttpClient();
2331
$http->addHeaders(["X-Api-Key" => "Fake-Key"]);
32+
2433
$response = $http->get("https://www.google.com");
2534

26-
$this->assertEquals($response->statusCode(), 200);
35+
$this->assertEquals(200, $response->statusCode());
2736
}
2837

29-
public function test_should_be_fail_with_get_method()
38+
public function test_get_method_fails_with_non_existent_path()
3039
{
3140
$http = new HttpClient("https://www.google.com");
32-
3341
$http->addHeaders(["X-Api-Key" => "Fake-Key"]);
42+
3443
$response = $http->get("/the-fake-url");
3544

36-
$this->assertEquals($response->statusCode(), 404);
45+
$this->assertEquals(404, $response->statusCode());
46+
}
47+
48+
public function test_get_method_with_base_url_in_constructor()
49+
{
50+
$http = new HttpClient("https://www.google.com");
51+
$response = $http->get("/");
52+
53+
$this->assertEquals(200, $response->statusCode());
54+
}
55+
56+
// ==================== POST Method Tests ====================
57+
58+
public function test_post_method_with_data()
59+
{
60+
$http = new HttpClient();
61+
$response = $http->post("https://httpbin.org/post", [
62+
'name' => 'test',
63+
'value' => 'example'
64+
]);
65+
66+
$this->assertEquals(200, $response->statusCode());
67+
$this->assertStringContainsString('test', $response->getContent());
68+
}
69+
70+
public function test_post_method_with_json_data()
71+
{
72+
$http = new HttpClient();
73+
$http->addHeaders(['Content-Type' => 'application/json']);
74+
75+
$response = $http->post("https://httpbin.org/post", [
76+
'name' => 'test',
77+
'value' => 'example'
78+
]);
79+
80+
$this->assertEquals(200, $response->statusCode());
81+
}
82+
83+
// ==================== PUT Method Tests ====================
84+
85+
public function test_put_method_with_data()
86+
{
87+
$http = new HttpClient();
88+
$response = $http->put("https://httpbin.org/put", [
89+
'name' => 'updated',
90+
'value' => 'example'
91+
]);
92+
93+
$this->assertEquals(200, $response->statusCode());
94+
$this->assertStringContainsString('updated', $response->getContent());
95+
}
96+
97+
// ==================== DELETE Method Tests ====================
98+
99+
public function test_delete_method()
100+
{
101+
$http = new HttpClient();
102+
$response = $http->delete("https://httpbin.org/delete");
103+
104+
$this->assertEquals(200, $response->statusCode());
105+
}
106+
107+
// ==================== Header Tests ====================
108+
109+
public function test_add_multiple_headers()
110+
{
111+
$http = new HttpClient();
112+
$http->addHeaders([
113+
"X-Api-Key" => "test-key",
114+
"X-Custom-Header" => "custom-value"
115+
]);
116+
117+
$response = $http->get("https://httpbin.org/headers");
118+
119+
$this->assertEquals(200, $response->statusCode());
120+
$this->assertStringContainsString('test-key', $response->getContent());
121+
}
122+
123+
public function test_user_agent_header()
124+
{
125+
$http = new HttpClient();
126+
$http->addHeaders(["User-Agent" => "BowFramework/1.0"]);
127+
128+
$response = $http->get("https://httpbin.org/user-agent");
129+
130+
$this->assertEquals(200, $response->statusCode());
131+
$this->assertStringContainsString('BowFramework', $response->getContent());
132+
}
133+
134+
// ==================== Response Tests ====================
135+
136+
public function test_response_body_is_retrievable()
137+
{
138+
$http = new HttpClient();
139+
$response = $http->get("https://www.google.com");
140+
141+
$body = $response->getContent();
142+
143+
$this->assertNotEmpty($body);
144+
$this->assertIsString($body);
145+
}
146+
147+
public function test_response_status_code_is_correct()
148+
{
149+
$http = new HttpClient();
150+
$response = $http->get("https://httpbin.org/status/201");
151+
152+
$this->assertEquals(201, $response->statusCode());
153+
}
154+
155+
// ==================== Error Handling Tests ====================
156+
157+
public function test_timeout_handling()
158+
{
159+
$http = new HttpClient();
160+
// This should work or timeout gracefully
161+
$response = $http->get("https://httpbin.org/delay/1");
162+
163+
$this->assertIsInt($response->statusCode());
164+
}
165+
166+
public function test_redirect_following()
167+
{
168+
$http = new HttpClient();
169+
$response = $http->get("https://httpbin.org/redirect/1");
170+
171+
$this->assertEquals(200, $response->statusCode());
37172
}
38173
}

tests/View/stubs/mail.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hello <?= $name ?? "Bow"; ?>
2+
3+
The mail content

0 commit comments

Comments
 (0)