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