SocketTest.php 6.5 KB

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