TestAdapterTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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-2010 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-2010 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 testFailRequestOnDemand()
  83. {
  84. $this->adapter->setNextRequestWillFail(true);
  85. try {
  86. // Make a connection that will fail
  87. $this->adapter->connect('http://foo');
  88. $this->fail();
  89. } catch (Zend_Http_Client_Adapter_Exception $e) {
  90. // Connect again to see that the next request does not fail
  91. $this->adapter->connect('http://foo');
  92. }
  93. }
  94. public function testReadDefaultResponse()
  95. {
  96. $expected = "HTTP/1.1 400 Bad Request\r\n\r\n";
  97. $this->assertEquals($expected, $this->adapter->read());
  98. }
  99. public function testReadingSingleResponse()
  100. {
  101. $expected = "HTTP/1.1 200 OK\r\n\r\n";
  102. $this->adapter->setResponse($expected);
  103. $this->assertEquals($expected, $this->adapter->read());
  104. $this->assertEquals($expected, $this->adapter->read());
  105. }
  106. public function testReadingResponseCycles()
  107. {
  108. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  109. "HTTP/1.1 302 Moved Temporarily\r\n\r\n");
  110. $this->adapter->setResponse($expected[0]);
  111. $this->adapter->addResponse($expected[1]);
  112. $this->assertEquals($expected[0], $this->adapter->read());
  113. $this->assertEquals($expected[1], $this->adapter->read());
  114. $this->assertEquals($expected[0], $this->adapter->read());
  115. }
  116. /**
  117. * Test that responses could be added as strings
  118. *
  119. * @dataProvider validHttpResponseProvider
  120. */
  121. public function testAddResponseAsString($testResponse)
  122. {
  123. $this->adapter->read(); // pop out first response
  124. $this->adapter->addResponse($testResponse);
  125. $this->assertEquals($testResponse, $this->adapter->read());
  126. }
  127. /**
  128. * Test that responses could be added as objects (ZF-7009)
  129. *
  130. * @link http://framework.zend.com/issues/browse/ZF-7009
  131. * @dataProvider validHttpResponseProvider
  132. */
  133. public function testAddResponseAsObject($testResponse)
  134. {
  135. $this->adapter->read(); // pop out first response
  136. $respObj = Zend_Http_Response::fromString($testResponse);
  137. $this->adapter->addResponse($respObj);
  138. $this->assertEquals($testResponse, $this->adapter->read());
  139. }
  140. public function testReadingResponseCyclesWhenSetByArray()
  141. {
  142. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  143. "HTTP/1.1 302 Moved Temporarily\r\n\r\n");
  144. $this->adapter->setResponse($expected);
  145. $this->assertEquals($expected[0], $this->adapter->read());
  146. $this->assertEquals($expected[1], $this->adapter->read());
  147. $this->assertEquals($expected[0], $this->adapter->read());
  148. }
  149. public function testSettingNextResponseByIndex()
  150. {
  151. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  152. "HTTP/1.1 302 Moved Temporarily\r\n\r\n",
  153. "HTTP/1.1 404 Not Found\r\n\r\n");
  154. $this->adapter->setResponse($expected);
  155. $this->assertEquals($expected[0], $this->adapter->read());
  156. foreach ($expected as $i => $expected) {
  157. $this->adapter->setResponseIndex($i);
  158. $this->assertEquals($expected, $this->adapter->read());
  159. }
  160. }
  161. public function testSettingNextResponseToAnInvalidIndex()
  162. {
  163. $indexes = array(-1, 1);
  164. foreach ($indexes as $i) {
  165. try {
  166. $this->adapter->setResponseIndex($i);
  167. $this->fail();
  168. } catch (Exception $e) {
  169. $class = 'Zend_Http_Client_Adapter_Exception';
  170. $this->assertType($class, $e);
  171. $this->assertRegexp('/out of range/i', $e->getMessage());
  172. }
  173. }
  174. }
  175. /**
  176. * Data Providers
  177. */
  178. /**
  179. * Provide valid HTTP responses as string
  180. *
  181. * @return array
  182. */
  183. static public function validHttpResponseProvider()
  184. {
  185. return array(
  186. array("HTTP/1.1 200 OK\r\n\r\n"),
  187. array("HTTP/1.1 302 Moved Temporarily\r\nLocation: http://example.com/baz\r\n\r\n"),
  188. array("HTTP/1.1 404 Not Found\r\n" .
  189. "Date: Sun, 14 Jun 2009 10:40:06 GMT\r\n" .
  190. "Server: Apache/2.2.3 (CentOS)\r\n" .
  191. "Content-length: 281\r\n" .
  192. "Connection: close\r\n" .
  193. "Content-type: text/html; charset=iso-8859-1\r\n\r\n" .
  194. "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" .
  195. "<html><head>\n" .
  196. "<title>404 Not Found</title>\n" .
  197. "</head><body>\n" .
  198. "<h1>Not Found</h1>\n" .
  199. "<p>The requested URL /foo/bar was not found on this server.</p>\n" .
  200. "<hr>\n" .
  201. "<address>Apache/2.2.3 (CentOS) Server at example.com Port 80</address>\n" .
  202. "</body></html>")
  203. );
  204. }
  205. }