follow up r61357
[lhc/web/wiklou.git] / tests / HttpTest.php
1 <?php
2
3 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
4 require_once( 'bootstrap.php' );
5 }
6
7 class HttpTest extends PhpUnit_Framework_TestCase {
8 static $content;
9 static $headers;
10 static $has_curl;
11 static $has_proxy = false;
12 static $proxy = "http://hulk:8080/";
13 var $test_geturl = array(
14 "http://www.example.com/",
15 "http://pecl.php.net/feeds/pkg_apc.rss",
16 "http://toolserver.org/~jan/poll/dev/main.php?page=wiki_output&id=3",
17 "http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw",
18 "http://www.mediawiki.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:MediaWiki_hooks&format=php",
19 );
20 var $test_requesturl = array( "http://en.wikipedia.org/wiki/Special:Export/User:MarkAHershberger" );
21
22 var $test_posturl = array( "http://www.comp.leeds.ac.uk/cgi-bin/Perl/environment-example" => "review=test" );
23
24 function setup() {
25 putenv("http_proxy"); /* Remove any proxy env var, so curl doesn't get confused */
26 if ( is_array( self::$content ) ) {
27 return;
28 }
29 self::$has_curl = function_exists( 'curl_init' );
30
31 if ( !file_exists("/usr/bin/curl") ) {
32 $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.");
33 }
34
35 $content = tempnam( sys_get_temp_dir(), "" );
36 $headers = tempnam( sys_get_temp_dir(), "" );
37 if ( !$content && !$headers ) {
38 die( "Couldn't create temp file!" );
39 }
40
41 // This probably isn't the best test for a proxy, but it works on my system!
42 system("curl -0 -o $content -s ".self::$proxy);
43 $out = file_get_contents( $content );
44 if( $out ) {
45 self::$has_proxy = true;
46 }
47
48 /* Maybe use wget instead of curl here ... just to use a different codebase? */
49 foreach ( $this->test_geturl as $u ) {
50 system( "curl -0 -s -D $headers '$u' -o $content" );
51 self::$content["GET $u"] = file_get_contents( $content );
52 self::$headers["GET $u"] = file_get_contents( $headers );
53 }
54 foreach ( $this->test_requesturl as $u ) {
55 system( "curl -0 -s -X POST -H 'Content-Length: 0' -D $headers '$u' -o $content" );
56 self::$content["POST $u"] = file_get_contents( $content );
57 self::$headers["POST $u"] = file_get_contents( $headers );
58 }
59 foreach ( $this->test_posturl as $u => $postData ) {
60 system( "curl -0 -s -X POST -d '$postData' -D $headers '$u' -o $content" );
61 self::$content["POST $u => $postData"] = file_get_contents( $content );
62 self::$headers["POST $u => $postData"] = file_get_contents( $headers );
63 }
64 unlink( $content );
65 unlink( $headers );
66 }
67
68
69 function testInstantiation() {
70 Http::$httpEngine = false;
71
72 $r = HttpRequest::factory("http://www.example.com/");
73 if ( self::$has_curl ) {
74 $this->assertThat($r, $this->isInstanceOf( 'CurlHttpRequest' ));
75 } else {
76 $this->assertThat($r, $this->isInstanceOf( 'PhpHttpRequest' ));
77 }
78 unset($r);
79
80 Http::$httpEngine = 'php';
81 $r = HttpRequest::factory("http://www.example.com/");
82 $this->assertThat($r, $this->isInstanceOf( 'PhpHttpRequest' ));
83 unset($r);
84
85 if( !self::$has_curl ) {
86 $this->setExpectedException( 'MWException' );
87 }
88 Http::$httpEngine = 'curl';
89 $r = HttpRequest::factory("http://www.example.com/");
90 if( self::$has_curl ) {
91 $this->assertThat($r, $this->isInstanceOf( 'CurlHttpRequest' ));
92 }
93 }
94
95 function runHTTPFailureChecks() {
96 // Each of the following requests should result in a failure.
97
98 $timeout = 1;
99 $start_time = time();
100 $r = HTTP::get( "http://www.example.com:1/", $timeout);
101 $end_time = time();
102 $this->assertLessThan($timeout+2, $end_time - $start_time,
103 "Request took less than {$timeout}s via ".Http::$httpEngine);
104 $this->assertEquals($r, false, "false -- what we get on error from Http::get()");
105 }
106
107 function testFailureDefault() {
108 Http::$httpEngine = false;
109 self::runHTTPFailureChecks();
110 }
111
112 function testFailurePhp() {
113 Http::$httpEngine = "php";
114 self::runHTTPFailureChecks();
115 }
116
117 function testFailureCurl() {
118 if (!self::$has_curl ) {
119 $this->markTestIncomplete("This test requires curl.");
120 }
121
122 Http::$httpEngine = "curl";
123 self::runHTTPFailureChecks();
124 }
125
126 /* ./phase3/includes/Import.php:1108: $data = Http::request( $method, $url ); */
127 /* ./includes/Import.php:1124: $link = Title::newFromText( "$interwiki:Special:Export/$page" ); */
128 /* ./includes/Import.php:1134: return ImportStreamSource::newFromURL( $url, "POST" ); */
129 function runHTTPRequests($proxy=null) {
130 $opt = array();
131
132 if($proxy) {
133 $opt['proxy'] = $proxy;
134 }
135
136 /* no postData here because the only request I could find in code so far didn't have any */
137 foreach ( $this->test_requesturl as $u ) {
138 $r = Http::request( "POST", $u, $opt );
139 $this->assertEquals( self::$content["POST $u"], "$r", "POST $u with ".Http::$httpEngine );
140 }
141 }
142
143 function testRequestDefault() {
144 Http::$httpEngine = false;
145 self::runHTTPRequests();
146 }
147
148 function testRequestPhp() {
149 Http::$httpEngine = "php";
150 self::runHTTPRequests();
151 }
152
153 function testRequestCurl() {
154 if (!self::$has_curl ) {
155 $this->markTestIncomplete("This test requires curl.");
156 }
157
158 Http::$httpEngine = "curl";
159 self::runHTTPRequests();
160 }
161
162 /* ./extensions/SpamBlacklist/SpamBlacklist_body.php:164: $httpText = Http::get( $fileName ); */
163 /* ./extensions/ApiSVGProxy/ApiSVGProxy.body.php:44: $contents = Http::get( $file->getFullUrl() ); */
164 /* ./extensions/BookInformation/drivers/IsbnDb.php:24: if( ( $xml = Http::get( $uri ) ) !== false ) { */
165 /* ./extensions/BookInformation/drivers/Amazon.php:23: if( ( $xml = Http::get( $uri ) ) !== false ) { */
166 /* ./extensions/TitleBlacklist/TitleBlacklist.list.php:217: $result = Http::get( $url ); */
167 /* ./extensions/TSPoll/TSPoll.php:68: $get_server = Http::get( 'http://toolserver.org/~jan/poll/dev/main.php?page=wiki_output&id='.$id ); */
168 /* ./extensions/TSPoll/TSPoll.php:70: $get_server = Http::get( 'http://toolserver.org/~jan/poll/main.php?page=wiki_output&id='.$id ); */
169 /* ./extensions/DoubleWiki/DoubleWiki.php:56: $translation = Http::get( $url.$sep.'action=render' ); */
170 /* ./extensions/ExternalPages/ExternalPages_body.php:177: $serializedText = Http::get( $this->mPageURL ); */
171 /* ./extensions/Translate/utils/TranslationHelpers.php:143: $suggestions = Http::get( $url, $timeout ); */
172 /* ./extensions/Translate/SpecialImportTranslations.php:169: $filedata = Http::get( $url ); ; */
173 /* ./extensions/Translate/TranslateEditAddons.php:338: $suggestions = Http::get( $url, $timeout ); */
174 /* ./extensions/SecurePoll/includes/user/Auth.php:283: $value = Http::get( $url, 20, $curlParams ); */
175 /* ./extensions/DumpHTML/dumpHTML.inc:778: $contents = Http::get( $url ); */
176 /* ./extensions/DumpHTML/dumpHTML.inc:1298: $contents = Http::get( $sourceUrl ); */
177 /* ./extensions/DumpHTML/dumpHTML.inc:1373: $contents = Http::get( $sourceUrl ); */
178 /* ./phase3/maintenance/rebuildInterwiki.inc:101: $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 ); */
179 /* ./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' ); */
180 /* ./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' ); */
181 /* ./phase3/maintenance/dumpInterwiki.inc:95: $intermap = Http::get( 'http://meta.wikimedia.org/w/index.php?title=Interwiki_map&action=raw', 30 ); */
182 /* ./phase3/includes/parser/Parser.php:3204: $text = Http::get($url); */
183 /* ./phase3/includes/filerepo/ForeignAPIRepo.php:131: $data = Http::get( $url ); */
184 /* ./phase3/includes/filerepo/ForeignAPIRepo.php:205: $thumb = Http::get( $foreignUrl ); */
185 /* ./phase3/includes/filerepo/File.php:1105: $res = Http::get( $renderUrl ); */
186 /* ./phase3/includes/GlobalFunctions.php:2760: * @deprecated Use Http::get() instead */
187 /* ./phase3/includes/GlobalFunctions.php:2764: return Http::get( $url ); */
188 /* ./phase3/includes/ExternalStoreHttp.php:18: $ret = Http::get( $url ); */
189 /* ./phase3/includes/Import.php:357: $data = Http::get( $src ); */
190 /* ./extensions/ExternalData/ED_Utils.php:291: return Http::get( $url, 'default', array(CURLOPT_SSL_VERIFYPEER => false) ); */
191 /* ./extensions/ExternalData/ED_Utils.php:293: return Http::get( $url ); */
192 /* ./extensions/ExternalData/ED_Utils.php:306: $page = Http::get( $url, 'default', array(CURLOPT_SSL_VERIFYPEER => false) ); */
193 /* ./extensions/ExternalData/ED_Utils.php:308: $page = Http::get( $url ); */
194 /* ./extensions/CodeReview/backend/Subversion.php:320: $blob = Http::get( $target, $this->mTimeout ); */
195 /* ./extensions/AmazonPlus/AmazonPlus.php:214: $this->response = Http::get( $urlstr ); */
196 /* ./extensions/StaticWiki/StaticWiki.php:24: $text = Http::get( $url ) ; */
197 /* ./extensions/StaticWiki/StaticWiki.php:64: $history = Http::get ( $wgStaticWikiExternalSite . "index.php?title=" . urlencode ( $url_title ) . "&action=history" ) ; */
198 /* ./extensions/Configure/scripts/findSettings.php:126: $cont = Http::get( "http://www.mediawiki.org/w/index.php?title={$page}&action=raw" ); */
199 /* ./extensions/TorBlock/TorBlock.class.php:148: $data = Http::get( $url ); */
200 /* ./extensions/HoneypotIntegration/HoneypotIntegration.class.php:60: $data = Http::get( $wgHoneypotURLSource, 'default', */
201 /* ./extensions/SemanticForms/includes/SF_Utils.inc:378: $page_contents = Http::get($url); */
202 /* ./extensions/LocalisationUpdate/LocalisationUpdate.class.php:172: $basefilecontents = Http::get( $basefile ); */
203 /* ./extensions/APC/SpecialAPC.php:245: $rss = Http::get( 'http://pecl.php.net/feeds/pkg_apc.rss' ); */
204 /* ./extensions/Interlanguage/Interlanguage.php:56: $a = Http::get( $url ); */
205 /* ./extensions/MWSearch/MWSearch_body.php:492: $data = Http::get( $searchUrl, $wgLuceneSearchTimeout, $httpOpts); */
206 function runHTTPGets($proxy=null) {
207 $opt = array();
208
209 if($proxy) {
210 $opt['proxy'] = $proxy;
211 }
212
213 foreach ( $this->test_geturl as $u ) {
214 $r = Http::get( $u, 30, $opt ); /* timeout of 30s */
215 $this->assertEquals( self::$content["GET $u"], "$r", "Get $u with ".Http::$httpEngine );
216 }
217 }
218
219 function testGetDefault() {
220 Http::$httpEngine = false;
221 self::runHTTPGets();
222 }
223
224 function testGetPhp() {
225 Http::$httpEngine = "php";
226 self::runHTTPGets();
227 }
228
229 function testGetCurl() {
230 if (!self::$has_curl ) {
231 $this->markTestIncomplete("This test requires curl.");
232 }
233
234 Http::$httpEngine = "curl";
235 self::runHTTPGets();
236 }
237
238 /* ./phase3/maintenance/parserTests.inc:1618: return Http::post( $url, array( 'postData' => wfArrayToCGI( $data ) ) ); */
239 function runHTTPPosts($proxy=null) {
240 $opt = array();
241
242 if($proxy) {
243 $opt['proxy'] = $proxy;
244 }
245
246 foreach ( $this->test_posturl as $u => $postData ) {
247 $opt['postData'] = $postData;
248 $r = Http::post( $u, $opt );
249 $this->assertEquals( self::$content["POST $u => $postData"], "$r",
250 "POST $u (postData=$postData) with ".Http::$httpEngine );
251 }
252 }
253
254 function testPostDefault() {
255 Http::$httpEngine = false;
256 self::runHTTPPosts();
257 }
258
259 function testPostPhp() {
260 Http::$httpEngine = "php";
261 self::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 self::runHTTPPosts();
271 }
272
273 function runProxyRequests() {
274 if(!self::$has_proxy) {
275 $this->markTestIncomplete("This test requires a proxy.");
276 }
277 self::runHTTPGets(self::$proxy);
278 self::runHTTPPosts(self::$proxy);
279 self::runHTTPRequests(self::$proxy);
280 }
281
282 function testProxyDefault() {
283 Http::$httpEngine = false;
284 self::runProxyRequests();
285 }
286
287 function testProxyPhp() {
288 Http::$httpEngine = 'php';
289 self::runProxyRequests();
290 }
291
292 function testProxyCurl() {
293 if (!self::$has_curl ) {
294 $this->markTestIncomplete("This test requires curl.");
295 }
296
297 Http::$httpEngine = 'curl';
298 self::runProxyRequests();
299 }
300
301 function testIsLocalUrl() {
302 }
303
304 /* ./extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php:559: $user_agent = Http::userAgent(); */
305 function testUserAgent() {
306 }
307
308 function testIsValidUrl() {
309 }
310
311 }