6 class WebRequestTest
extends MediaWikiTestCase
{
9 protected function setUp() {
12 $this->oldServer
= $_SERVER;
15 protected function tearDown() {
16 $_SERVER = $this->oldServer
;
22 * @dataProvider provideDetectServer
24 function testDetectServer( $expected, $input, $description ) {
26 $result = WebRequest
::detectServer();
27 $this->assertEquals( $expected, $result, $description );
30 public static function provideDetectServer() {
45 'Host header with secure'
53 'Default SERVER_PORT',
76 'Host server name precedence'
81 'HTTP_HOST' => '[::1]',
82 'SERVER_NAME' => '::1',
83 'SERVER_PORT' => '81',
90 'SERVER_NAME' => '[2001'
92 'Kind of like lighttpd per commit message in MW r83847',
95 'http://[2a01:e35:2eb4:1::2]:777',
97 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
99 'Possible lighttpd environment per bug 14977 comment 13',
105 * @dataProvider provideGetIP
107 function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
109 $this->setMwGlobals( array(
110 'wgSquidServersNoPurge' => $squid,
111 'wgUsePrivateIPs' => $private,
113 'IsTrustedProxy' => array(
114 function( &$ip, &$trusted ) use ( $xffList ) {
115 $trusted = $trusted ||
in_array( $ip, $xffList );
122 $request = new WebRequest();
123 $result = $request->getIP();
124 $this->assertEquals( $expected, $result, $description );
127 public static function provideGetIP() {
132 'REMOTE_ADDR' => '127.0.0.1'
142 'REMOTE_ADDR' => '::1'
152 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
153 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
155 array( 'ABCD:1:2:3:4:555:6666:7777' ),
163 'REMOTE_ADDR' => '12.0.0.1',
164 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
166 array( '12.0.0.1', '12.0.0.2' ),
169 'With X-Forwaded-For'
174 'REMOTE_ADDR' => '12.0.0.1',
175 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
180 'With X-Forwaded-For and disallowed server'
185 'REMOTE_ADDR' => '12.0.0.1',
186 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
191 'With multiple X-Forwaded-For and only one allowed server'
196 'REMOTE_ADDR' => '12.0.0.2',
197 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
199 array( '12.0.0.1', '12.0.0.2' ),
202 'With X-Forwaded-For and private IP (from cache proxy)'
207 'REMOTE_ADDR' => '12.0.0.2',
208 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
210 array( '12.0.0.1', '12.0.0.2', '10.0.0.3' ),
213 'With X-Forwaded-For and private IP (allowed)'
218 'REMOTE_ADDR' => '12.0.0.2',
219 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
221 array( '12.0.0.1', '12.0.0.2' ),
224 'With X-Forwaded-For and private IP (allowed)'
229 'REMOTE_ADDR' => '12.0.0.2',
230 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
232 array( '12.0.0.1', '12.0.0.2' ),
235 'With X-Forwaded-For and private IP (disallowed)'
240 'REMOTE_ADDR' => '12.0.0.1',
241 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
244 array( '12.0.0.1', '12.0.0.2' ),
246 'With X-Forwaded-For'
251 'REMOTE_ADDR' => '12.0.0.1',
252 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
257 'With multiple X-Forwaded-For and only one allowed server'
262 'REMOTE_ADDR' => '12.0.0.2',
263 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
268 'With X-Forwaded-For and private IP and hook (disallowed)'
274 * @expectedException MWException
276 function testGetIpLackOfRemoteAddrThrowAnException() {
277 $request = new WebRequest();
278 # Next call throw an exception about lacking an IP
282 public static function provideLanguageData() {
284 array( '', array(), 'Empty Accept-Language header' ),
285 array( 'en', array( 'en' => 1 ), 'One language' ),
286 array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
287 array( 'zh-cn,zh-tw', array( 'zh-cn' => 1, 'zh-tw' => 1 ), 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119' ),
288 array( 'es, en; q=0.5', array( 'es' => 1, 'en' => '0.5' ), 'Spanish as first language and English and second' ),
289 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
290 array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
291 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
292 array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
293 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' ),
294 array( 'en-gb, en-us; q=1', array( 'en-gb' => 1, 'en-us' => '1' ), 'Two equally prefered English variants' ),
299 * @dataProvider provideLanguageData
301 function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
302 $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
303 $request = new WebRequest();
304 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );