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