SocketTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * Stream context related tests
  36. */
  37. public function testGetNewStreamContext()
  38. {
  39. $adapter = new $this->config['adapter'];
  40. $context = $adapter->getStreamContext();
  41. $this->assertEquals('stream-context', get_resource_type($context));
  42. }
  43. public function testSetNewStreamContextResource()
  44. {
  45. $adapter = new $this->config['adapter'];
  46. $context = stream_context_create();
  47. $adapter->setStreamContext($context);
  48. $this->assertEquals($context, $adapter->getStreamContext());
  49. }
  50. public function testSetNewStreamContextOptions()
  51. {
  52. $adapter = new $this->config['adapter'];
  53. $options = array(
  54. 'socket' => array(
  55. 'bindto' => '1.2.3.4:0'
  56. ),
  57. 'ssl' => array(
  58. 'verify_peer' => true,
  59. 'allow_self_signed' => false
  60. )
  61. );
  62. $adapter->setStreamContext($options);
  63. $this->assertEquals($options, stream_context_get_options($adapter->getStreamContext()));
  64. }
  65. /**
  66. * Test that setting invalid options / context causes an exception
  67. *
  68. * @dataProvider invalidContextProvider
  69. * @expectedException Zend_Http_Client_Adapter_Exception
  70. */
  71. public function testSetInvalidContextOptions($invalid)
  72. {
  73. $adapter = new $this->config['adapter'];
  74. $adapter->setStreamContext($invalid);
  75. }
  76. public function testSetHttpsStreamContextParam()
  77. {
  78. if ($this->client->getUri()->getScheme() != 'https') {
  79. $this->markTestSkipped();
  80. }
  81. $adapter = new $this->config['adapter'];
  82. $adapter->setStreamContext(array(
  83. 'ssl' => array(
  84. 'capture_peer_cert' => true,
  85. 'capture_peer_chain' => true
  86. )
  87. ));
  88. $this->client->setAdapter($adapter);
  89. $this->client->setUri($this->baseuri . '/testSimpleRequests.php');
  90. $this->client->request();
  91. $opts = stream_context_get_options($adapter->getStreamContext());
  92. $this->assertTrue(isset($opts['ssl']['peer_certificate']));
  93. }
  94. /**
  95. * Test that we get the right exception after a socket timeout
  96. *
  97. * @link http://framework.zend.com/issues/browse/ZF-7309
  98. */
  99. public function testExceptionOnReadTimeout()
  100. {
  101. // Set 1 second timeout
  102. $this->client->setConfig(array('timeout' => 1));
  103. $start = microtime(true);
  104. try {
  105. $this->client->request();
  106. $this->fail("Expected a timeout Zend_Http_Client_Adapter_Exception");
  107. } catch (Zend_Http_Client_Adapter_Exception $e) {
  108. $this->assertEquals(Zend_Http_Client_Adapter_Exception::READ_TIMEOUT, $e->getCode());
  109. }
  110. $time = (microtime(true) - $start);
  111. // We should be very close to 1 second
  112. $this->assertLessThan(2, $time);
  113. }
  114. /**
  115. * Data Providers
  116. */
  117. /**
  118. * Provide invalid context resources / options
  119. *
  120. * @return array
  121. */
  122. static public function invalidContextProvider()
  123. {
  124. return array(
  125. array(new stdClass()),
  126. array(fopen('data://text/plain,', 'r')),
  127. array(false),
  128. array(null)
  129. );
  130. }
  131. }