-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientTest.php
More file actions
300 lines (266 loc) · 7.14 KB
/
ClientTest.php
File metadata and controls
300 lines (266 loc) · 7.14 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
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Http Component Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file if a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Http;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Http\Client;
/**
* Client class tests.
*/
final class ClientTest extends TestCase
{
private Client $client;
protected function setUp(): void
{
$this->client = new Client('https://httpbin.org');
}
/**
* Test client initialization.
*/
public function testClientInitialization(): void
{
$client = new Client('https://example.com');
$this->assertInstanceOf(Client::class, $client);
}
/**
* Test client initialization with params.
*/
public function testClientInitializationWithParams(): void
{
$params = ['timeout' => 30, 'redirect' => 5];
$client = new Client('https://example.com', $params);
$this->assertInstanceOf(Client::class, $client);
}
/**
* Test HTTP methods constants.
*/
public function testHttpMethodConstants(): void
{
$this->assertEquals('GET', Client::GET);
$this->assertEquals('POST', Client::POST);
$this->assertEquals('HEAD', Client::HEAD);
$this->assertEquals('PUT', Client::PUT);
$this->assertEquals('PATCH', Client::PATCH);
$this->assertEquals('OPTIONS', Client::OPTIONS);
$this->assertEquals('DELETE', Client::DELETE);
}
/**
* Test default timeout constant.
*/
public function testTimeoutConstant(): void
{
$this->assertEquals(10, Client::TIMEOUT);
}
/**
* Test default redirect constant.
*/
public function testRedirectConstant(): void
{
$this->assertEquals(3, Client::REDIRECT);
}
/**
* Test user agent constant.
*/
public function testUserAgentConstant(): void
{
$this->assertIsArray(Client::USERAGENT);
$this->assertNotEmpty(Client::USERAGENT);
}
/**
* Test GET request method.
*/
public function testGetRequest(): void
{
$response = $this->client->get('/get');
$this->assertInstanceOf(Client::class, $response);
}
/**
* Test POST request method.
*/
public function testPostRequest(): void
{
$response = $this->client->post('/post', ['key' => 'value']);
$this->assertInstanceOf(Client::class, $response);
}
/**
* Test HEAD request method.
*/
public function testHeadRequest(): void
{
$response = $this->client->head('/get');
$this->assertInstanceOf(Client::class, $response);
}
/**
* Test PUT request method.
*/
public function testPutRequest(): void
{
$response = $this->client->put('/put', ['key' => 'value']);
$this->assertInstanceOf(Client::class, $response);
}
/**
* Test PATCH request method.
*/
public function testPatchRequest(): void
{
$response = $this->client->patch('/patch', ['key' => 'value']);
$this->assertInstanceOf(Client::class, $response);
}
/**
* Test OPTIONS request method.
*/
public function testOptionsRequest(): void
{
$response = $this->client->options('/get');
$this->assertInstanceOf(Client::class, $response);
}
/**
* Test DELETE request method.
*/
public function testDeleteRequest(): void
{
$response = $this->client->delete('/delete');
$this->assertInstanceOf(Client::class, $response);
}
/**
* Test method chaining.
*/
public function testMethodChaining(): void
{
$response = $this->client
->setTimeout(15)
->setUserAgent('Custom-Agent')
->follow()
->return()
->get('/get');
$this->assertInstanceOf(Client::class, $response);
}
/**
* Test set timeout.
*/
public function testSetTimeout(): void
{
$result = $this->client->setTimeout(30);
$this->assertInstanceOf(Client::class, $result);
}
/**
* Test set redirect.
*/
public function testSetRedirect(): void
{
$result = $this->client->setRedirect(5);
$this->assertInstanceOf(Client::class, $result);
}
/**
* Test set encoding.
*/
public function testSetEncoding(): void
{
$result = $this->client->setEncoding('gzip');
$this->assertInstanceOf(Client::class, $result);
}
/**
* Test set user agent.
*/
public function testSetUserAgent(): void
{
$result = $this->client->setUserAgent('Test-Agent');
$this->assertInstanceOf(Client::class, $result);
}
/**
* Test return method.
*/
public function testReturn(): void
{
$result = $this->client->return();
$this->assertInstanceOf(Client::class, $result);
}
/**
* Test follow method.
*/
public function testFollow(): void
{
$result = $this->client->follow();
$this->assertInstanceOf(Client::class, $result);
}
/**
* Test header in method.
*/
public function testHeaderIn(): void
{
$result = $this->client->headerIn();
$this->assertInstanceOf(Client::class, $result);
}
/**
* Test get response.
*/
public function testGetResponse(): void
{
$this->client->get('/get');
$response = $this->client->getResponse();
$this->assertIsArray($response);
}
/**
* Test is gateway detection.
*/
public function testIsGateway(): void
{
$client = new Client();
$this->assertTrue($client->isCurl() || $client->isStream());
}
/**
* Test gateway availability.
*/
public function testHasGateway(): void
{
$this->assertTrue(Client::hasCurl() || Client::hasStream());
}
/**
* Test get user agent.
*/
public function testGetUserAgent(): void
{
$ua = Client::getUserAgent();
$this->assertIsString($ua);
$this->assertNotEmpty($ua);
}
/**
* Test get params.
*/
public function testGetParams(): void
{
$params = Client::getParams();
$this->assertIsArray($params);
$this->assertArrayHasKey('timeout', $params);
$this->assertArrayHasKey('redirect', $params);
}
/**
* Test format header.
*/
public function testFormatHeader(): void
{
$header = ['Content-Type' => 'application/json', 'Accept' => 'application/json'];
$formatted = Client::formatHeader($header);
$this->assertIsString($formatted);
$this->assertStringContainsString('Content-Type: application/json', $formatted);
}
/**
* Test pattern getters and setters.
*/
public function testPatternMethods(): void
{
Client::setPattern(['custom' => '/test/']);
$pattern = Client::getPattern('status');
$this->assertIsString($pattern);
}
}