Changed quoting function for oracleDB.
[mediawiki.git] / tests / phpunit / includes / WebRequestTest.php
blobd382f6f57c54a4e0449cb05dfa772ee5ff9ff321
1 <?php
3 class WebRequestTest extends MediaWikiTestCase {
4 protected $oldServer;
6 protected function setUp() {
7 parent::setUp();
9 $this->oldServer = $_SERVER;
12 protected function tearDown() {
13 $_SERVER = $this->oldServer;
15 parent::tearDown();
18 /**
19 * @dataProvider provideDetectServer
21 function testDetectServer( $expected, $input, $description ) {
22 $_SERVER = $input;
23 $result = WebRequest::detectServer();
24 $this->assertEquals( $expected, $result, $description );
27 public static function provideDetectServer() {
28 return array(
29 array(
30 'http://x',
31 array(
32 'HTTP_HOST' => 'x'
34 'Host header'
36 array(
37 'https://x',
38 array(
39 'HTTP_HOST' => 'x',
40 'HTTPS' => 'on',
42 'Host header with secure'
44 array(
45 'http://x',
46 array(
47 'HTTP_HOST' => 'x',
48 'SERVER_PORT' => 80,
50 'Default SERVER_PORT',
52 array(
53 'http://x',
54 array(
55 'HTTP_HOST' => 'x',
56 'HTTPS' => 'off',
58 'Secure off'
60 array(
61 'http://y',
62 array(
63 'SERVER_NAME' => 'y',
65 'Server name'
67 array(
68 'http://x',
69 array(
70 'HTTP_HOST' => 'x',
71 'SERVER_NAME' => 'y',
73 'Host server name precedence'
75 array(
76 'http://[::1]:81',
77 array(
78 'HTTP_HOST' => '[::1]',
79 'SERVER_NAME' => '::1',
80 'SERVER_PORT' => '81',
82 'Apache bug 26005'
84 array(
85 'http://localhost',
86 array(
87 'SERVER_NAME' => '[2001'
89 'Kind of like lighttpd per commit message in MW r83847',
91 array(
92 'http://[2a01:e35:2eb4:1::2]:777',
93 array(
94 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
96 'Possible lighttpd environment per bug 14977 comment 13',
102 * @dataProvider provideGetIP
104 function testGetIP( $expected, $input, $squid, $private, $description ) {
105 $_SERVER = $input;
106 $this->setMwGlobals( array(
107 'wgSquidServersNoPurge' => $squid,
108 'wgUsePrivateIPs' => $private,
109 ) );
111 $request = new WebRequest();
112 $result = $request->getIP();
113 $this->assertEquals( $expected, $result, $description );
116 public static function provideGetIP() {
117 return array(
118 array(
119 '127.0.0.1',
120 array(
121 'REMOTE_ADDR' => '127.0.0.1'
123 array(),
124 false,
125 'Simple IPv4'
127 array(
128 '::1',
129 array(
130 'REMOTE_ADDR' => '::1'
132 array(),
133 false,
134 'Simple IPv6'
136 array(
137 '12.0.0.3',
138 array(
139 'REMOTE_ADDR' => '12.0.0.1',
140 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
142 array( '12.0.0.1', '12.0.0.2' ),
143 false,
144 'With X-Forwaded-For'
146 array(
147 '12.0.0.1',
148 array(
149 'REMOTE_ADDR' => '12.0.0.1',
150 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
152 array(),
153 false,
154 'With X-Forwaded-For and disallowed server'
156 array(
157 '12.0.0.2',
158 array(
159 'REMOTE_ADDR' => '12.0.0.1',
160 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
162 array( '12.0.0.1' ),
163 false,
164 'With multiple X-Forwaded-For and only one allowed server'
166 array(
167 '12.0.0.2',
168 array(
169 'REMOTE_ADDR' => '12.0.0.2',
170 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
172 array( '12.0.0.1', '12.0.0.2' ),
173 false,
174 'With X-Forwaded-For and private IP'
176 array(
177 '10.0.0.3',
178 array(
179 'REMOTE_ADDR' => '12.0.0.2',
180 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
182 array( '12.0.0.1', '12.0.0.2' ),
183 true,
184 'With X-Forwaded-For and private IP (allowed)'
190 * @expectedException MWException
192 function testGetIpLackOfRemoteAddrThrowAnException() {
193 $request = new WebRequest();
194 # Next call throw an exception about lacking an IP
195 $request->getIP();
198 public static function provideLanguageData() {
199 return array(
200 array( '', array(), 'Empty Accept-Language header' ),
201 array( 'en', array( 'en' => 1 ), 'One language' ),
202 array( 'en, ar', array( 'en' => 1, 'ar' => 1 ), 'Two languages listed in appearance order.' ),
203 array( 'zh-cn,zh-tw', array( 'zh-cn' => 1, 'zh-tw' => 1 ), 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119' ),
204 array( 'es, en; q=0.5', array( 'es' => 1, 'en' => '0.5' ), 'Spanish as first language and English and second' ),
205 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Less prefered language first' ),
206 array( 'fr, en; q=0.5, es', array( 'fr' => 1, 'es' => 1, 'en' => '0.5' ), 'Three languages' ),
207 array( 'en; q=0.5, es', array( 'es' => 1, 'en' => '0.5' ), 'Two languages' ),
208 array( 'en, zh;q=0', array( 'en' => 1 ), "It's Chinese to me" ),
209 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' ),
210 array( 'en-gb, en-us; q=1', array( 'en-gb' => 1, 'en-us' => '1' ), 'Two equally prefered English variants' ),
215 * @dataProvider provideLanguageData
217 function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
218 $_SERVER = array( 'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader );
219 $request = new WebRequest();
220 $this->assertSame( $request->getAcceptLang(), $expectedLanguages, $description );