-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerTest.php
More file actions
351 lines (306 loc) · 8.65 KB
/
ServerTest.php
File metadata and controls
351 lines (306 loc) · 8.65 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
/**
* @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\Server;
/**
* Server class tests.
*/
final class ServerTest extends TestCase
{
protected function setUp(): void
{
// Set up some sample SERVER data
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/test/path';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTPS'] = 'on';
$_SERVER['SERVER_PORT'] = '443';
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 Test Browser';
$_SERVER['REMOTE_ADDR'] = '192.168.1.100';
$_SERVER['PHP_AUTH_USER'] = 'testuser';
$_SERVER['PHP_AUTH_PW'] = 'testpass';
}
/**
* Test get server value.
*/
public function testGetValue(): void
{
$host = Server::get('HTTP_HOST');
$this->assertEquals('example.com', $host);
}
/**
* Test get server value with formatting.
*/
public function testGetValueWithFormatting(): void
{
$host = Server::get('http-host', true);
$this->assertEquals('example.com', $host);
}
/**
* Test get server value without formatting.
*/
public function testGetValueWithoutFormatting(): void
{
$host = Server::get('http-host', false);
$this->assertNull($host); // Should be null since exact key doesn't exist
$host = Server::get('HTTP_HOST', false);
$this->assertEquals('example.com', $host);
}
/**
* Test get all server values.
*/
public function testGetAllValues(): void
{
$server = Server::get();
$this->assertIsArray($server);
$this->assertArrayHasKey('HTTP_HOST', $server);
}
/**
* Test get non-existent server value.
*/
public function testGetNonExistentValue(): void
{
$value = Server::get('NON_EXISTENT_KEY');
$this->assertNull($value);
}
/**
* Test check if server value is set.
*/
public function testIsSetted(): void
{
$this->assertTrue(Server::isSet('HTTP_HOST'));
$this->assertFalse(Server::isSet('NON_EXISTENT'));
}
/**
* Test check if any server values are set.
*/
public function testIsSettedAny(): void
{
$this->assertTrue(Server::isSet()); // $_SERVER should always have values
}
/**
* Test get request method.
*/
public function testGetMethod(): void
{
$method = Server::getMethod();
$this->assertEquals('GET', $method);
}
/**
* Test get host.
*/
public function testGetHost(): void
{
$host = Server::getHost();
$this->assertEquals('example.com', $host);
}
/**
* Test get URL.
*/
public function testGetUrl(): void
{
$url = Server::getUrl();
$this->assertIsString($url);
$this->assertStringContains('example.com', $url);
}
/**
* Test check basic authentication.
*/
public function testIsBasicAuth(): void
{
$this->assertTrue(Server::isBasicAuth());
}
/**
* Test get basic auth user.
*/
public function testGetBasicAuthUser(): void
{
$user = Server::getBasicAuthUser();
$this->assertEquals('testuser', $user);
}
/**
* Test get basic auth password.
*/
public function testGetBasicAuthPwd(): void
{
$password = Server::getBasicAuthPwd();
$this->assertEquals('testpass', $password);
}
/**
* Test get user agent.
*/
public function testGetUserAgent(): void
{
$ua = Server::getUserAgent();
$this->assertEquals('Mozilla/5.0 Test Browser', $ua);
}
/**
* Test get IP address.
*/
public function testGetIp(): void
{
$ip = Server::getIp();
$this->assertEquals('192.168.1.100', $ip);
}
/**
* Test check if HTTPS.
*/
public function testIsSsl(): void
{
$this->assertTrue(Server::isSsl());
}
/**
* Test check if AJAX.
*/
public function testIsAjax(): void
{
// Should be false initially
$this->assertFalse(Server::isAjax());
// Set AJAX header
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$this->assertTrue(Server::isAjax());
}
/**
* Test get protocol.
*/
public function testGetProtocol(): void
{
$protocol = Server::getProtocol();
$this->assertEquals('https', $protocol);
}
/**
* Test get port.
*/
public function testGetPort(): void
{
$port = Server::getPort();
$this->assertEquals('443', $port);
}
/**
* Test without HTTPS.
*/
public function testWithoutHttps(): void
{
unset($_SERVER['HTTPS']);
$_SERVER['SERVER_PORT'] = '80';
$this->assertFalse(Server::isSsl());
$protocol = Server::getProtocol();
$this->assertEquals('http', $protocol);
}
/**
* Test with different HTTPS values.
*/
public function testWithDifferentHttpsValues(): void
{
$_SERVER['HTTPS'] = 'off';
$this->assertFalse(Server::isSsl());
$_SERVER['HTTPS'] = '1';
$this->assertTrue(Server::isSsl());
$_SERVER['HTTPS'] = 'true';
$this->assertTrue(Server::isSsl());
}
/**
* Test get request URI.
*/
public function testGetRequestUri(): void
{
$uri = Server::get('REQUEST_URI');
$this->assertEquals('/test/path', $uri);
}
/**
* Test server with proxy headers.
*/
public function testWithProxyHeaders(): void
{
$_SERVER['HTTP_X_FORWARDED_FOR'] = '203.0.113.1, 192.168.1.100';
$_SERVER['HTTP_X_REAL_IP'] = '203.0.113.1';
// Test IP detection with proxy
$ip = Server::getIp();
$this->assertIsString($ip);
}
/**
* Test server with custom headers.
*/
public function testWithCustomHeaders(): void
{
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer token123';
$_SERVER['HTTP_ACCEPT'] = 'application/json';
$_SERVER['HTTP_CONTENT_TYPE'] = 'application/json';
$auth = Server::get('HTTP_AUTHORIZATION');
$this->assertEquals('Bearer token123', $auth);
$accept = Server::get('HTTP_ACCEPT');
$this->assertEquals('application/json', $accept);
}
/**
* Test authorization headers method.
*/
public function testGetAuthorizationHeaders(): void
{
$this->assertTrue(method_exists(Server::class, 'getAuthorizationHeaders'));
}
/**
* Test without basic auth.
*/
public function testWithoutBasicAuth(): void
{
unset($_SERVER['PHP_AUTH_USER']);
unset($_SERVER['PHP_AUTH_PW']);
$this->assertFalse(Server::isBasicAuth());
$this->assertEquals('', Server::getBasicAuthUser());
$this->assertEquals('', Server::getBasicAuthPwd());
}
/**
* Test server methods exist.
*/
public function testServerMethodsExist(): void
{
$methods = [
'get', 'isSetted', 'getMethod', 'getHost', 'getUrl',
'isBasicAuth', 'getBasicAuthUser', 'getBasicAuthPwd',
'getUserAgent', 'getIp', 'isSsl', 'isAjax', 'getProtocol',
'getPort', 'getAuthorizationHeaders'
];
foreach ($methods as $method) {
$this->assertTrue(method_exists(Server::class, $method), "Method {$method} should exist");
}
}
/**
* Test complex URL building.
*/
public function testComplexUrlBuilding(): void
{
$_SERVER['HTTP_HOST'] = 'api.example.com';
$_SERVER['REQUEST_URI'] = '/v1/users/123?include=profile';
$_SERVER['HTTPS'] = 'on';
$_SERVER['SERVER_PORT'] = '443';
$url = Server::getUrl();
$this->assertStringContains('api.example.com', $url);
$this->assertStringContains('https', $url);
}
/**
* Test edge cases.
*/
public function testEdgeCases(): void
{
// Test with empty values
$_SERVER['HTTP_HOST'] = '';
$host = Server::getHost();
$this->assertEquals('', $host);
// Test with null-like values
$_SERVER['HTTP_USER_AGENT'] = '';
$ua = Server::getUserAgent();
$this->assertEquals('', $ua);
}
}