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