2
0

ResponseTest.php 13 KB

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