CurlTest.php 7.5 KB

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