a48c7bb4f4996a160c615de4726770c7dafbc3fe
[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 function runHTTPGets( $proxy = null ) {
195 $opt = array();
196
197 if ( $proxy ) {
198 $opt['proxy'] = $proxy;
199 } elseif ( $proxy === false ) {
200 $opt['noProxy'] = true;
201 }
202
203 foreach ( $this->test_geturl as $u ) {
204 $r = Http::get( $u, 30, $opt ); /* timeout of 30s */
205 $this->assertEquals( self::$content["GET $u"], "$r", "Get $u with " . Http::$httpEngine );
206 }
207 }
208
209 function testGetDefault() {
210 Http::$httpEngine = false;
211 $this->runHTTPGets();
212 }
213
214 function testGetPhp() {
215 if ( !self::$has_fopen ) {
216 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
217 }
218
219 Http::$httpEngine = "php";
220 $this->runHTTPGets();
221 }
222
223 function testGetCurl() {
224 if ( !self::$has_curl ) {
225 $this->markTestIncomplete( "This test requires curl." );
226 }
227
228 Http::$httpEngine = "curl";
229 $this->runHTTPGets();
230 }
231
232 /* ./phase3/maintenance/parserTests.inc:1618: return Http::post( $url, array( 'postData' => wfArrayToCGI( $data ) ) ); */
233 function runHTTPPosts( $proxy = null ) {
234 $opt = array();
235
236 if ( $proxy ) {
237 $opt['proxy'] = $proxy;
238 } elseif ( $proxy === false ) {
239 $opt['noProxy'] = true;
240 }
241
242 foreach ( $this->test_posturl as $u => $postData ) {
243 $opt['postData'] = $postData;
244 $r = Http::post( $u, $opt );
245 $this->assertEquals( self::$content["POST $u => $postData"], "$r",
246 "POST $u (postData=$postData) with " . Http::$httpEngine );
247 }
248 }
249
250 function testPostDefault() {
251 Http::$httpEngine = false;
252 $this->runHTTPPosts();
253 }
254
255 function testPostPhp() {
256 if ( !self::$has_fopen ) {
257 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
258 }
259
260 Http::$httpEngine = "php";
261 $this->runHTTPPosts();
262 }
263
264 function testPostCurl() {
265 if ( !self::$has_curl ) {
266 $this->markTestIncomplete( "This test requires curl." );
267 }
268
269 Http::$httpEngine = "curl";
270 $this->runHTTPPosts();
271 }
272
273 function runProxyRequests() {
274 if ( !self::$has_proxy ) {
275 $this->markTestIncomplete( "This test requires a proxy." );
276 }
277 $this->runHTTPGets( self::$proxy );
278 $this->runHTTPPosts( self::$proxy );
279 $this->runHTTPRequests( self::$proxy );
280
281 // Set false here to do noProxy
282 $this->runHTTPGets( false );
283 $this->runHTTPPosts( false );
284 $this->runHTTPRequests( false );
285 }
286
287 function testProxyDefault() {
288 Http::$httpEngine = false;
289 $this->runProxyRequests();
290 }
291
292 function testProxyPhp() {
293 if ( !self::$has_fopen ) {
294 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
295 }
296
297 Http::$httpEngine = 'php';
298 $this->runProxyRequests();
299 }
300
301 function testProxyCurl() {
302 if ( !self::$has_curl ) {
303 $this->markTestIncomplete( "This test requires curl." );
304 }
305
306 Http::$httpEngine = 'curl';
307 $this->runProxyRequests();
308 }
309
310 function testIsLocalUrl() {
311 }
312
313 /* ./extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php:559: $user_agent = Http::userAgent(); */
314 function testUserAgent() {
315 }
316
317 function testIsValidUrl() {
318 }
319
320 function testValidateCookieDomain() {
321 $this->assertFalse( Cookie::validateCookieDomain( "co.uk" ) );
322 $this->assertFalse( Cookie::validateCookieDomain( ".co.uk" ) );
323 $this->assertFalse( Cookie::validateCookieDomain( "gov.uk" ) );
324 $this->assertFalse( Cookie::validateCookieDomain( ".gov.uk" ) );
325 $this->assertTrue( Cookie::validateCookieDomain( "supermarket.uk" ) );
326 $this->assertFalse( Cookie::validateCookieDomain( "uk" ) );
327 $this->assertFalse( Cookie::validateCookieDomain( ".uk" ) );
328 $this->assertFalse( Cookie::validateCookieDomain( "127.0.0." ) );
329 $this->assertFalse( Cookie::validateCookieDomain( "127." ) );
330 $this->assertFalse( Cookie::validateCookieDomain( "127.0.0.1." ) );
331 $this->assertTrue( Cookie::validateCookieDomain( "127.0.0.1" ) );
332 $this->assertFalse( Cookie::validateCookieDomain( "333.0.0.1" ) );
333 $this->assertTrue( Cookie::validateCookieDomain( "example.com" ) );
334 $this->assertFalse( Cookie::validateCookieDomain( "example.com." ) );
335 $this->assertTrue( Cookie::validateCookieDomain( ".example.com" ) );
336
337 $this->assertTrue( Cookie::validateCookieDomain( ".example.com", "www.example.com" ) );
338 $this->assertFalse( Cookie::validateCookieDomain( "example.com", "www.example.com" ) );
339 $this->assertTrue( Cookie::validateCookieDomain( "127.0.0.1", "127.0.0.1" ) );
340 $this->assertFalse( Cookie::validateCookieDomain( "127.0.0.1", "localhost" ) );
341
342
343 }
344
345 function testSetCooke() {
346 $c = new MockCookie( "name", "value",
347 array(
348 "domain" => "ac.th",
349 "path" => "/path/",
350 ) );
351 $this->assertFalse( $c->canServeDomain( "ac.th" ) );
352
353 $c = new MockCookie( "name", "value",
354 array(
355 "domain" => "example.com",
356 "path" => "/path/",
357 ) );
358
359 $this->assertTrue( $c->canServeDomain( "example.com" ) );
360 $this->assertFalse( $c->canServeDomain( "www.example.com" ) );
361
362 $c = new MockCookie( "name", "value",
363 array(
364 "domain" => ".example.com",
365 "path" => "/path/",
366 ) );
367
368 $this->assertFalse( $c->canServeDomain( "www.example.net" ) );
369 $this->assertFalse( $c->canServeDomain( "example.com" ) );
370 $this->assertTrue( $c->canServeDomain( "www.example.com" ) );
371
372 $this->assertFalse( $c->canServePath( "/" ) );
373 $this->assertFalse( $c->canServePath( "/bogus/path/" ) );
374 $this->assertFalse( $c->canServePath( "/path" ) );
375 $this->assertTrue( $c->canServePath( "/path/" ) );
376
377 $this->assertTrue( $c->isUnExpired() );
378
379 $this->assertEquals( "", $c->serializeToHttpRequest( "/path/", "www.example.net" ) );
380 $this->assertEquals( "", $c->serializeToHttpRequest( "/", "www.example.com" ) );
381 $this->assertEquals( "name=value", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
382
383 $c = new MockCookie( "name", "value",
384 array(
385 "domain" => "www.example.com",
386 "path" => "/path/",
387 ) );
388 $this->assertFalse( $c->canServeDomain( "example.com" ) );
389 $this->assertFalse( $c->canServeDomain( "www.example.net" ) );
390 $this->assertTrue( $c->canServeDomain( "www.example.com" ) );
391
392 $c = new MockCookie( "name", "value",
393 array(
394 "domain" => ".example.com",
395 "path" => "/path/",
396 "expires" => "-1 day",
397 ) );
398 $this->assertFalse( $c->isUnExpired() );
399 $this->assertEquals( "", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
400
401 $c = new MockCookie( "name", "value",
402 array(
403 "domain" => ".example.com",
404 "path" => "/path/",
405 "expires" => "+1 day",
406 ) );
407 $this->assertTrue( $c->isUnExpired() );
408 $this->assertEquals( "name=value", $c->serializeToHttpRequest( "/path/", "www.example.com" ) );
409 }
410
411 function testCookieJarSetCookie() {
412 $cj = new CookieJar;
413 $cj->setCookie( "name", "value",
414 array(
415 "domain" => ".example.com",
416 "path" => "/path/",
417 ) );
418 $cj->setCookie( "name2", "value",
419 array(
420 "domain" => ".example.com",
421 "path" => "/path/sub",
422 ) );
423 $cj->setCookie( "name3", "value",
424 array(
425 "domain" => ".example.com",
426 "path" => "/",
427 ) );
428 $cj->setCookie( "name4", "value",
429 array(
430 "domain" => ".example.net",
431 "path" => "/path/",
432 ) );
433 $cj->setCookie( "name5", "value",
434 array(
435 "domain" => ".example.net",
436 "path" => "/path/",
437 "expires" => "-1 day",
438 ) );
439
440 $this->assertEquals( "name4=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
441 $this->assertEquals( "name3=value", $cj->serializeToHttpRequest( "/", "www.example.com" ) );
442 $this->assertEquals( "name=value; name3=value", $cj->serializeToHttpRequest( "/path/", "www.example.com" ) );
443
444 $cj->setCookie( "name5", "value",
445 array(
446 "domain" => ".example.net",
447 "path" => "/path/",
448 "expires" => "+1 day",
449 ) );
450 $this->assertEquals( "name4=value; name5=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
451
452 $cj->setCookie( "name4", "value",
453 array(
454 "domain" => ".example.net",
455 "path" => "/path/",
456 "expires" => "-1 day",
457 ) );
458 $this->assertEquals( "name5=value", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
459 }
460
461 function testParseResponseHeader() {
462 $cj = new CookieJar;
463
464 $h[] = "Set-Cookie: name4=value; domain=.example.com; path=/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
465 $cj->parseCookieResponseHeader( $h[0], "www.example.com" );
466 $this->assertEquals( "name4=value", $cj->serializeToHttpRequest( "/", "www.example.com" ) );
467
468 $h[] = "name4=value2; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
469 $cj->parseCookieResponseHeader( $h[1], "www.example.com" );
470 $this->assertEquals( "", $cj->serializeToHttpRequest( "/", "www.example.com" ) );
471 $this->assertEquals( "name4=value2", $cj->serializeToHttpRequest( "/path/", "www.example.com" ) );
472
473 $h[] = "name5=value3; domain=.example.com; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
474 $cj->parseCookieResponseHeader( $h[2], "www.example.com" );
475 $this->assertEquals( "name4=value2; name5=value3", $cj->serializeToHttpRequest( "/path/", "www.example.com" ) );
476
477 $h[] = "name6=value3; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
478 $cj->parseCookieResponseHeader( $h[3], "www.example.com" );
479 $this->assertEquals( "", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
480
481 $h[] = "name6=value0; domain=.example.net; path=/path/; expires=Mon, 09-Dec-1999 13:46:00 GMT";
482 $cj->parseCookieResponseHeader( $h[4], "www.example.net" );
483 $this->assertEquals( "", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
484
485 $h[] = "name6=value4; domain=.example.net; path=/path/; expires=Mon, 09-Dec-2029 13:46:00 GMT";
486 $cj->parseCookieResponseHeader( $h[5], "www.example.net" );
487 $this->assertEquals( "name6=value4", $cj->serializeToHttpRequest( "/path/", "www.example.net" ) );
488 }
489
490 function runCookieRequests() {
491 $r = MWHttpRequest::factory( "http://www.php.net/manual", array( 'followRedirects' => true ) );
492 $r->execute();
493
494 $jar = $r->getCookieJar();
495 $this->assertThat( $jar, $this->isInstanceOf( 'CookieJar' ) );
496
497 if ( $r instanceof PhpHttpRequest && version_compare( '5.1.7', phpversion(), '>' ) ) {
498 $this->markTestSkipped( 'Redirection fails or crashes PHP on 5.1.6 and prior' );
499 }
500 $serialized = $jar->serializeToHttpRequest( "/search?q=test", "www.php.net" );
501 $this->assertRegExp( '/\bCOUNTRY=[^=;]+/', $serialized );
502 $this->assertRegExp( '/\bLAST_LANG=[^=;]+/', $serialized );
503 $this->assertEquals( '', $jar->serializeToHttpRequest( "/search?q=test", "www.php.com" ) );
504 }
505
506 function testCookieRequestDefault() {
507 Http::$httpEngine = false;
508 $this->runCookieRequests();
509 }
510 function testCookieRequestPhp() {
511 if ( !self::$has_fopen ) {
512 $this->markTestIncomplete( "This test requires allow_url_fopen=true." );
513 }
514
515 Http::$httpEngine = 'php';
516 $this->runCookieRequests();
517 }
518 function testCookieRequestCurl() {
519 if ( !self::$has_curl ) {
520 $this->markTestIncomplete( "This test requires curl." );
521 }
522
523 Http::$httpEngine = 'curl';
524 $this->runCookieRequests();
525 }
526 }