3 class WebRequestTest
extends MediaWikiTestCase
{
6 protected function setUp() {
9 $this->oldServer
= $_SERVER;
12 protected function tearDown() {
13 $_SERVER = $this->oldServer
;
19 * @dataProvider provideDetectServer
21 function testDetectServer( $expected, $input, $description ) {
23 $result = WebRequest
::detectServer();
24 $this->assertEquals( $expected, $result, $description );
27 public static function provideDetectServer() {
42 'Host header with secure'
50 'Default SERVER_PORT',
73 'Host server name precedence'
78 'HTTP_HOST' => '[::1]',
79 'SERVER_NAME' => '::1',
80 'SERVER_PORT' => '81',
87 'SERVER_NAME' => '[2001'
89 'Kind of like lighttpd per commit message in MW r83847',
92 'http://[2a01:e35:2eb4:1::2]:777',
94 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
96 'Possible lighttpd environment per bug 14977 comment 13',
102 * @dataProvider provideGetIP
104 function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
106 $this->setMwGlobals( array(
107 'wgSquidServersNoPurge' => $squid,
108 'wgUsePrivateIPs' => $private,
110 'IsTrustedProxy' => array(
111 function( &$ip, &$trusted ) use ( $xffList ) {
112 $trusted = $trusted ||
in_array( $ip, $xffList );
119 $request = new WebRequest();
120 $result = $request->getIP();
121 $this->assertEquals( $expected, $result, $description );
124 public static function provideGetIP() {
129 'REMOTE_ADDR' => '127.0.0.1'
139 'REMOTE_ADDR' => '::1'
149 'REMOTE_ADDR' => '12.0.0.1',
150 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
152 array( '12.0.0.1', '12.0.0.2' ),
155 'With X-Forwaded-For'
160 'REMOTE_ADDR' => '12.0.0.1',
161 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
166 'With X-Forwaded-For and disallowed server'
171 'REMOTE_ADDR' => '12.0.0.1',
172 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
177 'With multiple X-Forwaded-For and only one allowed server'
182 'REMOTE_ADDR' => '12.0.0.2',
183 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
185 array( '12.0.0.1', '12.0.0.2' ),
188 'With X-Forwaded-For and private IP (from cache proxy)'
193 'REMOTE_ADDR' => '12.0.0.2',
194 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
196 array( '12.0.0.1', '12.0.0.2', '10.0.0.3' ),
199 'With X-Forwaded-For and private IP (allowed)'
204 'REMOTE_ADDR' => '12.0.0.2',
205 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
207 array( '12.0.0.1', '12.0.0.2' ),
210 'With X-Forwaded-For and private IP (allowed)'
215 'REMOTE_ADDR' => '12.0.0.2',
216 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
218 array( '12.0.0.1', '12.0.0.2' ),
221 'With X-Forwaded-For and private IP (disallowed)'
226 'REMOTE_ADDR' => '12.0.0.1',
227 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
230 array( '12.0.0.1', '12.0.0.2' ),
232 'With X-Forwaded-For'
237 'REMOTE_ADDR' => '12.0.0.1',
238 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
243 'With multiple X-Forwaded-For and only one allowed server'
248 'REMOTE_ADDR' => '12.0.0.2',
249 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
254 'With X-Forwaded-For and private IP and hook (disallowed)'
260 * @expectedException MWException
262 function testGetIpLackOfRemoteAddrThrowAnException() {
263 $request = new WebRequest();
264 # Next call throw an exception about lacking an IP
268 public static function provideLanguageData() {
270 array( '', array(), 'Empty Accept-Language header' ),
271 array( 'en', array( 'en' => 1 ), 'One language' ),
272 array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
273 array( 'zh-cn,zh-tw', array( 'zh-cn' => 1, 'zh-tw' => 1 ), 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119' ),
274 array( 'es, en; q=0.5', array( 'es' => 1, 'en' => '0.5' ), 'Spanish as first language and English and second' ),
275 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
276 array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
277 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
278 array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
279 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' ),
280 array( 'en-gb, en-us; q=1', array( 'en-gb' => 1, 'en-us' => '1' ), 'Two equally prefered English variants' ),
285 * @dataProvider provideLanguageData
287 function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
288 $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
289 $request = new WebRequest();
290 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );