5
5
*/
6
6
namespace Magento \Framework \HTTP \Test \Unit \PhpEnvironment ;
7
7
8
+ use Magento \Framework \HTTP \PhpEnvironment \RemoteAddress ;
9
+ use Magento \Framework \App \Request \Http as HttpRequest ;
10
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
11
+
8
12
class RemoteAddressTest extends \PHPUnit \Framework \TestCase
9
13
{
10
14
/**
11
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Request\Http
15
+ * @var \PHPUnit_Framework_MockObject_MockObject|\HttpRequest
12
16
*/
13
17
protected $ _request ;
14
18
15
19
/**
16
- * @var \Magento\Framework\TestFramework\Unit\Helper\ ObjectManager
20
+ * @var ObjectManager
17
21
*/
18
22
protected $ _objectManager ;
19
23
24
+ /**
25
+ * @inheritdoc
26
+ */
20
27
protected function setUp ()
21
28
{
22
- $ this ->_request = $ this ->getMockBuilder (
23
- \Magento \Framework \App \Request \Http::class
24
- )->disableOriginalConstructor ()->setMethods (
25
- ['getServer ' ]
26
- )->getMock ();
29
+ $ this ->_request = $ this ->getMockBuilder (HttpRequest::class)
30
+ ->disableOriginalConstructor ()
31
+ ->setMethods (['getServer ' ])
32
+ ->getMock ();
27
33
28
- $ this ->_objectManager = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
34
+ $ this ->_objectManager = new ObjectManager ($ this );
29
35
}
30
36
31
37
/**
38
+ * @param string[] $alternativeHeaders
39
+ * @param array $serverValueMap
40
+ * @param string|bool $expected
41
+ * @param bool $ipToLong
42
+ * @param string[]|null $trustedProxies
43
+ * @return void
32
44
* @dataProvider getRemoteAddressProvider
33
45
*/
34
- public function testGetRemoteAddress ($ alternativeHeaders , $ serverValueMap , $ expected , $ ipToLong )
35
- {
46
+ public function testGetRemoteAddress (
47
+ array $ alternativeHeaders ,
48
+ array $ serverValueMap ,
49
+ $ expected ,
50
+ bool $ ipToLong ,
51
+ array $ trustedProxies = null
52
+ ): void {
36
53
$ remoteAddress = $ this ->_objectManager ->getObject (
37
- \Magento \Framework \HTTP \PhpEnvironment \RemoteAddress::class,
38
- ['httpRequest ' => $ this ->_request , 'alternativeHeaders ' => $ alternativeHeaders ]
54
+ RemoteAddress::class,
55
+ [
56
+ 'httpRequest ' => $ this ->_request ,
57
+ 'alternativeHeaders ' => $ alternativeHeaders ,
58
+ 'trustedProxies ' => $ trustedProxies ,
59
+ ]
39
60
);
40
- $ this ->_request ->expects ($ this ->any ())->method ('getServer ' )->will ($ this ->returnValueMap ($ serverValueMap ));
61
+ $ this ->_request ->expects ($ this ->any ())
62
+ ->method ('getServer ' )
63
+ ->will ($ this ->returnValueMap ($ serverValueMap ));
64
+
41
65
$ this ->assertEquals ($ expected , $ remoteAddress ->getRemoteAddress ($ ipToLong ));
42
66
}
43
67
44
68
/**
45
69
* @return array
70
+ *
71
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
46
72
*/
47
73
public function getRemoteAddressProvider ()
48
74
{
@@ -52,18 +78,21 @@ public function getRemoteAddressProvider()
52
78
'serverValueMap ' => [['REMOTE_ADDR ' , null , null ]],
53
79
'expected ' => false ,
54
80
'ipToLong ' => false ,
81
+ 'trustedProxies ' => null ,
55
82
],
56
83
[
57
84
'alternativeHeaders ' => [],
58
85
'serverValueMap ' => [['REMOTE_ADDR ' , null , '192.168.0.1 ' ]],
59
86
'expected ' => '192.168.0.1 ' ,
60
- 'ipToLong ' => false
87
+ 'ipToLong ' => false ,
88
+ 'trustedProxies ' => null ,
61
89
],
62
90
[
63
91
'alternativeHeaders ' => [],
64
92
'serverValueMap ' => [['REMOTE_ADDR ' , null , '192.168.1.1 ' ]],
65
93
'expected ' => ip2long ('192.168.1.1 ' ),
66
- 'ipToLong ' => true
94
+ 'ipToLong ' => true ,
95
+ 'trustedProxies ' => null ,
67
96
],
68
97
[
69
98
'alternativeHeaders ' => ['TEST_HEADER ' ],
@@ -73,7 +102,8 @@ public function getRemoteAddressProvider()
73
102
['TEST_HEADER ' , false , '192.168.0.1 ' ],
74
103
],
75
104
'expected ' => '192.168.0.1 ' ,
76
- 'ipToLong ' => false
105
+ 'ipToLong ' => false ,
106
+ 'trustedProxies ' => null ,
77
107
],
78
108
[
79
109
'alternativeHeaders ' => ['TEST_HEADER ' ],
@@ -83,8 +113,74 @@ public function getRemoteAddressProvider()
83
113
['TEST_HEADER ' , false , '192.168.0.1 ' ],
84
114
],
85
115
'expected ' => ip2long ('192.168.0.1 ' ),
86
- 'ipToLong ' => true
87
- ]
116
+ 'ipToLong ' => true ,
117
+ 'trustedProxies ' => null ,
118
+ ],
119
+ [
120
+ 'alternativeHeaders ' => [],
121
+ 'serverValueMap ' => [
122
+ ['REMOTE_ADDR ' , null , 'NotValidIp ' ],
123
+ ],
124
+ 'expected ' => false ,
125
+ 'ipToLong ' => false ,
126
+ 'trustedProxies ' => ['127.0.0.1 ' ],
127
+ ],
128
+ [
129
+ 'alternativeHeaders ' => ['TEST_HEADER ' ],
130
+ 'serverValueMap ' => [
131
+ ['TEST_HEADER ' , null , 'NotValid, 192.168.0.1 ' ],
132
+ ['TEST_HEADER ' , false , 'NotValid, 192.168.0.1 ' ],
133
+ ],
134
+ 'expected ' => '192.168.0.1 ' ,
135
+ 'ipToLong ' => false ,
136
+ 'trustedProxies ' => ['127.0.0.1 ' ],
137
+ ],
138
+ [
139
+ 'alternativeHeaders ' => ['TEST_HEADER ' ],
140
+ 'serverValueMap ' => [
141
+ ['TEST_HEADER ' , null , '192.168.0.2, 192.168.0.1 ' ],
142
+ ['TEST_HEADER ' , false , '192.168.0.2, 192.168.0.1 ' ],
143
+ ],
144
+ 'expected ' => '192.168.0.2 ' ,
145
+ 'ipToLong ' => false ,
146
+ 'trustedProxies ' => null ,
147
+ ],
148
+ [
149
+ 'alternativeHeaders ' => [],
150
+ 'serverValueMap ' => [
151
+ [
152
+ 'REMOTE_ADDR ' ,
153
+ null ,
154
+ '192.168.0.2, 192.168.0.1, 192.168.0.3 ' ,
155
+ ],
156
+ [
157
+ 'REMOTE_ADDR ' ,
158
+ false ,
159
+ '192.168.0.2, 192.168.0.1, 192.168.0.3 ' ,
160
+ ],
161
+ ],
162
+ 'expected ' => '192.168.0.1 ' ,
163
+ 'ipToLong ' => false ,
164
+ 'trustedProxies ' => ['192.168.0.3 ' ],
165
+ ],
166
+ [
167
+ 'alternativeHeaders ' => [],
168
+ 'serverValueMap ' => [
169
+ [
170
+ 'REMOTE_ADDR ' ,
171
+ null ,
172
+ '192.168.0.2, 192.168.0.1, 192.168.0.3 ' ,
173
+ ],
174
+ [
175
+ 'REMOTE_ADDR ' ,
176
+ false ,
177
+ '192.168.0.2, 192.168.0.1, 192.168.0.3 ' ,
178
+ ],
179
+ ],
180
+ 'expected ' => '192.168.0.3 ' ,
181
+ 'ipToLong ' => false ,
182
+ 'trustedProxies ' => [],
183
+ ],
88
184
];
89
185
}
90
186
}
0 commit comments