CurlTest.php 9.1 KB

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