MediaWiki  REL1_22
WebRequestTest.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class WebRequestTest extends MediaWikiTestCase {
00007     protected $oldServer;
00008 
00009     protected function setUp() {
00010         parent::setUp();
00011 
00012         $this->oldServer = $_SERVER;
00013     }
00014 
00015     protected function tearDown() {
00016         $_SERVER = $this->oldServer;
00017 
00018         parent::tearDown();
00019     }
00020 
00025     public function testDetectServer( $expected, $input, $description ) {
00026         $_SERVER = $input;
00027         $result = WebRequest::detectServer();
00028         $this->assertEquals( $expected, $result, $description );
00029     }
00030 
00031     public static function provideDetectServer() {
00032         return array(
00033             array(
00034                 'http://x',
00035                 array(
00036                     'HTTP_HOST' => 'x'
00037                 ),
00038                 'Host header'
00039             ),
00040             array(
00041                 'https://x',
00042                 array(
00043                     'HTTP_HOST' => 'x',
00044                     'HTTPS' => 'on',
00045                 ),
00046                 'Host header with secure'
00047             ),
00048             array(
00049                 'http://x',
00050                 array(
00051                     'HTTP_HOST' => 'x',
00052                     'SERVER_PORT' => 80,
00053                 ),
00054                 'Default SERVER_PORT',
00055             ),
00056             array(
00057                 'http://x',
00058                 array(
00059                     'HTTP_HOST' => 'x',
00060                     'HTTPS' => 'off',
00061                 ),
00062                 'Secure off'
00063             ),
00064             array(
00065                 'http://y',
00066                 array(
00067                     'SERVER_NAME' => 'y',
00068                 ),
00069                 'Server name'
00070             ),
00071             array(
00072                 'http://x',
00073                 array(
00074                     'HTTP_HOST' => 'x',
00075                     'SERVER_NAME' => 'y',
00076                 ),
00077                 'Host server name precedence'
00078             ),
00079             array(
00080                 'http://[::1]:81',
00081                 array(
00082                     'HTTP_HOST' => '[::1]',
00083                     'SERVER_NAME' => '::1',
00084                     'SERVER_PORT' => '81',
00085                 ),
00086                 'Apache bug 26005'
00087             ),
00088             array(
00089                 'http://localhost',
00090                 array(
00091                     'SERVER_NAME' => '[2001'
00092                 ),
00093                 'Kind of like lighttpd per commit message in MW r83847',
00094             ),
00095             array(
00096                 'http://[2a01:e35:2eb4:1::2]:777',
00097                 array(
00098                     'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
00099                 ),
00100                 'Possible lighttpd environment per bug 14977 comment 13',
00101             ),
00102         );
00103     }
00104 
00109     public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
00110         $_SERVER = $input;
00111         $this->setMwGlobals( array(
00112             'wgSquidServersNoPurge' => $squid,
00113             'wgUsePrivateIPs' => $private,
00114             'wgHooks' => array(
00115                 'IsTrustedProxy' => array(
00116                     function( &$ip, &$trusted ) use ( $xffList ) {
00117                         $trusted = $trusted || in_array( $ip, $xffList );
00118                         return true;
00119                     }
00120                 )
00121             )
00122         ) );
00123 
00124         $request = new WebRequest();
00125         $result = $request->getIP();
00126         $this->assertEquals( $expected, $result, $description );
00127     }
00128 
00129     public static function provideGetIP() {
00130         return array(
00131             array(
00132                 '127.0.0.1',
00133                 array(
00134                     'REMOTE_ADDR' => '127.0.0.1'
00135                 ),
00136                 array(),
00137                 array(),
00138                 false,
00139                 'Simple IPv4'
00140             ),
00141             array(
00142                 '::1',
00143                 array(
00144                     'REMOTE_ADDR' => '::1'
00145                 ),
00146                 array(),
00147                 array(),
00148                 false,
00149                 'Simple IPv6'
00150             ),
00151             array(
00152                 '12.0.0.1',
00153                 array(
00154                     'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
00155                     'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
00156                 ),
00157                 array( 'ABCD:1:2:3:4:555:6666:7777' ),
00158                 array(),
00159                 false,
00160                 'IPv6 normalisation'
00161             ),
00162             array(
00163                 '12.0.0.3',
00164                 array(
00165                     'REMOTE_ADDR' => '12.0.0.1',
00166                     'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
00167                 ),
00168                 array( '12.0.0.1', '12.0.0.2' ),
00169                 array(),
00170                 false,
00171                 'With X-Forwaded-For'
00172             ),
00173             array(
00174                 '12.0.0.1',
00175                 array(
00176                     'REMOTE_ADDR' => '12.0.0.1',
00177                     'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
00178                 ),
00179                 array(),
00180                 array(),
00181                 false,
00182                 'With X-Forwaded-For and disallowed server'
00183             ),
00184             array(
00185                 '12.0.0.2',
00186                 array(
00187                     'REMOTE_ADDR' => '12.0.0.1',
00188                     'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
00189                 ),
00190                 array( '12.0.0.1' ),
00191                 array(),
00192                 false,
00193                 'With multiple X-Forwaded-For and only one allowed server'
00194             ),
00195             array(
00196                 '10.0.0.3',
00197                 array(
00198                     'REMOTE_ADDR' => '12.0.0.2',
00199                     'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
00200                 ),
00201                 array( '12.0.0.1', '12.0.0.2' ),
00202                 array(),
00203                 false,
00204                 'With X-Forwaded-For and private IP (from cache proxy)'
00205             ),
00206             array(
00207                 '10.0.0.4',
00208                 array(
00209                     'REMOTE_ADDR' => '12.0.0.2',
00210                     'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
00211                 ),
00212                 array( '12.0.0.1', '12.0.0.2', '10.0.0.3' ),
00213                 array(),
00214                 true,
00215                 'With X-Forwaded-For and private IP (allowed)'
00216             ),
00217             array(
00218                 '10.0.0.4',
00219                 array(
00220                     'REMOTE_ADDR' => '12.0.0.2',
00221                     'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
00222                 ),
00223                 array( '12.0.0.1', '12.0.0.2' ),
00224                 array( '10.0.0.3' ),
00225                 true,
00226                 'With X-Forwaded-For and private IP (allowed)'
00227             ),
00228             array(
00229                 '10.0.0.3',
00230                 array(
00231                     'REMOTE_ADDR' => '12.0.0.2',
00232                     'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
00233                 ),
00234                 array( '12.0.0.1', '12.0.0.2' ),
00235                 array( '10.0.0.3' ),
00236                 false,
00237                 'With X-Forwaded-For and private IP (disallowed)'
00238             ),
00239             array(
00240                 '12.0.0.3',
00241                 array(
00242                     'REMOTE_ADDR' => '12.0.0.1',
00243                     'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
00244                 ),
00245                 array(),
00246                 array( '12.0.0.1', '12.0.0.2' ),
00247                 false,
00248                 'With X-Forwaded-For'
00249             ),
00250             array(
00251                 '12.0.0.2',
00252                 array(
00253                     'REMOTE_ADDR' => '12.0.0.1',
00254                     'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
00255                 ),
00256                 array(),
00257                 array( '12.0.0.1' ),
00258                 false,
00259                 'With multiple X-Forwaded-For and only one allowed server'
00260             ),
00261             array(
00262                 '12.0.0.2',
00263                 array(
00264                     'REMOTE_ADDR' => '12.0.0.2',
00265                     'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
00266                 ),
00267                 array(),
00268                 array( '12.0.0.2' ),
00269                 false,
00270                 'With X-Forwaded-For and private IP and hook (disallowed)'
00271             ),
00272         );
00273     }
00274 
00279     public function testGetIpLackOfRemoteAddrThrowAnException() {
00280         $request = new WebRequest();
00281         # Next call throw an exception about lacking an IP
00282         $request->getIP();
00283     }
00284 
00285     public static function provideLanguageData() {
00286         return array(
00287             array( '', array(), 'Empty Accept-Language header' ),
00288             array( 'en', array( 'en' => 1 ), 'One language' ),
00289             array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
00290             array( 'zh-cn,zh-tw', array( 'zh-cn' => 1, 'zh-tw' => 1 ), 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119' ),
00291             array( 'es, en; q=0.5', array( 'es' => 1, 'en' => '0.5' ), 'Spanish as first language and English and second' ),
00292             array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
00293             array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
00294             array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
00295             array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
00296             array( 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0', array( 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ), 'Preference for romance languages' ),
00297             array( 'en-gb, en-us; q=1', array( 'en-gb' => 1, 'en-us' => '1' ), 'Two equally prefered English variants' ),
00298         );
00299     }
00300 
00305     public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
00306         $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
00307         $request = new WebRequest();
00308         $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
00309     }
00310 }