]> git.mxchange.org Git - friendica.git/blob - tests/src/App/RequestTest.php
spelling: cached
[friendica.git] / tests / src / App / RequestTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\App;
23
24 use Friendica\App\Request;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Test\MockedTest;
27
28 class RequestTest extends MockedTest
29 {
30         public function dataServerArray(): array
31         {
32                 return [
33                         'default' => [
34                                 'server' => ['REMOTE_ADDR' => '1.2.3.4'],
35                                 'config' => [
36                                         'trusted_proxies'       => '',
37                                         'forwarded_for_headers' => '',
38                                 ],
39                                 'assertion' => '1.2.3.4',
40                         ],
41                         'proxy_1' => [
42                                 'server' => ['HTTP_X_FORWARDED_FOR' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
43                                 'config' => [
44                                         'trusted_proxies'       => '1.2.3.4',
45                                         'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
46                                 ],
47                                 'assertion' => '4.5.6.7',
48                         ],
49                         'proxy_2' => [
50                                 'server' => ['HTTP_X_FORWARDED_FOR' => '4.5.6.7, 1.2.3.4', 'REMOTE_ADDR' => '1.2.3.4'],
51                                 'config' => [
52                                         'trusted_proxies'       => '1.2.3.4',
53                                         'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
54                                 ],
55                                 'assertion' => '4.5.6.7',
56                         ],
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'],
59                                 'config' => [
60                                         'trusted_proxies'       => '10.0.0.0/16, 1.2.3.4',
61                                         'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
62                                 ],
63                                 'assertion' => '4.5.6.7',
64                         ],
65                         'proxy_wrong_CIDR' => [
66                                 'server' => ['HTTP_X_FORWARDED_FOR' => '4.5.6.7, 1.2.3.4', 'REMOTE_ADDR' => '10.1.0.1'],
67                                 'config' => [
68                                         'trusted_proxies'       => '10.0.0.0/24, 1.2.3.4',
69                                         'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
70                                 ],
71                                 'assertion' => '10.1.0.1',
72                         ],
73                         'proxy_3' => [
74                                 'server' => ['HTTP_X_FORWARDED_FOR' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
75                                 'config' => [
76                                         'trusted_proxies'       => '1.2.3.4',
77                                         'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR',
78                                 ],
79                                 'assertion' => '4.5.6.7',
80                         ],
81                         'proxy_multiple_header_1' => [
82                                 'server' => ['HTTP_X_FORWARDED' => '1.2.3.4, 4.5.6.7', 'REMOTE_ADDR' => '1.2.3.4'],
83                                 'config' => [
84                                         'trusted_proxies'       => '1.2.3.4',
85                                         'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR, HTTP_X_FORWARDED',
86                                 ],
87                                 'assertion' => '4.5.6.7',
88                         ],
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'],
91                                 'config' => [
92                                         'trusted_proxies'       => '1.2.3.4',
93                                         'forwarded_for_headers' => 'HTTP_X_FORWARDED_FOR, HTTP_X_FORWARDED',
94                                 ],
95                                 'assertion' => '4.5.6.7',
96                         ],
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'],
99                                 'config' => [
100                                         'trusted_proxies'       => '1.2.3.4',
101                                         'forwarded_for_headers' => '',
102                                 ],
103                                 'assertion' => '1.2.3.4',
104                         ],
105                         'no_remote_addr' => [
106                                 'server' => [],
107                                 'config' => [
108                                         'trusted_proxies'       => '1.2.3.4',
109                                         'forwarded_for_headers' => '',
110                                 ],
111                                 'assertion' => '0.0.0.0',
112                         ],
113                 ];
114         }
115
116         /**
117          * @dataProvider dataServerArray
118          */
119         public function testRemoteAddress(array $server, array $config, string $assertion)
120         {
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']);
124
125                 $request = new Request($configClass, $server);
126
127                 self::assertEquals($assertion, $request->getRemoteAddress());
128         }
129 }