3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\App;
24 use Friendica\App\Request;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Test\MockedTest;
28 class RequestTest extends MockedTest
30 public function dataServerArray(): array
34 'server' => ['REMOTE_ADDR' => '1.2.3.4'],
36 'trusted_proxies' => '',
37 'forwarded_for_headers' => '',
39 'assertion' => '1.2.3.4',
42 'server' => ['HTTP_X_FORWARDED_FOR' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
44 'trusted_proxies' => '1.2.3.4',
45 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
47 'assertion' => '4.5.6.7',
50 'server' => ['HTTP_X_FORWARDED_FOR' => '4.5.6.7, 1.2.3.4', 'REMOTE_ADDR' => '1.2.3.4'],
52 'trusted_proxies' => '1.2.3.4',
53 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
55 'assertion' => '4.5.6.7',
57 'proxy_CIDR_multiple_proxies' => [
58 'server' => ['HTTP_X_FORWARDED_FOR' => '4.5.6.7, 1.2.3.4', 'REMOTE_ADDR' => '10.0.1.1'],
60 'trusted_proxies' => '10.0.0.0/16, 1.2.3.4',
61 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
63 'assertion' => '4.5.6.7',
65 'proxy_wrong_CIDR' => [
66 'server' => ['HTTP_X_FORWARDED_FOR' => '4.5.6.7, 1.2.3.4', 'REMOTE_ADDR' => '10.1.0.1'],
68 'trusted_proxies' => '10.0.0.0/24, 1.2.3.4',
69 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
71 'assertion' => '10.1.0.1',
74 'server' => ['HTTP_X_FORWARDED_FOR' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
76 'trusted_proxies' => '1.2.3.4',
77 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
79 'assertion' => '4.5.6.7',
81 'proxy_multiple_header_1' => [
82 'server' => ['HTTP_X_FORWARDED' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
84 'trusted_proxies' => '1.2.3.4',
85 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR, HTTP_X_FORWARDED',
87 'assertion' => '4.5.6.7',
89 'proxy_multiple_header_2' => [
90 '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'],
92 'trusted_proxies' => '1.2.3.4',
93 'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR, HTTP_X_FORWARDED',
95 'assertion' => '4.5.6.7',
97 'proxy_multiple_header_wrong' => [
98 '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'],
100 'trusted_proxies' => '1.2.3.4',
101 'forwarded_for_headers' => '',
103 'assertion' => '1.2.3.4',
105 'no_remote_addr' => [
108 'trusted_proxies' => '1.2.3.4',
109 'forwarded_for_headers' => '',
111 'assertion' => '0.0.0.0',
117 * @dataProvider dataServerArray
119 public function testRemoteAddress(array $server, array $config, string $assertion)
121 $configClass = \Mockery::mock(IManageConfigValues::class);
122 $configClass->shouldReceive('get')->with('proxy', 'trusted_proxies', '')->andReturn($config['trusted_proxies']);
123 $configClass->shouldReceive('get')->with('proxy', 'forwarded_for_headers', Request::DEFAULT_FORWARD_FOR_HEADER)->andReturn($config['forwarded_for_headers']);
125 $request = new Request($configClass, $server);
127 self::assertEquals($assertion, $request->getRemoteAddress());