CurlTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http_Client
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Http_Client_CurlTest::main');
  24. }
  25. require_once dirname(__FILE__) . '/CommonHttpTests.php';
  26. require_once 'Zend/Http/Client/Adapter/Curl.php';
  27. /**
  28. * This Testsuite includes all Zend_Http_Client that require a working web
  29. * server to perform. It was designed to be extendable, so that several
  30. * test suites could be run against several servers, with different client
  31. * adapters and configurations.
  32. *
  33. * Note that $this->baseuri must point to a directory on a web server
  34. * containing all the files under the _files directory. You should symlink
  35. * or copy these files and set 'baseuri' properly.
  36. *
  37. * You can also set the proper constand in your test configuration file to
  38. * point to the right place.
  39. *
  40. * @category Zend
  41. * @package Zend_Http_Client
  42. * @subpackage UnitTests
  43. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. * @group Zend_Http
  46. * @group Zend_Http_Client
  47. */
  48. class Zend_Http_Client_CurlTest extends Zend_Http_Client_CommonHttpTests
  49. {
  50. /**
  51. * Configuration array
  52. *
  53. * @var array
  54. */
  55. protected $config = array(
  56. 'adapter' => 'Zend_Http_Client_Adapter_Curl'
  57. );
  58. public static function main()
  59. {
  60. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  61. $result = PHPUnit_TextUI_TestRunner::run($suite);
  62. }
  63. protected function setUp()
  64. {
  65. if (!extension_loaded('curl')) {
  66. $this->markTestSkipped('cURL is not installed, marking all Http Client Curl Adapter tests skipped.');
  67. }
  68. parent::setUp();
  69. }
  70. /**
  71. * Off-line common adapter tests
  72. */
  73. /**
  74. * Test that we can set a valid configuration array with some options
  75. *
  76. */
  77. public function testConfigSetAsArray()
  78. {
  79. $config = array(
  80. 'timeout' => 500,
  81. 'someoption' => 'hasvalue'
  82. );
  83. $this->_adapter->setConfig($config);
  84. $hasConfig = $this->_adapter->getConfig();
  85. foreach($config as $k => $v) {
  86. $this->assertEquals($v, $hasConfig[$k]);
  87. }
  88. }
  89. /**
  90. * Test that a Zend_Config object can be used to set configuration
  91. *
  92. * @link http://framework.zend.com/issues/browse/ZF-5577
  93. */
  94. public function testConfigSetAsZendConfig()
  95. {
  96. require_once 'Zend/Config.php';
  97. $config = new Zend_Config(array(
  98. 'timeout' => 400,
  99. 'nested' => array(
  100. 'item' => 'value',
  101. )
  102. ));
  103. $this->_adapter->setConfig($config);
  104. $hasConfig = $this->_adapter->getConfig();
  105. $this->assertEquals($config->timeout, $hasConfig['timeout']);
  106. $this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
  107. }
  108. /**
  109. * Check that an exception is thrown when trying to set invalid config
  110. *
  111. * @expectedException Zend_Http_Client_Adapter_Exception
  112. * @dataProvider invalidConfigProvider
  113. */
  114. public function testSetConfigInvalidConfig($config)
  115. {
  116. $this->_adapter->setConfig($config);
  117. }
  118. /**
  119. * CURLOPT_CLOSEPOLICY never worked and returns false on setopt always:
  120. * @link http://de2.php.net/manual/en/function.curl-setopt.php#84277
  121. *
  122. * This should throw an exception.
  123. *
  124. * @expectedException Zend_Http_Exception
  125. */
  126. public function testSettingInvalidCurlOption()
  127. {
  128. $config = array(
  129. 'adapter' => 'Zend_Http_Client_Adapter_Curl',
  130. 'curloptions' => array(CURLOPT_CLOSEPOLICY => true),
  131. );
  132. $this->client = new Zend_Http_Client($this->client->getUri(true), $config);
  133. $this->client->request('GET');
  134. $this->fail();
  135. }
  136. public function testRedirectWithGetOnly()
  137. {
  138. $this->client->setUri($this->baseuri . 'testRedirections.php');
  139. // Set some parameters
  140. $this->client->setParameterGet('swallow', 'african');
  141. // Request
  142. $res = $this->client->request('GET');
  143. $this->assertEquals(3, $this->client->getRedirectionsCount(), 'Redirection counter is not as expected');
  144. // Make sure the body does *not* contain the set parameters
  145. $this->assertNotContains('swallow', $res->getBody());
  146. $this->assertNotContains('Camelot', $res->getBody());
  147. }
  148. /**
  149. * This is a specific problem of the request type: If you let cURL handle redirects internally
  150. * but start with a POST request that sends data then the location ping-pong will lead to an
  151. * Content-Length: x\r\n GET request of the client that the server won't answer because no content is sent.
  152. *
  153. * Set CURLOPT_FOLLOWLOCATION = false for this type of request and let the Zend_Http_Client handle redirects
  154. * in his own loop.
  155. *
  156. * @expectedException Zend_Http_Client_Exception
  157. */
  158. public function testRedirectPostToGetWithCurlFollowLocationOptionLeadsToTimeout()
  159. {
  160. $adapter = new Zend_Http_Client_Adapter_Curl();
  161. $this->client->setAdapter($adapter);
  162. $adapter->setConfig(array('timeout' => 1, 'curloptions' => array(CURLOPT_FOLLOWLOCATION => true)));
  163. $this->client->setUri($this->baseuri . 'testRedirections.php');
  164. // Set some parameters
  165. $this->client->setParameterGet('swallow', 'african');
  166. $this->client->setParameterPost('Camelot', 'A silly place');
  167. $this->client->request("POST");
  168. }
  169. /**
  170. * @group ZF-3758
  171. * @link http://framework.zend.com/issues/browse/ZF-3758
  172. */
  173. public function testPutFileContentWithHttpClient()
  174. {
  175. // Method 1: Using the binary string of a file to PUT
  176. $this->client->setUri($this->baseuri . 'testRawPostData.php');
  177. $putFileContents = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  178. '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg');
  179. $this->client->setRawData($putFileContents);
  180. $this->client->request('PUT');
  181. $this->assertEquals($putFileContents, $this->client->getLastResponse()->getBody());
  182. }
  183. /**
  184. * @group ZF-3758
  185. * @link http://framework.zend.com/issues/browse/ZF-3758
  186. */
  187. public function testPutFileHandleWithHttpClient()
  188. {
  189. $this->client->setUri($this->baseuri . 'testRawPostData.php');
  190. $putFileContents = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  191. '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg');
  192. // Method 2: Using a File-Handle to the file to PUT the data
  193. $putFilePath = dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  194. '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg';
  195. $putFileHandle = fopen($putFilePath, "r");
  196. $putFileSize = filesize($putFilePath);
  197. $adapter = new Zend_Http_Client_Adapter_Curl();
  198. $this->client->setAdapter($adapter);
  199. $adapter->setConfig(array(
  200. 'curloptions' => array(CURLOPT_INFILE => $putFileHandle, CURLOPT_INFILESIZE => $putFileSize)
  201. ));
  202. $this->client->request('PUT');
  203. $this->assertEquals(gzcompress($putFileContents), gzcompress($this->client->getLastResponse()->getBody()));
  204. }
  205. public function testWritingAndNotConnectedWithCurlHandleThrowsException()
  206. {
  207. $this->setExpectedException("Zend_Http_Client_Adapter_Exception", "Trying to write but we are not connected");
  208. $adapter = new Zend_Http_Client_Adapter_Curl();
  209. $adapter->write("GET", "someUri");
  210. }
  211. public function testSetConfigIsNotArray()
  212. {
  213. $this->setExpectedException("Zend_Http_Client_Adapter_Exception");
  214. $adapter = new Zend_Http_Client_Adapter_Curl();
  215. $adapter->setConfig("foo");
  216. }
  217. public function testSetCurlOptions()
  218. {
  219. $adapter = new Zend_Http_Client_Adapter_Curl();
  220. $adapter->setCurlOption('foo', 'bar')
  221. ->setCurlOption('bar', 'baz');
  222. $this->assertEquals(
  223. array('curloptions' => array('foo' => 'bar', 'bar' => 'baz')),
  224. $this->readAttribute($adapter, '_config')
  225. );
  226. }
  227. public function testWorkWithProxyConfiguration()
  228. {
  229. $adapter = new Zend_Http_Client_Adapter_Curl();
  230. $adapter->setConfig(array(
  231. 'proxy_host' => 'localhost',
  232. 'proxy_port' => 80,
  233. 'proxy_user' => 'foo',
  234. 'proxy_pass' => 'baz',
  235. ));
  236. $expected = array(
  237. 'curloptions' => array(
  238. CURLOPT_PROXYUSERPWD => 'foo:baz',
  239. CURLOPT_PROXY => 'localhost',
  240. CURLOPT_PROXYPORT => 80,
  241. ),
  242. );
  243. $this->assertEquals(
  244. $expected, $this->readAttribute($adapter, '_config')
  245. );
  246. }
  247. /**
  248. * @group ZF-7040
  249. */
  250. public function testGetCurlHandle()
  251. {
  252. $adapter = new Zend_Http_Client_Adapter_Curl();
  253. $adapter->setConfig(array('timeout' => 2, 'maxredirects' => 1));
  254. $adapter->connect("http://framework.zend.com");
  255. $this->assertTrue(is_resource($adapter->getHandle()));
  256. }
  257. }
  258. if (PHPUnit_MAIN_METHOD == 'Zend_Http_Client_CurlTest::main') {
  259. Zend_Http_Client_CurlTest::main();
  260. }