TestAdapterTest.php 5.8 KB

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