TestAdapterTest.php 7.2 KB

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