3 namespace Friendica\Test\src\App;
5 use Friendica\App\Request;
6 use Friendica\Core\Config\Capability\IManageConfigValues;
7 use Friendica\Test\MockedTest;
9 class RequestTest extends MockedTest
11 public function dataServerArray(): array
15 'server' => ['REMOTE_ADDR' => '1.2.3.4'],
17 'trusted_proxies' => '',
18 'forwarded_for_headers' => '',
20 'assertion' => '1.2.3.4',
23 'server' => ['HTTP_X_FORWARDED_FOR' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
25 'trusted_proxies' => '1.2.3.4',
26 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
28 'assertion' => '4.5.6.7',
31 'server' => ['HTTP_X_FORWARDED_FOR' => '4.5.6.7, 1.2.3.4', 'REMOTE_ADDR' => '1.2.3.4'],
33 'trusted_proxies' => '1.2.3.4',
34 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
36 'assertion' => '4.5.6.7',
38 'proxy_CIDR_multiple_proxies' => [
39 'server' => ['HTTP_X_FORWARDED_FOR' => '4.5.6.7, 1.2.3.4', 'REMOTE_ADDR' => '10.0.1.1'],
41 'trusted_proxies' => '10.0.0.0/16, 1.2.3.4',
42 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
44 'assertion' => '4.5.6.7',
46 'proxy_wrong_CIDR' => [
47 'server' => ['HTTP_X_FORWARDED_FOR' => '4.5.6.7, 1.2.3.4', 'REMOTE_ADDR' => '10.1.0.1'],
49 'trusted_proxies' => '10.0.0.0/24, 1.2.3.4',
50 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
52 'assertion' => '10.1.0.1',
55 'server' => ['HTTP_X_FORWARDED_FOR' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
57 'trusted_proxies' => '1.2.3.4',
58 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
60 'assertion' => '4.5.6.7',
62 'proxy_multiple_header_1' => [
63 'server' => ['HTTP_X_FORWARDED' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
65 'trusted_proxies' => '1.2.3.4',
66 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR, HTTP_X_FORWARDED',
68 'assertion' => '4.5.6.7',
70 'proxy_multiple_header_2' => [
71 'server' => ['HTTP_X_FORWARDED_FOR' => '1.2.3.4', 'HTTP_X_FORWARDED' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
73 'trusted_proxies' => '1.2.3.4',
74 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR, HTTP_X_FORWARDED',
76 'assertion' => '4.5.6.7',
78 'proxy_multiple_header_wrong' => [
79 'server' => ['HTTP_X_FORWARDED_FOR' => '1.2.3.4', 'HTTP_X_FORWARDED' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
81 'trusted_proxies' => '1.2.3.4',
82 'forwarded_for_headers' => '',
84 'assertion' => '1.2.3.4',
89 'trusted_proxies' => '1.2.3.4',
90 'forwarded_for_headers' => '',
92 'assertion' => '0.0.0.0',
98 * @dataProvider dataServerArray
100 public function testRemoteAddress(array $server, array $config, string $assertion)
102 $configClass = \Mockery::mock(IManageConfigValues::class);
103 $configClass->shouldReceive('get')->with('proxy', 'trusted_proxies', '')->andReturn($config['trusted_proxies']);
104 $configClass->shouldReceive('get')->with('proxy', 'forwarded_for_headers')->andReturn($config['forwarded_for_headers']);
106 $request = new Request($configClass, $server);
108 self::assertEquals($assertion, $request->getRemoteAddress());