e72408f6b0213ec5035d4dee0608a4152ef895bd
[lhc/web/wiklou.git] / tests / phpunit / includes / WebRequestTest.php
1 <?php
2
3 class WebRequestTest extends MediaWikiTestCase {
4 /**
5 * @dataProvider provideDetectServer
6 */
7 function testDetectServer( $expected, $input, $description ) {
8 $oldServer = $_SERVER;
9 $_SERVER = $input;
10 $result = WebRequest::detectServer();
11 $_SERVER = $oldServer;
12 $this->assertEquals( $expected, $result, $description );
13 }
14
15 function provideDetectServer() {
16 return array(
17 array(
18 'http://x',
19 array(
20 'HTTP_HOST' => 'x'
21 ),
22 'Host header'
23 ),
24 array(
25 'https://x',
26 array(
27 'HTTP_HOST' => 'x',
28 'HTTPS' => 'on',
29 ),
30 'Host header with secure'
31 ),
32 array(
33 'http://x',
34 array(
35 'HTTP_HOST' => 'x',
36 'SERVER_PORT' => 80,
37 ),
38 'Default SERVER_PORT',
39 ),
40 array(
41 'http://x',
42 array(
43 'HTTP_HOST' => 'x',
44 'HTTPS' => 'off',
45 ),
46 'Secure off'
47 ),
48 array(
49 'http://y',
50 array(
51 'SERVER_NAME' => 'y',
52 ),
53 'Server name'
54 ),
55 array(
56 'http://x',
57 array(
58 'HTTP_HOST' => 'x',
59 'SERVER_NAME' => 'y',
60 ),
61 'Host server name precedence'
62 ),
63 array(
64 'http://[::1]:81',
65 array(
66 'HTTP_HOST' => '[::1]',
67 'SERVER_NAME' => '::1',
68 'SERVER_PORT' => '81',
69 ),
70 'Apache bug 26005'
71 ),
72 array(
73 'http://localhost',
74 array(
75 'SERVER_NAME' => '[2001'
76 ),
77 'Kind of like lighttpd per commit message in MW r83847',
78 ),
79 array(
80 'http://[2a01:e35:2eb4:1::2]:777',
81 array(
82 'SERVER_NAME' => '[2a01:e35:2eb4:1::2]:777'
83 ),
84 'Possible lighttpd environment per bug 14977 comment 13',
85 ),
86 );
87 }
88
89 /**
90 * @dataProvider provideGetIP
91 */
92 function testGetIP( $expected, $input, $squid, $private, $description ) {
93 global $wgSquidServersNoPurge, $wgUsePrivateIPs;
94 $oldServer = $_SERVER;
95 $_SERVER = $input;
96 $wgSquidServersNoPurge = $squid;
97 $wgUsePrivateIPs = $private;
98 $request = new WebRequest();
99 $result = $request->getIP();
100 $_SERVER = $oldServer;
101 $this->assertEquals( $expected, $result, $description );
102 }
103
104 function provideGetIP() {
105 return array(
106 array(
107 '127.0.0.1',
108 array(
109 'REMOTE_ADDR' => '127.0.0.1'
110 ),
111 array(),
112 false,
113 'Simple IPv4'
114 ),
115 array(
116 '::1',
117 array(
118 'REMOTE_ADDR' => '::1'
119 ),
120 array(),
121 false,
122 'Simple IPv6'
123 ),
124 array(
125 '12.0.0.3',
126 array(
127 'REMOTE_ADDR' => '12.0.0.1',
128 'HTTP_X_FORWARDED_FOR' => '12.0.0.3, 12.0.0.2'
129 ),
130 array( '12.0.0.1', '12.0.0.2' ),
131 false,
132 'With X-Forwaded-For'
133 ),
134 array(
135 '12.0.0.1',
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(),
141 false,
142 'With X-Forwaded-For and disallowed server'
143 ),
144 array(
145 '12.0.0.2',
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( '12.0.0.1' ),
151 false,
152 'With multiple X-Forwaded-For and only one allowed server'
153 ),
154 array(
155 '12.0.0.2',
156 array(
157 'REMOTE_ADDR' => '12.0.0.2',
158 'HTTP_X_FORWARDED_FOR' => '10.0.0.3, 12.0.0.2'
159 ),
160 array( '12.0.0.1', '12.0.0.2' ),
161 false,
162 'With X-Forwaded-For and private IP'
163 ),
164 array(
165 '10.0.0.3',
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 true,
172 'With X-Forwaded-For and private IP (allowed)'
173 ),
174 );
175 }
176
177 /**
178 * @expectedException MWException
179 */
180 function testGetIpLackOfRemoteAddrThrowAnException() {
181 $request = new WebRequest();
182 # Next call throw an exception about lacking an IP
183 $request->getIP();
184 }
185 }