|
7 | 7 |
|
8 | 8 | class HttpClientTest extends TestCase |
9 | 9 | { |
10 | | - public function test_get_method() |
| 10 | + // ==================== GET Method Tests ==================== |
| 11 | + |
| 12 | + public function test_get_method_fails_with_invalid_domain() |
11 | 13 | { |
12 | 14 | $http = new HttpClient(); |
13 | | - |
14 | 15 | $response = $http->get("https://www.oogle.com"); |
15 | 16 |
|
16 | | - $this->assertEquals($response->statusCode(), 503); |
| 17 | + $this->assertEquals(503, $response->statusCode()); |
17 | 18 | } |
18 | 19 |
|
19 | | - public function test_get_method_with_custom_headers() |
| 20 | + public function test_get_method_succeeds_with_valid_url() |
20 | 21 | { |
21 | 22 | $http = new HttpClient(); |
| 23 | + $response = $http->get("https://www.google.com"); |
| 24 | + |
| 25 | + $this->assertEquals(200, $response->statusCode()); |
| 26 | + } |
22 | 27 |
|
| 28 | + public function test_get_method_with_custom_headers() |
| 29 | + { |
| 30 | + $http = new HttpClient(); |
23 | 31 | $http->addHeaders(["X-Api-Key" => "Fake-Key"]); |
| 32 | + |
24 | 33 | $response = $http->get("https://www.google.com"); |
25 | 34 |
|
26 | | - $this->assertEquals($response->statusCode(), 200); |
| 35 | + $this->assertEquals(200, $response->statusCode()); |
27 | 36 | } |
28 | 37 |
|
29 | | - public function test_should_be_fail_with_get_method() |
| 38 | + public function test_get_method_fails_with_non_existent_path() |
30 | 39 | { |
31 | 40 | $http = new HttpClient("https://www.google.com"); |
32 | | - |
33 | 41 | $http->addHeaders(["X-Api-Key" => "Fake-Key"]); |
| 42 | + |
34 | 43 | $response = $http->get("/the-fake-url"); |
35 | 44 |
|
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()); |
37 | 172 | } |
38 | 173 | } |
0 commit comments