-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathHttpClientTest.php
More file actions
351 lines (257 loc) · 10 KB
/
HttpClientTest.php
File metadata and controls
351 lines (257 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
namespace Bow\Tests\Support;
use Bow\Http\Client\HttpClient;
use PHPUnit\Framework\TestCase;
class HttpClientTest extends TestCase
{
public function test_get_method_fails_with_invalid_domain()
{
$this->expectException(\Bow\Http\Client\HttpClientException::class);
$http = new HttpClient();
$http->get("https://invalid-domain.invalid");
}
public function test_get_method_succeeds_with_valid_url()
{
$http = new HttpClient();
$response = $http->get("https://www.google.com");
$this->assertEquals(200, $response->statusCode());
}
public function test_get_method_with_custom_headers()
{
$http = new HttpClient();
$http->withHeaders(["X-Api-Key" => "Fake-Key"]);
$response = $http->get("https://www.google.com");
$this->assertEquals(200, $response->statusCode());
}
public function test_get_method_fails_with_non_existent_path()
{
$http = new HttpClient("https://www.google.com");
$http->withHeaders(["X-Api-Key" => "Fake-Key"]);
$response = $http->get("/the-fake-url");
$this->assertEquals(404, $response->statusCode());
}
public function test_get_method_with_base_url_in_constructor()
{
$http = new HttpClient("https://www.google.com");
$response = $http->get("/");
$this->assertEquals(200, $response->statusCode());
}
// ==================== POST Method Tests ====================
public function test_post_method_with_data()
{
$http = new HttpClient();
$response = $http->post("https://httpbin.org/post", [
'name' => 'test',
'value' => 'example'
]);
$this->assertEquals(200, $response->statusCode());
$this->assertStringContainsString('test', $response->getContent());
}
public function test_post_method_with_json_data()
{
$http = new HttpClient();
$http->withHeaders(['Content-Type' => 'application/json']);
$response = $http->post("https://httpbin.org/post", [
'name' => 'test',
'value' => 'example'
]);
$this->assertEquals(200, $response->statusCode());
}
// ==================== PUT Method Tests ====================
public function test_put_method_with_data()
{
$http = new HttpClient();
$response = $http->put("https://httpbin.org/put", [
'name' => 'updated',
'value' => 'example'
]);
$this->assertEquals(200, $response->statusCode());
$this->assertStringContainsString('updated', $response->getContent());
}
// ==================== DELETE Method Tests ====================
public function test_delete_method()
{
$http = new HttpClient();
$response = $http->delete("https://httpbin.org/delete");
$this->assertEquals(200, $response->statusCode());
}
// ==================== PATCH Method Tests ====================
public function test_patch_method_with_data()
{
$http = new HttpClient();
$response = $http->patch("https://httpbin.org/patch", [
'name' => 'patched',
'value' => 'example'
]);
$this->assertEquals(200, $response->statusCode());
$this->assertStringContainsString('patched', $response->getContent());
}
public function test_patch_method_with_json_data()
{
$http = new HttpClient();
$http->acceptJson();
$response = $http->patch("https://httpbin.org/patch", [
'name' => 'patched',
'value' => 'json-example'
]);
$this->assertEquals(200, $response->statusCode());
$this->assertStringContainsString('json-example', $response->getContent());
}
// ==================== HEAD Method Tests ====================
public function test_head_method()
{
$http = new HttpClient();
$response = $http->head("https://httpbin.org/get");
$this->assertEquals(200, $response->statusCode());
// HEAD should not return body content
$this->assertEmpty($response->getContent());
}
public function test_head_method_with_query_params()
{
$http = new HttpClient();
$response = $http->head("https://httpbin.org/get", ['key' => 'value']);
$this->assertEquals(200, $response->statusCode());
}
// ==================== OPTIONS Method Tests ====================
public function test_options_method()
{
$http = new HttpClient();
$response = $http->options("https://httpbin.org/get");
$this->assertEquals(200, $response->statusCode());
}
// ==================== Header Tests ====================
public function test_add_multiple_headers()
{
$http = new HttpClient();
$http->withHeaders([
"X-Api-Key" => "test-key",
"X-Custom-Header" => "custom-value"
]);
$response = $http->get("https://httpbin.org/headers");
$this->assertEquals(200, $response->statusCode());
$this->assertStringContainsString('test-key', $response->getContent());
}
public function test_user_agent_header()
{
$http = new HttpClient();
$http->withHeaders(["User-Agent" => "BowFramework/1.0"]);
$response = $http->get("https://httpbin.org/user-agent");
$this->assertEquals(200, $response->statusCode());
$this->assertStringContainsString('BowFramework', $response->getContent());
}
// ==================== Response Tests ====================
public function test_response_body_is_retrievable()
{
$http = new HttpClient();
$response = $http->get("https://www.google.com");
$body = $response->getContent();
$this->assertNotEmpty($body);
$this->assertIsString($body);
}
public function test_response_status_code_is_correct()
{
$http = new HttpClient();
$response = $http->get("https://httpbin.org/status/201");
$this->assertEquals(201, $response->statusCode());
}
// ==================== Error Handling Tests ====================
public function test_timeout_handling()
{
$http = new HttpClient();
// This should work or timeout gracefully
$response = $http->get("https://httpbin.org/delay/1");
$this->assertIsInt($response->statusCode());
}
public function test_redirect_following()
{
$http = new HttpClient();
$response = $http->get("https://httpbin.org/redirect/1");
$this->assertEquals(200, $response->statusCode());
}
// ==================== Authentication Tests ====================
public function test_basic_auth_with_valid_credentials()
{
$http = new HttpClient();
$http->basicAuth('user', 'passwd');
$response = $http->get("https://httpbin.org/basic-auth/user/passwd");
$this->assertEquals(200, $response->statusCode());
$this->assertStringContainsString('authenticated', $response->getContent());
}
public function test_basic_auth_with_invalid_credentials()
{
$http = new HttpClient();
$http->basicAuth('wrong', 'credentials');
$response = $http->get("https://httpbin.org/basic-auth/user/passwd");
$this->assertEquals(401, $response->statusCode());
}
public function test_bearer_auth_sends_token_in_header()
{
$http = new HttpClient();
$http->bearerAuth('my-test-token');
$response = $http->get("https://httpbin.org/bearer");
$this->assertEquals(200, $response->statusCode());
$this->assertStringContainsString('authenticated', $response->getContent());
}
public function test_bearer_auth_fails_without_token()
{
$http = new HttpClient();
$response = $http->get("https://httpbin.org/bearer");
$this->assertEquals(401, $response->statusCode());
}
// ==================== Accept JSON Tests ====================
public function test_accept_json_sets_content_type_header()
{
$http = new HttpClient();
$http->acceptJson();
$response = $http->post("https://httpbin.org/post", [
'name' => 'test',
'value' => 'example'
]);
$this->assertEquals(200, $response->statusCode());
$content = json_decode($response->getContent(), true);
$this->assertEquals('application/json', $content['headers']['Content-Type']);
}
// ==================== Timeout Configuration Tests ====================
public function test_connect_timeout_configuration()
{
$http = new HttpClient();
$http->connectTimeout(5);
$response = $http->get("https://httpbin.org/get");
$this->assertEquals(200, $response->statusCode());
}
public function test_timeout_configuration()
{
$http = new HttpClient();
$http->timeout(10);
$response = $http->get("https://httpbin.org/get");
$this->assertEquals(200, $response->statusCode());
}
// ==================== SSL Verification Tests ====================
public function test_disable_ssl_verification()
{
$http = new HttpClient();
$http->disableSslVerification();
$response = $http->get("https://httpbin.org/get");
$this->assertEquals(200, $response->statusCode());
}
// ==================== Base URL Tests ====================
public function test_set_base_url_method()
{
$http = new HttpClient();
$http->setBaseUrl("https://httpbin.org");
$response = $http->get("/get");
$this->assertEquals(200, $response->statusCode());
}
// ==================== Method Chaining Tests ====================
public function test_method_chaining()
{
$http = new HttpClient();
$response = $http
->withHeaders(['X-Custom' => 'value'])
->acceptJson()
->timeout(10)
->connectTimeout(5)
->post("https://httpbin.org/post", ['key' => 'value']);
$this->assertEquals(200, $response->statusCode());
}
}