Merge "Make image pagination not require a page load."
[lhc/web/wiklou.git] / tests / phpunit / includes / WebRequestTest.php
1 <?php
2
3 class WebRequestTest extends MediaWikiTestCase {
4 protected $oldServer;
5
6 protected function setUp() {
7 parent::setUp();
8
9 $this->oldServer = $_SERVER;
10 }
11
12 protected function tearDown() {
13 $_SERVER = $this->oldServer;
14
15 parent::tearDown();
16 }
17
18 /**
19 * @dataProvider provideDetectServer
20 */
21 function testDetectServer( $expected, $input, $description ) {
22 $_SERVER = $input;
23 $result = WebRequest::detectServer();
24 $this->assertEquals( $expected, $result, $description );
25 }
26
27 public static function provideDetectServer() {
28 return array(
29 array(
30 'http://x',
31 array(
32 'HTTP_HOST' => 'x'
33 ),
34 'Host header'
35 ),
36 array(
37 'https://x',
38 array(
39 'HTTP_HOST' => 'x',
40 'HTTPS' => 'on',
41 ),
42 'Host header with secure'
43 ),
44 array(
45 'http://x',
46 array(
47 'HTTP_HOST' => 'x',
48 'SERVER_PORT' => 80,
49 ),
50 'Default SERVER_PORT',
51 ),
52 array(
53 'http://x',
54 array(
55 'HTTP_HOST' => 'x',
56 'HTTPS' => 'off',
57 ),
58 'Secure off'
59 ),
60 array(
61 'http://y',
62 array(
63 'SERVER_NAME' => 'y',
64 ),
65 'Server name'
66 ),
67 array(
68 'http://x',
69 array(
70 'HTTP_HOST' => 'x',
71 'SERVER_NAME' => 'y',
72 ),
73 'Host server name precedence'
74 ),
75 array(
76 'http://[::1]:81',
77 array(
78 'HTTP_HOST' => '[::1]',
79 'SERVER_NAME' => '::1',
80 'SERVER_PORT' => '81',
81 ),
82 'Apache bug 26005'
83 ),
84 array(
85 'http://localhost',
86 array(
87 'SERVER_NAME' => '[2001'
88 ),
89 'Kind of like lighttpd per commit message in MW r83847',
90 ),
91 array(
92 'http://[2a01:e35:2eb4:1::2]:777',
93 array(
94 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
95 ),
96 'Possible lighttpd environment per bug 14977 comment 13',
97 ),
98 );
99 }
100
101 /**
102 * @dataProvider provideGetIP
103 */
104 function testGetIP( $expected, $input, $squid, $xffList, $private, $description ) {
105 $_SERVER = $input;
106 $this->setMwGlobals( array(
107 'wgSquidServersNoPurge' => $squid,
108 'wgUsePrivateIPs' => $private,
109 'wgHooks' => array(
110 'IsTrustedProxy' => array(
111 function( &$ip, &$trusted ) use ( $xffList ) {
112 $trusted = $trusted || in_array( $ip, $xffList );
113 return true;
114 }
115 )
116 )
117 ) );
118
119 $request = new WebRequest();
120 $result = $request->getIP();
121 $this->assertEquals( $expected, $result, $description );
122 }
123
124 public static function provideGetIP() {
125 return array(
126 array(
127 '127.0.0.1',
128 array(
129 'REMOTE_ADDR' => '127.0.0.1'
130 ),
131 array(),
132 array(),
133 false,
134 'Simple IPv4'
135 ),
136 array(
137 '::1',
138 array(
139 'REMOTE_ADDR' => '::1'
140 ),
141 array(),
142 array(),
143 false,
144 'Simple IPv6'
145 ),
146 array(
147 '12.0.0.3',
148 array(
149 'REMOTE_ADDR' => '12.0.0.1',
150 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
151 ),
152 array( '12.0.0.1', '12.0.0.2' ),
153 array(),
154 false,
155 'With X-Forwaded-For'
156 ),
157 array(
158 '12.0.0.1',
159 array(
160 'REMOTE_ADDR' => '12.0.0.1',
161 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
162 ),
163 array(),
164 array(),
165 false,
166 'With X-Forwaded-For and disallowed server'
167 ),
168 array(
169 '12.0.0.2',
170 array(
171 'REMOTE_ADDR' => '12.0.0.1',
172 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
173 ),
174 array( '12.0.0.1' ),
175 array(),
176 false,
177 'With multiple X-Forwaded-For and only one allowed server'
178 ),
179 array(
180 '10.0.0.3',
181 array(
182 'REMOTE_ADDR' => '12.0.0.2',
183 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
184 ),
185 array( '12.0.0.1', '12.0.0.2' ),
186 array(),
187 false,
188 'With X-Forwaded-For and private IP (from cache proxy)'
189 ),
190 array(
191 '10.0.0.4',
192 array(
193 'REMOTE_ADDR' => '12.0.0.2',
194 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
195 ),
196 array( '12.0.0.1', '12.0.0.2', '10.0.0.3' ),
197 array(),
198 true,
199 'With X-Forwaded-For and private IP (allowed)'
200 ),
201 array(
202 '10.0.0.4',
203 array(
204 'REMOTE_ADDR' => '12.0.0.2',
205 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
206 ),
207 array( '12.0.0.1', '12.0.0.2' ),
208 array( '10.0.0.3' ),
209 true,
210 'With X-Forwaded-For and private IP (allowed)'
211 ),
212 array(
213 '10.0.0.3',
214 array(
215 'REMOTE_ADDR' => '12.0.0.2',
216 'HTTP_X_FORWARDED_FOR' => '10.0.0.4, 10.0.0.3, 12.0.0.2'
217 ),
218 array( '12.0.0.1', '12.0.0.2' ),
219 array( '10.0.0.3' ),
220 false,
221 'With X-Forwaded-For and private IP (disallowed)'
222 ),
223 array(
224 '12.0.0.3',
225 array(
226 'REMOTE_ADDR' => '12.0.0.1',
227 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
228 ),
229 array(),
230 array( '12.0.0.1', '12.0.0.2' ),
231 false,
232 'With X-Forwaded-For'
233 ),
234 array(
235 '12.0.0.2',
236 array(
237 'REMOTE_ADDR' => '12.0.0.1',
238 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
239 ),
240 array(),
241 array( '12.0.0.1' ),
242 false,
243 'With multiple X-Forwaded-For and only one allowed server'
244 ),
245 array(
246 '12.0.0.2',
247 array(
248 'REMOTE_ADDR' => '12.0.0.2',
249 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
250 ),
251 array(),
252 array( '12.0.0.2' ),
253 false,
254 'With X-Forwaded-For and private IP and hook (disallowed)'
255 ),
256 );
257 }
258
259 /**
260 * @expectedException MWException
261 */
262 function testGetIpLackOfRemoteAddrThrowAnException() {
263 $request = new WebRequest();
264 # Next call throw an exception about lacking an IP
265 $request->getIP();
266 }
267
268 public static function provideLanguageData() {
269 return array(
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' ),
281 );
282 }
283
284 /**
285 * @dataProvider provideLanguageData
286 */
287 function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
288 $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
289 $request = new WebRequest();
290 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );
291 }
292 }