Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / WebRequestTest.php
blob12d7d2a39afb54bf2961c57ca897db507c793647
1 <?php
3 /**
4 * @group WebRequest
5 */
6 class WebRequestTest extends MediaWikiTestCase {
7 protected $oldServer;
9 protected function setUp() {
10 parent::setUp();
12 $this->oldServer = $_SERVER;
13 IP::clearCaches();
16 protected function tearDown() {
17 $_SERVER = $this->oldServer;
18 IP::clearCaches();
20 parent::tearDown();
23 /**
24 * @dataProvider provideDetectServer
25 * @covers WebRequest::detectServer
27 public function testDetectServer( $expected, $input, $description ) {
28 $_SERVER = $input;
29 $result = WebRequest::detectServer();
30 $this->assertEquals( $expected, $result, $description );
33 public static function provideDetectServer() {
34 return array(
35 array(
36 'http://x',
37 array(
38 'HTTP_HOST' => 'x'
40 'Host header'
42 array(
43 'https://x',
44 array(
45 'HTTP_HOST' => 'x',
46 'HTTPS' => 'on',
48 'Host header with secure'
50 array(
51 'http://x',
52 array(
53 'HTTP_HOST' => 'x',
54 'SERVER_PORT' => 80,
56 'Default SERVER_PORT',
58 array(
59 'http://x',
60 array(
61 'HTTP_HOST' => 'x',
62 'HTTPS' => 'off',
64 'Secure off'
66 array(
67 'http://y',
68 array(
69 'SERVER_NAME' => 'y',
71 'Server name'
73 array(
74 'http://x',
75 array(
76 'HTTP_HOST' => 'x',
77 'SERVER_NAME' => 'y',
79 'Host server name precedence'
81 array(
82 'http://[::1]:81',
83 array(
84 'HTTP_HOST' => '[::1]',
85 'SERVER_NAME' => '::1',
86 'SERVER_PORT' => '81',
88 'Apache bug 26005'
90 array(
91 'http://localhost',
92 array(
93 'SERVER_NAME' => '[2001'
95 'Kind of like lighttpd per commit message in MW r83847',
97 array(
98 'http://[2a01:e35:2eb4:1::2]:777',
99 array(
100 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
102 'Possible lighttpd environment per bug 14977 comment 13',
108 * @dataProvider provideGetIP
109 * @covers WebRequest::getIP
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;
124 ) );
126 $request = new WebRequest();
127 $result = $request->getIP();
128 $this->assertEquals( $expected, $result, $description );
131 public static function provideGetIP() {
132 return array(
133 array(
134 '127.0.0.1',
135 array(
136 'REMOTE_ADDR' => '127.0.0.1'
138 array(),
139 array(),
140 false,
141 'Simple IPv4'
143 array(
144 '::1',
145 array(
146 'REMOTE_ADDR' => '::1'
148 array(),
149 array(),
150 false,
151 'Simple IPv6'
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',
159 array( 'ABCD:1:2:3:4:555:6666:7777' ),
160 array(),
161 false,
162 'IPv6 normalisation'
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'
170 array( '12.0.0.1', '12.0.0.2' ),
171 array(),
172 false,
173 'With X-Forwaded-For'
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'
181 array(),
182 array(),
183 false,
184 'With X-Forwaded-For and disallowed server'
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'
192 array( '12.0.0.1' ),
193 array(),
194 false,
195 'With multiple X-Forwaded-For and only one allowed server'
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'
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)'
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'
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)'
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'
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)'
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'
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)'
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'
247 array(),
248 array( '12.0.0.1', '12.0.0.2' ),
249 false,
250 'With X-Forwaded-For'
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'
258 array(),
259 array( '12.0.0.1' ),
260 false,
261 'With multiple X-Forwaded-For and only one allowed server'
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'
269 array(),
270 array( '12.0.0.2' ),
271 false,
272 'With X-Forwaded-For and private IP and hook (disallowed)'
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',
280 array( 'ABCD:1:2:3::/64' ),
281 array(),
282 false,
283 'IPv6 CIDR'
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'
291 array( '12.0.0.0/24' ),
292 array(),
293 false,
294 'IPv4 CIDR'
300 * @expectedException MWException
301 * @covers WebRequest::getIP
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 ) );
312 $request = new WebRequest();
313 # Next call throw an exception about lacking an IP
314 $request->getIP();
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'
327 array(
328 'es, en; q=0.5',
329 array( 'es' => 1, 'en' => '0.5' ),
330 'Spanish as first language and English and second'
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'
341 array(
342 'en-gb, en-us; q=1',
343 array( 'en-gb' => 1, 'en-us' => '1' ),
344 'Two equally prefered English variants'
350 * @dataProvider provideLanguageData
351 * @covers WebRequest::getAcceptLang
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 );