Merge "mw.Upload.BookletLayout/Dialog: Add determinate progress bar"
[lhc/web/wiklou.git] / tests / phpunit / includes / WebRequestTest.php
1 <?php
2
3 /**
4 * @group WebRequest
5 */
6 class WebRequestTest extends MediaWikiTestCase {
7 protected $oldServer;
8
9 protected function setUp() {
10 parent::setUp();
11
12 $this->oldServer = $_SERVER;
13 IP::clearCaches();
14 }
15
16 protected function tearDown() {
17 $_SERVER = $this->oldServer;
18 IP::clearCaches();
19
20 parent::tearDown();
21 }
22
23 /**
24 * @dataProvider provideDetectServer
25 * @covers WebRequest::detectServer
26 */
27 public function testDetectServer( $expected, $input, $description ) {
28 $_SERVER = $input;
29 $result = WebRequest::detectServer();
30 $this->assertEquals( $expected, $result, $description );
31 }
32
33 public static function provideDetectServer() {
34 return [
35 [
36 'http://x',
37 [
38 'HTTP_HOST' => 'x'
39 ],
40 'Host header'
41 ],
42 [
43 'https://x',
44 [
45 'HTTP_HOST' => 'x',
46 'HTTPS' => 'on',
47 ],
48 'Host header with secure'
49 ],
50 [
51 'http://x',
52 [
53 'HTTP_HOST' => 'x',
54 'SERVER_PORT' => 80,
55 ],
56 'Default SERVER_PORT',
57 ],
58 [
59 'http://x',
60 [
61 'HTTP_HOST' => 'x',
62 'HTTPS' => 'off',
63 ],
64 'Secure off'
65 ],
66 [
67 'http://y',
68 [
69 'SERVER_NAME' => 'y',
70 ],
71 'Server name'
72 ],
73 [
74 'http://x',
75 [
76 'HTTP_HOST' => 'x',
77 'SERVER_NAME' => 'y',
78 ],
79 'Host server name precedence'
80 ],
81 [
82 'http://[::1]:81',
83 [
84 'HTTP_HOST' => '[::1]',
85 'SERVER_NAME' => '::1',
86 'SERVER_PORT' => '81',
87 ],
88 'Apache bug 26005'
89 ],
90 [
91 'http://localhost',
92 [
93 'SERVER_NAME' => '[2001'
94 ],
95 'Kind of like lighttpd per commit message in MW r83847',
96 ],
97 [
98 'http://[2a01:e35:2eb4:1::2]:777',
99 [
100 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
101 ],
102 'Possible lighttpd environment per bug 14977 comment 13',
103 ],
104 ];
105 }
106
107 /**
108 * @dataProvider provideGetIP
109 * @covers WebRequest::getIP
110 */
111 public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
112 $_SERVER = $input;
113 $this->setMwGlobals( [
114 'wgSquidServersNoPurge' => $squid,
115 'wgUsePrivateIPs' => $private,
116 'wgHooks' => [
117 'IsTrustedProxy' => [
118 function ( &$ip, &$trusted ) use ( $xffList ) {
119 $trusted = $trusted || in_array( $ip, $xffList );
120 return true;
121 }
122 ]
123 ]
124 ] );
125
126 $request = new WebRequest();
127 $result = $request->getIP();
128 $this->assertEquals( $expected, $result, $description );
129 }
130
131 public static function provideGetIP() {
132 return [
133 [
134 '127.0.0.1',
135 [
136 'REMOTE_ADDR' => '127.0.0.1'
137 ],
138 [],
139 [],
140 false,
141 'Simple IPv4'
142 ],
143 [
144 '::1',
145 [
146 'REMOTE_ADDR' => '::1'
147 ],
148 [],
149 [],
150 false,
151 'Simple IPv6'
152 ],
153 [
154 '12.0.0.1',
155 [
156 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
157 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
158 ],
159 [ 'ABCD:1:2:3:4:555:6666:7777' ],
160 [],
161 false,
162 'IPv6 normalisation'
163 ],
164 [
165 '12.0.0.3',
166 [
167 'REMOTE_ADDR' => '12.0.0.1',
168 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
169 ],
170 [ '12.0.0.1', '12.0.0.2' ],
171 [],
172 false,
173 'With X-Forwaded-For'
174 ],
175 [
176 '12.0.0.1',
177 [
178 'REMOTE_ADDR' => '12.0.0.1',
179 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
180 ],
181 [],
182 [],
183 false,
184 'With X-Forwaded-For and disallowed server'
185 ],
186 [
187 '12.0.0.2',
188 [
189 'REMOTE_ADDR' => '12.0.0.1',
190 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
191 ],
192 [ '12.0.0.1' ],
193 [],
194 false,
195 'With multiple X-Forwaded-For and only one allowed server'
196 ],
197 [
198 '10.0.0.3',
199 [
200 'REMOTE_ADDR' => '12.0.0.2',
201 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
202 ],
203 [ '12.0.0.1', '12.0.0.2' ],
204 [],
205 false,
206 'With X-Forwaded-For and private IP (from cache proxy)'
207 ],
208 [
209 '10.0.0.4',
210 [
211 'REMOTE_ADDR' => '12.0.0.2',
212 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
213 ],
214 [ '12.0.0.1', '12.0.0.2', '10.0.0.3' ],
215 [],
216 true,
217 'With X-Forwaded-For and private IP (allowed)'
218 ],
219 [
220 '10.0.0.4',
221 [
222 'REMOTE_ADDR' => '12.0.0.2',
223 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
224 ],
225 [ '12.0.0.1', '12.0.0.2' ],
226 [ '10.0.0.3' ],
227 true,
228 'With X-Forwaded-For and private IP (allowed)'
229 ],
230 [
231 '10.0.0.3',
232 [
233 'REMOTE_ADDR' => '12.0.0.2',
234 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
235 ],
236 [ '12.0.0.1', '12.0.0.2' ],
237 [ '10.0.0.3' ],
238 false,
239 'With X-Forwaded-For and private IP (disallowed)'
240 ],
241 [
242 '12.0.0.3',
243 [
244 'REMOTE_ADDR' => '12.0.0.1',
245 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
246 ],
247 [],
248 [ '12.0.0.1', '12.0.0.2' ],
249 false,
250 'With X-Forwaded-For'
251 ],
252 [
253 '12.0.0.2',
254 [
255 'REMOTE_ADDR' => '12.0.0.1',
256 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
257 ],
258 [],
259 [ '12.0.0.1' ],
260 false,
261 'With multiple X-Forwaded-For and only one allowed server'
262 ],
263 [
264 '12.0.0.2',
265 [
266 'REMOTE_ADDR' => '12.0.0.2',
267 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
268 ],
269 [],
270 [ '12.0.0.2' ],
271 false,
272 'With X-Forwaded-For and private IP and hook (disallowed)'
273 ],
274 [
275 '12.0.0.1',
276 [
277 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
278 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
279 ],
280 [ 'ABCD:1:2:3::/64' ],
281 [],
282 false,
283 'IPv6 CIDR'
284 ],
285 [
286 '12.0.0.3',
287 [
288 'REMOTE_ADDR' => '12.0.0.1',
289 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
290 ],
291 [ '12.0.0.0/24' ],
292 [],
293 false,
294 'IPv4 CIDR'
295 ],
296 ];
297 }
298
299 /**
300 * @expectedException MWException
301 * @covers WebRequest::getIP
302 */
303 public function testGetIpLackOfRemoteAddrThrowAnException() {
304 // ensure that local install state doesn't interfere with test
305 $this->setMwGlobals( [
306 'wgSquidServersNoPurge' => [],
307 'wgSquidServers' => [],
308 'wgUsePrivateIPs' => false,
309 'wgHooks' => [],
310 ] );
311
312 $request = new WebRequest();
313 # Next call throw an exception about lacking an IP
314 $request->getIP();
315 }
316
317 public static function provideLanguageData() {
318 return [
319 [ '', [], 'Empty Accept-Language header' ],
320 [ 'en', [ 'en' => 1 ], 'One language' ],
321 [ 'en, ar', [ 'en' => 1, 'ar' => 1 ], 'Two languages listed in appearance order.' ],
322 [
323 'zh-cn,zh-tw',
324 [ 'zh-cn' => 1, 'zh-tw' => 1 ],
325 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
326 ],
327 [
328 'es, en; q=0.5',
329 [ 'es' => 1, 'en' => '0.5' ],
330 'Spanish as first language and English and second'
331 ],
332 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Less prefered language first' ],
333 [ 'fr, en; q=0.5, es', [ 'fr' => 1, 'es' => 1, 'en' => '0.5' ], 'Three languages' ],
334 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Two languages' ],
335 [ 'en, zh;q=0', [ 'en' => 1 ], "It's Chinese to me" ],
336 [
337 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
338 [ 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ],
339 'Preference for Romance languages'
340 ],
341 [
342 'en-gb, en-us; q=1',
343 [ 'en-gb' => 1, 'en-us' => '1' ],
344 'Two equally prefered English variants'
345 ],
346 ];
347 }
348
349 /**
350 * @dataProvider provideLanguageData
351 * @covers WebRequest::getAcceptLang
352 */
353 public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
354 $_SERVER = [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ];
355 $request = new WebRequest();
356 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
357 }
358 }