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