2
0

TestAdapterTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. public function testSetConfigThrowsOnInvalidConfig()
  39. {
  40. try {
  41. $this->adapter->setConfig('foo');
  42. } catch (Exception $e) {
  43. $class = 'Zend_Http_Client_Adapter_Exception';
  44. $this->assertType($class, $e);
  45. $this->assertRegexp('/expects an array/i', $e->getMessage());
  46. }
  47. }
  48. public function testSetConfigReturnsQuietly()
  49. {
  50. $this->adapter->setConfig(array('foo' => 'bar'));
  51. }
  52. public function testConnectReturnsQuietly()
  53. {
  54. $this->adapter->connect('http://foo');
  55. }
  56. public function testCloseReturnsQuietly()
  57. {
  58. $this->adapter->close();
  59. }
  60. public function testReadDefaultResponse()
  61. {
  62. $expected = "HTTP/1.1 400 Bad Request\r\n\r\n";
  63. $this->assertEquals($expected, $this->adapter->read());
  64. }
  65. public function testReadingSingleResponse()
  66. {
  67. $expected = "HTTP/1.1 200 OK\r\n\r\n";
  68. $this->adapter->setResponse($expected);
  69. $this->assertEquals($expected, $this->adapter->read());
  70. $this->assertEquals($expected, $this->adapter->read());
  71. }
  72. public function testReadingResponseCycles()
  73. {
  74. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  75. "HTTP/1.1 302 Moved Temporarily\r\n\r\n");
  76. $this->adapter->setResponse($expected[0]);
  77. $this->adapter->addResponse($expected[1]);
  78. $this->assertEquals($expected[0], $this->adapter->read());
  79. $this->assertEquals($expected[1], $this->adapter->read());
  80. $this->assertEquals($expected[0], $this->adapter->read());
  81. }
  82. /**
  83. * Test that responses could be added as strings
  84. *
  85. * @dataProvider validHttpResponseProvider
  86. */
  87. public function testAddResponseAsString($testResponse)
  88. {
  89. $this->adapter->read(); // pop out first response
  90. $this->adapter->addResponse($testResponse);
  91. $this->assertEquals($testResponse, $this->adapter->read());
  92. }
  93. /**
  94. * Test that responses could be added as objects (ZF-7009)
  95. *
  96. * @link http://framework.zend.com/issues/browse/ZF-7009
  97. * @dataProvider validHttpResponseProvider
  98. */
  99. public function testAddResponseAsObject($testResponse)
  100. {
  101. $this->adapter->read(); // pop out first response
  102. $respObj = Zend_Http_Response::fromString($testResponse);
  103. $this->adapter->addResponse($respObj);
  104. $this->assertEquals($testResponse, $this->adapter->read());
  105. }
  106. public function testReadingResponseCyclesWhenSetByArray()
  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);
  111. $this->assertEquals($expected[0], $this->adapter->read());
  112. $this->assertEquals($expected[1], $this->adapter->read());
  113. $this->assertEquals($expected[0], $this->adapter->read());
  114. }
  115. public function testSettingNextResponseByIndex()
  116. {
  117. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  118. "HTTP/1.1 302 Moved Temporarily\r\n\r\n",
  119. "HTTP/1.1 404 Not Found\r\n\r\n");
  120. $this->adapter->setResponse($expected);
  121. $this->assertEquals($expected[0], $this->adapter->read());
  122. foreach ($expected as $i => $expected) {
  123. $this->adapter->setResponseIndex($i);
  124. $this->assertEquals($expected, $this->adapter->read());
  125. }
  126. }
  127. public function testSettingNextResponseToAnInvalidIndex()
  128. {
  129. $indexes = array(-1, 1);
  130. foreach ($indexes as $i) {
  131. try {
  132. $this->adapter->setResponseIndex($i);
  133. $this->fail();
  134. } catch (Exception $e) {
  135. $class = 'Zend_Http_Client_Adapter_Exception';
  136. $this->assertType($class, $e);
  137. $this->assertRegexp('/out of range/i', $e->getMessage());
  138. }
  139. }
  140. }
  141. /**
  142. * Data Providers
  143. */
  144. /**
  145. * Provide valid HTTP responses as string
  146. *
  147. * @return array
  148. */
  149. static public function validHttpResponseProvider()
  150. {
  151. return array(
  152. array("HTTP/1.1 200 OK\r\n\r\n"),
  153. array("HTTP/1.1 302 Moved Temporarily\r\nLocation: http://example.com/baz\r\n\r\n"),
  154. array("HTTP/1.1 404 Not Found\r\n" .
  155. "Date: Sun, 14 Jun 2009 10:40:06 GMT\r\n" .
  156. "Server: Apache/2.2.3 (CentOS)\r\n" .
  157. "Content-length: 281\r\n" .
  158. "Connection: close\r\n" .
  159. "Content-type: text/html; charset=iso-8859-1\r\n\r\n" .
  160. "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" .
  161. "<html><head>\n" .
  162. "<title>404 Not Found</title>\n" .
  163. "</head><body>\n" .
  164. "<h1>Not Found</h1>\n" .
  165. "<p>The requested URL /foo/bar was not found on this server.</p>\n" .
  166. "<hr>\n" .
  167. "<address>Apache/2.2.3 (CentOS) Server at example.com Port 80</address>\n" .
  168. "</body></html>")
  169. );
  170. }
  171. }