TestAdapterTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-2009 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 realpath(dirname(__FILE__) . '/../../../') . '/TestHelper.php';
  23. require_once 'Zend/Http/Client.php';
  24. require_once 'Zend/Http/Client/Adapter/Test.php';
  25. require_once 'PHPUnit/Framework/TestCase.php';
  26. /**
  27. * Exercises Zend_Http_Client_Adapter_Test
  28. *
  29. * @category Zend
  30. * @package Zend_Http_Client
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Http
  35. * @group Zend_Http_Client
  36. */
  37. class Zend_Http_Client_TestAdapterTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Test adapter
  41. *
  42. * @var Zend_Http_Client_Adapter_Test
  43. */
  44. protected $adapter;
  45. /**
  46. * Set up the test adapter before running the test
  47. *
  48. */
  49. public function setUp()
  50. {
  51. $this->adapter = new Zend_Http_Client_Adapter_Test();
  52. }
  53. /**
  54. * Tear down the test adapter after running the test
  55. *
  56. */
  57. public function tearDown()
  58. {
  59. $this->adapter = null;
  60. }
  61. /**
  62. * Make sure an exception is thrown on invalid cofiguration
  63. *
  64. * @expectedException Zend_Http_Client_Adapter_Exception
  65. */
  66. public function testSetConfigThrowsOnInvalidConfig()
  67. {
  68. $this->adapter->setConfig('foo');
  69. }
  70. public function testSetConfigReturnsQuietly()
  71. {
  72. $this->adapter->setConfig(array('foo' => 'bar'));
  73. }
  74. public function testConnectReturnsQuietly()
  75. {
  76. $this->adapter->connect('http://foo');
  77. }
  78. public function testCloseReturnsQuietly()
  79. {
  80. $this->adapter->close();
  81. }
  82. public function testReadDefaultResponse()
  83. {
  84. $expected = "HTTP/1.1 400 Bad Request\r\n\r\n";
  85. $this->assertEquals($expected, $this->adapter->read());
  86. }
  87. public function testReadingSingleResponse()
  88. {
  89. $expected = "HTTP/1.1 200 OK\r\n\r\n";
  90. $this->adapter->setResponse($expected);
  91. $this->assertEquals($expected, $this->adapter->read());
  92. $this->assertEquals($expected, $this->adapter->read());
  93. }
  94. public function testReadingResponseCycles()
  95. {
  96. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  97. "HTTP/1.1 302 Moved Temporarily\r\n\r\n");
  98. $this->adapter->setResponse($expected[0]);
  99. $this->adapter->addResponse($expected[1]);
  100. $this->assertEquals($expected[0], $this->adapter->read());
  101. $this->assertEquals($expected[1], $this->adapter->read());
  102. $this->assertEquals($expected[0], $this->adapter->read());
  103. }
  104. /**
  105. * Test that responses could be added as strings
  106. *
  107. * @dataProvider validHttpResponseProvider
  108. */
  109. public function testAddResponseAsString($testResponse)
  110. {
  111. $this->adapter->read(); // pop out first response
  112. $this->adapter->addResponse($testResponse);
  113. $this->assertEquals($testResponse, $this->adapter->read());
  114. }
  115. /**
  116. * Test that responses could be added as objects (ZF-7009)
  117. *
  118. * @link http://framework.zend.com/issues/browse/ZF-7009
  119. * @dataProvider validHttpResponseProvider
  120. */
  121. public function testAddResponseAsObject($testResponse)
  122. {
  123. $this->adapter->read(); // pop out first response
  124. $respObj = Zend_Http_Response::fromString($testResponse);
  125. $this->adapter->addResponse($respObj);
  126. $this->assertEquals($testResponse, $this->adapter->read());
  127. }
  128. public function testReadingResponseCyclesWhenSetByArray()
  129. {
  130. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  131. "HTTP/1.1 302 Moved Temporarily\r\n\r\n");
  132. $this->adapter->setResponse($expected);
  133. $this->assertEquals($expected[0], $this->adapter->read());
  134. $this->assertEquals($expected[1], $this->adapter->read());
  135. $this->assertEquals($expected[0], $this->adapter->read());
  136. }
  137. public function testSettingNextResponseByIndex()
  138. {
  139. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  140. "HTTP/1.1 302 Moved Temporarily\r\n\r\n",
  141. "HTTP/1.1 404 Not Found\r\n\r\n");
  142. $this->adapter->setResponse($expected);
  143. $this->assertEquals($expected[0], $this->adapter->read());
  144. foreach ($expected as $i => $expected) {
  145. $this->adapter->setResponseIndex($i);
  146. $this->assertEquals($expected, $this->adapter->read());
  147. }
  148. }
  149. public function testSettingNextResponseToAnInvalidIndex()
  150. {
  151. $indexes = array(-1, 1);
  152. foreach ($indexes as $i) {
  153. try {
  154. $this->adapter->setResponseIndex($i);
  155. $this->fail();
  156. } catch (Exception $e) {
  157. $class = 'Zend_Http_Client_Adapter_Exception';
  158. $this->assertType($class, $e);
  159. $this->assertRegexp('/out of range/i', $e->getMessage());
  160. }
  161. }
  162. }
  163. /**
  164. * Data Providers
  165. */
  166. /**
  167. * Provide valid HTTP responses as string
  168. *
  169. * @return array
  170. */
  171. static public function validHttpResponseProvider()
  172. {
  173. return array(
  174. array("HTTP/1.1 200 OK\r\n\r\n"),
  175. array("HTTP/1.1 302 Moved Temporarily\r\nLocation: http://example.com/baz\r\n\r\n"),
  176. array("HTTP/1.1 404 Not Found\r\n" .
  177. "Date: Sun, 14 Jun 2009 10:40:06 GMT\r\n" .
  178. "Server: Apache/2.2.3 (CentOS)\r\n" .
  179. "Content-length: 281\r\n" .
  180. "Connection: close\r\n" .
  181. "Content-type: text/html; charset=iso-8859-1\r\n\r\n" .
  182. "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" .
  183. "<html><head>\n" .
  184. "<title>404 Not Found</title>\n" .
  185. "</head><body>\n" .
  186. "<h1>Not Found</h1>\n" .
  187. "<p>The requested URL /foo/bar was not found on this server.</p>\n" .
  188. "<hr>\n" .
  189. "<address>Apache/2.2.3 (CentOS) Server at example.com Port 80</address>\n" .
  190. "</body></html>")
  191. );
  192. }
  193. }