ResponseTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Http
  5. * @subpackage UnitTests
  6. * @version $Id$
  7. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com/)
  8. * @license http://framework.zend.com/license/new-bsd New BSD License
  9. */
  10. /**
  11. * Test helper
  12. */
  13. require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  14. /**
  15. * Zend_Http_Response
  16. */
  17. require_once 'Zend/Http/Response.php';
  18. /**
  19. * PHPUnit test case
  20. */
  21. require_once 'PHPUnit/Framework/TestCase.php';
  22. /**
  23. * Zend_Http_Response unit tests
  24. *
  25. * @category Zend
  26. * @package Zend_Http
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com/)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Http_ResponseTest extends PHPUnit_Framework_TestCase
  32. {
  33. public function setUp()
  34. { }
  35. public function testGzipResponse ()
  36. {
  37. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_gzip');
  38. $res = Zend_Http_Response::fromString($response_text);
  39. $this->assertEquals('gzip', $res->getHeader('Content-encoding'));
  40. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  41. $this->assertEquals('f24dd075ba2ebfb3bf21270e3fdc5303', md5($res->getRawBody()));
  42. }
  43. public function testDeflateResponse ()
  44. {
  45. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_deflate');
  46. $res = Zend_Http_Response::fromString($response_text);
  47. $this->assertEquals('deflate', $res->getHeader('Content-encoding'));
  48. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  49. $this->assertEquals('ad62c21c3aa77b6a6f39600f6dd553b8', md5($res->getRawBody()));
  50. }
  51. public function testChunkedResponse ()
  52. {
  53. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_chunked');
  54. $res = Zend_Http_Response::fromString($response_text);
  55. $this->assertEquals('chunked', $res->getHeader('Transfer-encoding'));
  56. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  57. $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getRawBody()));
  58. }
  59. public function testLineBreaksCompatibility()
  60. {
  61. $response_text_lf = $this->readResponse('response_lfonly');
  62. $res_lf = Zend_Http_Response::fromString($response_text_lf);
  63. $response_text_crlf = $this->readResponse('response_crlf');
  64. $res_crlf = Zend_Http_Response::fromString($response_text_crlf);
  65. $this->assertEquals($res_lf->getHeadersAsString(true), $res_crlf->getHeadersAsString(true), 'Responses headers do not match');
  66. $this->assertEquals($res_lf->getBody(), $res_crlf->getBody(), 'Response bodies do not match');
  67. }
  68. public function testExtractMessageCrlf()
  69. {
  70. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_crlf');
  71. $this->assertEquals("OK", Zend_Http_Response::extractMessage($response_text), "Response message is not 'OK' as expected");
  72. }
  73. public function testExtractMessageLfonly()
  74. {
  75. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_lfonly');
  76. $this->assertEquals("OK", Zend_Http_Response::extractMessage($response_text), "Response message is not 'OK' as expected");
  77. }
  78. public function test404IsError()
  79. {
  80. $response_text = $this->readResponse('response_404');
  81. $response = Zend_Http_Response::fromString($response_text);
  82. $this->assertEquals(404, $response->getStatus(), 'Response code is expected to be 404, but it\'s not.');
  83. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  84. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  85. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  86. }
  87. public function test500isError()
  88. {
  89. $response_text = $this->readResponse('response_500');
  90. $response = Zend_Http_Response::fromString($response_text);
  91. $this->assertEquals(500, $response->getStatus(), 'Response code is expected to be 500, but it\'s not.');
  92. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  93. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  94. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  95. }
  96. public function test300isRedirect()
  97. {
  98. $response = Zend_Http_Response::fromString($this->readResponse('response_302'));
  99. $this->assertEquals(302, $response->getStatus(), 'Response code is expected to be 302, but it\'s not.');
  100. $this->assertTrue($response->isRedirect(), 'Response is a redirection, but isRedirect() returned false');
  101. $this->assertFalse($response->isError(), 'Response is a redirection, but isError() returned true');
  102. $this->assertFalse($response->isSuccessful(), 'Response is a redirection, but isSuccessful() returned true');
  103. }
  104. public function test200Ok()
  105. {
  106. $response = Zend_Http_Response::fromString($this->readResponse('response_deflate'));
  107. $this->assertEquals(200, $response->getStatus(), 'Response code is expected to be 200, but it\'s not.');
  108. $this->assertFalse($response->isError(), 'Response is OK, but isError() returned true');
  109. $this->assertTrue($response->isSuccessful(), 'Response is OK, but isSuccessful() returned false');
  110. $this->assertFalse($response->isRedirect(), 'Response is OK, but isRedirect() returned true');
  111. }
  112. public function test100Continue()
  113. {
  114. $this->markTestIncomplete();
  115. }
  116. public function testAutoMessageSet()
  117. {
  118. $response = Zend_Http_Response::fromString($this->readResponse('response_403_nomessage'));
  119. $this->assertEquals(403, $response->getStatus(), 'Response status is expected to be 403, but it isn\'t');
  120. $this->assertEquals('Forbidden', $response->getMessage(), 'Response is 403, but message is not "Forbidden" as expected');
  121. // While we're here, make sure it's classified as error...
  122. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  123. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  124. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  125. }
  126. public function testAsString()
  127. {
  128. $response_str = $this->readResponse('response_404');
  129. $response = Zend_Http_Response::fromString($response_str);
  130. $this->assertEquals(strtolower($response_str), strtolower($response->asString()), 'Response convertion to string does not match original string');
  131. $this->assertEquals(strtolower($response_str), strtolower((string)$response), 'Response convertion to string does not match original string');
  132. }
  133. public function testGetHeaders()
  134. {
  135. $response = Zend_Http_Response::fromString($this->readResponse('response_deflate'));
  136. $headers = $response->getHeaders();
  137. $this->assertEquals(8, count($headers), 'Header count is not as expected');
  138. $this->assertEquals('Apache', $headers['Server'], 'Server header is not as expected');
  139. $this->assertEquals('deflate', $headers['Content-encoding'], 'Content-type header is not as expected');
  140. }
  141. public function testGetVersion()
  142. {
  143. $response = Zend_Http_Response::fromString($this->readResponse('response_chunked'));
  144. $this->assertEquals(1.1, $response->getVersion(), 'Version is expected to be 1.1');
  145. }
  146. public function testResponseCodeAsText()
  147. {
  148. // This is an entirely static test
  149. // Test some response codes
  150. $this->assertEquals('Continue', Zend_Http_Response::responseCodeAsText(100));
  151. $this->assertEquals('OK', Zend_Http_Response::responseCodeAsText(200));
  152. $this->assertEquals('Multiple Choices', Zend_Http_Response::responseCodeAsText(300));
  153. $this->assertEquals('Bad Request', Zend_Http_Response::responseCodeAsText(400));
  154. $this->assertEquals('Internal Server Error', Zend_Http_Response::responseCodeAsText(500));
  155. // Make sure that invalid codes return 'Unkown'
  156. $this->assertEquals('Unknown', Zend_Http_Response::responseCodeAsText(600));
  157. // Check HTTP/1.0 value for 302
  158. $this->assertEquals('Found', Zend_Http_Response::responseCodeAsText(302));
  159. $this->assertEquals('Moved Temporarily', Zend_Http_Response::responseCodeAsText(302, false));
  160. // Check we get an array if no code is passed
  161. $codes = Zend_Http_Response::responseCodeAsText();
  162. $this->assertType('array', $codes);
  163. $this->assertEquals('OK', $codes[200]);
  164. }
  165. public function testUnknownCode()
  166. {
  167. $response_str = $this->readResponse('response_unknown');
  168. $response = Zend_Http_Response::fromString($response_str);
  169. // Check that dynamically the message is parsed
  170. $this->assertEquals(550, $response->getStatus(), 'Status is expected to be a non-standard 550');
  171. $this->assertEquals('Printer On Fire', $response->getMessage(), 'Message is expected to be extracted');
  172. // Check that statically, an Unknown string is returned for the 550 code
  173. $this->assertEquals('Unknown', Zend_Http_Response::responseCodeAsText($response_str));
  174. }
  175. public function testMultilineHeader()
  176. {
  177. $response = Zend_Http_Response::fromString($this->readResponse('response_multiline_header'));
  178. // Make sure we got the corrent no. of headers
  179. $this->assertEquals(6, count($response->getHeaders()), 'Header count is expected to be 6');
  180. // Check header integrity
  181. $this->assertEquals('timeout=15, max=100', $response->getHeader('keep-alive'));
  182. $this->assertEquals('text/html; charset=iso-8859-1', $response->getHeader('content-type'));
  183. }
  184. public function testExceptInvalidChunkedBody()
  185. {
  186. try {
  187. Zend_Http_Response::decodeChunkedBody($this->readResponse('response_deflate'));
  188. $this->fail('An expected exception was not thrown');
  189. } catch (Zend_Http_Exception $e) {
  190. // We are ok!
  191. }
  192. }
  193. public function testExtractorsOnInvalidString()
  194. {
  195. // Try with an empty string
  196. $response_str = '';
  197. $this->assertTrue(Zend_Http_Response::extractCode($response_str) === false);
  198. $this->assertTrue(Zend_Http_Response::extractMessage($response_str) === false);
  199. $this->assertTrue(Zend_Http_Response::extractVersion($response_str) === false);
  200. $this->assertTrue(Zend_Http_Response::extractBody($response_str) === '');
  201. $this->assertTrue(Zend_Http_Response::extractHeaders($response_str) === array());
  202. }
  203. /**
  204. * Make sure a response with some leading whitespace in the response body
  205. * does not get modified (see ZF-1924)
  206. *
  207. */
  208. public function testLeadingWhitespaceBody()
  209. {
  210. $body = Zend_Http_Response::extractBody($this->readResponse('response_leadingws'));
  211. $this->assertEquals($body, "\r\n\t \n\r\tx", 'Extracted body is not identical to expected body');
  212. }
  213. /**
  214. * Helper function: read test response from file
  215. *
  216. * @param string $response
  217. * @return string
  218. */
  219. protected function readResponse($response)
  220. {
  221. return file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $response);
  222. }
  223. }