f3df5ab34c377b891b0a5005f9c5b95aa41178a9
[lhc/web/wiklou.git] / maintenance / tests / 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 class HttpTest extends PhpUnit_Framework_TestCase {
10 static $content;
11 static $headers;
12 static $has_curl;
13 static $has_fopen;
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",
22 );
23 var $test_requesturl = array( "http://en.wikipedia.org/wiki/Special:Export/User:MarkAHershberger" );
24
25 var $test_posturl = array( "http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example" => "review=test" );
26
27 function setup() {
28 putenv( "http_proxy" ); /* Remove any proxy env var, so curl doesn't get confused */
29 if ( is_array( self::$content ) ) {
30 return;
31 }
32 self::$has_curl = function_exists( 'curl_init' );
33 self::$has_fopen = wfIniGetBool( 'allow_url_fopen' );
34
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." );
37 }
38
39 $content = tempnam( wfTempDir(), "" );
40 $headers = tempnam( wfTempDir(), "" );
41 if ( !$content && !$headers ) {
42 die( "Couldn't create temp file!" );
43 }
44
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 );
48 if ( $out ) {
49 self::$has_proxy = true;
50 }
51
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 );
57 }
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 );
62 }
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 );
67 }
68 unlink( $content );
69 unlink( $headers );
70 }
71
72
73 function testInstantiation() {
74 Http::$httpEngine = false;
75
76 $r = HttpRequest::factory( "http://www.example.com/" );
77 if ( self::$has_curl ) {
78 $this->assertThat( $r, $this->isInstanceOf( 'CurlHttpRequest' ) );
79 } else {
80 $this->assertThat( $r, $this->isInstanceOf( 'PhpHttpRequest' ) );
81 }
82 unset( $r );
83
84 if ( !self::$has_fopen ) {
85 $this->setExpectedException( 'MWException' );
86 }
87 Http::$httpEngine = 'php';
88 $r = HttpRequest::factory( "http://www.example.com/" );
89 $this->assertThat( $r, $this->isInstanceOf( 'PhpHttpRequest' ) );
90 unset( $r );
91
92 if ( !self::$has_curl ) {
93 $this->setExpectedException( 'MWException' );
94 }
95 Http::$httpEngine = 'curl';
96 $r = HttpRequest::factory( "http://www.example.com/" );
97 if ( self::$has_curl ) {
98 $this->assertThat( $r, $this->isInstanceOf( 'CurlHttpRequest' ) );
99 }
100 }
101
102 function runHTTPFailureChecks() {
103 // Each of the following requests should result in a failure.
104
105 $timeout = 1;
106 $start_time = time();
107 $r = Http::get( "http://www.example.com:1/", $timeout );
108 $end_time = time();
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()" );
112
113 $r = Http::get( "http://www.example.com/this-file-does-not-exist", $timeout );
114 $this->assertFalse( $r, "False on 404s" );
115
116
117 $r = HttpRequest::factory( "http://www.example.com/this-file-does-not-exist" );
118 $er = $r->execute();
119 if ( is_a( $r, 'PhpHttpRequest' ) && version_compare( '5.2.10', phpversion(), '>' ) ) {
120 $this->assertRegexp( "/HTTP request failed/", $er->getWikiText() );
121 } else {
122 $this->assertRegexp( "/404 Not Found/", $er->getWikiText() );
123 }
124 }
125
126 function testFailureDefault() {
127 Http::$httpEngine = false;
128 $this->runHTTPFailureChecks();
129 }
130
131 function testFailurePhp() {
132 if ( !self::$has_fopen ) {
133 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
134 }
135
136 Http::$httpEngine = "php";
137 $this->runHTTPFailureChecks();
138 }
139
140 function testFailureCurl() {
141 if ( !self::$has_curl ) {
142 $this->markTestIncomplete( "This test requires curl." );
143 }
144
145 Http::$httpEngine = "curl";
146 $this->runHTTPFailureChecks();
147 }
148
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 ) {
153 $opt = array();
154
155 if ( $proxy ) {
156 $opt['proxy'] = $proxy;
157 } elseif ( $proxy === false ) {
158 $opt['noProxy'] = true;
159 }
160
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 );
165 }
166 }
167
168 function testRequestDefault() {
169 Http::$httpEngine = false;
170 $this->runHTTPRequests();
171 }
172
173 function testRequestPhp() {
174 if ( !self::$has_fopen ) {
175 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
176 }
177
178 Http::$httpEngine = "php";
179 $this->runHTTPRequests();
180 }
181
182 function testRequestCurl() {
183 if ( !self::$has_curl ) {
184 $this->markTestIncomplete( "This test requires curl." );
185 }
186
187 Http::$httpEngine = "curl";
188 $this->runHTTPRequests();
189 }
190
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 ) {
236 $opt = array();
237
238 if ( $proxy ) {
239 $opt['proxy'] = $proxy;
240 } elseif ( $proxy === false ) {
241 $opt['noProxy'] = true;
242 }
243
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 );
247 }
248 }
249
250 function testGetDefault() {
251 Http::$httpEngine = false;
252 $this->runHTTPGets();
253 }
254
255 function testGetPhp() {
256 if ( !self::$has_fopen ) {
257 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
258 }
259
260 Http::$httpEngine = "php";
261 $this->runHTTPGets();
262 }
263
264 function testGetCurl() {
265 if ( !self::$has_curl ) {
266 $this->markTestIncomplete( "This test requires curl." );
267 }
268
269 Http::$httpEngine = "curl";
270 $this->runHTTPGets();
271 }
272
273 /* ./phase3/maintenance/parserTests.inc:1618: return Http::post( $url, array( 'postData' => wfArrayToCGI( $data ) ) ); */
274 function runHTTPPosts( $proxy = null ) {
275 $opt = array();
276
277 if ( $proxy ) {
278 $opt['proxy'] = $proxy;
279 } elseif ( $proxy === false ) {
280 $opt['noProxy'] = true;
281 }
282
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 );
288 }
289 }
290
291 function testPostDefault() {
292 Http::$httpEngine = false;
293 $this->runHTTPPosts();
294 }
295
296 function testPostPhp() {
297 if ( !self::$has_fopen ) {
298 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
299 }
300
301 Http::$httpEngine = "php";
302 $this->runHTTPPosts();
303 }
304
305 function testPostCurl() {
306 if ( !self::$has_curl ) {
307 $this->markTestIncomplete( "This test requires curl." );
308 }
309
310 Http::$httpEngine = "curl";
311 $this->runHTTPPosts();
312 }
313
314 function runProxyRequests() {
315 if ( !self::$has_proxy ) {
316 $this->markTestIncomplete( "This test requires a proxy." );
317 }
318 $this->runHTTPGets( self::$proxy );
319 $this->runHTTPPosts( self::$proxy );
320 $this->runHTTPRequests( self::$proxy );
321
322 // Set false here to do noProxy
323 $this->runHTTPGets( false );
324 $this->runHTTPPosts( false );
325 $this->runHTTPRequests( false );
326 }
327
328 function testProxyDefault() {
329 Http::$httpEngine = false;
330 $this->runProxyRequests();
331 }
332
333 function testProxyPhp() {
334 if ( !self::$has_fopen ) {
335 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
336 }
337
338 Http::$httpEngine = 'php';
339 $this->runProxyRequests();
340 }
341
342 function testProxyCurl() {
343 if ( !self::$has_curl ) {
344 $this->markTestIncomplete( "This test requires curl." );
345 }
346
347 Http::$httpEngine = 'curl';
348 $this->runProxyRequests();
349 }
350
351 function testIsLocalUrl() {
352 }
353
354 /* ./extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php:559: $user_agent = Http::userAgent(); */
355 function testUserAgent() {
356 }
357
358 function testIsValidUrl() {
359 }
360
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" ) );
377
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" ) );
382
383
384 }
385
386 function testSetCooke() {
387 $c = new MockCookie( "name", "value",
388 array(
389 "domain" => "ac.th",
390 "path" => "/path/",
391 ) );
392 $this->assertFalse( $c->canServeDomain( "ac.th" ) );
393
394 $c = new MockCookie( "name", "value",
395 array(
396 "domain" => "example.com",
397 "path" => "/path/",
398 ) );
399
400 $this->assertTrue( $c->canServeDomain( "example.com" ) );
401 $this->assertFalse( $c->canServeDomain( "www.example.com" ) );
402
403 $c = new MockCookie( "name", "value",
404 array(
405 "domain" => ".example.com",
406 "path" => "/path/",
407 ) );
408
409 $this->assertFalse( $c->canServeDomain( "www.example.net" ) );
410 $this->assertFalse( $c->canServeDomain( "example.com" ) );
411 $this->assertTrue( $c->canServeDomain( "www.example.com" ) );
412
413 $this->assertFalse( $c->canServePath( "/" ) );
414 $this->assertFalse( $c->canServePath( "/bogus/path/" ) );
415 $this->assertFalse( $c->canServePath( "/path" ) );
416 $this->assertTrue( $c->canServePath( "/path/" ) );
417
418 $this->assertTrue( $c->isUnExpired() );
419
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" ) );
423
424 $c = new MockCookie( "name", "value",
425 array(
426 "domain" => "www.example.com",
427 "path" => "/path/",
428 ) );
429 $this->assertFalse( $c->canServeDomain( "example.com" ) );
430 $this->assertFalse( $c->canServeDomain( "www.example.net" ) );
431 $this->assertTrue( $c->canServeDomain( "www.example.com" ) );
432
433 $c = new MockCookie( "name", "value",
434 array(
435 "domain" => ".example.com",
436 "path" => "/path/",
437 "expires" => "-1 day",
438 ) );
439 $this->assertFalse( $c->isUnExpired() );
440 $this->assertEquals( "", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
441
442 $c = new MockCookie( "name", "value",
443 array(
444 "domain" => ".example.com",
445 "path" => "/path/",
446 "expires" => "+1 day",
447 ) );
448 $this->assertTrue( $c->isUnExpired() );
449 $this->assertEquals( "name=value", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
450 }
451
452 function testCookieJarSetCookie() {
453 $cj = new CookieJar;
454 $cj->setCookie( "name", "value",
455 array(
456 "domain" => ".example.com",
457 "path" => "/path/",
458 ) );
459 $cj->setCookie( "name2", "value",
460 array(
461 "domain" => ".example.com",
462 "path" => "/path/sub",
463 ) );
464 $cj->setCookie( "name3", "value",
465 array(
466 "domain" => ".example.com",
467 "path" => "/",
468 ) );
469 $cj->setCookie( "name4", "value",
470 array(
471 "domain" => ".example.net",
472 "path" => "/path/",
473 ) );
474 $cj->setCookie( "name5", "value",
475 array(
476 "domain" => ".example.net",
477 "path" => "/path/",
478 "expires" => "-1 day",
479 ) );
480
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" ) );
484
485 $cj->setCookie( "name5", "value",
486 array(
487 "domain" => ".example.net",
488 "path" => "/path/",
489 "expires" => "+1 day",
490 ) );
491 $this->assertEquals( "name4=value; name5=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
492
493 $cj->setCookie( "name4", "value",
494 array(
495 "domain" => ".example.net",
496 "path" => "/path/",
497 "expires" => "-1 day",
498 ) );
499 $this->assertEquals( "name5=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
500 }
501
502 function testParseResponseHeader() {
503 $cj = new CookieJar;
504
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" ) );
508
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" ) );
513
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" ) );
517
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" ) );
521
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" ) );
525
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" ) );
529 }
530
531 function runCookieRequests() {
532 $r = HttpRequest::factory( "http://www.php.net/manual", array( 'followRedirects' => true ) );
533 $r->execute();
534
535 $jar = $r->getCookieJar();
536 $this->assertThat( $jar, $this->isInstanceOf( 'CookieJar' ) );
537
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' );
540 }
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" ) );
545 }
546
547 function testCookieRequestDefault() {
548 Http::$httpEngine = false;
549 $this->runCookieRequests();
550 }
551 function testCookieRequestPhp() {
552 if ( !self::$has_fopen ) {
553 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
554 }
555
556 Http::$httpEngine = 'php';
557 $this->runCookieRequests();
558 }
559 function testCookieRequestCurl() {
560 if ( !self::$has_curl ) {
561 $this->markTestIncomplete( "This test requires curl." );
562 }
563
564 Http::$httpEngine = 'curl';
565 $this->runCookieRequests();
566 }
567 }