MediaWiki  REL1_19
WebRequestTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class WebRequestTest extends MediaWikiTestCase {
00007         function testDetectServer( $expected, $input, $description ) {
00008                 $oldServer = $_SERVER;
00009                 $_SERVER = $input;
00010                 $result = WebRequest::detectServer();
00011                 $_SERVER = $oldServer;
00012                 $this->assertEquals( $expected, $result, $description );
00013         }
00014 
00015         function provideDetectServer() {
00016                 return array(
00017                         array(
00018                                 'http://x',
00019                                 array(
00020                                         'HTTP_HOST' => 'x'
00021                                 ),
00022                                 'Host header'
00023                         ),
00024                         array(
00025                                 'https://x',
00026                                 array(
00027                                         'HTTP_HOST' => 'x',
00028                                         'HTTPS' => 'on',
00029                                 ),
00030                                 'Host header with secure'
00031                         ),
00032                         array(
00033                                 'http://x',
00034                                 array(
00035                                         'HTTP_HOST' => 'x',
00036                                         'SERVER_PORT' => 80,
00037                                 ),
00038                                 'Default SERVER_PORT',
00039                         ),
00040                         array(
00041                                 'http://x',
00042                                 array(
00043                                         'HTTP_HOST' => 'x',
00044                                         'HTTPS' => 'off',
00045                                 ),
00046                                 'Secure off'
00047                         ),
00048                         array(
00049                                 'http://y',
00050                                 array(
00051                                         'SERVER_NAME' => 'y',
00052                                 ),
00053                                 'Server name'
00054                         ),
00055                         array(
00056                                 'http://x',
00057                                 array(
00058                                         'HTTP_HOST' => 'x',
00059                                         'SERVER_NAME' => 'y',
00060                                 ),
00061                                 'Host server name precedence'
00062                         ),
00063                         array(
00064                                 'http://[::1]:81',
00065                                 array(
00066                                         'HTTP_HOST' => '[::1]',
00067                                         'SERVER_NAME' => '::1',
00068                                         'SERVER_PORT' => '81',
00069                                 ),
00070                                 'Apache bug 26005'
00071                         ),
00072                         array(
00073                                 'http://localhost',
00074                                 array(
00075                                         'SERVER_NAME' => '[2001'
00076                                 ),
00077                                 'Kind of like lighttpd per commit message in MW r83847',
00078                         ),
00079                         array(
00080                                 'http://[2a01:e35:2eb4:1::2]:777',
00081                                 array(
00082                                         'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
00083                                 ),
00084                                 'Possible lighttpd environment per bug 14977 comment 13',
00085                         ),
00086                 );
00087         }
00088 
00092         function testGetIP( $expected, $input, $squid, $private, $description ) {
00093                 global $wgSquidServersNoPurge, $wgUsePrivateIPs;
00094                 $oldServer = $_SERVER;
00095                 $_SERVER = $input;
00096                 $wgSquidServersNoPurge = $squid;
00097                 $wgUsePrivateIPs = $private;
00098                 $request = new WebRequest();
00099                 $result = $request->getIP();
00100                 $_SERVER = $oldServer;
00101                 $this->assertEquals( $expected, $result, $description );
00102         }
00103 
00104         function provideGetIP() {
00105                 return array(
00106                         array(
00107                                 '127.0.0.1',
00108                                 array(
00109                                         'REMOTE_ADDR' => '127.0.0.1'
00110                                 ),
00111                                 array(),
00112                                 false,
00113                                 'Simple IPv4'
00114                         ),
00115                         array(
00116                                 '::1',
00117                                 array(
00118                                         'REMOTE_ADDR' => '::1'
00119                                 ),
00120                                 array(),
00121                                 false,
00122                                 'Simple IPv6'
00123                         ),
00124                         array(
00125                                 '12.0.0.3',
00126                                 array(
00127                                         'REMOTE_ADDR' => '12.0.0.1',
00128                                         'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
00129                                 ),
00130                                 array( '12.0.0.1', '12.0.0.2' ),
00131                                 false,
00132                                 'With X-Forwaded-For'
00133                         ),
00134                         array(
00135                                 '12.0.0.1',
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(),
00141                                 false,
00142                                 'With X-Forwaded-For and disallowed server'
00143                         ),
00144                         array(
00145                                 '12.0.0.2',
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( '12.0.0.1' ),
00151                                 false,
00152                                 'With multiple X-Forwaded-For and only one allowed server'
00153                         ),
00154                         array(
00155                                 '12.0.0.2',
00156                                 array(
00157                                         'REMOTE_ADDR' => '12.0.0.2',
00158                                         'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
00159                                 ),
00160                                 array( '12.0.0.1', '12.0.0.2' ),
00161                                 false,
00162                                 'With X-Forwaded-For and private IP'
00163                         ),
00164                         array(
00165                                 '10.0.0.3',
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                                 true,
00172                                 'With X-Forwaded-For and private IP (allowed)'
00173                         ),
00174                 );
00175         }
00176 
00180         function testGetIpLackOfRemoteAddrThrowAnException() {
00181                 $request = new WebRequest();
00182                 # Next call throw an exception about lacking an IP
00183                 $request->getIP();
00184         }
00185 }