ResponseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http_Response
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  26. /**
  27. * Zend_Http_Response
  28. */
  29. require_once 'Zend/Http/Response.php';
  30. /**
  31. * PHPUnit test case
  32. */
  33. require_once 'PHPUnit/Framework/TestCase.php';
  34. /**
  35. * Zend_Http_Response unit tests
  36. *
  37. * @category Zend
  38. * @package Zend_Http_Response
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Http
  43. * @group Zend_Http_Response
  44. */
  45. class Zend_Http_ResponseTest extends PHPUnit_Framework_TestCase
  46. {
  47. public function setUp()
  48. { }
  49. public function testGzipResponse ()
  50. {
  51. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_gzip');
  52. $res = Zend_Http_Response::fromString($response_text);
  53. $this->assertEquals('gzip', $res->getHeader('Content-encoding'));
  54. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  55. $this->assertEquals('f24dd075ba2ebfb3bf21270e3fdc5303', md5($res->getRawBody()));
  56. }
  57. public function testDeflateResponse ()
  58. {
  59. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_deflate');
  60. $res = Zend_Http_Response::fromString($response_text);
  61. $this->assertEquals('deflate', $res->getHeader('Content-encoding'));
  62. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  63. $this->assertEquals('ad62c21c3aa77b6a6f39600f6dd553b8', md5($res->getRawBody()));
  64. }
  65. /**
  66. * Make sure wer can handle non-RFC complient "deflate" responses.
  67. *
  68. * Unlike stanrdard 'deflate' response, those do not contain the zlib header
  69. * and trailer. Unfortunately some buggy servers (read: IIS) send those and
  70. * we need to support them.
  71. *
  72. * @link http://framework.zend.com/issues/browse/ZF-6040
  73. */
  74. public function testNonStandardDeflateResponseZF6040()
  75. {
  76. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_deflate_iis');
  77. $res = Zend_Http_Response::fromString($response_text);
  78. $this->assertEquals('deflate', $res->getHeader('Content-encoding'));
  79. $this->assertEquals('d82c87e3d5888db0193a3fb12396e616', md5($res->getBody()));
  80. $this->assertEquals('c830dd74bb502443cf12514c185ff174', md5($res->getRawBody()));
  81. }
  82. public function testChunkedResponse ()
  83. {
  84. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_chunked');
  85. $res = Zend_Http_Response::fromString($response_text);
  86. $this->assertEquals('chunked', $res->getHeader('Transfer-encoding'));
  87. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  88. $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getRawBody()));
  89. }
  90. public function testChunkedResponseCaseInsensitiveZF5438()
  91. {
  92. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_chunked_case');
  93. $res = Zend_Http_Response::fromString($response_text);
  94. $this->assertEquals('chunked', strtolower($res->getHeader('Transfer-encoding')));
  95. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  96. $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getRawBody()));
  97. }
  98. public function testLineBreaksCompatibility()
  99. {
  100. $response_text_lf = $this->readResponse('response_lfonly');
  101. $res_lf = Zend_Http_Response::fromString($response_text_lf);
  102. $response_text_crlf = $this->readResponse('response_crlf');
  103. $res_crlf = Zend_Http_Response::fromString($response_text_crlf);
  104. $this->assertEquals($res_lf->getHeadersAsString(true), $res_crlf->getHeadersAsString(true), 'Responses headers do not match');
  105. $this->assertEquals($res_lf->getBody(), $res_crlf->getBody(), 'Response bodies do not match');
  106. }
  107. public function testExtractMessageCrlf()
  108. {
  109. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_crlf');
  110. $this->assertEquals("OK", Zend_Http_Response::extractMessage($response_text), "Response message is not 'OK' as expected");
  111. }
  112. public function testExtractMessageLfonly()
  113. {
  114. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_lfonly');
  115. $this->assertEquals("OK", Zend_Http_Response::extractMessage($response_text), "Response message is not 'OK' as expected");
  116. }
  117. public function test404IsError()
  118. {
  119. $response_text = $this->readResponse('response_404');
  120. $response = Zend_Http_Response::fromString($response_text);
  121. $this->assertEquals(404, $response->getStatus(), 'Response code is expected to be 404, but it\'s not.');
  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 test500isError()
  127. {
  128. $response_text = $this->readResponse('response_500');
  129. $response = Zend_Http_Response::fromString($response_text);
  130. $this->assertEquals(500, $response->getStatus(), 'Response code is expected to be 500, but it\'s not.');
  131. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  132. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  133. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  134. }
  135. /**
  136. * @group ZF-5520
  137. */
  138. public function test302LocationHeaderMatches()
  139. {
  140. $headerName = 'Location';
  141. $headerValue = 'http://www.google.com/ig?hl=en';
  142. $response = Zend_Http_Response::fromString($this->readResponse('response_302'));
  143. $responseIis = Zend_Http_Response::fromString($this->readResponse('response_302_iis'));
  144. $this->assertEquals($headerValue, $response->getHeader($headerName));
  145. $this->assertEquals($headerValue, $responseIis->getHeader($headerName));
  146. }
  147. public function test300isRedirect()
  148. {
  149. $response = Zend_Http_Response::fromString($this->readResponse('response_302'));
  150. $this->assertEquals(302, $response->getStatus(), 'Response code is expected to be 302, but it\'s not.');
  151. $this->assertTrue($response->isRedirect(), 'Response is a redirection, but isRedirect() returned false');
  152. $this->assertFalse($response->isError(), 'Response is a redirection, but isError() returned true');
  153. $this->assertFalse($response->isSuccessful(), 'Response is a redirection, but isSuccessful() returned true');
  154. }
  155. public function test200Ok()
  156. {
  157. $response = Zend_Http_Response::fromString($this->readResponse('response_deflate'));
  158. $this->assertEquals(200, $response->getStatus(), 'Response code is expected to be 200, but it\'s not.');
  159. $this->assertFalse($response->isError(), 'Response is OK, but isError() returned true');
  160. $this->assertTrue($response->isSuccessful(), 'Response is OK, but isSuccessful() returned false');
  161. $this->assertFalse($response->isRedirect(), 'Response is OK, but isRedirect() returned true');
  162. }
  163. public function test100Continue()
  164. {
  165. $this->markTestIncomplete();
  166. }
  167. public function testAutoMessageSet()
  168. {
  169. $response = Zend_Http_Response::fromString($this->readResponse('response_403_nomessage'));
  170. $this->assertEquals(403, $response->getStatus(), 'Response status is expected to be 403, but it isn\'t');
  171. $this->assertEquals('Forbidden', $response->getMessage(), 'Response is 403, but message is not "Forbidden" as expected');
  172. // While we're here, make sure it's classified as error...
  173. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  174. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  175. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  176. }
  177. public function testAsString()
  178. {
  179. $response_str = $this->readResponse('response_404');
  180. $response = Zend_Http_Response::fromString($response_str);
  181. $this->assertEquals(strtolower($response_str), strtolower($response->asString()), 'Response convertion to string does not match original string');
  182. $this->assertEquals(strtolower($response_str), strtolower((string)$response), 'Response convertion to string does not match original string');
  183. }
  184. public function testGetHeaders()
  185. {
  186. $response = Zend_Http_Response::fromString($this->readResponse('response_deflate'));
  187. $headers = $response->getHeaders();
  188. $this->assertEquals(8, count($headers), 'Header count is not as expected');
  189. $this->assertEquals('Apache', $headers['Server'], 'Server header is not as expected');
  190. $this->assertEquals('deflate', $headers['Content-encoding'], 'Content-type header is not as expected');
  191. }
  192. public function testGetVersion()
  193. {
  194. $response = Zend_Http_Response::fromString($this->readResponse('response_chunked'));
  195. $this->assertEquals(1.1, $response->getVersion(), 'Version is expected to be 1.1');
  196. }
  197. public function testResponseCodeAsText()
  198. {
  199. // This is an entirely static test
  200. // Test some response codes
  201. $this->assertEquals('Continue', Zend_Http_Response::responseCodeAsText(100));
  202. $this->assertEquals('OK', Zend_Http_Response::responseCodeAsText(200));
  203. $this->assertEquals('Multiple Choices', Zend_Http_Response::responseCodeAsText(300));
  204. $this->assertEquals('Bad Request', Zend_Http_Response::responseCodeAsText(400));
  205. $this->assertEquals('Internal Server Error', Zend_Http_Response::responseCodeAsText(500));
  206. // Make sure that invalid codes return 'Unkown'
  207. $this->assertEquals('Unknown', Zend_Http_Response::responseCodeAsText(600));
  208. // Check HTTP/1.0 value for 302
  209. $this->assertEquals('Found', Zend_Http_Response::responseCodeAsText(302));
  210. $this->assertEquals('Moved Temporarily', Zend_Http_Response::responseCodeAsText(302, false));
  211. // Check we get an array if no code is passed
  212. $codes = Zend_Http_Response::responseCodeAsText();
  213. $this->assertType('array', $codes);
  214. $this->assertEquals('OK', $codes[200]);
  215. }
  216. public function testUnknownCode()
  217. {
  218. $response_str = $this->readResponse('response_unknown');
  219. $response = Zend_Http_Response::fromString($response_str);
  220. // Check that dynamically the message is parsed
  221. $this->assertEquals(550, $response->getStatus(), 'Status is expected to be a non-standard 550');
  222. $this->assertEquals('Printer On Fire', $response->getMessage(), 'Message is expected to be extracted');
  223. // Check that statically, an Unknown string is returned for the 550 code
  224. $this->assertEquals('Unknown', Zend_Http_Response::responseCodeAsText($response_str));
  225. }
  226. public function testMultilineHeader()
  227. {
  228. $response = Zend_Http_Response::fromString($this->readResponse('response_multiline_header'));
  229. // Make sure we got the corrent no. of headers
  230. $this->assertEquals(6, count($response->getHeaders()), 'Header count is expected to be 6');
  231. // Check header integrity
  232. $this->assertEquals('timeout=15, max=100', $response->getHeader('keep-alive'));
  233. $this->assertEquals('text/html; charset=iso-8859-1', $response->getHeader('content-type'));
  234. }
  235. public function testExceptInvalidChunkedBody()
  236. {
  237. try {
  238. Zend_Http_Response::decodeChunkedBody($this->readResponse('response_deflate'));
  239. $this->fail('An expected exception was not thrown');
  240. } catch (Zend_Http_Exception $e) {
  241. // We are ok!
  242. }
  243. }
  244. public function testExtractorsOnInvalidString()
  245. {
  246. // Try with an empty string
  247. $response_str = '';
  248. $this->assertTrue(Zend_Http_Response::extractCode($response_str) === false);
  249. $this->assertTrue(Zend_Http_Response::extractMessage($response_str) === false);
  250. $this->assertTrue(Zend_Http_Response::extractVersion($response_str) === false);
  251. $this->assertTrue(Zend_Http_Response::extractBody($response_str) === '');
  252. $this->assertTrue(Zend_Http_Response::extractHeaders($response_str) === array());
  253. }
  254. /**
  255. * Make sure a response with some leading whitespace in the response body
  256. * does not get modified (see ZF-1924)
  257. *
  258. */
  259. public function testLeadingWhitespaceBody()
  260. {
  261. $body = Zend_Http_Response::extractBody($this->readResponse('response_leadingws'));
  262. $this->assertEquals($body, "\r\n\t \n\r\tx", 'Extracted body is not identical to expected body');
  263. }
  264. /**
  265. * Test that parsing a multibyte-encoded chunked response works.
  266. *
  267. * This can potentially fail on different PHP environments - for example
  268. * when mbstring.func_overload is set to overload strlen().
  269. *
  270. */
  271. public function testMultibyteChunkedResponse()
  272. {
  273. $md5 = 'ab952f1617d0e28724932401f2d3c6ae';
  274. $response = Zend_Http_Response::fromString($this->readResponse('response_multibyte_body'));
  275. $this->assertEquals($md5, md5($response->getBody()));
  276. }
  277. /**
  278. * Helper function: read test response from file
  279. *
  280. * @param string $response
  281. * @return string
  282. */
  283. protected function readResponse($response)
  284. {
  285. return file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $response);
  286. }
  287. }