MediaWiki  REL1_20
WebRequestTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class WebRequestTest extends MediaWikiTestCase {
00004         static $oldServer;
00005 
00006         function setUp() {
00007                 self::$oldServer = $_SERVER;
00008         }
00009 
00010         function tearDown() {
00011                 $_SERVER = self::$oldServer;
00012         }
00013 
00017         function testDetectServer( $expected, $input, $description ) {
00018                 $_SERVER = $input;
00019                 $result = WebRequest::detectServer();
00020                 $this->assertEquals( $expected, $result, $description );
00021         }
00022 
00023         function provideDetectServer() {
00024                 return array(
00025                         array(
00026                                 'http://x',
00027                                 array(
00028                                         'HTTP_HOST' => 'x'
00029                                 ),
00030                                 'Host header'
00031                         ),
00032                         array(
00033                                 'https://x',
00034                                 array(
00035                                         'HTTP_HOST' => 'x',
00036                                         'HTTPS' => 'on',
00037                                 ),
00038                                 'Host header with secure'
00039                         ),
00040                         array(
00041                                 'http://x',
00042                                 array(
00043                                         'HTTP_HOST' => 'x',
00044                                         'SERVER_PORT' => 80,
00045                                 ),
00046                                 'Default SERVER_PORT',
00047                         ),
00048                         array(
00049                                 'http://x',
00050                                 array(
00051                                         'HTTP_HOST' => 'x',
00052                                         'HTTPS' => 'off',
00053                                 ),
00054                                 'Secure off'
00055                         ),
00056                         array(
00057                                 'http://y',
00058                                 array(
00059                                         'SERVER_NAME' => 'y',
00060                                 ),
00061                                 'Server name'
00062                         ),
00063                         array(
00064                                 'http://x',
00065                                 array(
00066                                         'HTTP_HOST' => 'x',
00067                                         'SERVER_NAME' => 'y',
00068                                 ),
00069                                 'Host server name precedence'
00070                         ),
00071                         array(
00072                                 'http://[::1]:81',
00073                                 array(
00074                                         'HTTP_HOST' => '[::1]',
00075                                         'SERVER_NAME' => '::1',
00076                                         'SERVER_PORT' => '81',
00077                                 ),
00078                                 'Apache bug 26005'
00079                         ),
00080                         array(
00081                                 'http://localhost',
00082                                 array(
00083                                         'SERVER_NAME' => '[2001'
00084                                 ),
00085                                 'Kind of like lighttpd per commit message in MW r83847',
00086                         ),
00087                         array(
00088                                 'http://[2a01:e35:2eb4:1::2]:777',
00089                                 array(
00090                                         'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
00091                                 ),
00092                                 'Possible lighttpd environment per bug 14977 comment 13',
00093                         ),
00094                 );
00095         }
00096 
00100         function testGetIP( $expected, $input, $squid, $private, $description ) {
00101                 global $wgSquidServersNoPurge, $wgUsePrivateIPs;
00102                 $_SERVER = $input;
00103                 $wgSquidServersNoPurge = $squid;
00104                 $wgUsePrivateIPs = $private;
00105                 $request = new WebRequest();
00106                 $result = $request->getIP();
00107                 $this->assertEquals( $expected, $result, $description );
00108         }
00109 
00110         function provideGetIP() {
00111                 return array(
00112                         array(
00113                                 '127.0.0.1',
00114                                 array(
00115                                         'REMOTE_ADDR' => '127.0.0.1'
00116                                 ),
00117                                 array(),
00118                                 false,
00119                                 'Simple IPv4'
00120                         ),
00121                         array(
00122                                 '::1',
00123                                 array(
00124                                         'REMOTE_ADDR' => '::1'
00125                                 ),
00126                                 array(),
00127                                 false,
00128                                 'Simple IPv6'
00129                         ),
00130                         array(
00131                                 '12.0.0.3',
00132                                 array(
00133                                         'REMOTE_ADDR' => '12.0.0.1',
00134                                         'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
00135                                 ),
00136                                 array( '12.0.0.1', '12.0.0.2' ),
00137                                 false,
00138                                 'With X-Forwaded-For'
00139                         ),
00140                         array(
00141                                 '12.0.0.1',
00142                                 array(
00143                                         'REMOTE_ADDR' => '12.0.0.1',
00144                                         'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
00145                                 ),
00146                                 array(),
00147                                 false,
00148                                 'With X-Forwaded-For and disallowed server'
00149                         ),
00150                         array(
00151                                 '12.0.0.2',
00152                                 array(
00153                                         'REMOTE_ADDR' => '12.0.0.1',
00154                                         'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
00155                                 ),
00156                                 array( '12.0.0.1' ),
00157                                 false,
00158                                 'With multiple X-Forwaded-For and only one allowed server'
00159                         ),
00160                         array(
00161                                 '12.0.0.2',
00162                                 array(
00163                                         'REMOTE_ADDR' => '12.0.0.2',
00164                                         'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
00165                                 ),
00166                                 array( '12.0.0.1', '12.0.0.2' ),
00167                                 false,
00168                                 'With X-Forwaded-For and private IP'
00169                         ),
00170                         array(
00171                                 '10.0.0.3',
00172                                 array(
00173                                         'REMOTE_ADDR' => '12.0.0.2',
00174                                         'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
00175                                 ),
00176                                 array( '12.0.0.1', '12.0.0.2' ),
00177                                 true,
00178                                 'With X-Forwaded-For and private IP (allowed)'
00179                         ),
00180                 );
00181         }
00182 
00186         function testGetIpLackOfRemoteAddrThrowAnException() {
00187                 $request = new WebRequest();
00188                 # Next call throw an exception about lacking an IP
00189                 $request->getIP();
00190         }
00191 
00192         function languageProvider() {
00193                 return array(
00194                         array( '', array(), 'Empty Accept-Language header' ),
00195                         array( 'en', array( 'en' => 1 ), 'One language' ),
00196                         array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
00197                         array( 'zh-cn,zh-tw', array( 'zh-cn' => 1, 'zh-tw' => 1 ), 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119' ),
00198                         array( 'es, en; q=0.5', array( 'es' => 1, 'en' => '0.5' ), 'Spanish as first language and English and second' ),
00199                         array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
00200                         array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
00201                         array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
00202                         array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
00203                         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' ),
00204                         array( 'en-gb, en-us; q=1', array( 'en-gb' => 1, 'en-us' => '1' ), 'Two equally prefered English variants' ),
00205                 );
00206         }
00207 
00211         function testAcceptLang($acceptLanguageHeader, $expectedLanguages, $description) {
00212                 $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
00213                 $request = new WebRequest();
00214                 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description);
00215         }
00216 }