Compress images
[mediawiki.git] / tests / phpunit / includes / WebRequestTest.php
blob1fc0b4b3408dc8d6e879448222284fd255bf2f9a
1 <?php
3 class WebRequestTest extends MediaWikiTestCase {
4 static $oldServer;
6 function setUp() {
7 self::$oldServer = $_SERVER;
10 function tearDown() {
11 $_SERVER = self::$oldServer;
14 /**
15 * @dataProvider provideDetectServer
17 function testDetectServer( $expected, $input, $description ) {
18 $_SERVER = $input;
19 $result = WebRequest::detectServer();
20 $this->assertEquals( $expected, $result, $description );
23 function provideDetectServer() {
24 return array(
25 array(
26 'http://x',
27 array(
28 'HTTP_HOST' => 'x'
30 'Host header'
32 array(
33 'https://x',
34 array(
35 'HTTP_HOST' => 'x',
36 'HTTPS' => 'on',
38 'Host header with secure'
40 array(
41 'http://x',
42 array(
43 'HTTP_HOST' => 'x',
44 'SERVER_PORT' => 80,
46 'Default SERVER_PORT',
48 array(
49 'http://x',
50 array(
51 'HTTP_HOST' => 'x',
52 'HTTPS' => 'off',
54 'Secure off'
56 array(
57 'http://y',
58 array(
59 'SERVER_NAME' => 'y',
61 'Server name'
63 array(
64 'http://x',
65 array(
66 'HTTP_HOST' => 'x',
67 'SERVER_NAME' => 'y',
69 'Host server name precedence'
71 array(
72 'http://[::1]:81',
73 array(
74 'HTTP_HOST' => '[::1]',
75 'SERVER_NAME' => '::1',
76 'SERVER_PORT' => '81',
78 'Apache bug 26005'
80 array(
81 'http://localhost',
82 array(
83 'SERVER_NAME' => '[2001'
85 'Kind of like lighttpd per commit message in MW r83847',
87 array(
88 'http://[2a01:e35:2eb4:1::2]:777',
89 array(
90 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
92 'Possible lighttpd environment per bug 14977 comment 13',
97 /**
98 * @dataProvider provideGetIP
100 function testGetIP( $expected, $input, $squid, $private, $description ) {
101 global $wgSquidServersNoPurge, $wgUsePrivateIPs;
102 $_SERVER = $input;
103 $wgSquidServersNoPurge = $squid;
104 $wgUsePrivateIPs = $private;
105 $request = new WebRequest();
106 $result = $request->getIP();
107 $this->assertEquals( $expected, $result, $description );
110 function provideGetIP() {
111 return array(
112 array(
113 '127.0.0.1',
114 array(
115 'REMOTE_ADDR' => '127.0.0.1'
117 array(),
118 false,
119 'Simple IPv4'
121 array(
122 '::1',
123 array(
124 'REMOTE_ADDR' => '::1'
126 array(),
127 false,
128 'Simple IPv6'
130 array(
131 '12.0.0.3',
132 array(
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' ),
137 false,
138 'With X-Forwaded-For'
140 array(
141 '12.0.0.1',
142 array(
143 'REMOTE_ADDR' => '12.0.0.1',
144 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
146 array(),
147 false,
148 'With X-Forwaded-For and disallowed server'
150 array(
151 '12.0.0.2',
152 array(
153 'REMOTE_ADDR' => '12.0.0.1',
154 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
156 array( '12.0.0.1' ),
157 false,
158 'With multiple X-Forwaded-For and only one allowed server'
160 array(
161 '12.0.0.2',
162 array(
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' ),
167 false,
168 'With X-Forwaded-For and private IP'
170 array(
171 '10.0.0.3',
172 array(
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' ),
177 true,
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
189 $request->getIP();
192 function languageProvider() {
193 return array(
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);