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 function testFailureDefault() {
118 Http
::$httpEngine = false;
119 self
::runHTTPFailureChecks();
122 function testFailurePhp() {
123 if ( !self
::$has_fopen ) {
124 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
127 Http
::$httpEngine = "php";
128 self
::runHTTPFailureChecks();
131 function testFailureCurl() {
132 if ( !self
::$has_curl ) {
133 $this->markTestIncomplete( "This test requires curl." );
136 Http
::$httpEngine = "curl";
137 self
::runHTTPFailureChecks();
140 /* ./phase3/includes/Import.php:1108: $data = Http::request( $method, $url ); */
141 /* ./includes/Import.php:1124: $link = Title::newFromText( "$interwiki:Special:Export/$page" ); */
142 /* ./includes/Import.php:1134: return ImportStreamSource::newFromURL( $url, "POST" ); */
143 function runHTTPRequests($proxy=null) {
147 $opt['proxy'] = $proxy;
148 } elseif( $proxy === false ) {
149 $opt['noProxy'] = true;
152 /* no postData here because the only request I could find in code so far didn't have any */
153 foreach ( $this->test_requesturl
as $u ) {
154 $r = Http
::request( "POST", $u, $opt );
155 $this->assertEquals( self
::$content["POST $u"], "$r", "POST $u with ".Http
::$httpEngine );
159 function testRequestDefault() {
160 Http
::$httpEngine = false;
161 self
::runHTTPRequests();
164 function testRequestPhp() {
165 if ( !self
::$has_fopen ) {
166 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
169 Http
::$httpEngine = "php";
170 self
::runHTTPRequests();
173 function testRequestCurl() {
174 if ( !self
::$has_curl ) {
175 $this->markTestIncomplete( "This test requires curl." );
178 Http
::$httpEngine = "curl";
179 self
::runHTTPRequests();
182 /* ./extensions/SpamBlacklist/SpamBlacklist_body.php:164: $httpText = Http::get( $fileName ); */
183 /* ./extensions/ApiSVGProxy/ApiSVGProxy.body.php:44: $contents = Http::get( $file->getFullUrl() ); */
184 /* ./extensions/BookInformation/drivers/IsbnDb.php:24: if( ( $xml = Http::get( $uri ) ) !== false ) { */
185 /* ./extensions/BookInformation/drivers/Amazon.php:23: if( ( $xml = Http::get( $uri ) ) !== false ) { */
186 /* ./extensions/TitleBlacklist/TitleBlacklist.list.php:217: $result = Http::get( $url ); */
187 /* ./extensions/TSPoll/TSPoll.php:68: $get_server = Http::get( 'http://toolserver.org/~jan/poll/dev/main.php?page=wiki_output&id='.$id ); */
188 /* ./extensions/TSPoll/TSPoll.php:70: $get_server = Http::get( 'http://toolserver.org/~jan/poll/main.php?page=wiki_output&id='.$id ); */
189 /* ./extensions/DoubleWiki/DoubleWiki.php:56: $translation = Http::get( $url.$sep.'action=render' ); */
190 /* ./extensions/ExternalPages/ExternalPages_body.php:177: $serializedText = Http::get( $this->mPageURL ); */
191 /* ./extensions/Translate/utils/TranslationHelpers.php:143: $suggestions = Http::get( $url, $timeout ); */
192 /* ./extensions/Translate/SpecialImportTranslations.php:169: $filedata = Http::get( $url ); ; */
193 /* ./extensions/Translate/TranslateEditAddons.php:338: $suggestions = Http::get( $url, $timeout ); */
194 /* ./extensions/SecurePoll/includes/user/Auth.php:283: $value = Http::get( $url, 20, $curlParams ); */
195 /* ./extensions/DumpHTML/dumpHTML.inc:778: $contents = Http::get( $url ); */
196 /* ./extensions/DumpHTML/dumpHTML.inc:1298: $contents = Http::get( $sourceUrl ); */
197 /* ./extensions/DumpHTML/dumpHTML.inc:1373: $contents = Http::get( $sourceUrl ); */
198 /* ./phase3/maintenance/rebuildInterwiki.inc:101: $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 ); */
199 /* ./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' ); */
200 /* ./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' ); */
201 /* ./phase3/maintenance/dumpInterwiki.inc:95: $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 ); */
202 /* ./phase3/includes/parser/Parser.php:3204: $text = Http::get($url); */
203 /* ./phase3/includes/filerepo/ForeignAPIRepo.php:131: $data = Http::get( $url ); */
204 /* ./phase3/includes/filerepo/ForeignAPIRepo.php:205: $thumb = Http::get( $foreignUrl ); */
205 /* ./phase3/includes/filerepo/File.php:1105: $res = Http::get( $renderUrl ); */
206 /* ./phase3/includes/GlobalFunctions.php:2760: * @deprecated Use Http::get() instead */
207 /* ./phase3/includes/GlobalFunctions.php:2764: return Http::get( $url ); */
208 /* ./phase3/includes/ExternalStoreHttp.php:18: $ret = Http::get( $url ); */
209 /* ./phase3/includes/Import.php:357: $data = Http::get( $src ); */
210 /* ./extensions/ExternalData/ED_Utils.php:291: return Http::get( $url, 'default', array(CURLOPT_SSL_VERIFYPEER => false) ); */
211 /* ./extensions/ExternalData/ED_Utils.php:293: return Http::get( $url ); */
212 /* ./extensions/ExternalData/ED_Utils.php:306: $page = Http::get( $url, 'default', array(CURLOPT_SSL_VERIFYPEER => false) ); */
213 /* ./extensions/ExternalData/ED_Utils.php:308: $page = Http::get( $url ); */
214 /* ./extensions/CodeReview/backend/Subversion.php:320: $blob = Http::get( $target, $this->mTimeout ); */
215 /* ./extensions/AmazonPlus/AmazonPlus.php:214: $this->response = Http::get( $urlstr ); */
216 /* ./extensions/StaticWiki/StaticWiki.php:24: $text = Http::get( $url ) ; */
217 /* ./extensions/StaticWiki/StaticWiki.php:64: $history = Http::get ( $wgStaticWikiExternalSite . "index.php?title=" . urlencode ( $url_title ) . "&action=history" ) ; */
218 /* ./extensions/Configure/scripts/findSettings.php:126: $cont = Http::get( "http://www.mediawiki.org/w/index.php?title={$page}&action=raw" ); */
219 /* ./extensions/TorBlock/TorBlock.class.php:148: $data = Http::get( $url ); */
220 /* ./extensions/HoneypotIntegration/HoneypotIntegration.class.php:60: $data = Http::get( $wgHoneypotURLSource, 'default', */
221 /* ./extensions/SemanticForms/includes/SF_Utils.inc:378: $page_contents = Http::get($url); */
222 /* ./extensions/LocalisationUpdate/LocalisationUpdate.class.php:172: $basefilecontents = Http::get( $basefile ); */
223 /* ./extensions/APC/SpecialAPC.php:245: $rss = Http::get( 'http://pecl.php.net/feeds/pkg_apc.rss' ); */
224 /* ./extensions/Interlanguage/Interlanguage.php:56: $a = Http::get( $url ); */
225 /* ./extensions/MWSearch/MWSearch_body.php:492: $data = Http::get( $searchUrl, $wgLuceneSearchTimeout, $httpOpts); */
226 function runHTTPGets($proxy=null) {
230 $opt['proxy'] = $proxy;
231 } elseif( $proxy === false ) {
232 $opt['noProxy'] = true;
235 foreach ( $this->test_geturl
as $u ) {
236 $r = Http
::get( $u, 30, $opt ); /* timeout of 30s */
237 $this->assertEquals( self
::$content["GET $u"], "$r", "Get $u with ".Http
::$httpEngine );
241 function testGetDefault() {
242 Http
::$httpEngine = false;
246 function testGetPhp() {
247 if ( !self
::$has_fopen ) {
248 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
251 Http
::$httpEngine = "php";
255 function testGetCurl() {
256 if ( !self
::$has_curl ) {
257 $this->markTestIncomplete( "This test requires curl." );
260 Http
::$httpEngine = "curl";
264 /* ./phase3/maintenance/parserTests.inc:1618: return Http::post( $url, array( 'postData' => wfArrayToCGI( $data ) ) ); */
265 function runHTTPPosts($proxy=null) {
269 $opt['proxy'] = $proxy;
270 } elseif( $proxy === false ) {
271 $opt['noProxy'] = true;
274 foreach ( $this->test_posturl
as $u => $postData ) {
275 $opt['postData'] = $postData;
276 $r = Http
::post( $u, $opt );
277 $this->assertEquals( self
::$content["POST $u => $postData"], "$r",
278 "POST $u (postData=$postData) with ".Http
::$httpEngine );
282 function testPostDefault() {
283 Http
::$httpEngine = false;
284 self
::runHTTPPosts();
287 function testPostPhp() {
288 if ( !self
::$has_fopen ) {
289 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
292 Http
::$httpEngine = "php";
293 self
::runHTTPPosts();
296 function testPostCurl() {
297 if ( !self
::$has_curl ) {
298 $this->markTestIncomplete( "This test requires curl." );
301 Http
::$httpEngine = "curl";
302 self
::runHTTPPosts();
305 function runProxyRequests() {
306 if(!self
::$has_proxy) {
307 $this->markTestIncomplete( "This test requires a proxy." );
309 self
::runHTTPGets(self
::$proxy);
310 self
::runHTTPPosts(self
::$proxy);
311 self
::runHTTPRequests(self
::$proxy);
313 // Set false here to do noProxy
314 self
::runHTTPGets(false);
315 self
::runHTTPPosts(false);
316 self
::runHTTPRequests(false);
319 function testProxyDefault() {
320 Http
::$httpEngine = false;
321 self
::runProxyRequests();
324 function testProxyPhp() {
325 if ( !self
::$has_fopen ) {
326 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
329 Http
::$httpEngine = 'php';
330 self
::runProxyRequests();
333 function testProxyCurl() {
334 if ( !self
::$has_curl ) {
335 $this->markTestIncomplete( "This test requires curl." );
338 Http
::$httpEngine = 'curl';
339 self
::runProxyRequests();
342 function testIsLocalUrl() {
345 /* ./extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php:559: $user_agent = Http::userAgent(); */
346 function testUserAgent() {
349 function testIsValidUrl() {
352 function testValidateCookieDomain() {
353 $this->assertFalse( Cookie
::validateCookieDomain( "co.uk" ) );
354 $this->assertFalse( Cookie
::validateCookieDomain( ".co.uk" ) );
355 $this->assertFalse( Cookie
::validateCookieDomain( "gov.uk" ) );
356 $this->assertFalse( Cookie
::validateCookieDomain( ".gov.uk" ) );
357 $this->assertTrue( Cookie
::validateCookieDomain( "supermarket.uk" ) );
358 $this->assertFalse( Cookie
::validateCookieDomain( "uk" ) );
359 $this->assertFalse( Cookie
::validateCookieDomain( ".uk" ) );
360 $this->assertFalse( Cookie
::validateCookieDomain( "127.0.0." ) );
361 $this->assertFalse( Cookie
::validateCookieDomain( "127." ) );
362 $this->assertFalse( Cookie
::validateCookieDomain( "127.0.0.1." ) );
363 $this->assertTrue( Cookie
::validateCookieDomain( "127.0.0.1" ) );
364 $this->assertFalse( Cookie
::validateCookieDomain( "333.0.0.1" ) );
365 $this->assertTrue( Cookie
::validateCookieDomain( "example.com" ) );
366 $this->assertFalse( Cookie
::validateCookieDomain( "example.com." ) );
367 $this->assertTrue( Cookie
::validateCookieDomain( ".example.com" ) );
369 $this->assertTrue( Cookie
::validateCookieDomain( ".example.com", "www.example.com" ) );
370 $this->assertFalse( Cookie
::validateCookieDomain( "example.com", "www.example.com" ) );
371 $this->assertTrue( Cookie
::validateCookieDomain( "127.0.0.1", "127.0.0.1" ) );
372 $this->assertFalse( Cookie
::validateCookieDomain( "127.0.0.1", "localhost" ) );
377 function testSetCooke() {
378 $c = new MockCookie( "name", "value",
383 $this->assertFalse($c->canServeDomain("ac.th"));
385 $c = new MockCookie( "name", "value",
387 "domain" => "example.com",
391 $this->assertTrue($c->canServeDomain("example.com"));
392 $this->assertFalse($c->canServeDomain("www.example.com"));
394 $c = new MockCookie( "name", "value",
396 "domain" => ".example.com",
400 $this->assertFalse($c->canServeDomain("www.example.net"));
401 $this->assertFalse($c->canServeDomain("example.com"));
402 $this->assertTrue($c->canServeDomain("www.example.com"));
404 $this->assertFalse($c->canServePath("/"));
405 $this->assertFalse($c->canServePath("/bogus/path/"));
406 $this->assertFalse($c->canServePath("/path"));
407 $this->assertTrue($c->canServePath("/path/"));
409 $this->assertTrue($c->isUnExpired());
411 $this->assertEquals("", $c->serializeToHttpRequest("/path/", "www.example.net"));
412 $this->assertEquals("", $c->serializeToHttpRequest("/", "www.example.com"));
413 $this->assertEquals("name=value", $c->serializeToHttpRequest("/path/", "www.example.com"));
415 $c = new MockCookie( "name", "value",
417 "domain" => "www.example.com",
420 $this->assertFalse($c->canServeDomain("example.com"));
421 $this->assertFalse($c->canServeDomain("www.example.net"));
422 $this->assertTrue($c->canServeDomain("www.example.com"));
424 $c = new MockCookie( "name", "value",
426 "domain" => ".example.com",
428 "expires" => "-1 day",
430 $this->assertFalse($c->isUnExpired());
431 $this->assertEquals("", $c->serializeToHttpRequest("/path/", "www.example.com"));
433 $c = new MockCookie( "name", "value",
435 "domain" => ".example.com",
437 "expires" => "+1 day",
439 $this->assertTrue($c->isUnExpired());
440 $this->assertEquals("name=value", $c->serializeToHttpRequest("/path/", "www.example.com"));
443 function testCookieJarSetCookie() {
445 $cj->setCookie( "name", "value",
447 "domain" => ".example.com",
450 $cj->setCookie( "name2", "value",
452 "domain" => ".example.com",
453 "path" => "/path/sub",
455 $cj->setCookie( "name3", "value",
457 "domain" => ".example.com",
460 $cj->setCookie( "name4", "value",
462 "domain" => ".example.net",
465 $cj->setCookie( "name5", "value",
467 "domain" => ".example.net",
469 "expires" => "-1 day",
472 $this->assertEquals("name4=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
473 $this->assertEquals("name3=value", $cj->serializeToHttpRequest("/", "www.example.com"));
474 $this->assertEquals("name=value; name3=value", $cj->serializeToHttpRequest("/path/", "www.example.com"));
476 $cj->setCookie( "name5", "value",
478 "domain" => ".example.net",
480 "expires" => "+1 day",
482 $this->assertEquals("name4=value; name5=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
484 $cj->setCookie( "name4", "value",
486 "domain" => ".example.net",
488 "expires" => "-1 day",
490 $this->assertEquals("name5=value", $cj->serializeToHttpRequest("/path/", "www.example.net"));
493 function testParseResponseHeader() {
496 $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
497 $cj->parseCookieResponseHeader( $h[0], "www.example.com" );
498 $this->assertEquals("name4=value", $cj->serializeToHttpRequest("/", "www.example.com"));
500 $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
501 $cj->parseCookieResponseHeader( $h[1], "www.example.com" );
502 $this->assertEquals("", $cj->serializeToHttpRequest("/", "www.example.com"));
503 $this->assertEquals("name4=value2", $cj->serializeToHttpRequest("/path/", "www.example.com"));
505 $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
506 $cj->parseCookieResponseHeader( $h[2], "www.example.com" );
507 $this->assertEquals("name4=value2; name5=value3", $cj->serializeToHttpRequest("/path/", "www.example.com"));
509 $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
510 $cj->parseCookieResponseHeader( $h[3], "www.example.com" );
511 $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net"));
513 $h[] = "name6=value0; domain=.example.net; path=/path/; expires=Mon, 09-Dec-1999 13:46:00 GMT";
514 $cj->parseCookieResponseHeader( $h[4], "www.example.net" );
515 $this->assertEquals("", $cj->serializeToHttpRequest("/path/", "www.example.net"));
517 $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
518 $cj->parseCookieResponseHeader( $h[5], "www.example.net" );
519 $this->assertEquals("name6=value4", $cj->serializeToHttpRequest("/path/", "www.example.net"));
522 function runCookieRequests() {
523 $r = HttpRequest
::factory( "http://www.php.net/manual" );
526 $jar = $r->getCookieJar();
528 $this->assertThat( $jar, $this->isInstanceOf( 'CookieJar' ) );
529 $this->assertRegExp( '/^COUNTRY=.*; LAST_LANG=.*$/', $jar->serializeToHttpRequest( "/search?q=test", "www.php.net" ) );
530 $this->assertEquals( '', $jar->serializeToHttpRequest( "/search?q=test", "www.php.com" ) );
533 function testCookieRequestDefault() {
534 Http
::$httpEngine = false;
535 self
::runCookieRequests();
537 function testCookieRequestPhp() {
538 if ( !self
::$has_fopen ) {
539 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
542 Http
::$httpEngine = 'php';
543 self
::runCookieRequests();
545 function testCookieRequestCurl() {
546 if ( !self
::$has_curl ) {
547 $this->markTestIncomplete( "This test requires curl." );
550 Http
::$httpEngine = 'curl';
551 self
::runCookieRequests();