PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Http/Client/CurlTest.php

https://github.com/Exercise/zf2
PHP | 307 lines | 162 code | 40 blank | 105 comment | 1 complexity | 074350778cc72259c525062f2319f48d MD5 | raw file
  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-2010 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. /**
  23. * @namespace
  24. */
  25. namespace ZendTest\Http\Client;
  26. use Zend\Http\Client\Adapter;
  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-2010 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 CurlTest extends CommonHttpTests
  49. {
  50. /**
  51. * Configuration array
  52. *
  53. * @var array
  54. */
  55. protected $config = array(
  56. 'adapter' => 'Zend\Http\Client\Adapter\Curl'
  57. );
  58. protected function setUp()
  59. {
  60. if (!extension_loaded('curl')) {
  61. $this->markTestSkipped('cURL is not installed, marking all Http Client Curl Adapter tests skipped.');
  62. }
  63. parent::setUp();
  64. }
  65. /**
  66. * Off-line common adapter tests
  67. */
  68. /**
  69. * Test that we can set a valid configuration array with some options
  70. *
  71. */
  72. public function testConfigSetAsArray()
  73. {
  74. $config = array(
  75. 'timeout' => 500,
  76. 'someoption' => 'hasvalue'
  77. );
  78. $this->_adapter->setConfig($config);
  79. $hasConfig = $this->_adapter->getConfig();
  80. foreach($config as $k => $v) {
  81. $this->assertEquals($v, $hasConfig[$k]);
  82. }
  83. }
  84. /**
  85. * Test that a Zend_Config object can be used to set configuration
  86. *
  87. * @link http://framework.zend.com/issues/browse/ZF-5577
  88. */
  89. public function testConfigSetAsZendConfig()
  90. {
  91. $config = new \Zend\Config\Config(array(
  92. 'timeout' => 400,
  93. 'nested' => array(
  94. 'item' => 'value',
  95. )
  96. ));
  97. $this->_adapter->setConfig($config);
  98. $hasConfig = $this->_adapter->getConfig();
  99. $this->assertEquals($config->timeout, $hasConfig['timeout']);
  100. $this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
  101. }
  102. /**
  103. * Check that an exception is thrown when trying to set invalid config
  104. *
  105. * @expectedException Zend\Http\Client\Adapter\Exception
  106. * @dataProvider invalidConfigProvider
  107. */
  108. public function testSetConfigInvalidConfig($config)
  109. {
  110. $this->_adapter->setConfig($config);
  111. }
  112. /**
  113. * CURLOPT_CLOSEPOLICY never worked and returns false on setopt always:
  114. * @link http://de2.php.net/manual/en/function.curl-setopt.php#84277
  115. *
  116. * This should throw an exception.
  117. *
  118. * @expectedException Zend\Http\Exception
  119. */
  120. public function testSettingInvalidCurlOption()
  121. {
  122. $config = array(
  123. 'adapter' => 'Zend\Http\Client\Adapter\Curl',
  124. 'curloptions' => array(CURLOPT_CLOSEPOLICY => true),
  125. );
  126. $this->client = new \Zend\Http\Client($this->client->getUri(true), $config);
  127. $this->client->request('GET');
  128. $this->fail();
  129. }
  130. public function testRedirectWithGetOnly()
  131. {
  132. $this->client->setUri($this->baseuri . 'testRedirections.php');
  133. // Set some parameters
  134. $this->client->setParameterGet('swallow', 'african');
  135. // Request
  136. $res = $this->client->request('GET');
  137. $this->assertEquals(3, $this->client->getRedirectionsCount(), 'Redirection counter is not as expected');
  138. // Make sure the body does *not* contain the set parameters
  139. $this->assertNotContains('swallow', $res->getBody());
  140. $this->assertNotContains('Camelot', $res->getBody());
  141. }
  142. /**
  143. * This is a specific problem of the request type: If you let cURL handle redirects internally
  144. * but start with a POST request that sends data then the location ping-pong will lead to an
  145. * Content-Length: x\r\n GET request of the client that the server won't answer because no content is sent.
  146. *
  147. * Set CURLOPT_FOLLOWLOCATION = false for this type of request and let the Zend_Http_Client handle redirects
  148. * in his own loop.
  149. *
  150. * @expectedException Zend\Http\Client\Exception
  151. */
  152. public function testRedirectPostToGetWithCurlFollowLocationOptionLeadsToTimeout()
  153. {
  154. $adapter = new Adapter\Curl();
  155. $this->client->setAdapter($adapter);
  156. $adapter->setConfig(array(
  157. 'curloptions' => array(
  158. CURLOPT_FOLLOWLOCATION => true,
  159. CURLOPT_TIMEOUT => 1,
  160. ))
  161. );
  162. $this->client->setUri($this->baseuri . 'testRedirections.php');
  163. // Set some parameters
  164. $this->client->setParameterGet('swallow', 'african');
  165. $this->client->setParameterPost('Camelot', 'A silly place');
  166. $this->client->request("POST");
  167. }
  168. /**
  169. * @group ZF-3758
  170. * @link http://framework.zend.com/issues/browse/ZF-3758
  171. */
  172. public function testPutFileContentWithHttpClient()
  173. {
  174. // Method 1: Using the binary string of a file to PUT
  175. $this->client->setUri($this->baseuri . 'testRawPostData.php');
  176. $putFileContents = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  177. '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg');
  178. $this->client->setRawData($putFileContents);
  179. $this->client->request('PUT');
  180. $this->assertEquals($putFileContents, $this->client->getLastResponse()->getBody());
  181. }
  182. /**
  183. * @group ZF-3758
  184. * @link http://framework.zend.com/issues/browse/ZF-3758
  185. */
  186. public function testPutFileHandleWithHttpClient()
  187. {
  188. $this->client->setUri($this->baseuri . 'testRawPostData.php');
  189. $putFileContents = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  190. '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg');
  191. // Method 2: Using a File-Handle to the file to PUT the data
  192. $putFilePath = dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  193. '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg';
  194. $putFileHandle = fopen($putFilePath, "r");
  195. $putFileSize = filesize($putFilePath);
  196. $adapter = new Adapter\Curl();
  197. $this->client->setAdapter($adapter);
  198. $adapter->setConfig(array(
  199. 'curloptions' => array(CURLOPT_INFILE => $putFileHandle, CURLOPT_INFILESIZE => $putFileSize)
  200. ));
  201. $this->client->request('PUT');
  202. $this->assertEquals(gzcompress($putFileContents), gzcompress($this->client->getLastResponse()->getBody()));
  203. }
  204. public function testWritingAndNotConnectedWithCurlHandleThrowsException()
  205. {
  206. $this->setExpectedException('Zend\Http\Client\Adapter\Exception', "Trying to write but we are not connected");
  207. $adapter = new Adapter\Curl();
  208. $adapter->write("GET", "someUri");
  209. }
  210. public function testSetConfigIsNotArray()
  211. {
  212. $this->setExpectedException('Zend\Http\Client\Adapter\Exception');
  213. $adapter = new Adapter\Curl();
  214. $adapter->setConfig("foo");
  215. }
  216. public function testSetCurlOptions()
  217. {
  218. $adapter = new Adapter\Curl();
  219. $adapter->setCurlOption('foo', 'bar')
  220. ->setCurlOption('bar', 'baz');
  221. $this->assertEquals(
  222. array('curloptions' => array('foo' => 'bar', 'bar' => 'baz')),
  223. $this->readAttribute($adapter, '_config')
  224. );
  225. }
  226. public function testWorkWithProxyConfiguration()
  227. {
  228. $adapter = new Adapter\Curl();
  229. $adapter->setConfig(array(
  230. 'proxy_host' => 'localhost',
  231. 'proxy_port' => 80,
  232. 'proxy_user' => 'foo',
  233. 'proxy_pass' => 'baz',
  234. ));
  235. $expected = array(
  236. 'curloptions' => array(
  237. CURLOPT_PROXYUSERPWD => 'foo:baz',
  238. CURLOPT_PROXY => 'localhost',
  239. CURLOPT_PROXYPORT => 80,
  240. ),
  241. );
  242. $this->assertEquals(
  243. $expected, $this->readAttribute($adapter, '_config')
  244. );
  245. }
  246. /**
  247. * @group ZF-7040
  248. */
  249. public function testGetCurlHandle()
  250. {
  251. $adapter = new Adapter\Curl();
  252. $adapter->setConfig(array('timeout' => 2, 'maxredirects' => 1));
  253. $adapter->connect("http://framework.zend.com");
  254. $this->assertTrue(is_resource($adapter->getHandle()));
  255. }
  256. /**
  257. * @group ZF-9857
  258. */
  259. public function testHeadRequest()
  260. {
  261. $this->client->setUri($this->baseuri . 'testRawPostData.php');
  262. $adapter = new Adapter\Curl();
  263. $this->client->setAdapter($adapter);
  264. $this->client->request('HEAD');
  265. $this->assertEquals('', $this->client->getLastResponse()->getBody());
  266. }
  267. }