6 class WebRequestTest
extends MediaWikiTestCase
{
9 protected function setUp() {
12 $this->oldServer
= $_SERVER;
15 protected function tearDown() {
16 $_SERVER = $this->oldServer
;
22 * @dataProvider provideDetectServer
23 * @covers WebRequest::detectServer
24 * @covers WebRequest::detectProtocol
26 public function testDetectServer( $expected, $input, $description ) {
27 $this->setMwGlobals( 'wgAssumeProxiesUseDefaultProtocolPorts', true );
30 $result = WebRequest
::detectServer();
31 $this->assertEquals( $expected, $result, $description );
34 public static function provideDetectServer() {
49 'Host header with secure'
57 'Default SERVER_PORT',
71 'HTTP_X_FORWARDED_PROTO' => 'https',
80 'SERVER_PORT' => '81',
81 'HTTP_X_FORWARDED_PROTO' => 'https',
98 'Host server name precedence'
103 'HTTP_HOST' => '[::1]',
104 'SERVER_NAME' => '::1',
105 'SERVER_PORT' => '81',
112 'SERVER_NAME' => '[2001'
114 'Kind of like lighttpd per commit message in MW r83847',
117 'http://[2a01:e35:2eb4:1::2]:777',
119 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
121 'Possible lighttpd environment per bug 14977 comment 13',
126 protected function mockWebRequest( $data = [] ) {
127 // Cannot use PHPUnit getMockBuilder() as it does not support
128 // overriding protected properties afterwards
129 $reflection = new ReflectionClass( 'WebRequest' );
130 $req = $reflection->newInstanceWithoutConstructor();
132 $prop = $reflection->getProperty( 'data' );
133 $prop->setAccessible( true );
134 $prop->setValue( $req, $data );
136 $prop = $reflection->getProperty( 'requestTime' );
137 $prop->setAccessible( true );
138 $prop->setValue( $req, microtime( true ) );
144 * @covers WebRequest::getElapsedTime
146 public function testGetElapsedTime() {
147 $req = $this->mockWebRequest();
148 $this->assertGreaterThanOrEqual( 0.0, $req->getElapsedTime() );
149 $this->assertEquals( 0.0, $req->getElapsedTime(), '', /*delta*/ 0.2 );
153 * @covers WebRequest::getVal
154 * @covers WebRequest::getGPCVal
155 * @covers WebRequest::normalizeUnicode
157 public function testGetValNormal() {
158 // Assert that WebRequest normalises GPC data using UtfNormal\Validator
159 $input = "a \x00 null";
160 $normal = "a \xef\xbf\xbd null";
161 $req = $this->mockWebRequest( [ 'x' => $input, 'y' => [ $input, $input ] ] );
162 $this->assertSame( $normal, $req->getVal( 'x' ) );
163 $this->assertNotSame( $input, $req->getVal( 'x' ) );
164 $this->assertSame( [ $normal, $normal ], $req->getArray( 'y' ) );
168 * @covers WebRequest::getVal
169 * @covers WebRequest::getGPCVal
171 public function testGetVal() {
172 $req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => [ 'a' ], 'crlf' => "A\r\nb" ] );
173 $this->assertSame( 'Value', $req->getVal( 'x' ), 'Simple value' );
174 $this->assertSame( null, $req->getVal( 'z' ), 'Not found' );
175 $this->assertSame( null, $req->getVal( 'y' ), 'Array is ignored' );
176 $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
180 * @covers WebRequest::getRawVal
182 public function testGetRawVal() {
183 $req = $this->mockWebRequest( [
188 $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
189 $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
190 $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
191 $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
195 * @covers WebRequest::getArray
197 public function testGetArray() {
198 $req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => [ 'a', 'b' ] ] );
199 $this->assertSame( [ 'Value' ], $req->getArray( 'x' ), 'Value becomes array' );
200 $this->assertSame( null, $req->getArray( 'z' ), 'Not found' );
201 $this->assertSame( [ 'a', 'b' ], $req->getArray( 'y' ) );
205 * @covers WebRequest::getIntArray
207 public function testGetIntArray() {
208 $req = $this->mockWebRequest( [ 'x' => [ 'Value' ], 'y' => [ '0', '4.2', '-2' ] ] );
209 $this->assertSame( [ 0 ], $req->getIntArray( 'x' ), 'Text becomes 0' );
210 $this->assertSame( null, $req->getIntArray( 'z' ), 'Not found' );
211 $this->assertSame( [ 0, 4, -2 ], $req->getIntArray( 'y' ) );
215 * @covers WebRequest::getInt
217 public function testGetInt() {
218 $req = $this->mockWebRequest( [
225 $this->assertSame( 0, $req->getInt( 'x' ), 'Text' );
226 $this->assertSame( 0, $req->getInt( 'y' ), 'Array' );
227 $this->assertSame( 0, $req->getInt( 'z' ), 'Not found' );
228 $this->assertSame( 0, $req->getInt( 'zero' ) );
229 $this->assertSame( 4, $req->getInt( 'answer' ) );
230 $this->assertSame( -2, $req->getInt( 'neg' ) );
234 * @covers WebRequest::getIntOrNull
236 public function testGetIntOrNull() {
237 $req = $this->mockWebRequest( [
244 $this->assertSame( null, $req->getIntOrNull( 'x' ), 'Text' );
245 $this->assertSame( null, $req->getIntOrNull( 'y' ), 'Array' );
246 $this->assertSame( null, $req->getIntOrNull( 'z' ), 'Not found' );
247 $this->assertSame( 0, $req->getIntOrNull( 'zero' ) );
248 $this->assertSame( 4, $req->getIntOrNull( 'answer' ) );
249 $this->assertSame( -2, $req->getIntOrNull( 'neg' ) );
253 * @covers WebRequest::getFloat
255 public function testGetFloat() {
256 $req = $this->mockWebRequest( [
263 $this->assertSame( 0.0, $req->getFloat( 'x' ), 'Text' );
264 $this->assertSame( 0.0, $req->getFloat( 'y' ), 'Array' );
265 $this->assertSame( 0.0, $req->getFloat( 'z' ), 'Not found' );
266 $this->assertSame( 0.0, $req->getFloat( 'zero' ) );
267 $this->assertSame( 4.2, $req->getFloat( 'answer' ) );
268 $this->assertSame( -2.0, $req->getFloat( 'neg' ) );
272 * @covers WebRequest::getBool
274 public function testGetBool() {
275 $req = $this->mockWebRequest( [
282 $this->assertSame( true, $req->getBool( 'x' ), 'Text' );
283 $this->assertSame( false, $req->getBool( 'y' ), 'Array' );
284 $this->assertSame( false, $req->getBool( 'z' ), 'Not found' );
285 $this->assertSame( false, $req->getBool( 'zero' ) );
286 $this->assertSame( true, $req->getBool( 'f' ) );
287 $this->assertSame( true, $req->getBool( 't' ) );
290 public static function provideFuzzyBool() {
293 [ '', false, '(empty string)' ],
306 * @dataProvider provideFuzzyBool
307 * @covers WebRequest::getFuzzyBool
309 public function testGetFuzzyBool( $value, $expected, $message = null ) {
310 $req = $this->mockWebRequest( [ 'x' => $value ] );
311 $this->assertSame( $expected, $req->getFuzzyBool( 'x' ), $message ?
: "Value: '$value'" );
315 * @covers WebRequest::getFuzzyBool
317 public function testGetFuzzyBoolDefault() {
318 $req = $this->mockWebRequest();
319 $this->assertSame( false, $req->getFuzzyBool( 'z' ), 'Not found' );
323 * @covers WebRequest::getCheck
325 public function testGetCheck() {
326 $req = $this->mockWebRequest( [ 'x' => 'Value', 'zero' => '0' ] );
327 $this->assertSame( false, $req->getCheck( 'z' ), 'Not found' );
328 $this->assertSame( true, $req->getCheck( 'x' ), 'Text' );
329 $this->assertSame( true, $req->getCheck( 'zero' ) );
333 * @covers WebRequest::getText
335 public function testGetText() {
336 // Avoid FauxRequest (overrides getText)
337 $req = $this->mockWebRequest( [ 'crlf' => "Va\r\nlue" ] );
338 $this->assertSame( "Va\nlue", $req->getText( 'crlf' ), 'CR stripped' );
342 * @covers WebRequest::getValues
344 public function testGetValues() {
345 $values = [ 'x' => 'Value', 'y' => '' ];
346 // Avoid FauxRequest (overrides getValues)
347 $req = $this->mockWebRequest( $values );
348 $this->assertSame( $values, $req->getValues() );
349 $this->assertSame( [ 'x' => 'Value' ], $req->getValues( 'x' ), 'Specific keys' );
353 * @covers WebRequest::getValueNames
355 public function testGetValueNames() {
356 $req = $this->mockWebRequest( [ 'x' => 'Value', 'y' => '' ] );
357 $this->assertSame( [ 'x', 'y' ], $req->getValueNames() );
358 $this->assertSame( [ 'x' ], $req->getValueNames( [ 'y' ] ), 'Exclude keys' );
362 * @dataProvider provideGetIP
363 * @covers WebRequest::getIP
365 public function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
367 $this->setMwGlobals( [
368 'wgUsePrivateIPs' => $private,
370 'IsTrustedProxy' => [
371 function ( &$ip, &$trusted ) use ( $xffList ) {
372 $trusted = $trusted ||
in_array( $ip, $xffList );
379 $this->setService( 'ProxyLookup', new ProxyLookup( [], $squid ) );
381 $request = new WebRequest();
382 $result = $request->getIP();
383 $this->assertEquals( $expected, $result, $description );
386 public static function provideGetIP() {
391 'REMOTE_ADDR' => '127.0.0.1'
401 'REMOTE_ADDR' => '::1'
411 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
412 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
414 [ 'ABCD:1:2:3:4:555:6666:7777' ],
422 'REMOTE_ADDR' => '12.0.0.1',
423 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
425 [ '12.0.0.1', '12.0.0.2' ],
428 'With X-Forwaded-For'
433 'REMOTE_ADDR' => '12.0.0.1',
434 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
439 'With X-Forwaded-For and disallowed server'
444 'REMOTE_ADDR' => '12.0.0.1',
445 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
450 'With multiple X-Forwaded-For and only one allowed server'
455 'REMOTE_ADDR' => '12.0.0.2',
456 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
458 [ '12.0.0.1', '12.0.0.2' ],
461 'With X-Forwaded-For and private IP (from cache proxy)'
466 'REMOTE_ADDR' => '12.0.0.2',
467 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
469 [ '12.0.0.1', '12.0.0.2', '10.0.0.3' ],
472 'With X-Forwaded-For and private IP (allowed)'
477 'REMOTE_ADDR' => '12.0.0.2',
478 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
480 [ '12.0.0.1', '12.0.0.2' ],
483 'With X-Forwaded-For and private IP (allowed)'
488 'REMOTE_ADDR' => '12.0.0.2',
489 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
491 [ '12.0.0.1', '12.0.0.2' ],
494 'With X-Forwaded-For and private IP (disallowed)'
499 'REMOTE_ADDR' => '12.0.0.1',
500 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
503 [ '12.0.0.1', '12.0.0.2' ],
505 'With X-Forwaded-For'
510 'REMOTE_ADDR' => '12.0.0.1',
511 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
516 'With multiple X-Forwaded-For and only one allowed server'
521 'REMOTE_ADDR' => '12.0.0.2',
522 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
527 'With X-Forwaded-For and private IP and hook (disallowed)'
532 'REMOTE_ADDR' => 'abcd:0001:002:03:4:555:6666:7777',
533 'HTTP_X_FORWARDED_FOR' => '12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
535 [ 'ABCD:1:2:3::/64' ],
543 'REMOTE_ADDR' => '12.0.0.1',
544 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
555 * @expectedException MWException
556 * @covers WebRequest::getIP
558 public function testGetIpLackOfRemoteAddrThrowAnException() {
559 // ensure that local install state doesn't interfere with test
560 $this->setMwGlobals( [
561 'wgSquidServersNoPurge' => [],
562 'wgSquidServers' => [],
563 'wgUsePrivateIPs' => false,
566 $this->setService( 'ProxyLookup', new ProxyLookup( [], [] ) );
568 $request = new WebRequest();
569 # Next call throw an exception about lacking an IP
573 public static function provideLanguageData() {
575 [ '', [], 'Empty Accept-Language header' ],
576 [ 'en', [ 'en' => 1 ], 'One language' ],
577 [ 'en, ar', [ 'en' => 1, 'ar' => 1 ], 'Two languages listed in appearance order.' ],
580 [ 'zh-cn' => 1, 'zh-tw' => 1 ],
581 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
585 [ 'es' => 1, 'en' => '0.5' ],
586 'Spanish as first language and English and second'
588 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Less prefered language first' ],
589 [ 'fr, en; q=0.5, es', [ 'fr' => 1, 'es' => 1, 'en' => '0.5' ], 'Three languages' ],
590 [ 'en; q=0.5, es', [ 'es' => 1, 'en' => '0.5' ], 'Two languages' ],
591 [ 'en, zh;q=0', [ 'en' => 1 ], "It's Chinese to me" ],
593 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
594 [ 'es' => '1', 'pt' => '0.7', 'it' => '0.6', 'de' => '0.1' ],
595 'Preference for Romance languages'
599 [ 'en-gb' => 1, 'en-us' => '1' ],
600 'Two equally prefered English variants'
602 [ '_', [], 'Invalid input' ],
607 * @dataProvider provideLanguageData
608 * @covers WebRequest::getAcceptLang
610 public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
611 $_SERVER = [ 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ];
612 $request = new WebRequest();
613 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );