6 class WebRequestTest
extends MediaWikiTestCase
{
9 protected function setUp() {
12 $this->oldServer
= $_SERVER;
16 protected function tearDown() {
17 $_SERVER = $this->oldServer
;
24 * @dataProvider provideDetectServer
25 * @covers WebRequest::detectServer
27 public function testDetectServer( $expected, $input, $description ) {
29 $result = WebRequest
::detectServer();
30 $this->assertEquals( $expected, $result, $description );
33 public static function provideDetectServer() {
48 'Host header with secure'
56 'Default SERVER_PORT',
79 'Host server name precedence'
84 'HTTP_HOST' => '[::1]',
85 'SERVER_NAME' => '::1',
86 'SERVER_PORT' => '81',
93 'SERVER_NAME' => '[2001'
95 'Kind of like lighttpd per commit message in MW r83847',
98 'http://[2a01:e35:2eb4:1::2]:777',
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 ) {
113 $this->setMwGlobals( array(
114 'wgSquidServersNoPurge' => $squid,
115 'wgUsePrivateIPs' => $private,
117 'IsTrustedProxy' => array(
118 function ( &$ip, &$trusted ) use ( $xffList ) {
119 $trusted = $trusted ||
in_array( $ip, $xffList );
126 $request = new WebRequest();
127 $result = $request->getIP();
128 $this->assertEquals( $expected, $result, $description );
131 public static function provideGetIP() {
136 'REMOTE_ADDR' => '127.0.0.1'
146 'REMOTE_ADDR' => '::1'
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' ),
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' ),
173 'With X-Forwaded-For'
178 'REMOTE_ADDR' => '12.0.0.1',
179 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
184 'With X-Forwaded-For and disallowed server'
189 'REMOTE_ADDR' => '12.0.0.1',
190 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
195 'With multiple X-Forwaded-For and only one allowed server'
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' ),
206 'With X-Forwaded-For and private IP (from cache proxy)'
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' ),
217 'With X-Forwaded-For and private IP (allowed)'
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' ),
228 'With X-Forwaded-For and private IP (allowed)'
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' ),
239 'With X-Forwaded-For and private IP (disallowed)'
244 'REMOTE_ADDR' => '12.0.0.1',
245 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
248 array( '12.0.0.1', '12.0.0.2' ),
250 'With X-Forwaded-For'
255 'REMOTE_ADDR' => '12.0.0.1',
256 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
261 'With multiple X-Forwaded-For and only one allowed server'
266 'REMOTE_ADDR' => '12.0.0.2',
267 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
272 'With X-Forwaded-For and private IP and hook (disallowed)'
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' ),
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' ),
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(),
312 $request = new WebRequest();
313 # Next call throw an exception about lacking an IP
317 public static function provideLanguageData() {
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.' ),
324 array( 'zh-cn' => 1, 'zh-tw' => 1 ),
325 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
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" ),
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'
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 );