SocketTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * Data Providers
  96. */
  97. /**
  98. * Provide invalid context resources / options
  99. *
  100. * @return array
  101. */
  102. static public function invalidContextProvider()
  103. {
  104. return array(
  105. array(new stdClass()),
  106. array(fopen('data://text/plain,', 'r')),
  107. array(false),
  108. array(null)
  109. );
  110. }
  111. }