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