3 class MockCookie
extends Cookie
{
4 public function canServeDomain($arg) { return parent
::canServeDomain($arg); }
5 public function canServePath($arg) { return parent
::canServePath($arg); }
6 public function isUnExpired() { return parent
::isUnExpired(); }
9 class HttpTest
extends PhpUnit_Framework_TestCase
{
14 static $has_proxy = false;
15 static $proxy = "http://hulk:8080/";
16 var $test_geturl = array(
17 "http://www.example.com/",
18 "http://pecl.php.net/feeds/pkg_apc.rss",
19 "http://toolserver.org/~jan/poll/dev/main.php?page=wiki_output&id=3",
20 "http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw",
21 "http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&format=php",
23 var $test_requesturl = array( "http://en.wikipedia.org/wiki/Special:Export/User:MarkAHershberger" );
25 var $test_posturl = array( "http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example" => "review=test" );
28 putenv("http_proxy"); /* Remove any proxy env var, so curl doesn't get confused */
29 if ( is_array( self
::$content ) ) {
32 self
::$has_curl = function_exists( 'curl_init' );
33 self
::$has_fopen = wfIniGetBool( 'allow_url_fopen' );
35 if ( !file_exists("/usr/bin/curl") ) {
36 $this->markTestIncomplete("This test requires the curl binary at /usr/bin/curl. If you have curl, please file a bug on this test, or, better yet, provide a patch.");
39 $content = tempnam( wfTempDir(), "" );
40 $headers = tempnam( wfTempDir(), "" );
41 if ( !$content && !$headers ) {
42 die( "Couldn't create temp file!" );
45 // This probably isn't the best test for a proxy, but it works on my system!
46 system("curl -0 -o $content -s ".self
::$proxy);
47 $out = file_get_contents( $content );
49 self
::$has_proxy = true;
52 /* Maybe use wget instead of curl here ... just to use a different codebase? */
53 foreach ( $this->test_geturl
as $u ) {
54 system( "curl -0 -s -D $headers '$u' -o $content" );
55 self
::$content["GET $u"] = file_get_contents( $content );
56 self
::$headers["GET $u"] = file_get_contents( $headers );
58 foreach ( $this->test_requesturl
as $u ) {
59 system( "curl -0 -s -X POST -H 'Content-Length: 0' -D $headers '$u' -o $content" );
60 self
::$content["POST $u"] = file_get_contents( $content );
61 self
::$headers["POST $u"] = file_get_contents( $headers );
63 foreach ( $this->test_posturl
as $u => $postData ) {
64 system( "curl -0 -s -X POST -d '$postData' -D $headers '$u' -o $content" );
65 self
::$content["POST $u => $postData"] = file_get_contents( $content );
66 self
::$headers["POST $u => $postData"] = file_get_contents( $headers );
73 function testInstantiation() {
74 Http
::$httpEngine = false;
76 $r = HttpRequest
::factory("http://www.example.com/");
77 if ( self
::$has_curl ) {
78 $this->assertThat($r, $this->isInstanceOf( 'CurlHttpRequest' ));
80 $this->assertThat($r, $this->isInstanceOf( 'PhpHttpRequest' ));
84 if( !self
::$has_fopen ) {
85 $this->setExpectedException( 'MWException' );
87 Http
::$httpEngine = 'php';
88 $r = HttpRequest
::factory("http://www.example.com/");
89 $this->assertThat($r, $this->isInstanceOf( 'PhpHttpRequest' ));
92 if( !self
::$has_curl ) {
93 $this->setExpectedException( 'MWException' );
95 Http
::$httpEngine = 'curl';
96 $r = HttpRequest
::factory("http://www.example.com/");
97 if( self
::$has_curl ) {
98 $this->assertThat($r, $this->isInstanceOf( 'CurlHttpRequest' ));
102 function runHTTPFailureChecks() {
103 // Each of the following requests should result in a failure.
106 $start_time = time();
107 $r = HTTP
::get( "http://www.example.com:1/", $timeout);
109 $this->assertLessThan($timeout+
2, $end_time - $start_time,
110 "Request took less than {$timeout}s via ".Http
::$httpEngine);
111 $this->assertEquals($r, false, "false -- what we get on error from Http::get()");
113 $r = HTTP
::get( "http://www.example.com/this-file-does-not-exist", $timeout);
114 $this->assertFalse($r, "False on 404s");
117 $r = HttpRequest
::factory( "http://www.example.com/this-file-does-not-exist" );
119 if ( is_a($r, 'PhpHttpRequest') && version_compare( '5.2.10', phpversion(), '>' ) ) {
120 $this->assertRegexp("/HTTP request failed/", $er->getWikiText());
122 $this->assertRegexp("/404 Not Found/", $er->getWikiText());
126 function testFailureDefault() {
127 Http
::$httpEngine = false;
128 self
::runHTTPFailureChecks();
131 function testFailurePhp() {
132 if ( !self
::$has_fopen ) {
133 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
136 Http
::$httpEngine = "php";
137 self
::runHTTPFailureChecks();
140 function testFailureCurl() {
141 if ( !self
::$has_curl ) {
142 $this->markTestIncomplete( "This test requires curl." );
145 Http
::$httpEngine = "curl";
146 self
::runHTTPFailureChecks();
149 /* ./phase3/includes/Import.php:1108: $data = Http::request( $method, $url ); */
150 /* ./includes/Import.php:1124: $link = Title::newFromText( "$interwiki:Special:Export/$page" ); */
151 /* ./includes/Import.php:1134: return ImportStreamSource::newFromURL( $url, "POST" ); */
152 function runHTTPRequests($proxy=null) {
156 $opt['proxy'] = $proxy;
157 } elseif( $proxy === false ) {
158 $opt['noProxy'] = true;
161 /* no postData here because the only request I could find in code so far didn't have any */
162 foreach ( $this->test_requesturl
as $u ) {
163 $r = Http
::request( "POST", $u, $opt );
164 $this->assertEquals( self
::$content["POST $u"], "$r", "POST $u with ".Http
::$httpEngine );
168 function testRequestDefault() {
169 Http
::$httpEngine = false;
170 self
::runHTTPRequests();
173 function testRequestPhp() {
174 if ( !self
::$has_fopen ) {
175 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
178 Http
::$httpEngine = "php";
179 self
::runHTTPRequests();
182 function testRequestCurl() {
183 if ( !self
::$has_curl ) {
184 $this->markTestIncomplete( "This test requires curl." );
187 Http
::$httpEngine = "curl";
188 self
::runHTTPRequests();
191 /* ./extensions/SpamBlacklist/SpamBlacklist_body.php:164: $httpText = Http::get( $fileName ); */
192 /* ./extensions/ApiSVGProxy/ApiSVGProxy.body.php:44: $contents = Http::get( $file->getFullUrl() ); */
193 /* ./extensions/BookInformation/drivers/IsbnDb.php:24: if( ( $xml = Http::get( $uri ) ) !== false ) { */
194 /* ./extensions/BookInformation/drivers/Amazon.php:23: if( ( $xml = Http::get( $uri ) ) !== false ) { */
195 /* ./extensions/TitleBlacklist/TitleBlacklist.list.php:217: $result = Http::get( $url ); */
196 /* ./extensions/TSPoll/TSPoll.php:68: $get_server = Http::get( 'http://toolserver.org/~jan/poll/dev/main.php?page=wiki_output&id='.$id ); */
197 /* ./extensions/TSPoll/TSPoll.php:70: $get_server = Http::get( 'http://toolserver.org/~jan/poll/main.php?page=wiki_output&id='.$id ); */
198 /* ./extensions/DoubleWiki/DoubleWiki.php:56: $translation = Http::get( $url.$sep.'action=render' ); */
199 /* ./extensions/ExternalPages/ExternalPages_body.php:177: $serializedText = Http::get( $this->mPageURL ); */
200 /* ./extensions/Translate/utils/TranslationHelpers.php:143: $suggestions = Http::get( $url, $timeout ); */
201 /* ./extensions/Translate/SpecialImportTranslations.php:169: $filedata = Http::get( $url ); ; */
202 /* ./extensions/Translate/TranslateEditAddons.php:338: $suggestions = Http::get( $url, $timeout ); */
203 /* ./extensions/SecurePoll/includes/user/Auth.php:283: $value = Http::get( $url, 20, $curlParams ); */
204 /* ./extensions/DumpHTML/dumpHTML.inc:778: $contents = Http::get( $url ); */
205 /* ./extensions/DumpHTML/dumpHTML.inc:1298: $contents = Http::get( $sourceUrl ); */
206 /* ./extensions/DumpHTML/dumpHTML.inc:1373: $contents = Http::get( $sourceUrl ); */
207 /* ./phase3/maintenance/rebuildInterwiki.inc:101: $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 ); */
208 /* ./phase3/maintenance/findhooks.php:98: $allhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&cmlimit=500&format=php' ); */
209 /* ./phase3/maintenance/findhooks.php:109: $oldhookdata = Http::get( 'http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Removed_hooks&cmlimit=500&format=php' ); */
210 /* ./phase3/maintenance/dumpInterwiki.inc:95: $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 ); */
211 /* ./phase3/includes/parser/Parser.php:3204: $text = Http::get($url); */
212 /* ./phase3/includes/filerepo/ForeignAPIRepo.php:131: $data = Http::get( $url ); */
213 /* ./phase3/includes/filerepo/ForeignAPIRepo.php:205: $thumb = Http::get( $foreignUrl ); */
214 /* ./phase3/includes/filerepo/File.php:1105: $res = Http::get( $renderUrl ); */
215 /* ./phase3/includes/GlobalFunctions.php:2760: * @deprecated Use Http::get() instead */
216 /* ./phase3/includes/GlobalFunctions.php:2764: return Http::get( $url ); */
217 /* ./phase3/includes/ExternalStoreHttp.php:18: $ret = Http::get( $url ); */
218 /* ./phase3/includes/Import.php:357: $data = Http::get( $src ); */
219 /* ./extensions/ExternalData/ED_Utils.php:291: return Http::get( $url, 'default', array(CURLOPT_SSL_VERIFYPEER => false) ); */
220 /* ./extensions/ExternalData/ED_Utils.php:293: return Http::get( $url ); */
221 /* ./extensions/ExternalData/ED_Utils.php:306: $page = Http::get( $url, 'default', array(CURLOPT_SSL_VERIFYPEER => false) ); */
222 /* ./extensions/ExternalData/ED_Utils.php:308: $page = Http::get( $url ); */
223 /* ./extensions/CodeReview/backend/Subversion.php:320: $blob = Http::get( $target, $this->mTimeout ); */
224 /* ./extensions/AmazonPlus/AmazonPlus.php:214: $this->response = Http::get( $urlstr ); */
225 /* ./extensions/StaticWiki/StaticWiki.php:24: $text = Http::get( $url ) ; */
226 /* ./extensions/StaticWiki/StaticWiki.php:64: $history = Http::get ( $wgStaticWikiExternalSite . "index.php?title=" . urlencode ( $url_title ) . "&action=history" ) ; */
227 /* ./extensions/Configure/scripts/findSettings.php:126: $cont = Http::get( "http://www.mediawiki.org/w/index.php?title={$page}&action=raw" ); */
228 /* ./extensions/TorBlock/TorBlock.class.php:148: $data = Http::get( $url ); */
229 /* ./extensions/HoneypotIntegration/HoneypotIntegration.class.php:60: $data = Http::get( $wgHoneypotURLSource, 'default', */
230 /* ./extensions/SemanticForms/includes/SF_Utils.inc:378: $page_contents = Http::get($url); */
231 /* ./extensions/LocalisationUpdate/LocalisationUpdate.class.php:172: $basefilecontents = Http::get( $basefile ); */
232 /* ./extensions/APC/SpecialAPC.php:245: $rss = Http::get( 'http://pecl.php.net/feeds/pkg_apc.rss' ); */
233 /* ./extensions/Interlanguage/Interlanguage.php:56: $a = Http::get( $url ); */
234 /* ./extensions/MWSearch/MWSearch_body.php:492: $data = Http::get( $searchUrl, $wgLuceneSearchTimeout, $httpOpts); */
235 function runHTTPGets($proxy=null) {
239 $opt['proxy'] = $proxy;
240 } elseif( $proxy === false ) {
241 $opt['noProxy'] = true;
244 foreach ( $this->test_geturl
as $u ) {
245 $r = Http
::get( $u, 30, $opt ); /* timeout of 30s */
246 $this->assertEquals( self
::$content["GET $u"], "$r", "Get $u with ".Http
::$httpEngine );
250 function testGetDefault() {
251 Http
::$httpEngine = false;
255 function testGetPhp() {
256 if ( !self
::$has_fopen ) {
257 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
260 Http
::$httpEngine = "php";
264 function testGetCurl() {
265 if ( !self
::$has_curl ) {
266 $this->markTestIncomplete( "This test requires curl." );
269 Http
::$httpEngine = "curl";
273 /* ./phase3/maintenance/parserTests.inc:1618: return Http::post( $url, array( 'postData' => wfArrayToCGI( $data ) ) ); */
274 function runHTTPPosts($proxy=null) {
278 $opt['proxy'] = $proxy;
279 } elseif( $proxy === false ) {
280 $opt['noProxy'] = true;
283 foreach ( $this->test_posturl
as $u => $postData ) {
284 $opt['postData'] = $postData;
285 $r = Http
::post( $u, $opt );
286 $this->assertEquals( self
::$content["POST $u => $postData"], "$r",
287 "POST $u (postData=$postData) with ".Http
::$httpEngine );
291 function testPostDefault() {
292 Http
::$httpEngine = false;
293 self
::runHTTPPosts();
296 function testPostPhp() {
297 if ( !self
::$has_fopen ) {
298 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
301 Http
::$httpEngine = "php";
302 self
::runHTTPPosts();
305 function testPostCurl() {
306 if ( !self
::$has_curl ) {
307 $this->markTestIncomplete( "This test requires curl." );
310 Http
::$httpEngine = "curl";
311 self
::runHTTPPosts();
314 function runProxyRequests() {
315 if(!self
::$has_proxy) {
316 $this->markTestIncomplete( "This test requires a proxy." );
318 self
::runHTTPGets(self
::$proxy);
319 self
::runHTTPPosts(self
::$proxy);
320 self
::runHTTPRequests(self
::$proxy);
322 // Set false here to do noProxy
323 self
::runHTTPGets(false);
324 self
::runHTTPPosts(false);
325 self
::runHTTPRequests(false);
328 function testProxyDefault() {
329 Http
::$httpEngine = false;
330 self
::runProxyRequests();
333 function testProxyPhp() {
334 if ( !self
::$has_fopen ) {
335 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
338 Http
::$httpEngine = 'php';
339 self
::runProxyRequests();
342 function testProxyCurl() {
343 if ( !self
::$has_curl ) {
344 $this->markTestIncomplete( "This test requires curl." );
347 Http
::$httpEngine = 'curl';
348 self
::runProxyRequests();
351 function testIsLocalUrl() {
354 /* ./extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php:559: $user_agent = Http::userAgent(); */
355 function testUserAgent() {
358 function testIsValidUrl() {
361 function testValidateCookieDomain() {
362 $this->assertFalse( Cookie
::validateCookieDomain( "co.uk" ) );
363 $this->assertFalse( Cookie
::validateCookieDomain( ".co.uk" ) );
364 $this->assertFalse( Cookie
::validateCookieDomain( "gov.uk" ) );
365 $this->assertFalse( Cookie
::validateCookieDomain( ".gov.uk" ) );
366 $this->assertTrue( Cookie
::validateCookieDomain( "supermarket.uk" ) );
367 $this->assertFalse( Cookie
::validateCookieDomain( "uk" ) );
368 $this->assertFalse( Cookie
::validateCookieDomain( ".uk" ) );
369 $this->assertFalse( Cookie
::validateCookieDomain( "127.0.0." ) );
370 $this->assertFalse( Cookie
::validateCookieDomain( "127." ) );
371 $this->assertFalse( Cookie
::validateCookieDomain( "127.0.0.1." ) );
372 $this->assertTrue( Cookie
::validateCookieDomain( "127.0.0.1" ) );
373 $this->assertFalse( Cookie
::validateCookieDomain( "333.0.0.1" ) );
374 $this->assertTrue( Cookie
::validateCookieDomain( "example.com" ) );
375 $this->assertFalse( Cookie
::validateCookieDomain( "example.com." ) );
376 $this->assertTrue( Cookie
::validateCookieDomain( ".example.com" ) );
378 $this->assertTrue( Cookie
::validateCookieDomain( ".example.com", "www.example.com" ) );
379 $this->assertFalse( Cookie
::validateCookieDomain( "example.com", "www.example.com" ) );
380 $this->assertTrue( Cookie
::validateCookieDomain( "127.0.0.1", "127.0.0.1" ) );
381 $this->assertFalse( Cookie
::validateCookieDomain( "127.0.0.1", "localhost" ) );
386 function testSetCooke() {
387 $c = new MockCookie( "name", "value",
392 $this->assertFalse($c->canServeDomain("ac.th"));
394 $c = new MockCookie( "name", "value",
396 "domain" => "example.com",
400 $this->assertTrue($c->canServeDomain("example.com"));
401 $this->assertFalse($c->canServeDomain("www.example.com"));
403 $c = new MockCookie( "name", "value",
405 "domain" => ".example.com",
409 $this->assertFalse($c->canServeDomain("www.example.net"));
410 $this->assertFalse($c->canServeDomain("example.com"));
411 $this->assertTrue($c->canServeDomain("www.example.com"));
413 $this->assertFalse($c->canServePath("/"));
414 $this->assertFalse($c->canServePath("/bogus/path/"));
415 $this->assertFalse($c->canServePath("/path"));
416 $this->assertTrue($c->canServePath("/path/"));
418 $this->assertTrue($c->isUnExpired());
420 $this->assertEquals("", $c->serializeToHttpRequest("/path/", "www.example.net"));
421 $this->assertEquals("", $c->serializeToHttpRequest("/", "www.example.com"));
422 $this->assertEquals("name=value", $c->serializeToHttpRequest("/path/", "www.example.com"));
424 $c = new MockCookie( "name", "value",
426 "domain" => "www.example.com",
429 $this->assertFalse($c->canServeDomain("example.com"));
430 $this->assertFalse($c->canServeDomain("www.example.net"));
431 $this->assertTrue($c->canServeDomain("www.example.com"));
433 $c = new MockCookie( "name", "value",
435 "domain" => ".example.com",
437 "expires" => "-1 day",
439 $this->assertFalse($c->isUnExpired());
440 $this->assertEquals("", $c->serializeToHttpRequest("/path/", "www.example.com"));
442 $c = new MockCookie( "name", "value",
444 "domain" => ".example.com",
446 "expires" => "+1 day",
448 $this->assertTrue($c->isUnExpired());
449 $this->assertEquals("name=value", $c->serializeToHttpRequest("/path/", "www.example.com"));
452 function testCookieJarSetCookie() {
454 $cj->setCookie( "name", "value",
456 "domain" => ".example.com",
459 $cj->setCookie( "name2", "value",
461 "domain" => ".example.com",
462 "path" => "/path/sub",
464 $cj->setCookie( "name3", "value",
466 "domain" => ".example.com",
469 $cj->setCookie( "name4", "value",
471 "domain" => ".example.net",
474 $cj->setCookie( "name5", "value",
476 "domain" => ".example.net",
478 "expires" => "-1 day",
481 $this->assertEquals("name4=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
482 $this->assertEquals("name3=value", $cj->serializeToHttpRequest("/", "www.example.com"));
483 $this->assertEquals("name=value; name3=value", $cj->serializeToHttpRequest("/path/", "www.example.com"));
485 $cj->setCookie( "name5", "value",
487 "domain" => ".example.net",
489 "expires" => "+1 day",
491 $this->assertEquals("name4=value; name5=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
493 $cj->setCookie( "name4", "value",
495 "domain" => ".example.net",
497 "expires" => "-1 day",
499 $this->assertEquals("name5=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
502 function testParseResponseHeader() {
505 $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
506 $cj->parseCookieResponseHeader( $h[0], "www.example.com" );
507 $this->assertEquals("name4=value", $cj->serializeToHttpRequest("/", "www.example.com"));
509 $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
510 $cj->parseCookieResponseHeader( $h[1], "www.example.com" );
511 $this->assertEquals("", $cj->serializeToHttpRequest("/", "www.example.com"));
512 $this->assertEquals("name4=value2", $cj->serializeToHttpRequest("/path/", "www.example.com"));
514 $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
515 $cj->parseCookieResponseHeader( $h[2], "www.example.com" );
516 $this->assertEquals("name4=value2; name5=value3", $cj->serializeToHttpRequest("/path/", "www.example.com"));
518 $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
519 $cj->parseCookieResponseHeader( $h[3], "www.example.com" );
520 $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net"));
522 $h[] = "name6=value0; domain=.example.net; path=/path/; expires=Mon, 09-Dec-1999 13:46:00 GMT";
523 $cj->parseCookieResponseHeader( $h[4], "www.example.net" );
524 $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net"));
526 $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
527 $cj->parseCookieResponseHeader( $h[5], "www.example.net" );
528 $this->assertEquals("name6=value4", $cj->serializeToHttpRequest("/path/", "www.example.net"));
531 function runCookieRequests() {
532 $r = HttpRequest
::factory( "http://www.php.net/manual" );
535 $jar = $r->getCookieJar();
536 $this->assertThat( $jar, $this->isInstanceOf( 'CookieJar' ) );
538 if ( is_a($r, 'PhpHttpRequest') && version_compare( '5.1.7', phpversion(), '>' ) ) {
539 $this->markTestSkipped( 'Redirection fails or crashes PHP on 5.1.6 and prior' );
541 $serialized = $jar->serializeToHttpRequest( "/search?q=test", "www.php.net" );
542 $this->assertRegExp( '/\bCOUNTRY=[^=;]+/', $serialized );
543 $this->assertRegExp( '/\bLAST_LANG=[^=;]+/', $serialized );
544 $this->assertEquals( '', $jar->serializeToHttpRequest( "/search?q=test", "www.php.com" ) );
547 function testCookieRequestDefault() {
548 Http
::$httpEngine = false;
549 self
::runCookieRequests();
551 function testCookieRequestPhp() {
552 if ( !self
::$has_fopen ) {
553 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
556 Http
::$httpEngine = 'php';
557 self
::runCookieRequests();
559 function testCookieRequestCurl() {
560 if ( !self
::$has_curl ) {
561 $this->markTestIncomplete( "This test requires curl." );
564 Http
::$httpEngine = 'curl';
565 self
::runCookieRequests();