Merge "Optimize WikiMap::getWikiFromUrl() for the common local wiki case"
[lhc/web/wiklou.git] / tests / phpunit / includes / http / HttpTest.php
1 <?php
2
3 /**
4 * @group Http
5 * @group small
6 */
7 class HttpTest extends MediaWikiTestCase {
8 /**
9 * @dataProvider cookieDomains
10 * @covers Cookie::validateCookieDomain
11 */
12 public function testValidateCookieDomain( $expected, $domain, $origin = null ) {
13 if ( $origin ) {
14 $ok = Cookie::validateCookieDomain( $domain, $origin );
15 $msg = "$domain against origin $origin";
16 } else {
17 $ok = Cookie::validateCookieDomain( $domain );
18 $msg = "$domain";
19 }
20 $this->assertEquals( $expected, $ok, $msg );
21 }
22
23 public static function cookieDomains() {
24 return [
25 [ false, "org" ],
26 [ false, ".org" ],
27 [ true, "wikipedia.org" ],
28 [ true, ".wikipedia.org" ],
29 [ false, "co.uk" ],
30 [ false, ".co.uk" ],
31 [ false, "gov.uk" ],
32 [ false, ".gov.uk" ],
33 [ true, "supermarket.uk" ],
34 [ false, "uk" ],
35 [ false, ".uk" ],
36 [ false, "127.0.0." ],
37 [ false, "127." ],
38 [ false, "127.0.0.1." ],
39 [ true, "127.0.0.1" ],
40 [ false, "333.0.0.1" ],
41 [ true, "example.com" ],
42 [ false, "example.com." ],
43 [ true, ".example.com" ],
44
45 [ true, ".example.com", "www.example.com" ],
46 [ false, "example.com", "www.example.com" ],
47 [ true, "127.0.0.1", "127.0.0.1" ],
48 [ false, "127.0.0.1", "localhost" ],
49 ];
50 }
51
52 /**
53 * Test Http::isValidURI()
54 * T29854 : Http::isValidURI is too lax
55 * @dataProvider provideURI
56 * @covers Http::isValidURI
57 */
58 public function testIsValidUri( $expect, $URI, $message = '' ) {
59 $this->assertEquals(
60 $expect,
61 (bool)Http::isValidURI( $URI ),
62 $message
63 );
64 }
65
66 /**
67 * @covers Http::getProxy
68 */
69 public function testGetProxy() {
70 $this->hideDeprecated( 'Http::getProxy' );
71
72 $this->setMwGlobals( 'wgHTTPProxy', false );
73 $this->assertEquals(
74 '',
75 Http::getProxy(),
76 'default setting'
77 );
78
79 $this->setMwGlobals( 'wgHTTPProxy', 'proxy.domain.tld' );
80 $this->assertEquals(
81 'proxy.domain.tld',
82 Http::getProxy()
83 );
84 }
85
86 /**
87 * Feeds URI to test a long regular expression in Http::isValidURI
88 */
89 public static function provideURI() {
90 /** Format: 'boolean expectation', 'URI to test', 'Optional message' */
91 return [
92 [ false, '¿non sens before!! http://a', 'Allow anything before URI' ],
93
94 # (http|https) - only two schemes allowed
95 [ true, 'http://www.example.org/' ],
96 [ true, 'https://www.example.org/' ],
97 [ true, 'http://www.example.org', 'URI without directory' ],
98 [ true, 'http://a', 'Short name' ],
99 [ true, 'http://étoile', 'Allow UTF-8 in hostname' ], # 'étoile' is french for 'star'
100 [ false, '\\host\directory', 'CIFS share' ],
101 [ false, 'gopher://host/dir', 'Reject gopher scheme' ],
102 [ false, 'telnet://host', 'Reject telnet scheme' ],
103
104 # :\/\/ - double slashes
105 [ false, 'http//example.org', 'Reject missing colon in protocol' ],
106 [ false, 'http:/example.org', 'Reject missing slash in protocol' ],
107 [ false, 'http:example.org', 'Must have two slashes' ],
108 # Following fail since hostname can be made of anything
109 [ false, 'http:///example.org', 'Must have exactly two slashes, not three' ],
110
111 # (\w+:{0,1}\w*@)? - optional user:pass
112 [ true, 'http://user@host', 'Username provided' ],
113 [ true, 'http://user:@host', 'Username provided, no password' ],
114 [ true, 'http://user:pass@host', 'Username and password provided' ],
115
116 # (\S+) - host part is made of anything not whitespaces
117 // commented these out in order to remove @group Broken
118 // @todo are these valid tests? if so, fix Http::isValidURI so it can handle them
119 // [ false, 'http://!"èèè¿¿¿~~\'', 'hostname is made of any non whitespace' ],
120 // [ false, 'http://exam:ple.org/', 'hostname can not use colons!' ],
121
122 # (:[0-9]+)? - port number
123 [ true, 'http://example.org:80/' ],
124 [ true, 'https://example.org:80/' ],
125 [ true, 'http://example.org:443/' ],
126 [ true, 'https://example.org:443/' ],
127
128 # Part after the hostname is / or / with something else
129 [ true, 'http://example/#' ],
130 [ true, 'http://example/!' ],
131 [ true, 'http://example/:' ],
132 [ true, 'http://example/.' ],
133 [ true, 'http://example/?' ],
134 [ true, 'http://example/+' ],
135 [ true, 'http://example/=' ],
136 [ true, 'http://example/&' ],
137 [ true, 'http://example/%' ],
138 [ true, 'http://example/@' ],
139 [ true, 'http://example/-' ],
140 [ true, 'http://example//' ],
141 [ true, 'http://example/&' ],
142
143 # Fragment
144 [ true, 'http://exam#ple.org', ], # This one is valid, really!
145 [ true, 'http://example.org:80#anchor' ],
146 [ true, 'http://example.org/?id#anchor' ],
147 [ true, 'http://example.org/?#anchor' ],
148
149 [ false, 'http://a ¿non !!sens after', 'Allow anything after URI' ],
150 ];
151 }
152
153 public static function provideRelativeRedirects() {
154 return [
155 [
156 'location' => [ 'http://newsite/file.ext', '/newfile.ext' ],
157 'final' => 'http://newsite/newfile.ext',
158 'Relative file path Location: interpreted as full URL'
159 ],
160 [
161 'location' => [ 'https://oldsite/file.ext' ],
162 'final' => 'https://oldsite/file.ext',
163 'Location to the HTTPS version of the site'
164 ],
165 [
166 'location' => [
167 '/anotherfile.ext',
168 'http://anotherfile/hoster.ext',
169 'https://anotherfile/hoster.ext'
170 ],
171 'final' => 'https://anotherfile/hoster.ext',
172 'Relative file path Location: should keep the latest host and scheme!'
173 ],
174 [
175 'location' => [ '/anotherfile.ext' ],
176 'final' => 'http://oldsite/anotherfile.ext',
177 'Relative Location without domain '
178 ],
179 [
180 'location' => null,
181 'final' => 'http://oldsite/file.ext',
182 'No Location (no redirect) '
183 ],
184 ];
185 }
186
187 /**
188 * Warning:
189 *
190 * These tests are for code that makes use of an artifact of how CURL
191 * handles header reporting on redirect pages, and will need to be
192 * rewritten when T31232 is taken care of (high-level handling of HTTP redirects).
193 *
194 * @dataProvider provideRelativeRedirects
195 * @covers MWHttpRequest::getFinalUrl
196 */
197 public function testRelativeRedirections( $location, $final, $message = null ) {
198 $h = MWHttpRequestTester::factory( 'http://oldsite/file.ext', [], __METHOD__ );
199 // Forge a Location header
200 $h->setRespHeaders( 'location', $location );
201 // Verify it correctly fixes the Location
202 $this->assertEquals( $final, $h->getFinalUrl(), $message );
203 }
204
205 /**
206 * Constant values are from PHP 5.3.28 using cURL 7.24.0
207 * @see https://www.php.net/manual/en/curl.constants.php
208 *
209 * All constant values are present so that developers don’t need to remember
210 * to add them if added at a later date. The commented out constants were
211 * not found anywhere in the MediaWiki core code.
212 *
213 * Commented out constants that were not available in:
214 * HipHop VM 3.3.0 (rel)
215 * Compiler: heads/master-0-g08810d920dfff59e0774cf2d651f92f13a637175
216 * Repo schema: 3214fc2c684a4520485f715ee45f33f2182324b1
217 * Extension API: 20140829
218 *
219 * Commented out constants that were removed in PHP 5.6.0
220 */
221 public function provideCurlConstants() {
222 return [
223 [ 'CURLAUTH_ANY' ],
224 [ 'CURLAUTH_ANYSAFE' ],
225 [ 'CURLAUTH_BASIC' ],
226 [ 'CURLAUTH_DIGEST' ],
227 [ 'CURLAUTH_GSSNEGOTIATE' ],
228 [ 'CURLAUTH_NTLM' ],
229 // [ 'CURLCLOSEPOLICY_CALLBACK' ], // removed in PHP 5.6.0
230 // [ 'CURLCLOSEPOLICY_LEAST_RECENTLY_USED' ], // removed in PHP 5.6.0
231 // [ 'CURLCLOSEPOLICY_LEAST_TRAFFIC' ], // removed in PHP 5.6.0
232 // [ 'CURLCLOSEPOLICY_OLDEST' ], // removed in PHP 5.6.0
233 // [ 'CURLCLOSEPOLICY_SLOWEST' ], // removed in PHP 5.6.0
234 [ 'CURLE_ABORTED_BY_CALLBACK' ],
235 [ 'CURLE_BAD_CALLING_ORDER' ],
236 [ 'CURLE_BAD_CONTENT_ENCODING' ],
237 [ 'CURLE_BAD_FUNCTION_ARGUMENT' ],
238 [ 'CURLE_BAD_PASSWORD_ENTERED' ],
239 [ 'CURLE_COULDNT_CONNECT' ],
240 [ 'CURLE_COULDNT_RESOLVE_HOST' ],
241 [ 'CURLE_COULDNT_RESOLVE_PROXY' ],
242 [ 'CURLE_FAILED_INIT' ],
243 [ 'CURLE_FILESIZE_EXCEEDED' ],
244 [ 'CURLE_FILE_COULDNT_READ_FILE' ],
245 [ 'CURLE_FTP_ACCESS_DENIED' ],
246 [ 'CURLE_FTP_BAD_DOWNLOAD_RESUME' ],
247 [ 'CURLE_FTP_CANT_GET_HOST' ],
248 [ 'CURLE_FTP_CANT_RECONNECT' ],
249 [ 'CURLE_FTP_COULDNT_GET_SIZE' ],
250 [ 'CURLE_FTP_COULDNT_RETR_FILE' ],
251 [ 'CURLE_FTP_COULDNT_SET_ASCII' ],
252 [ 'CURLE_FTP_COULDNT_SET_BINARY' ],
253 [ 'CURLE_FTP_COULDNT_STOR_FILE' ],
254 [ 'CURLE_FTP_COULDNT_USE_REST' ],
255 [ 'CURLE_FTP_PORT_FAILED' ],
256 [ 'CURLE_FTP_QUOTE_ERROR' ],
257 [ 'CURLE_FTP_SSL_FAILED' ],
258 [ 'CURLE_FTP_USER_PASSWORD_INCORRECT' ],
259 [ 'CURLE_FTP_WEIRD_227_FORMAT' ],
260 [ 'CURLE_FTP_WEIRD_PASS_REPLY' ],
261 [ 'CURLE_FTP_WEIRD_PASV_REPLY' ],
262 [ 'CURLE_FTP_WEIRD_SERVER_REPLY' ],
263 [ 'CURLE_FTP_WEIRD_USER_REPLY' ],
264 [ 'CURLE_FTP_WRITE_ERROR' ],
265 [ 'CURLE_FUNCTION_NOT_FOUND' ],
266 [ 'CURLE_GOT_NOTHING' ],
267 [ 'CURLE_HTTP_NOT_FOUND' ],
268 [ 'CURLE_HTTP_PORT_FAILED' ],
269 [ 'CURLE_HTTP_POST_ERROR' ],
270 [ 'CURLE_HTTP_RANGE_ERROR' ],
271 [ 'CURLE_LDAP_CANNOT_BIND' ],
272 [ 'CURLE_LDAP_INVALID_URL' ],
273 [ 'CURLE_LDAP_SEARCH_FAILED' ],
274 [ 'CURLE_LIBRARY_NOT_FOUND' ],
275 [ 'CURLE_MALFORMAT_USER' ],
276 [ 'CURLE_OBSOLETE' ],
277 [ 'CURLE_OK' ],
278 [ 'CURLE_OPERATION_TIMEOUTED' ],
279 [ 'CURLE_OUT_OF_MEMORY' ],
280 [ 'CURLE_PARTIAL_FILE' ],
281 [ 'CURLE_READ_ERROR' ],
282 [ 'CURLE_RECV_ERROR' ],
283 [ 'CURLE_SEND_ERROR' ],
284 [ 'CURLE_SHARE_IN_USE' ],
285 // [ 'CURLE_SSH' ], // not present in HHVM 3.3.0-dev
286 [ 'CURLE_SSL_CACERT' ],
287 [ 'CURLE_SSL_CERTPROBLEM' ],
288 [ 'CURLE_SSL_CIPHER' ],
289 [ 'CURLE_SSL_CONNECT_ERROR' ],
290 [ 'CURLE_SSL_ENGINE_NOTFOUND' ],
291 [ 'CURLE_SSL_ENGINE_SETFAILED' ],
292 [ 'CURLE_SSL_PEER_CERTIFICATE' ],
293 [ 'CURLE_TELNET_OPTION_SYNTAX' ],
294 [ 'CURLE_TOO_MANY_REDIRECTS' ],
295 [ 'CURLE_UNKNOWN_TELNET_OPTION' ],
296 [ 'CURLE_UNSUPPORTED_PROTOCOL' ],
297 [ 'CURLE_URL_MALFORMAT' ],
298 [ 'CURLE_URL_MALFORMAT_USER' ],
299 [ 'CURLE_WRITE_ERROR' ],
300 [ 'CURLFTPAUTH_DEFAULT' ],
301 [ 'CURLFTPAUTH_SSL' ],
302 [ 'CURLFTPAUTH_TLS' ],
303 // [ 'CURLFTPMETHOD_MULTICWD' ], // not present in HHVM 3.3.0-dev
304 // [ 'CURLFTPMETHOD_NOCWD' ], // not present in HHVM 3.3.0-dev
305 // [ 'CURLFTPMETHOD_SINGLECWD' ], // not present in HHVM 3.3.0-dev
306 [ 'CURLFTPSSL_ALL' ],
307 [ 'CURLFTPSSL_CONTROL' ],
308 [ 'CURLFTPSSL_NONE' ],
309 [ 'CURLFTPSSL_TRY' ],
310 // [ 'CURLINFO_CERTINFO' ], // not present in HHVM 3.3.0-dev
311 [ 'CURLINFO_CONNECT_TIME' ],
312 [ 'CURLINFO_CONTENT_LENGTH_DOWNLOAD' ],
313 [ 'CURLINFO_CONTENT_LENGTH_UPLOAD' ],
314 [ 'CURLINFO_CONTENT_TYPE' ],
315 [ 'CURLINFO_EFFECTIVE_URL' ],
316 [ 'CURLINFO_FILETIME' ],
317 [ 'CURLINFO_HEADER_OUT' ],
318 [ 'CURLINFO_HEADER_SIZE' ],
319 [ 'CURLINFO_HTTP_CODE' ],
320 [ 'CURLINFO_NAMELOOKUP_TIME' ],
321 [ 'CURLINFO_PRETRANSFER_TIME' ],
322 [ 'CURLINFO_PRIVATE' ],
323 [ 'CURLINFO_REDIRECT_COUNT' ],
324 [ 'CURLINFO_REDIRECT_TIME' ],
325 // [ 'CURLINFO_REDIRECT_URL' ], // not present in HHVM 3.3.0-dev
326 [ 'CURLINFO_REQUEST_SIZE' ],
327 [ 'CURLINFO_SIZE_DOWNLOAD' ],
328 [ 'CURLINFO_SIZE_UPLOAD' ],
329 [ 'CURLINFO_SPEED_DOWNLOAD' ],
330 [ 'CURLINFO_SPEED_UPLOAD' ],
331 [ 'CURLINFO_SSL_VERIFYRESULT' ],
332 [ 'CURLINFO_STARTTRANSFER_TIME' ],
333 [ 'CURLINFO_TOTAL_TIME' ],
334 [ 'CURLMSG_DONE' ],
335 [ 'CURLM_BAD_EASY_HANDLE' ],
336 [ 'CURLM_BAD_HANDLE' ],
337 [ 'CURLM_CALL_MULTI_PERFORM' ],
338 [ 'CURLM_INTERNAL_ERROR' ],
339 [ 'CURLM_OK' ],
340 [ 'CURLM_OUT_OF_MEMORY' ],
341 [ 'CURLOPT_AUTOREFERER' ],
342 [ 'CURLOPT_BINARYTRANSFER' ],
343 [ 'CURLOPT_BUFFERSIZE' ],
344 [ 'CURLOPT_CAINFO' ],
345 [ 'CURLOPT_CAPATH' ],
346 // [ 'CURLOPT_CERTINFO' ], // not present in HHVM 3.3.0-dev
347 // [ 'CURLOPT_CLOSEPOLICY' ], // removed in PHP 5.6.0
348 [ 'CURLOPT_CONNECTTIMEOUT' ],
349 [ 'CURLOPT_CONNECTTIMEOUT_MS' ],
350 [ 'CURLOPT_COOKIE' ],
351 [ 'CURLOPT_COOKIEFILE' ],
352 [ 'CURLOPT_COOKIEJAR' ],
353 [ 'CURLOPT_COOKIESESSION' ],
354 [ 'CURLOPT_CRLF' ],
355 [ 'CURLOPT_CUSTOMREQUEST' ],
356 [ 'CURLOPT_DNS_CACHE_TIMEOUT' ],
357 [ 'CURLOPT_DNS_USE_GLOBAL_CACHE' ],
358 [ 'CURLOPT_EGDSOCKET' ],
359 [ 'CURLOPT_ENCODING' ],
360 [ 'CURLOPT_FAILONERROR' ],
361 [ 'CURLOPT_FILE' ],
362 [ 'CURLOPT_FILETIME' ],
363 [ 'CURLOPT_FOLLOWLOCATION' ],
364 [ 'CURLOPT_FORBID_REUSE' ],
365 [ 'CURLOPT_FRESH_CONNECT' ],
366 [ 'CURLOPT_FTPAPPEND' ],
367 [ 'CURLOPT_FTPLISTONLY' ],
368 [ 'CURLOPT_FTPPORT' ],
369 [ 'CURLOPT_FTPSSLAUTH' ],
370 [ 'CURLOPT_FTP_CREATE_MISSING_DIRS' ],
371 // [ 'CURLOPT_FTP_FILEMETHOD' ], // not present in HHVM 3.3.0-dev
372 // [ 'CURLOPT_FTP_SKIP_PASV_IP' ], // not present in HHVM 3.3.0-dev
373 [ 'CURLOPT_FTP_SSL' ],
374 [ 'CURLOPT_FTP_USE_EPRT' ],
375 [ 'CURLOPT_FTP_USE_EPSV' ],
376 [ 'CURLOPT_HEADER' ],
377 [ 'CURLOPT_HEADERFUNCTION' ],
378 [ 'CURLOPT_HTTP200ALIASES' ],
379 [ 'CURLOPT_HTTPAUTH' ],
380 [ 'CURLOPT_HTTPGET' ],
381 [ 'CURLOPT_HTTPHEADER' ],
382 [ 'CURLOPT_HTTPPROXYTUNNEL' ],
383 [ 'CURLOPT_HTTP_VERSION' ],
384 [ 'CURLOPT_INFILE' ],
385 [ 'CURLOPT_INFILESIZE' ],
386 [ 'CURLOPT_INTERFACE' ],
387 [ 'CURLOPT_IPRESOLVE' ],
388 // [ 'CURLOPT_KEYPASSWD' ], // not present in HHVM 3.3.0-dev
389 [ 'CURLOPT_KRB4LEVEL' ],
390 [ 'CURLOPT_LOW_SPEED_LIMIT' ],
391 [ 'CURLOPT_LOW_SPEED_TIME' ],
392 [ 'CURLOPT_MAXCONNECTS' ],
393 [ 'CURLOPT_MAXREDIRS' ],
394 // [ 'CURLOPT_MAX_RECV_SPEED_LARGE' ], // not present in HHVM 3.3.0-dev
395 // [ 'CURLOPT_MAX_SEND_SPEED_LARGE' ], // not present in HHVM 3.3.0-dev
396 [ 'CURLOPT_NETRC' ],
397 [ 'CURLOPT_NOBODY' ],
398 [ 'CURLOPT_NOPROGRESS' ],
399 [ 'CURLOPT_NOSIGNAL' ],
400 [ 'CURLOPT_PORT' ],
401 [ 'CURLOPT_POST' ],
402 [ 'CURLOPT_POSTFIELDS' ],
403 [ 'CURLOPT_POSTQUOTE' ],
404 [ 'CURLOPT_POSTREDIR' ],
405 [ 'CURLOPT_PRIVATE' ],
406 [ 'CURLOPT_PROGRESSFUNCTION' ],
407 // [ 'CURLOPT_PROTOCOLS' ], // not present in HHVM 3.3.0-dev
408 [ 'CURLOPT_PROXY' ],
409 [ 'CURLOPT_PROXYAUTH' ],
410 [ 'CURLOPT_PROXYPORT' ],
411 [ 'CURLOPT_PROXYTYPE' ],
412 [ 'CURLOPT_PROXYUSERPWD' ],
413 [ 'CURLOPT_PUT' ],
414 [ 'CURLOPT_QUOTE' ],
415 [ 'CURLOPT_RANDOM_FILE' ],
416 [ 'CURLOPT_RANGE' ],
417 [ 'CURLOPT_READDATA' ],
418 [ 'CURLOPT_READFUNCTION' ],
419 // [ 'CURLOPT_REDIR_PROTOCOLS' ], // not present in HHVM 3.3.0-dev
420 [ 'CURLOPT_REFERER' ],
421 [ 'CURLOPT_RESUME_FROM' ],
422 [ 'CURLOPT_RETURNTRANSFER' ],
423 // [ 'CURLOPT_SSH_AUTH_TYPES' ], // not present in HHVM 3.3.0-dev
424 // [ 'CURLOPT_SSH_HOST_PUBLIC_KEY_MD5' ], // not present in HHVM 3.3.0-dev
425 // [ 'CURLOPT_SSH_PRIVATE_KEYFILE' ], // not present in HHVM 3.3.0-dev
426 // [ 'CURLOPT_SSH_PUBLIC_KEYFILE' ], // not present in HHVM 3.3.0-dev
427 [ 'CURLOPT_SSLCERT' ],
428 [ 'CURLOPT_SSLCERTPASSWD' ],
429 [ 'CURLOPT_SSLCERTTYPE' ],
430 [ 'CURLOPT_SSLENGINE' ],
431 [ 'CURLOPT_SSLENGINE_DEFAULT' ],
432 [ 'CURLOPT_SSLKEY' ],
433 [ 'CURLOPT_SSLKEYPASSWD' ],
434 [ 'CURLOPT_SSLKEYTYPE' ],
435 [ 'CURLOPT_SSLVERSION' ],
436 [ 'CURLOPT_SSL_CIPHER_LIST' ],
437 [ 'CURLOPT_SSL_VERIFYHOST' ],
438 [ 'CURLOPT_SSL_VERIFYPEER' ],
439 [ 'CURLOPT_STDERR' ],
440 [ 'CURLOPT_TCP_NODELAY' ],
441 [ 'CURLOPT_TIMECONDITION' ],
442 [ 'CURLOPT_TIMEOUT' ],
443 [ 'CURLOPT_TIMEOUT_MS' ],
444 [ 'CURLOPT_TIMEVALUE' ],
445 [ 'CURLOPT_TRANSFERTEXT' ],
446 [ 'CURLOPT_UNRESTRICTED_AUTH' ],
447 [ 'CURLOPT_UPLOAD' ],
448 [ 'CURLOPT_URL' ],
449 [ 'CURLOPT_USERAGENT' ],
450 [ 'CURLOPT_USERPWD' ],
451 [ 'CURLOPT_VERBOSE' ],
452 [ 'CURLOPT_WRITEFUNCTION' ],
453 [ 'CURLOPT_WRITEHEADER' ],
454 // [ 'CURLPROTO_ALL' ], // not present in HHVM 3.3.0-dev
455 // [ 'CURLPROTO_DICT' ], // not present in HHVM 3.3.0-dev
456 // [ 'CURLPROTO_FILE' ], // not present in HHVM 3.3.0-dev
457 // [ 'CURLPROTO_FTP' ], // not present in HHVM 3.3.0-dev
458 // [ 'CURLPROTO_FTPS' ], // not present in HHVM 3.3.0-dev
459 // [ 'CURLPROTO_HTTP' ], // not present in HHVM 3.3.0-dev
460 // [ 'CURLPROTO_HTTPS' ], // not present in HHVM 3.3.0-dev
461 // [ 'CURLPROTO_LDAP' ], // not present in HHVM 3.3.0-dev
462 // [ 'CURLPROTO_LDAPS' ], // not present in HHVM 3.3.0-dev
463 // [ 'CURLPROTO_SCP' ], // not present in HHVM 3.3.0-dev
464 // [ 'CURLPROTO_SFTP' ], // not present in HHVM 3.3.0-dev
465 // [ 'CURLPROTO_TELNET' ], // not present in HHVM 3.3.0-dev
466 // [ 'CURLPROTO_TFTP' ], // not present in HHVM 3.3.0-dev
467 [ 'CURLPROXY_HTTP' ],
468 // [ 'CURLPROXY_SOCKS4' ], // not present in HHVM 3.3.0-dev
469 [ 'CURLPROXY_SOCKS5' ],
470 // [ 'CURLSSH_AUTH_DEFAULT' ], // not present in HHVM 3.3.0-dev
471 // [ 'CURLSSH_AUTH_HOST' ], // not present in HHVM 3.3.0-dev
472 // [ 'CURLSSH_AUTH_KEYBOARD' ], // not present in HHVM 3.3.0-dev
473 // [ 'CURLSSH_AUTH_NONE' ], // not present in HHVM 3.3.0-dev
474 // [ 'CURLSSH_AUTH_PASSWORD' ], // not present in HHVM 3.3.0-dev
475 // [ 'CURLSSH_AUTH_PUBLICKEY' ], // not present in HHVM 3.3.0-dev
476 [ 'CURLVERSION_NOW' ],
477 [ 'CURL_HTTP_VERSION_1_0' ],
478 [ 'CURL_HTTP_VERSION_1_1' ],
479 [ 'CURL_HTTP_VERSION_NONE' ],
480 [ 'CURL_IPRESOLVE_V4' ],
481 [ 'CURL_IPRESOLVE_V6' ],
482 [ 'CURL_IPRESOLVE_WHATEVER' ],
483 [ 'CURL_NETRC_IGNORED' ],
484 [ 'CURL_NETRC_OPTIONAL' ],
485 [ 'CURL_NETRC_REQUIRED' ],
486 [ 'CURL_TIMECOND_IFMODSINCE' ],
487 [ 'CURL_TIMECOND_IFUNMODSINCE' ],
488 [ 'CURL_TIMECOND_LASTMOD' ],
489 [ 'CURL_VERSION_IPV6' ],
490 [ 'CURL_VERSION_KERBEROS4' ],
491 [ 'CURL_VERSION_LIBZ' ],
492 [ 'CURL_VERSION_SSL' ],
493 ];
494 }
495
496 /**
497 * Added this test based on an issue experienced with HHVM 3.3.0-dev
498 * where it did not define a cURL constant. T72570
499 *
500 * @dataProvider provideCurlConstants
501 * @coversNothing
502 */
503 public function testCurlConstants( $value ) {
504 $this->checkPHPExtension( 'curl' );
505
506 $this->assertTrue( defined( $value ), $value . ' not defined' );
507 }
508 }
509
510 /**
511 * Class to let us overwrite MWHttpRequest respHeaders variable
512 */
513 class MWHttpRequestTester extends MWHttpRequest {
514 // function derived from the MWHttpRequest factory function but
515 // returns appropriate tester class here
516 public static function factory( $url, array $options = null, $caller = __METHOD__ ) {
517 if ( !Http::$httpEngine ) {
518 Http::$httpEngine = 'guzzle';
519 } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) {
520 throw new DomainException( __METHOD__ . ': curl (https://www.php.net/curl) is not ' .
521 'installed, but Http::$httpEngine is set to "curl"' );
522 }
523
524 switch ( Http::$httpEngine ) {
525 case 'guzzle':
526 return new GuzzleHttpRequestTester( $url, $options, $caller );
527 case 'curl':
528 return new CurlHttpRequestTester( $url, $options, $caller );
529 case 'php':
530 if ( !wfIniGetBool( 'allow_url_fopen' ) ) {
531 throw new DomainException( __METHOD__ .
532 ': allow_url_fopen needs to be enabled for pure PHP HTTP requests to work. '
533 . 'If possible, curl should be used instead. See https://www.php.net/curl.' );
534 }
535
536 return new PhpHttpRequestTester( $url, $options, $caller );
537 default:
538 }
539 }
540 }
541
542 class GuzzleHttpRequestTester extends GuzzleHttpRequest {
543 function setRespHeaders( $name, $value ) {
544 $this->respHeaders[$name] = $value;
545 }
546 }
547
548 class CurlHttpRequestTester extends CurlHttpRequest {
549 function setRespHeaders( $name, $value ) {
550 $this->respHeaders[$name] = $value;
551 }
552 }
553
554 class PhpHttpRequestTester extends PhpHttpRequest {
555 function setRespHeaders( $name, $value ) {
556 $this->respHeaders[$name] = $value;
557 }
558 }