3 class WebRequestTest
extends MediaWikiTestCase
{
7 self
::$oldServer = $_SERVER;
11 $_SERVER = self
::$oldServer;
15 * @dataProvider provideDetectServer
17 function testDetectServer( $expected, $input, $description ) {
19 $result = WebRequest
::detectServer();
20 $this->assertEquals( $expected, $result, $description );
23 function provideDetectServer() {
38 'Host header with secure'
46 'Default SERVER_PORT',
69 'Host server name precedence'
74 'HTTP_HOST' => '[::1]',
75 'SERVER_NAME' => '::1',
76 'SERVER_PORT' => '81',
83 'SERVER_NAME' => '[2001'
85 'Kind of like lighttpd per commit message in MW r83847',
88 'http://[2a01:e35:2eb4:1::2]:777',
90 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
92 'Possible lighttpd environment per bug 14977 comment 13',
98 * @dataProvider provideGetIP
100 function testGetIP( $expected, $input, $squid, $private, $description ) {
101 global $wgSquidServersNoPurge, $wgUsePrivateIPs;
103 $wgSquidServersNoPurge = $squid;
104 $wgUsePrivateIPs = $private;
105 $request = new WebRequest();
106 $result = $request->getIP();
107 $this->assertEquals( $expected, $result, $description );
110 function provideGetIP() {
115 'REMOTE_ADDR' => '127.0.0.1'
124 'REMOTE_ADDR' => '::1'
133 'REMOTE_ADDR' => '12.0.0.1',
134 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
136 array( '12.0.0.1', '12.0.0.2' ),
138 'With X-Forwaded-For'
143 'REMOTE_ADDR' => '12.0.0.1',
144 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
148 'With X-Forwaded-For and disallowed server'
153 'REMOTE_ADDR' => '12.0.0.1',
154 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
158 'With multiple X-Forwaded-For and only one allowed server'
163 'REMOTE_ADDR' => '12.0.0.2',
164 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
166 array( '12.0.0.1', '12.0.0.2' ),
168 'With X-Forwaded-For and private IP'
173 'REMOTE_ADDR' => '12.0.0.2',
174 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
176 array( '12.0.0.1', '12.0.0.2' ),
178 'With X-Forwaded-For and private IP (allowed)'
184 * @expectedException MWException
186 function testGetIpLackOfRemoteAddrThrowAnException() {
187 $request = new WebRequest();
188 # Next call throw an exception about lacking an IP
192 function languageProvider() {
194 array( '', array(), 'Empty Accept-Language header' ),
195 array( 'en', array( 'en' => 1 ), 'One language' ),
196 array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
197 array( 'zh-cn,zh-tw', array( 'zh-cn' => 1, 'zh-tw' => 1 ), 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119' ),
198 array( 'es, en; q=0.5', array( 'es' => 1, 'en' => '0.5' ), 'Spanish as first language and English and second' ),
199 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
200 array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
201 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
202 array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
203 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' ),
204 array( 'en-gb, en-us; q=1', array( 'en-gb' => 1, 'en-us' => '1' ), 'Two equally prefered English variants' ),
209 * @dataProvider languageProvider
211 function testAcceptLang($acceptLanguageHeader, $expectedLanguages, $description) {
212 $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
213 $request = new WebRequest();
214 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description);