SocketTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. require_once dirname(__FILE__) . '/CommonHttpTests.php';
  23. require_once 'Zend/Http/Client/Adapter/Socket.php';
  24. /**
  25. * This Testsuite includes all Zend_Http_Client that require a working web
  26. * server to perform. It was designed to be extendable, so that several
  27. * test suites could be run against several servers, with different client
  28. * adapters and configurations.
  29. *
  30. * Note that $this->baseuri must point to a directory on a web server
  31. * containing all the files under the _files directory. You should symlink
  32. * or copy these files and set 'baseuri' properly.
  33. *
  34. * You can also set the proper constant in your test configuration file to
  35. * point to the right place.
  36. *
  37. * @category Zend
  38. * @package Zend_Http_Client
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Http
  43. * @group Zend_Http_Client
  44. */
  45. class Zend_Http_Client_SocketTest extends Zend_Http_Client_CommonHttpTests
  46. {
  47. /**
  48. * Configuration array
  49. *
  50. * @var array
  51. */
  52. protected $config = array(
  53. 'adapter' => 'Zend_Http_Client_Adapter_Socket'
  54. );
  55. /**
  56. * Off-line common adapter tests
  57. */
  58. /**
  59. * Test that we can set a valid configuration array with some options
  60. * @group ZHC001
  61. */
  62. public function testConfigSetAsArray()
  63. {
  64. $config = array(
  65. 'timeout' => 500,
  66. 'someoption' => 'hasvalue'
  67. );
  68. $this->_adapter->setConfig($config);
  69. $hasConfig = $this->_adapter->getConfig();
  70. foreach($config as $k => $v) {
  71. $this->assertEquals($v, $hasConfig[$k]);
  72. }
  73. }
  74. /**
  75. * Test that a Zend_Config object can be used to set configuration
  76. *
  77. * @link http://framework.zend.com/issues/browse/ZF-5577
  78. */
  79. public function testConfigSetAsZendConfig()
  80. {
  81. require_once 'Zend/Config.php';
  82. $config = new Zend_Config(array(
  83. 'timeout' => 400,
  84. 'nested' => array(
  85. 'item' => 'value',
  86. )
  87. ));
  88. $this->_adapter->setConfig($config);
  89. $hasConfig = $this->_adapter->getConfig();
  90. $this->assertEquals($config->timeout, $hasConfig['timeout']);
  91. $this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
  92. }
  93. /**
  94. * Check that an exception is thrown when trying to set invalid config
  95. *
  96. * @expectedException Zend_Http_Client_Adapter_Exception
  97. * @dataProvider invalidConfigProvider
  98. */
  99. public function testSetConfigInvalidConfig($config)
  100. {
  101. $this->_adapter->setConfig($config);
  102. }
  103. /**
  104. * Stream context related tests
  105. */
  106. public function testGetNewStreamContext()
  107. {
  108. $adapter = new $this->config['adapter'];
  109. $context = $adapter->getStreamContext();
  110. $this->assertEquals('stream-context', get_resource_type($context));
  111. }
  112. public function testSetNewStreamContextResource()
  113. {
  114. $adapter = new $this->config['adapter'];
  115. $context = stream_context_create();
  116. $adapter->setStreamContext($context);
  117. $this->assertEquals($context, $adapter->getStreamContext());
  118. }
  119. public function testSetNewStreamContextOptions()
  120. {
  121. $adapter = new $this->config['adapter'];
  122. $options = array(
  123. 'socket' => array(
  124. 'bindto' => '1.2.3.4:0'
  125. ),
  126. 'ssl' => array(
  127. 'verify_peer' => true,
  128. 'allow_self_signed' => false
  129. )
  130. );
  131. $adapter->setStreamContext($options);
  132. $this->assertEquals($options, stream_context_get_options($adapter->getStreamContext()));
  133. }
  134. /**
  135. * Test that setting invalid options / context causes an exception
  136. *
  137. * @dataProvider invalidContextProvider
  138. * @expectedException Zend_Http_Client_Adapter_Exception
  139. */
  140. public function testSetInvalidContextOptions($invalid)
  141. {
  142. $adapter = new $this->config['adapter'];
  143. $adapter->setStreamContext($invalid);
  144. }
  145. public function testSetHttpsStreamContextParam()
  146. {
  147. if ($this->client->getUri()->getScheme() != 'https') {
  148. $this->markTestSkipped();
  149. }
  150. $adapter = new $this->config['adapter'];
  151. $adapter->setStreamContext(array(
  152. 'ssl' => array(
  153. 'capture_peer_cert' => true,
  154. 'capture_peer_chain' => true
  155. )
  156. ));
  157. $this->client->setAdapter($adapter);
  158. $this->client->setUri($this->baseuri . '/testSimpleRequests.php');
  159. $this->client->request();
  160. $opts = stream_context_get_options($adapter->getStreamContext());
  161. $this->assertTrue(isset($opts['ssl']['peer_certificate']));
  162. }
  163. /**
  164. * Test that we get the right exception after a socket timeout
  165. *
  166. * @link http://framework.zend.com/issues/browse/ZF-7309
  167. */
  168. public function testExceptionOnReadTimeout()
  169. {
  170. // Set 1 second timeout
  171. $this->client->setConfig(array('timeout' => 1));
  172. $start = microtime(true);
  173. try {
  174. $this->client->request();
  175. $this->fail("Expected a timeout Zend_Http_Client_Adapter_Exception");
  176. } catch (Zend_Http_Client_Adapter_Exception $e) {
  177. $this->assertEquals(Zend_Http_Client_Adapter_Exception::READ_TIMEOUT, $e->getCode());
  178. }
  179. $time = (microtime(true) - $start);
  180. // We should be very close to 1 second
  181. $this->assertLessThan(2, $time);
  182. }
  183. /**
  184. * Test that a chunked response with multibyte characters is properly read
  185. *
  186. * This can fail in various PHP environments - for example, when mbstring
  187. * overloads substr() and strlen(), and mbstring's internal encoding is
  188. * not a single-byte encoding.
  189. *
  190. * @link http://framework.zend.com/issues/browse/ZF-6218
  191. */
  192. public function testMultibyteChunkedResponseZF6218()
  193. {
  194. $md5 = '7667818873302f9995be3798d503d8d3';
  195. $response = $this->client->request();
  196. $this->assertEquals($md5, md5($response->getBody()));
  197. }
  198. /**
  199. * Data Providers
  200. */
  201. /**
  202. * Provide invalid context resources / options
  203. *
  204. * @return array
  205. */
  206. static public function invalidContextProvider()
  207. {
  208. return array(
  209. array(new stdClass()),
  210. array(fopen('data://text/plain,', 'r')),
  211. array(false),
  212. array(null)
  213. );
  214. }
  215. }