6 class WebRequestTest
extends MediaWikiTestCase
{
9 protected function setUp() {
12 $this->oldServer
= $_SERVER;
15 protected function tearDown() {
16 $_SERVER = $this->oldServer
;
22 * @dataProvider provideDetectServer
23 * @covers WebRequest::detectServer
25 public function testDetectServer( $expected, $input, $description ) {
27 $result = WebRequest
::detectServer();
28 $this->assertEquals( $expected, $result, $description );
31 public static function provideDetectServer() {
46 'Host header with secure'
54 'Default SERVER_PORT',
77 'Host server name precedence'
82 'HTTP_HOST' => '[::1]',
83 'SERVER_NAME' => '::1',
84 'SERVER_PORT' => '81',
91 'SERVER_NAME' => '[2001'
93 'Kind of like lighttpd per commit message in MW r83847',
96 'http://[2a01:e35:2eb4:1::2]:777',
98 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
100 'Possible lighttpd environment per bug 14977 comment 13',
106 * @dataProvider provideGetIP
107 * @covers WebRequest::getIP
109 public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
111 $this->setMwGlobals( array(
112 'wgSquidServersNoPurge' => $squid,
113 'wgUsePrivateIPs' => $private,
115 'IsTrustedProxy' => array(
116 function( &$ip, &$trusted ) use ( $xffList ) {
117 $trusted = $trusted ||
in_array( $ip, $xffList );
124 $request = new WebRequest();
125 $result = $request->getIP();
126 $this->assertEquals( $expected, $result, $description );
129 public static function provideGetIP() {
134 'REMOTE_ADDR' => '127.0.0.1'
144 'REMOTE_ADDR' => '::1'
154 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
155 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
157 array( 'ABCD:1:2:3:4:555:6666:7777' ),
165 'REMOTE_ADDR' => '12.0.0.1',
166 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
168 array( '12.0.0.1', '12.0.0.2' ),
171 'With X-Forwaded-For'
176 'REMOTE_ADDR' => '12.0.0.1',
177 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
182 'With X-Forwaded-For and disallowed server'
187 'REMOTE_ADDR' => '12.0.0.1',
188 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
193 'With multiple X-Forwaded-For and only one allowed server'
198 'REMOTE_ADDR' => '12.0.0.2',
199 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
201 array( '12.0.0.1', '12.0.0.2' ),
204 'With X-Forwaded-For and private IP (from cache proxy)'
209 'REMOTE_ADDR' => '12.0.0.2',
210 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
212 array( '12.0.0.1', '12.0.0.2', '10.0.0.3' ),
215 'With X-Forwaded-For and private IP (allowed)'
220 'REMOTE_ADDR' => '12.0.0.2',
221 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
223 array( '12.0.0.1', '12.0.0.2' ),
226 'With X-Forwaded-For and private IP (allowed)'
231 'REMOTE_ADDR' => '12.0.0.2',
232 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
234 array( '12.0.0.1', '12.0.0.2' ),
237 'With X-Forwaded-For and private IP (disallowed)'
242 'REMOTE_ADDR' => '12.0.0.1',
243 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
246 array( '12.0.0.1', '12.0.0.2' ),
248 'With X-Forwaded-For'
253 'REMOTE_ADDR' => '12.0.0.1',
254 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
259 'With multiple X-Forwaded-For and only one allowed server'
264 'REMOTE_ADDR' => '12.0.0.2',
265 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
270 'With X-Forwaded-For and private IP and hook (disallowed)'
275 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
276 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
278 array( 'ABCD:1:2:3::/64' ),
286 'REMOTE_ADDR' => '12.0.0.1',
287 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
289 array( '12.0.0.0/24' ),
298 * @expectedException MWException
299 * @covers WebRequest::getIP
301 public function testGetIpLackOfRemoteAddrThrowAnException() {
302 // ensure that local install state doesn't interfere with test
303 $this->setMwGlobals( array(
304 'wgSquidServersNoPurge' => array(),
305 'wgSquidServers' => array(),
306 'wgUsePrivateIPs' => false,
307 'wgHooks' => array(),
310 $request = new WebRequest();
311 # Next call throw an exception about lacking an IP
315 public static function provideLanguageData() {
317 array( '', array(), 'Empty Accept-Language header' ),
318 array( 'en', array( 'en' => 1 ), 'One language' ),
319 array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
322 array( 'zh-cn' => 1, 'zh-tw' => 1 ),
323 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
327 array( 'es' => 1, 'en' => '0.5' ),
328 'Spanish as first language and English and second'
330 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
331 array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
332 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
333 array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
335 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
336 array( 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ),
337 'Preference for Romance languages'
341 array( 'en-gb' => 1, 'en-us' => '1' ),
342 'Two equally prefered English variants'
348 * @dataProvider provideLanguageData
349 * @covers WebRequest::getAcceptLang
351 public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
352 $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
353 $request = new WebRequest();
354 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );