CurlTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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-2015 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-2015 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(
  163. 'curloptions' => array(
  164. CURLOPT_FOLLOWLOCATION => true,
  165. CURLOPT_TIMEOUT => 1,
  166. ))
  167. );
  168. $this->client->setUri($this->baseuri . 'testRedirections.php');
  169. // Set some parameters
  170. $this->client->setParameterGet('swallow', 'african');
  171. $this->client->setParameterPost('Camelot', 'A silly place');
  172. $this->client->request("POST");
  173. }
  174. /**
  175. * @group ZF-3758
  176. * @link http://framework.zend.com/issues/browse/ZF-3758
  177. */
  178. public function testPutFileContentWithHttpClient()
  179. {
  180. // Method 1: Using the binary string of a file to PUT
  181. $this->client->setUri($this->baseuri . 'testRawPostData.php');
  182. $putFileContents = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  183. '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg');
  184. $this->client->setRawData($putFileContents);
  185. $this->client->request('PUT');
  186. $this->assertEquals($putFileContents, $this->client->getLastResponse()->getBody());
  187. }
  188. /**
  189. * @group ZF-3758
  190. * @link http://framework.zend.com/issues/browse/ZF-3758
  191. */
  192. public function testPutFileHandleWithHttpClient()
  193. {
  194. $this->client->setUri($this->baseuri . 'testRawPostData.php');
  195. $putFileContents = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  196. '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg');
  197. // Method 2: Using a File-Handle to the file to PUT the data
  198. $putFilePath = dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  199. '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg';
  200. $putFileHandle = fopen($putFilePath, "r");
  201. $putFileSize = filesize($putFilePath);
  202. $adapter = new Zend_Http_Client_Adapter_Curl();
  203. $this->client->setAdapter($adapter);
  204. $adapter->setConfig(array(
  205. 'curloptions' => array(CURLOPT_INFILE => $putFileHandle, CURLOPT_INFILESIZE => $putFileSize)
  206. ));
  207. $this->client->request('PUT');
  208. $this->assertEquals(gzcompress($putFileContents), gzcompress($this->client->getLastResponse()->getBody()));
  209. }
  210. public function testWritingAndNotConnectedWithCurlHandleThrowsException()
  211. {
  212. $this->setExpectedException("Zend_Http_Client_Adapter_Exception", "Trying to write but we are not connected");
  213. $adapter = new Zend_Http_Client_Adapter_Curl();
  214. $adapter->write("GET", "someUri");
  215. }
  216. public function testSetConfigIsNotArray()
  217. {
  218. $this->setExpectedException("Zend_Http_Client_Adapter_Exception");
  219. $adapter = new Zend_Http_Client_Adapter_Curl();
  220. $adapter->setConfig("foo");
  221. }
  222. public function testSetCurlOptions()
  223. {
  224. $adapter = new Zend_Http_Client_Adapter_Curl();
  225. $adapter->setCurlOption('foo', 'bar')
  226. ->setCurlOption('bar', 'baz');
  227. $this->assertEquals(
  228. array('curloptions' => array('foo' => 'bar', 'bar' => 'baz')),
  229. $this->readAttribute($adapter, '_config')
  230. );
  231. }
  232. public function testWorkWithProxyConfiguration()
  233. {
  234. $adapter = new Zend_Http_Client_Adapter_Curl();
  235. $adapter->setConfig(array(
  236. 'proxy_host' => 'localhost',
  237. 'proxy_port' => 80,
  238. 'proxy_user' => 'foo',
  239. 'proxy_pass' => 'baz',
  240. ));
  241. $expected = array(
  242. 'curloptions' => array(
  243. CURLOPT_PROXYUSERPWD => 'foo:baz',
  244. CURLOPT_PROXY => 'localhost',
  245. CURLOPT_PROXYPORT => 80,
  246. ),
  247. );
  248. $this->assertEquals(
  249. $expected, $this->readAttribute($adapter, '_config')
  250. );
  251. }
  252. /**
  253. * @group ZF-7040
  254. */
  255. public function testGetCurlHandle()
  256. {
  257. $adapter = new Zend_Http_Client_Adapter_Curl();
  258. $adapter->setConfig(array('timeout' => 2, 'maxredirects' => 1));
  259. $adapter->connect("http://framework.zend.com");
  260. $this->assertTrue(is_resource($adapter->getHandle()));
  261. }
  262. /**
  263. * @group ZF-9857
  264. */
  265. public function testHeadRequest()
  266. {
  267. $this->client->setUri($this->baseuri . 'testRawPostData.php');
  268. $adapter = new Zend_Http_Client_Adapter_Curl;
  269. $this->client->setAdapter($adapter);
  270. $this->client->request('HEAD');
  271. $this->assertEquals('', $this->client->getLastResponse()->getBody());
  272. }
  273. }
  274. if (PHPUnit_MAIN_METHOD == 'Zend_Http_Client_CurlTest::main') {
  275. Zend_Http_Client_CurlTest::main();
  276. }