LocalisationCache: try harder to use LCStoreCDB
[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 array(
35 array(
36 'http://x',
37 array(
38 'HTTP_HOST' => 'x'
39 ),
40 'Host header'
41 ),
42 array(
43 'https://x',
44 array(
45 'HTTP_HOST' => 'x',
46 'HTTPS' => 'on',
47 ),
48 'Host header with secure'
49 ),
50 array(
51 'http://x',
52 array(
53 'HTTP_HOST' => 'x',
54 'SERVER_PORT' => 80,
55 ),
56 'Default SERVER_PORT',
57 ),
58 array(
59 'http://x',
60 array(
61 'HTTP_HOST' => 'x',
62 'HTTPS' => 'off',
63 ),
64 'Secure off'
65 ),
66 array(
67 'http://y',
68 array(
69 'SERVER_NAME' => 'y',
70 ),
71 'Server name'
72 ),
73 array(
74 'http://x',
75 array(
76 'HTTP_HOST' => 'x',
77 'SERVER_NAME' => 'y',
78 ),
79 'Host server name precedence'
80 ),
81 array(
82 'http://[::1]:81',
83 array(
84 'HTTP_HOST' => '[::1]',
85 'SERVER_NAME' => '::1',
86 'SERVER_PORT' => '81',
87 ),
88 'Apache bug 26005'
89 ),
90 array(
91 'http://localhost',
92 array(
93 'SERVER_NAME' => '[2001'
94 ),
95 'Kind of like lighttpd per commit message in MW r83847',
96 ),
97 array(
98 'http://[2a01:e35:2eb4:1::2]:777',
99 array(
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( array(
114 'wgSquidServersNoPurge' => $squid,
115 'wgUsePrivateIPs' => $private,
116 'wgHooks' => array(
117 'IsTrustedProxy' => array(
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 array(
133 array(
134 '127.0.0.1',
135 array(
136 'REMOTE_ADDR' => '127.0.0.1'
137 ),
138 array(),
139 array(),
140 false,
141 'Simple IPv4'
142 ),
143 array(
144 '::1',
145 array(
146 'REMOTE_ADDR' => '::1'
147 ),
148 array(),
149 array(),
150 false,
151 'Simple IPv6'
152 ),
153 array(
154 '12.0.0.1',
155 array(
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 array( 'ABCD:1:2:3:4:555:6666:7777' ),
160 array(),
161 false,
162 'IPv6 normalisation'
163 ),
164 array(
165 '12.0.0.3',
166 array(
167 'REMOTE_ADDR' => '12.0.0.1',
168 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
169 ),
170 array( '12.0.0.1', '12.0.0.2' ),
171 array(),
172 false,
173 'With X-Forwaded-For'
174 ),
175 array(
176 '12.0.0.1',
177 array(
178 'REMOTE_ADDR' => '12.0.0.1',
179 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
180 ),
181 array(),
182 array(),
183 false,
184 'With X-Forwaded-For and disallowed server'
185 ),
186 array(
187 '12.0.0.2',
188 array(
189 'REMOTE_ADDR' => '12.0.0.1',
190 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
191 ),
192 array( '12.0.0.1' ),
193 array(),
194 false,
195 'With multiple X-Forwaded-For and only one allowed server'
196 ),
197 array(
198 '10.0.0.3',
199 array(
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 array( '12.0.0.1', '12.0.0.2' ),
204 array(),
205 false,
206 'With X-Forwaded-For and private IP (from cache proxy)'
207 ),
208 array(
209 '10.0.0.4',
210 array(
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 array( '12.0.0.1', '12.0.0.2', '10.0.0.3' ),
215 array(),
216 true,
217 'With X-Forwaded-For and private IP (allowed)'
218 ),
219 array(
220 '10.0.0.4',
221 array(
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 array( '12.0.0.1', '12.0.0.2' ),
226 array( '10.0.0.3' ),
227 true,
228 'With X-Forwaded-For and private IP (allowed)'
229 ),
230 array(
231 '10.0.0.3',
232 array(
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 array( '12.0.0.1', '12.0.0.2' ),
237 array( '10.0.0.3' ),
238 false,
239 'With X-Forwaded-For and private IP (disallowed)'
240 ),
241 array(
242 '12.0.0.3',
243 array(
244 'REMOTE_ADDR' => '12.0.0.1',
245 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
246 ),
247 array(),
248 array( '12.0.0.1', '12.0.0.2' ),
249 false,
250 'With X-Forwaded-For'
251 ),
252 array(
253 '12.0.0.2',
254 array(
255 'REMOTE_ADDR' => '12.0.0.1',
256 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
257 ),
258 array(),
259 array( '12.0.0.1' ),
260 false,
261 'With multiple X-Forwaded-For and only one allowed server'
262 ),
263 array(
264 '12.0.0.2',
265 array(
266 'REMOTE_ADDR' => '12.0.0.2',
267 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
268 ),
269 array(),
270 array( '12.0.0.2' ),
271 false,
272 'With X-Forwaded-For and private IP and hook (disallowed)'
273 ),
274 array(
275 '12.0.0.1',
276 array(
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 array( 'ABCD:1:2:3::/64' ),
281 array(),
282 false,
283 'IPv6 CIDR'
284 ),
285 array(
286 '12.0.0.3',
287 array(
288 'REMOTE_ADDR' => '12.0.0.1',
289 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
290 ),
291 array( '12.0.0.0/24' ),
292 array(),
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( array(
306 'wgSquidServersNoPurge' => array(),
307 'wgSquidServers' => array(),
308 'wgUsePrivateIPs' => false,
309 'wgHooks' => array(),
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 array(
319 array( '', array(), 'Empty Accept-Language header' ),
320 array( 'en', array( 'en' => 1 ), 'One language' ),
321 array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
322 array(
323 'zh-cn,zh-tw',
324 array( 'zh-cn' => 1, 'zh-tw' => 1 ),
325 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
326 ),
327 array(
328 'es, en; q=0.5',
329 array( 'es' => 1, 'en' => '0.5' ),
330 'Spanish as first language and English and second'
331 ),
332 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
333 array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
334 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
335 array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
336 array(
337 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
338 array( 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ),
339 'Preference for Romance languages'
340 ),
341 array(
342 'en-gb, en-us; q=1',
343 array( '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 = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
355 $request = new WebRequest();
356 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
357 }
358 }