Merge "[FileBackend] Changed copy script to use batches for concurrency."
[lhc/web/wiklou.git] / tests / phpunit / includes / WebRequestTest.php
1 <?php
2
3 class WebRequestTest extends MediaWikiTestCase {
4 static $oldServer;
5
6 function setUp() {
7 self::$oldServer = $_SERVER;
8 }
9
10 function tearDown() {
11 $_SERVER = self::$oldServer;
12 }
13
14 /**
15 * @dataProvider provideDetectServer
16 */
17 function testDetectServer( $expected, $input, $description ) {
18 $_SERVER = $input;
19 $result = WebRequest::detectServer();
20 $this->assertEquals( $expected, $result, $description );
21 }
22
23 function provideDetectServer() {
24 return array(
25 array(
26 'http://x',
27 array(
28 'HTTP_HOST' => 'x'
29 ),
30 'Host header'
31 ),
32 array(
33 'https://x',
34 array(
35 'HTTP_HOST' => 'x',
36 'HTTPS' => 'on',
37 ),
38 'Host header with secure'
39 ),
40 array(
41 'http://x',
42 array(
43 'HTTP_HOST' => 'x',
44 'SERVER_PORT' => 80,
45 ),
46 'Default SERVER_PORT',
47 ),
48 array(
49 'http://x',
50 array(
51 'HTTP_HOST' => 'x',
52 'HTTPS' => 'off',
53 ),
54 'Secure off'
55 ),
56 array(
57 'http://y',
58 array(
59 'SERVER_NAME' => 'y',
60 ),
61 'Server name'
62 ),
63 array(
64 'http://x',
65 array(
66 'HTTP_HOST' => 'x',
67 'SERVER_NAME' => 'y',
68 ),
69 'Host server name precedence'
70 ),
71 array(
72 'http://[::1]:81',
73 array(
74 'HTTP_HOST' => '[::1]',
75 'SERVER_NAME' => '::1',
76 'SERVER_PORT' => '81',
77 ),
78 'Apache bug 26005'
79 ),
80 array(
81 'http://localhost',
82 array(
83 'SERVER_NAME' => '[2001'
84 ),
85 'Kind of like lighttpd per commit message in MW r83847',
86 ),
87 array(
88 'http://[2a01:e35:2eb4:1::2]:777',
89 array(
90 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
91 ),
92 'Possible lighttpd environment per bug 14977 comment 13',
93 ),
94 );
95 }
96
97 /**
98 * @dataProvider provideGetIP
99 */
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 );
108 }
109
110 function provideGetIP() {
111 return array(
112 array(
113 '127.0.0.1',
114 array(
115 'REMOTE_ADDR' => '127.0.0.1'
116 ),
117 array(),
118 false,
119 'Simple IPv4'
120 ),
121 array(
122 '::1',
123 array(
124 'REMOTE_ADDR' => '::1'
125 ),
126 array(),
127 false,
128 'Simple IPv6'
129 ),
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'
135 ),
136 array( '12.0.0.1', '12.0.0.2' ),
137 false,
138 'With X-Forwaded-For'
139 ),
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'
145 ),
146 array(),
147 false,
148 'With X-Forwaded-For and disallowed server'
149 ),
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'
155 ),
156 array( '12.0.0.1' ),
157 false,
158 'With multiple X-Forwaded-For and only one allowed server'
159 ),
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'
165 ),
166 array( '12.0.0.1', '12.0.0.2' ),
167 false,
168 'With X-Forwaded-For and private IP'
169 ),
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'
175 ),
176 array( '12.0.0.1', '12.0.0.2' ),
177 true,
178 'With X-Forwaded-For and private IP (allowed)'
179 ),
180 );
181 }
182
183 /**
184 * @expectedException MWException
185 */
186 function testGetIpLackOfRemoteAddrThrowAnException() {
187 $request = new WebRequest();
188 # Next call throw an exception about lacking an IP
189 $request->getIP();
190 }
191
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' ),
205 );
206 }
207
208 /**
209 * @dataProvider languageProvider
210 */
211 function testAcceptLang($acceptLanguageHeader, $expectedLanguages, $description) {
212 $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
213 $request = new WebRequest();
214 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description);
215 }
216 }