2
0

ResponseTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 testChunkedResponseCaseInsensitiveZF5438()
  60. {
  61. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_chunked_case');
  62. $res = Zend_Http_Response::fromString($response_text);
  63. $this->assertEquals('chunked', strtolower($res->getHeader('Transfer-encoding')));
  64. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  65. $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getRawBody()));
  66. }
  67. public function testLineBreaksCompatibility()
  68. {
  69. $response_text_lf = $this->readResponse('response_lfonly');
  70. $res_lf = Zend_Http_Response::fromString($response_text_lf);
  71. $response_text_crlf = $this->readResponse('response_crlf');
  72. $res_crlf = Zend_Http_Response::fromString($response_text_crlf);
  73. $this->assertEquals($res_lf->getHeadersAsString(true), $res_crlf->getHeadersAsString(true), 'Responses headers do not match');
  74. $this->assertEquals($res_lf->getBody(), $res_crlf->getBody(), 'Response bodies do not match');
  75. }
  76. public function testExtractMessageCrlf()
  77. {
  78. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_crlf');
  79. $this->assertEquals("OK", Zend_Http_Response::extractMessage($response_text), "Response message is not 'OK' as expected");
  80. }
  81. public function testExtractMessageLfonly()
  82. {
  83. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_lfonly');
  84. $this->assertEquals("OK", Zend_Http_Response::extractMessage($response_text), "Response message is not 'OK' as expected");
  85. }
  86. public function test404IsError()
  87. {
  88. $response_text = $this->readResponse('response_404');
  89. $response = Zend_Http_Response::fromString($response_text);
  90. $this->assertEquals(404, $response->getStatus(), 'Response code is expected to be 404, but it\'s not.');
  91. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  92. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  93. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  94. }
  95. public function test500isError()
  96. {
  97. $response_text = $this->readResponse('response_500');
  98. $response = Zend_Http_Response::fromString($response_text);
  99. $this->assertEquals(500, $response->getStatus(), 'Response code is expected to be 500, but it\'s not.');
  100. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  101. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  102. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  103. }
  104. public function test300isRedirect()
  105. {
  106. $response = Zend_Http_Response::fromString($this->readResponse('response_302'));
  107. $this->assertEquals(302, $response->getStatus(), 'Response code is expected to be 302, but it\'s not.');
  108. $this->assertTrue($response->isRedirect(), 'Response is a redirection, but isRedirect() returned false');
  109. $this->assertFalse($response->isError(), 'Response is a redirection, but isError() returned true');
  110. $this->assertFalse($response->isSuccessful(), 'Response is a redirection, but isSuccessful() returned true');
  111. }
  112. public function test200Ok()
  113. {
  114. $response = Zend_Http_Response::fromString($this->readResponse('response_deflate'));
  115. $this->assertEquals(200, $response->getStatus(), 'Response code is expected to be 200, but it\'s not.');
  116. $this->assertFalse($response->isError(), 'Response is OK, but isError() returned true');
  117. $this->assertTrue($response->isSuccessful(), 'Response is OK, but isSuccessful() returned false');
  118. $this->assertFalse($response->isRedirect(), 'Response is OK, but isRedirect() returned true');
  119. }
  120. public function test100Continue()
  121. {
  122. $this->markTestIncomplete();
  123. }
  124. public function testAutoMessageSet()
  125. {
  126. $response = Zend_Http_Response::fromString($this->readResponse('response_403_nomessage'));
  127. $this->assertEquals(403, $response->getStatus(), 'Response status is expected to be 403, but it isn\'t');
  128. $this->assertEquals('Forbidden', $response->getMessage(), 'Response is 403, but message is not "Forbidden" as expected');
  129. // While we're here, make sure it's classified as error...
  130. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  131. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  132. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  133. }
  134. public function testAsString()
  135. {
  136. $response_str = $this->readResponse('response_404');
  137. $response = Zend_Http_Response::fromString($response_str);
  138. $this->assertEquals(strtolower($response_str), strtolower($response->asString()), 'Response convertion to string does not match original string');
  139. $this->assertEquals(strtolower($response_str), strtolower((string)$response), 'Response convertion to string does not match original string');
  140. }
  141. public function testGetHeaders()
  142. {
  143. $response = Zend_Http_Response::fromString($this->readResponse('response_deflate'));
  144. $headers = $response->getHeaders();
  145. $this->assertEquals(8, count($headers), 'Header count is not as expected');
  146. $this->assertEquals('Apache', $headers['Server'], 'Server header is not as expected');
  147. $this->assertEquals('deflate', $headers['Content-encoding'], 'Content-type header is not as expected');
  148. }
  149. public function testGetVersion()
  150. {
  151. $response = Zend_Http_Response::fromString($this->readResponse('response_chunked'));
  152. $this->assertEquals(1.1, $response->getVersion(), 'Version is expected to be 1.1');
  153. }
  154. public function testResponseCodeAsText()
  155. {
  156. // This is an entirely static test
  157. // Test some response codes
  158. $this->assertEquals('Continue', Zend_Http_Response::responseCodeAsText(100));
  159. $this->assertEquals('OK', Zend_Http_Response::responseCodeAsText(200));
  160. $this->assertEquals('Multiple Choices', Zend_Http_Response::responseCodeAsText(300));
  161. $this->assertEquals('Bad Request', Zend_Http_Response::responseCodeAsText(400));
  162. $this->assertEquals('Internal Server Error', Zend_Http_Response::responseCodeAsText(500));
  163. // Make sure that invalid codes return 'Unkown'
  164. $this->assertEquals('Unknown', Zend_Http_Response::responseCodeAsText(600));
  165. // Check HTTP/1.0 value for 302
  166. $this->assertEquals('Found', Zend_Http_Response::responseCodeAsText(302));
  167. $this->assertEquals('Moved Temporarily', Zend_Http_Response::responseCodeAsText(302, false));
  168. // Check we get an array if no code is passed
  169. $codes = Zend_Http_Response::responseCodeAsText();
  170. $this->assertType('array', $codes);
  171. $this->assertEquals('OK', $codes[200]);
  172. }
  173. public function testUnknownCode()
  174. {
  175. $response_str = $this->readResponse('response_unknown');
  176. $response = Zend_Http_Response::fromString($response_str);
  177. // Check that dynamically the message is parsed
  178. $this->assertEquals(550, $response->getStatus(), 'Status is expected to be a non-standard 550');
  179. $this->assertEquals('Printer On Fire', $response->getMessage(), 'Message is expected to be extracted');
  180. // Check that statically, an Unknown string is returned for the 550 code
  181. $this->assertEquals('Unknown', Zend_Http_Response::responseCodeAsText($response_str));
  182. }
  183. public function testMultilineHeader()
  184. {
  185. $response = Zend_Http_Response::fromString($this->readResponse('response_multiline_header'));
  186. // Make sure we got the corrent no. of headers
  187. $this->assertEquals(6, count($response->getHeaders()), 'Header count is expected to be 6');
  188. // Check header integrity
  189. $this->assertEquals('timeout=15, max=100', $response->getHeader('keep-alive'));
  190. $this->assertEquals('text/html; charset=iso-8859-1', $response->getHeader('content-type'));
  191. }
  192. public function testExceptInvalidChunkedBody()
  193. {
  194. try {
  195. Zend_Http_Response::decodeChunkedBody($this->readResponse('response_deflate'));
  196. $this->fail('An expected exception was not thrown');
  197. } catch (Zend_Http_Exception $e) {
  198. // We are ok!
  199. }
  200. }
  201. public function testExtractorsOnInvalidString()
  202. {
  203. // Try with an empty string
  204. $response_str = '';
  205. $this->assertTrue(Zend_Http_Response::extractCode($response_str) === false);
  206. $this->assertTrue(Zend_Http_Response::extractMessage($response_str) === false);
  207. $this->assertTrue(Zend_Http_Response::extractVersion($response_str) === false);
  208. $this->assertTrue(Zend_Http_Response::extractBody($response_str) === '');
  209. $this->assertTrue(Zend_Http_Response::extractHeaders($response_str) === array());
  210. }
  211. /**
  212. * Make sure a response with some leading whitespace in the response body
  213. * does not get modified (see ZF-1924)
  214. *
  215. */
  216. public function testLeadingWhitespaceBody()
  217. {
  218. $body = Zend_Http_Response::extractBody($this->readResponse('response_leadingws'));
  219. $this->assertEquals($body, "\r\n\t \n\r\tx", 'Extracted body is not identical to expected body');
  220. }
  221. /**
  222. * Helper function: read test response from file
  223. *
  224. * @param string $response
  225. * @return string
  226. */
  227. protected function readResponse($response)
  228. {
  229. return file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $response);
  230. }
  231. }