CurlTest.php 7.6 KB

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