More is_a() -> instanceof
[lhc/web/wiklou.git] / maintenance / tests / phpunit / includes / HttpTest.php
1 <?php
2
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(); }
7 }
8
9 /**
10 * @group Broken
11 */
12 class HttpTest extends PHPUnit_Framework_TestCase {
13 static $content;
14 static $headers;
15 static $has_curl;
16 static $has_fopen;
17 static $has_proxy = false;
18 static $proxy = "http://hulk:8080/";
19 var $test_geturl = array(
20 "http://www.example.com/",
21 "http://pecl.php.net/feeds/pkg_apc.rss",
22 "http://toolserver.org/~jan/poll/dev/main.php?page=wiki_output&id=3",
23 "http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw",
24 "http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&format=php",
25 );
26 var $test_requesturl = array( "http://en.wikipedia.org/wiki/Special:Export/User:MarkAHershberger" );
27
28 var $test_posturl = array( "http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example" => "review=test" );
29
30 function setUp() {
31 putenv( "http_proxy" ); /* Remove any proxy env var, so curl doesn't get confused */
32 if ( is_array( self::$content ) ) {
33 return;
34 }
35 self::$has_curl = function_exists( 'curl_init' );
36 self::$has_fopen = wfIniGetBool( 'allow_url_fopen' );
37
38 if ( !file_exists( "/usr/bin/curl" ) ) {
39 $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." );
40 }
41
42 $content = tempnam( wfTempDir(), "" );
43 $headers = tempnam( wfTempDir(), "" );
44 if ( !$content && !$headers ) {
45 die( "Couldn't create temp file!" );
46 }
47
48 // This probably isn't the best test for a proxy, but it works on my system!
49 system( "curl -0 -o $content -s " . self::$proxy );
50 $out = file_get_contents( $content );
51 if ( $out ) {
52 self::$has_proxy = true;
53 }
54
55 /* Maybe use wget instead of curl here ... just to use a different codebase? */
56 foreach ( $this->test_geturl as $u ) {
57 system( "curl -0 -s -D $headers '$u' -o $content" );
58 self::$content["GET $u"] = file_get_contents( $content );
59 self::$headers["GET $u"] = file_get_contents( $headers );
60 }
61 foreach ( $this->test_requesturl as $u ) {
62 system( "curl -0 -s -X POST -H 'Content-Length: 0' -D $headers '$u' -o $content" );
63 self::$content["POST $u"] = file_get_contents( $content );
64 self::$headers["POST $u"] = file_get_contents( $headers );
65 }
66 foreach ( $this->test_posturl as $u => $postData ) {
67 system( "curl -0 -s -X POST -d '$postData' -D $headers '$u' -o $content" );
68 self::$content["POST $u => $postData"] = file_get_contents( $content );
69 self::$headers["POST $u => $postData"] = file_get_contents( $headers );
70 }
71 unlink( $content );
72 unlink( $headers );
73 }
74
75
76 function testInstantiation() {
77 Http::$httpEngine = false;
78
79 $r = MWHttpRequest::factory( "http://www.example.com/" );
80 if ( self::$has_curl ) {
81 $this->assertThat( $r, $this->isInstanceOf( 'CurlHttpRequest' ) );
82 } else {
83 $this->assertThat( $r, $this->isInstanceOf( 'PhpHttpRequest' ) );
84 }
85 unset( $r );
86
87 if ( !self::$has_fopen ) {
88 $this->setExpectedException( 'MWException' );
89 }
90 Http::$httpEngine = 'php';
91 $r = MWHttpRequest::factory( "http://www.example.com/" );
92 $this->assertThat( $r, $this->isInstanceOf( 'PhpHttpRequest' ) );
93 unset( $r );
94
95 if ( !self::$has_curl ) {
96 $this->setExpectedException( 'MWException' );
97 }
98 Http::$httpEngine = 'curl';
99 $r = MWHttpRequest::factory( "http://www.example.com/" );
100 if ( self::$has_curl ) {
101 $this->assertThat( $r, $this->isInstanceOf( 'CurlHttpRequest' ) );
102 }
103 }
104
105 function runHTTPFailureChecks() {
106 // Each of the following requests should result in a failure.
107
108 $timeout = 1;
109 $start_time = time();
110 $r = Http::get( "http://www.example.com:1/", $timeout );
111 $end_time = time();
112 $this->assertLessThan( $timeout + 2, $end_time - $start_time,
113 "Request took less than {$timeout}s via " . Http::$httpEngine );
114 $this->assertEquals( $r, false, "false -- what we get on error from Http::get()" );
115
116 $r = Http::get( "http://www.example.com/this-file-does-not-exist", $timeout );
117 $this->assertFalse( $r, "False on 404s" );
118
119
120 $r = MWHttpRequest::factory( "http://www.example.com/this-file-does-not-exist" );
121 $er = $r->execute();
122 if ( $r instanceof PhpHttpRequest && version_compare( '5.2.10', phpversion(), '>' ) ) {
123 $this->assertRegexp( "/HTTP request failed/", $er->getWikiText() );
124 } else {
125 $this->assertRegexp( "/404 Not Found/", $er->getWikiText() );
126 }
127 }
128
129 function testFailureDefault() {
130 Http::$httpEngine = false;
131 $this->runHTTPFailureChecks();
132 }
133
134 function testFailurePhp() {
135 if ( !self::$has_fopen ) {
136 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
137 }
138
139 Http::$httpEngine = "php";
140 $this->runHTTPFailureChecks();
141 }
142
143 function testFailureCurl() {
144 if ( !self::$has_curl ) {
145 $this->markTestIncomplete( "This test requires curl." );
146 }
147
148 Http::$httpEngine = "curl";
149 $this->runHTTPFailureChecks();
150 }
151
152 /* ./phase3/includes/Import.php:1108: $data = Http::request( $method, $url ); */
153 /* ./includes/Import.php:1124: $link = Title::newFromText( "$interwiki:Special:Export/$page" ); */
154 /* ./includes/Import.php:1134: return ImportStreamSource::newFromURL( $url, "POST" ); */
155 function runHTTPRequests( $proxy = null ) {
156 $opt = array();
157
158 if ( $proxy ) {
159 $opt['proxy'] = $proxy;
160 } elseif ( $proxy === false ) {
161 $opt['noProxy'] = true;
162 }
163
164 /* no postData here because the only request I could find in code so far didn't have any */
165 foreach ( $this->test_requesturl as $u ) {
166 $r = Http::request( "POST", $u, $opt );
167 $this->assertEquals( self::$content["POST $u"], "$r", "POST $u with " . Http::$httpEngine );
168 }
169 }
170
171 function testRequestDefault() {
172 Http::$httpEngine = false;
173 $this->runHTTPRequests();
174 }
175
176 function testRequestPhp() {
177 if ( !self::$has_fopen ) {
178 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
179 }
180
181 Http::$httpEngine = "php";
182 $this->runHTTPRequests();
183 }
184
185 function testRequestCurl() {
186 if ( !self::$has_curl ) {
187 $this->markTestIncomplete( "This test requires curl." );
188 }
189
190 Http::$httpEngine = "curl";
191 $this->runHTTPRequests();
192 }
193
194 /* ./extensions/SpamBlacklist/SpamBlacklist_body.php:164: $httpText = Http::get( $fileName ); */
195 /* ./extensions/ApiSVGProxy/ApiSVGProxy.body.php:44: $contents = Http::get( $file->getFullUrl() ); */
196 /* ./extensions/BookInformation/drivers/IsbnDb.php:24: if( ( $xml = Http::get( $uri ) ) !== false ) { */
197 /* ./extensions/BookInformation/drivers/Amazon.php:23: if( ( $xml = Http::get( $uri ) ) !== false ) { */
198 /* ./extensions/TitleBlacklist/TitleBlacklist.list.php:217: $result = Http::get( $url ); */
199 /* ./extensions/TSPoll/TSPoll.php:68: $get_server = Http::get( 'http://toolserver.org/~jan/poll/dev/main.php?page=wiki_output&id='.$id ); */
200 /* ./extensions/TSPoll/TSPoll.php:70: $get_server = Http::get( 'http://toolserver.org/~jan/poll/main.php?page=wiki_output&id='.$id ); */
201 /* ./extensions/DoubleWiki/DoubleWiki.php:56: $translation = Http::get( $url.$sep.'action=render' ); */
202 /* ./extensions/ExternalPages/ExternalPages_body.php:177: $serializedText = Http::get( $this->mPageURL ); */
203 /* ./extensions/Translate/utils/TranslationHelpers.php:143: $suggestions = Http::get( $url, $timeout ); */
204 /* ./extensions/Translate/SpecialImportTranslations.php:169: $filedata = Http::get( $url ); ; */
205 /* ./extensions/Translate/TranslateEditAddons.php:338: $suggestions = Http::get( $url, $timeout ); */
206 /* ./extensions/SecurePoll/includes/user/Auth.php:283: $value = Http::get( $url, 20, $curlParams ); */
207 /* ./extensions/DumpHTML/dumpHTML.inc:778: $contents = Http::get( $url ); */
208 /* ./extensions/DumpHTML/dumpHTML.inc:1298: $contents = Http::get( $sourceUrl ); */
209 /* ./extensions/DumpHTML/dumpHTML.inc:1373: $contents = Http::get( $sourceUrl ); */
210 /* ./phase3/maintenance/rebuildInterwiki.inc:101: $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 ); */
211 /* ./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' ); */
212 /* ./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' ); */
213 /* ./phase3/maintenance/dumpInterwiki.inc:95: $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 ); */
214 /* ./phase3/includes/parser/Parser.php:3204: $text = Http::get($url); */
215 /* ./phase3/includes/filerepo/ForeignAPIRepo.php:131: $data = Http::get( $url ); */
216 /* ./phase3/includes/filerepo/ForeignAPIRepo.php:205: $thumb = Http::get( $foreignUrl ); */
217 /* ./phase3/includes/filerepo/File.php:1105: $res = Http::get( $renderUrl ); */
218 /* ./phase3/includes/GlobalFunctions.php:2760: * @deprecated Use Http::get() instead */
219 /* ./phase3/includes/GlobalFunctions.php:2764: return Http::get( $url ); */
220 /* ./phase3/includes/ExternalStoreHttp.php:18: $ret = Http::get( $url ); */
221 /* ./phase3/includes/Import.php:357: $data = Http::get( $src ); */
222 /* ./extensions/ExternalData/ED_Utils.php:291: return Http::get( $url, 'default', array(CURLOPT_SSL_VERIFYPEER => false) ); */
223 /* ./extensions/ExternalData/ED_Utils.php:293: return Http::get( $url ); */
224 /* ./extensions/ExternalData/ED_Utils.php:306: $page = Http::get( $url, 'default', array(CURLOPT_SSL_VERIFYPEER => false) ); */
225 /* ./extensions/ExternalData/ED_Utils.php:308: $page = Http::get( $url ); */
226 /* ./extensions/CodeReview/backend/Subversion.php:320: $blob = Http::get( $target, $this->mTimeout ); */
227 /* ./extensions/AmazonPlus/AmazonPlus.php:214: $this->response = Http::get( $urlstr ); */
228 /* ./extensions/StaticWiki/StaticWiki.php:24: $text = Http::get( $url ) ; */
229 /* ./extensions/StaticWiki/StaticWiki.php:64: $history = Http::get ( $wgStaticWikiExternalSite . "index.php?title=" . urlencode ( $url_title ) . "&action=history" ) ; */
230 /* ./extensions/Configure/scripts/findSettings.php:126: $cont = Http::get( "http://www.mediawiki.org/w/index.php?title={$page}&action=raw" ); */
231 /* ./extensions/TorBlock/TorBlock.class.php:148: $data = Http::get( $url ); */
232 /* ./extensions/HoneypotIntegration/HoneypotIntegration.class.php:60: $data = Http::get( $wgHoneypotURLSource, 'default', */
233 /* ./extensions/SemanticForms/includes/SF_Utils.inc:378: $page_contents = Http::get($url); */
234 /* ./extensions/LocalisationUpdate/LocalisationUpdate.class.php:172: $basefilecontents = Http::get( $basefile ); */
235 /* ./extensions/APC/SpecialAPC.php:245: $rss = Http::get( 'http://pecl.php.net/feeds/pkg_apc.rss' ); */
236 /* ./extensions/Interlanguage/Interlanguage.php:56: $a = Http::get( $url ); */
237 /* ./extensions/MWSearch/MWSearch_body.php:492: $data = Http::get( $searchUrl, $wgLuceneSearchTimeout, $httpOpts); */
238 function runHTTPGets( $proxy = null ) {
239 $opt = array();
240
241 if ( $proxy ) {
242 $opt['proxy'] = $proxy;
243 } elseif ( $proxy === false ) {
244 $opt['noProxy'] = true;
245 }
246
247 foreach ( $this->test_geturl as $u ) {
248 $r = Http::get( $u, 30, $opt ); /* timeout of 30s */
249 $this->assertEquals( self::$content["GET $u"], "$r", "Get $u with " . Http::$httpEngine );
250 }
251 }
252
253 function testGetDefault() {
254 Http::$httpEngine = false;
255 $this->runHTTPGets();
256 }
257
258 function testGetPhp() {
259 if ( !self::$has_fopen ) {
260 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
261 }
262
263 Http::$httpEngine = "php";
264 $this->runHTTPGets();
265 }
266
267 function testGetCurl() {
268 if ( !self::$has_curl ) {
269 $this->markTestIncomplete( "This test requires curl." );
270 }
271
272 Http::$httpEngine = "curl";
273 $this->runHTTPGets();
274 }
275
276 /* ./phase3/maintenance/parserTests.inc:1618: return Http::post( $url, array( 'postData' => wfArrayToCGI( $data ) ) ); */
277 function runHTTPPosts( $proxy = null ) {
278 $opt = array();
279
280 if ( $proxy ) {
281 $opt['proxy'] = $proxy;
282 } elseif ( $proxy === false ) {
283 $opt['noProxy'] = true;
284 }
285
286 foreach ( $this->test_posturl as $u => $postData ) {
287 $opt['postData'] = $postData;
288 $r = Http::post( $u, $opt );
289 $this->assertEquals( self::$content["POST $u => $postData"], "$r",
290 "POST $u (postData=$postData) with " . Http::$httpEngine );
291 }
292 }
293
294 function testPostDefault() {
295 Http::$httpEngine = false;
296 $this->runHTTPPosts();
297 }
298
299 function testPostPhp() {
300 if ( !self::$has_fopen ) {
301 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
302 }
303
304 Http::$httpEngine = "php";
305 $this->runHTTPPosts();
306 }
307
308 function testPostCurl() {
309 if ( !self::$has_curl ) {
310 $this->markTestIncomplete( "This test requires curl." );
311 }
312
313 Http::$httpEngine = "curl";
314 $this->runHTTPPosts();
315 }
316
317 function runProxyRequests() {
318 if ( !self::$has_proxy ) {
319 $this->markTestIncomplete( "This test requires a proxy." );
320 }
321 $this->runHTTPGets( self::$proxy );
322 $this->runHTTPPosts( self::$proxy );
323 $this->runHTTPRequests( self::$proxy );
324
325 // Set false here to do noProxy
326 $this->runHTTPGets( false );
327 $this->runHTTPPosts( false );
328 $this->runHTTPRequests( false );
329 }
330
331 function testProxyDefault() {
332 Http::$httpEngine = false;
333 $this->runProxyRequests();
334 }
335
336 function testProxyPhp() {
337 if ( !self::$has_fopen ) {
338 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
339 }
340
341 Http::$httpEngine = 'php';
342 $this->runProxyRequests();
343 }
344
345 function testProxyCurl() {
346 if ( !self::$has_curl ) {
347 $this->markTestIncomplete( "This test requires curl." );
348 }
349
350 Http::$httpEngine = 'curl';
351 $this->runProxyRequests();
352 }
353
354 function testIsLocalUrl() {
355 }
356
357 /* ./extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php:559: $user_agent = Http::userAgent(); */
358 function testUserAgent() {
359 }
360
361 function testIsValidUrl() {
362 }
363
364 function testValidateCookieDomain() {
365 $this->assertFalse( Cookie::validateCookieDomain( "co.uk" ) );
366 $this->assertFalse( Cookie::validateCookieDomain( ".co.uk" ) );
367 $this->assertFalse( Cookie::validateCookieDomain( "gov.uk" ) );
368 $this->assertFalse( Cookie::validateCookieDomain( ".gov.uk" ) );
369 $this->assertTrue( Cookie::validateCookieDomain( "supermarket.uk" ) );
370 $this->assertFalse( Cookie::validateCookieDomain( "uk" ) );
371 $this->assertFalse( Cookie::validateCookieDomain( ".uk" ) );
372 $this->assertFalse( Cookie::validateCookieDomain( "127.0.0." ) );
373 $this->assertFalse( Cookie::validateCookieDomain( "127." ) );
374 $this->assertFalse( Cookie::validateCookieDomain( "127.0.0.1." ) );
375 $this->assertTrue( Cookie::validateCookieDomain( "127.0.0.1" ) );
376 $this->assertFalse( Cookie::validateCookieDomain( "333.0.0.1" ) );
377 $this->assertTrue( Cookie::validateCookieDomain( "example.com" ) );
378 $this->assertFalse( Cookie::validateCookieDomain( "example.com." ) );
379 $this->assertTrue( Cookie::validateCookieDomain( ".example.com" ) );
380
381 $this->assertTrue( Cookie::validateCookieDomain( ".example.com", "www.example.com" ) );
382 $this->assertFalse( Cookie::validateCookieDomain( "example.com", "www.example.com" ) );
383 $this->assertTrue( Cookie::validateCookieDomain( "127.0.0.1", "127.0.0.1" ) );
384 $this->assertFalse( Cookie::validateCookieDomain( "127.0.0.1", "localhost" ) );
385
386
387 }
388
389 function testSetCooke() {
390 $c = new MockCookie( "name", "value",
391 array(
392 "domain" => "ac.th",
393 "path" => "/path/",
394 ) );
395 $this->assertFalse( $c->canServeDomain( "ac.th" ) );
396
397 $c = new MockCookie( "name", "value",
398 array(
399 "domain" => "example.com",
400 "path" => "/path/",
401 ) );
402
403 $this->assertTrue( $c->canServeDomain( "example.com" ) );
404 $this->assertFalse( $c->canServeDomain( "www.example.com" ) );
405
406 $c = new MockCookie( "name", "value",
407 array(
408 "domain" => ".example.com",
409 "path" => "/path/",
410 ) );
411
412 $this->assertFalse( $c->canServeDomain( "www.example.net" ) );
413 $this->assertFalse( $c->canServeDomain( "example.com" ) );
414 $this->assertTrue( $c->canServeDomain( "www.example.com" ) );
415
416 $this->assertFalse( $c->canServePath( "/" ) );
417 $this->assertFalse( $c->canServePath( "/bogus/path/" ) );
418 $this->assertFalse( $c->canServePath( "/path" ) );
419 $this->assertTrue( $c->canServePath( "/path/" ) );
420
421 $this->assertTrue( $c->isUnExpired() );
422
423 $this->assertEquals( "", $c->serializeToHttpRequest( "/path/", "www.example.net" ) );
424 $this->assertEquals( "", $c->serializeToHttpRequest( "/", "www.example.com" ) );
425 $this->assertEquals( "name=value", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
426
427 $c = new MockCookie( "name", "value",
428 array(
429 "domain" => "www.example.com",
430 "path" => "/path/",
431 ) );
432 $this->assertFalse( $c->canServeDomain( "example.com" ) );
433 $this->assertFalse( $c->canServeDomain( "www.example.net" ) );
434 $this->assertTrue( $c->canServeDomain( "www.example.com" ) );
435
436 $c = new MockCookie( "name", "value",
437 array(
438 "domain" => ".example.com",
439 "path" => "/path/",
440 "expires" => "-1 day",
441 ) );
442 $this->assertFalse( $c->isUnExpired() );
443 $this->assertEquals( "", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
444
445 $c = new MockCookie( "name", "value",
446 array(
447 "domain" => ".example.com",
448 "path" => "/path/",
449 "expires" => "+1 day",
450 ) );
451 $this->assertTrue( $c->isUnExpired() );
452 $this->assertEquals( "name=value", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
453 }
454
455 function testCookieJarSetCookie() {
456 $cj = new CookieJar;
457 $cj->setCookie( "name", "value",
458 array(
459 "domain" => ".example.com",
460 "path" => "/path/",
461 ) );
462 $cj->setCookie( "name2", "value",
463 array(
464 "domain" => ".example.com",
465 "path" => "/path/sub",
466 ) );
467 $cj->setCookie( "name3", "value",
468 array(
469 "domain" => ".example.com",
470 "path" => "/",
471 ) );
472 $cj->setCookie( "name4", "value",
473 array(
474 "domain" => ".example.net",
475 "path" => "/path/",
476 ) );
477 $cj->setCookie( "name5", "value",
478 array(
479 "domain" => ".example.net",
480 "path" => "/path/",
481 "expires" => "-1 day",
482 ) );
483
484 $this->assertEquals( "name4=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
485 $this->assertEquals( "name3=value", $cj->serializeToHttpRequest( "/", "www.example.com" ) );
486 $this->assertEquals( "name=value; name3=value", $cj->serializeToHttpRequest( "/path/", "www.example.com" ) );
487
488 $cj->setCookie( "name5", "value",
489 array(
490 "domain" => ".example.net",
491 "path" => "/path/",
492 "expires" => "+1 day",
493 ) );
494 $this->assertEquals( "name4=value; name5=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
495
496 $cj->setCookie( "name4", "value",
497 array(
498 "domain" => ".example.net",
499 "path" => "/path/",
500 "expires" => "-1 day",
501 ) );
502 $this->assertEquals( "name5=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
503 }
504
505 function testParseResponseHeader() {
506 $cj = new CookieJar;
507
508 $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
509 $cj->parseCookieResponseHeader( $h[0], "www.example.com" );
510 $this->assertEquals( "name4=value", $cj->serializeToHttpRequest( "/", "www.example.com" ) );
511
512 $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
513 $cj->parseCookieResponseHeader( $h[1], "www.example.com" );
514 $this->assertEquals( "", $cj->serializeToHttpRequest( "/", "www.example.com" ) );
515 $this->assertEquals( "name4=value2", $cj->serializeToHttpRequest( "/path/", "www.example.com" ) );
516
517 $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
518 $cj->parseCookieResponseHeader( $h[2], "www.example.com" );
519 $this->assertEquals( "name4=value2; name5=value3", $cj->serializeToHttpRequest( "/path/", "www.example.com" ) );
520
521 $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
522 $cj->parseCookieResponseHeader( $h[3], "www.example.com" );
523 $this->assertEquals( "", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
524
525 $h[] = "name6=value0; domain=.example.net; path=/path/; expires=Mon, 09-Dec-1999 13:46:00 GMT";
526 $cj->parseCookieResponseHeader( $h[4], "www.example.net" );
527 $this->assertEquals( "", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
528
529 $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
530 $cj->parseCookieResponseHeader( $h[5], "www.example.net" );
531 $this->assertEquals( "name6=value4", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
532 }
533
534 function runCookieRequests() {
535 $r = MWHttpRequest::factory( "http://www.php.net/manual", array( 'followRedirects' => true ) );
536 $r->execute();
537
538 $jar = $r->getCookieJar();
539 $this->assertThat( $jar, $this->isInstanceOf( 'CookieJar' ) );
540
541 if ( $r instanceof PhpHttpRequest && version_compare( '5.1.7', phpversion(), '>' ) ) {
542 $this->markTestSkipped( 'Redirection fails or crashes PHP on 5.1.6 and prior' );
543 }
544 $serialized = $jar->serializeToHttpRequest( "/search?q=test", "www.php.net" );
545 $this->assertRegExp( '/\bCOUNTRY=[^=;]+/', $serialized );
546 $this->assertRegExp( '/\bLAST_LANG=[^=;]+/', $serialized );
547 $this->assertEquals( '', $jar->serializeToHttpRequest( "/search?q=test", "www.php.com" ) );
548 }
549
550 function testCookieRequestDefault() {
551 Http::$httpEngine = false;
552 $this->runCookieRequests();
553 }
554 function testCookieRequestPhp() {
555 if ( !self::$has_fopen ) {
556 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
557 }
558
559 Http::$httpEngine = 'php';
560 $this->runCookieRequests();
561 }
562 function testCookieRequestCurl() {
563 if ( !self::$has_curl ) {
564 $this->markTestIncomplete( "This test requires curl." );
565 }
566
567 Http::$httpEngine = 'curl';
568 $this->runCookieRequests();
569 }
570 }