Merge "Fixed dependencies for jquery.collapsibleTabs"
[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, $private, $description ) {
105 global $wgSquidServersNoPurge, $wgUsePrivateIPs;
106 $_SERVER = $input;
107 $wgSquidServersNoPurge = $squid;
108 $wgUsePrivateIPs = $private;
109 $request = new WebRequest();
110 $result = $request->getIP();
111 $this->assertEquals( $expected, $result, $description );
112 }
113
114 public static function provideGetIP() {
115 return array(
116 array(
117 '127.0.0.1',
118 array(
119 'REMOTE_ADDR' => '127.0.0.1'
120 ),
121 array(),
122 false,
123 'Simple IPv4'
124 ),
125 array(
126 '::1',
127 array(
128 'REMOTE_ADDR' => '::1'
129 ),
130 array(),
131 false,
132 'Simple IPv6'
133 ),
134 array(
135 '12.0.0.3',
136 array(
137 'REMOTE_ADDR' => '12.0.0.1',
138 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
139 ),
140 array( '12.0.0.1', '12.0.0.2' ),
141 false,
142 'With X-Forwaded-For'
143 ),
144 array(
145 '12.0.0.1',
146 array(
147 'REMOTE_ADDR' => '12.0.0.1',
148 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
149 ),
150 array(),
151 false,
152 'With X-Forwaded-For and disallowed server'
153 ),
154 array(
155 '12.0.0.2',
156 array(
157 'REMOTE_ADDR' => '12.0.0.1',
158 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
159 ),
160 array( '12.0.0.1' ),
161 false,
162 'With multiple X-Forwaded-For and only one allowed server'
163 ),
164 array(
165 '12.0.0.2',
166 array(
167 'REMOTE_ADDR' => '12.0.0.2',
168 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
169 ),
170 array( '12.0.0.1', '12.0.0.2' ),
171 false,
172 'With X-Forwaded-For and private IP'
173 ),
174 array(
175 '10.0.0.3',
176 array(
177 'REMOTE_ADDR' => '12.0.0.2',
178 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
179 ),
180 array( '12.0.0.1', '12.0.0.2' ),
181 true,
182 'With X-Forwaded-For and private IP (allowed)'
183 ),
184 );
185 }
186
187 /**
188 * @expectedException MWException
189 */
190 function testGetIpLackOfRemoteAddrThrowAnException() {
191 $request = new WebRequest();
192 # Next call throw an exception about lacking an IP
193 $request->getIP();
194 }
195
196 public static function provideLanguageData() {
197 return array(
198 array( '', array(), 'Empty Accept-Language header' ),
199 array( 'en', array( 'en' => 1 ), 'One language' ),
200 array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
201 array( 'zh-cn,zh-tw', array( 'zh-cn' => 1, 'zh-tw' => 1 ), 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119' ),
202 array( 'es, en; q=0.5', array( 'es' => 1, 'en' => '0.5' ), 'Spanish as first language and English and second' ),
203 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
204 array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
205 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
206 array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
207 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' ),
208 array( 'en-gb, en-us; q=1', array( 'en-gb' => 1, 'en-us' => '1' ), 'Two equally prefered English variants' ),
209 );
210 }
211
212 /**
213 * @dataProvider provideLanguageData
214 */
215 function testAcceptLang($acceptLanguageHeader, $expectedLanguages, $description) {
216 $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
217 $request = new WebRequest();
218 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description);
219 }
220 }