ResponseTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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-2015 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. * Zend_Http_Response
  24. */
  25. require_once 'Zend/Http/Response.php';
  26. /**
  27. * Zend_Http_Response unit tests
  28. *
  29. * @category Zend
  30. * @package Zend_Http_Response
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Http
  35. * @group Zend_Http_Response
  36. */
  37. class Zend_Http_ResponseTest extends PHPUnit_Framework_TestCase
  38. {
  39. public function setUp()
  40. { }
  41. public function testGzipResponse ()
  42. {
  43. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_gzip');
  44. $res = Zend_Http_Response::fromString($response_text);
  45. $this->assertEquals('gzip', $res->getHeader('Content-encoding'));
  46. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  47. $this->assertEquals('f24dd075ba2ebfb3bf21270e3fdc5303', md5($res->getRawBody()));
  48. }
  49. public function testDeflateResponse ()
  50. {
  51. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_deflate');
  52. $res = Zend_Http_Response::fromString($response_text);
  53. $this->assertEquals('deflate', $res->getHeader('Content-encoding'));
  54. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  55. $this->assertEquals('ad62c21c3aa77b6a6f39600f6dd553b8', md5($res->getRawBody()));
  56. }
  57. /**
  58. * Make sure we can handle non-RFC complient "deflate" responses.
  59. *
  60. * Unlike stanrdard 'deflate' response, those do not contain the zlib header
  61. * and trailer. Unfortunately some buggy servers (read: IIS) send those and
  62. * we need to support them.
  63. *
  64. * @link http://framework.zend.com/issues/browse/ZF-6040
  65. */
  66. public function testNonStandardDeflateResponseZF6040()
  67. {
  68. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_deflate_iis');
  69. // Ensure headers are correctly formatted (i.e., separated with "\r\n" sequence)
  70. //
  71. // Line endings are an issue inside the canned response; the
  72. // following uses a negative lookbehind assertion, and replaces any \n
  73. // not preceded by \r with the sequence \r\n within the headers,
  74. // ensuring that the message is well-formed.
  75. list($headers, $message) = explode("\n\n", $response_text, 2);
  76. $headers = preg_replace("#(?<!\r)\n#", "\r\n", $headers);
  77. $response_text = $headers . "\r\n\r\n" . $message;
  78. $res = Zend_Http_Response::fromString($response_text);
  79. $this->assertEquals('deflate', $res->getHeader('Content-encoding'));
  80. $this->assertEquals('d82c87e3d5888db0193a3fb12396e616', md5($res->getBody()));
  81. $this->assertEquals('c830dd74bb502443cf12514c185ff174', md5($res->getRawBody()));
  82. }
  83. public function testChunkedResponse ()
  84. {
  85. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_chunked');
  86. $res = Zend_Http_Response::fromString($response_text);
  87. $this->assertEquals('chunked', $res->getHeader('Transfer-encoding'));
  88. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  89. $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getRawBody()));
  90. }
  91. public function testChunkedResponseCaseInsensitiveZF5438()
  92. {
  93. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_chunked_case');
  94. $res = Zend_Http_Response::fromString($response_text);
  95. $this->assertEquals('chunked', strtolower($res->getHeader('Transfer-encoding')));
  96. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  97. $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getRawBody()));
  98. }
  99. public function testExtractMessageCrlf()
  100. {
  101. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_crlf');
  102. $this->assertEquals("OK", Zend_Http_Response::extractMessage($response_text), "Response message is not 'OK' as expected");
  103. }
  104. public function testExtractMessageLfonly()
  105. {
  106. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_lfonly');
  107. $this->assertEquals("OK", Zend_Http_Response::extractMessage($response_text), "Response message is not 'OK' as expected");
  108. }
  109. public function test404IsError()
  110. {
  111. $response_text = $this->readResponse('response_404');
  112. $response = Zend_Http_Response::fromString($response_text);
  113. $this->assertEquals(404, $response->getStatus(), 'Response code is expected to be 404, but it\'s not.');
  114. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  115. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  116. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  117. }
  118. public function test500isError()
  119. {
  120. $response_text = $this->readResponse('response_500');
  121. $response = Zend_Http_Response::fromString($response_text);
  122. $this->assertEquals(500, $response->getStatus(), 'Response code is expected to be 500, but it\'s not.');
  123. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  124. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  125. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  126. }
  127. /**
  128. * @group ZF-5520
  129. */
  130. public function test302LocationHeaderMatches()
  131. {
  132. $headerName = 'Location';
  133. $headerValue = 'http://www.google.com/ig?hl=en';
  134. $response = Zend_Http_Response::fromString($this->readResponse('response_302'));
  135. $responseIis = Zend_Http_Response::fromString($this->readResponse('response_302_iis'));
  136. $this->assertEquals($headerValue, $response->getHeader($headerName));
  137. $this->assertEquals($headerValue, $responseIis->getHeader($headerName));
  138. }
  139. public function test300isRedirect()
  140. {
  141. $response = Zend_Http_Response::fromString($this->readResponse('response_302'));
  142. $this->assertEquals(302, $response->getStatus(), 'Response code is expected to be 302, but it\'s not.');
  143. $this->assertTrue($response->isRedirect(), 'Response is a redirection, but isRedirect() returned false');
  144. $this->assertFalse($response->isError(), 'Response is a redirection, but isError() returned true');
  145. $this->assertFalse($response->isSuccessful(), 'Response is a redirection, but isSuccessful() returned true');
  146. }
  147. public function test200Ok()
  148. {
  149. $response = Zend_Http_Response::fromString($this->readResponse('response_deflate'));
  150. $this->assertEquals(200, $response->getStatus(), 'Response code is expected to be 200, but it\'s not.');
  151. $this->assertFalse($response->isError(), 'Response is OK, but isError() returned true');
  152. $this->assertTrue($response->isSuccessful(), 'Response is OK, but isSuccessful() returned false');
  153. $this->assertFalse($response->isRedirect(), 'Response is OK, but isRedirect() returned true');
  154. }
  155. public function test100Continue()
  156. {
  157. $this->markTestIncomplete();
  158. }
  159. public function testAutoMessageSet()
  160. {
  161. $response = Zend_Http_Response::fromString($this->readResponse('response_403_nomessage'));
  162. $this->assertEquals(403, $response->getStatus(), 'Response status is expected to be 403, but it isn\'t');
  163. $this->assertEquals('Forbidden', $response->getMessage(), 'Response is 403, but message is not "Forbidden" as expected');
  164. // While we're here, make sure it's classified as error...
  165. $this->assertTrue($response->isError(), 'Response is an error, but isError() returned false');
  166. $this->assertFalse($response->isSuccessful(), 'Response is an error, but isSuccessful() returned true');
  167. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  168. }
  169. public function testAsString()
  170. {
  171. $response_str = $this->readResponse('response_404');
  172. $response = Zend_Http_Response::fromString($response_str);
  173. $this->assertEquals(strtolower($response_str), strtolower($response->asString()), 'Response conversion to string does not match original string');
  174. $this->assertEquals(strtolower($response_str), strtolower((string) $response), 'Response conversion to string does not match original string');
  175. }
  176. public function testGetHeaders()
  177. {
  178. $response = Zend_Http_Response::fromString($this->readResponse('response_deflate'));
  179. $headers = $response->getHeaders();
  180. $this->assertEquals(8, count($headers), 'Header count is not as expected');
  181. $this->assertEquals('Apache', $headers['Server'], 'Server header is not as expected');
  182. $this->assertEquals('deflate', $headers['Content-encoding'], 'Content-type header is not as expected');
  183. }
  184. public function testGetVersion()
  185. {
  186. $response = Zend_Http_Response::fromString($this->readResponse('response_chunked'));
  187. $this->assertEquals(1.1, $response->getVersion(), 'Version is expected to be 1.1');
  188. }
  189. public function testResponseCodeAsText()
  190. {
  191. // This is an entirely static test
  192. // Test some response codes
  193. $this->assertEquals('Continue', Zend_Http_Response::responseCodeAsText(100));
  194. $this->assertEquals('OK', Zend_Http_Response::responseCodeAsText(200));
  195. $this->assertEquals('Multiple Choices', Zend_Http_Response::responseCodeAsText(300));
  196. $this->assertEquals('Bad Request', Zend_Http_Response::responseCodeAsText(400));
  197. $this->assertEquals('Internal Server Error', Zend_Http_Response::responseCodeAsText(500));
  198. // Make sure that invalid codes return 'Unknown'
  199. $this->assertEquals('Unknown', Zend_Http_Response::responseCodeAsText(600));
  200. // Check HTTP/1.0 value for 302
  201. $this->assertEquals('Found', Zend_Http_Response::responseCodeAsText(302));
  202. $this->assertEquals('Moved Temporarily', Zend_Http_Response::responseCodeAsText(302, false));
  203. // Check we get an array if no code is passed
  204. $codes = Zend_Http_Response::responseCodeAsText();
  205. $this->assertTrue(is_array($codes));
  206. $this->assertEquals('OK', $codes[200]);
  207. }
  208. public function testUnknownCode()
  209. {
  210. $response_str = $this->readResponse('response_unknown');
  211. $response = Zend_Http_Response::fromString($response_str);
  212. // Check that dynamically the message is parsed
  213. $this->assertEquals(550, $response->getStatus(), 'Status is expected to be a non-standard 550');
  214. $this->assertEquals('Printer On Fire', $response->getMessage(), 'Message is expected to be extracted');
  215. // Check that statically, an Unknown string is returned for the 550 code
  216. $this->assertEquals('Unknown', Zend_Http_Response::responseCodeAsText($response_str));
  217. }
  218. public function testMultilineHeader()
  219. {
  220. $response = Zend_Http_Response::fromString($this->readResponse('response_multiline_header'));
  221. // Make sure we got the corrent no. of headers
  222. $this->assertEquals(6, count($response->getHeaders()), 'Header count is expected to be 6');
  223. // Check header integrity
  224. $this->assertEquals('timeout=15, max=100', $response->getHeader('keep-alive'));
  225. $this->assertEquals('text/html; charset=iso-8859-1', $response->getHeader('content-type'));
  226. }
  227. public function testExceptInvalidChunkedBody()
  228. {
  229. try {
  230. Zend_Http_Response::decodeChunkedBody($this->readResponse('response_deflate'));
  231. $this->fail('An expected exception was not thrown');
  232. } catch (Zend_Http_Exception $e) {
  233. // We are ok!
  234. }
  235. }
  236. public function testExtractorsOnInvalidString()
  237. {
  238. // Try with an empty string
  239. $response_str = '';
  240. $this->assertTrue(Zend_Http_Response::extractCode($response_str) === false);
  241. $this->assertTrue(Zend_Http_Response::extractMessage($response_str) === false);
  242. $this->assertTrue(Zend_Http_Response::extractVersion($response_str) === false);
  243. $this->assertTrue(Zend_Http_Response::extractBody($response_str) === '');
  244. $this->assertTrue(Zend_Http_Response::extractHeaders($response_str) === array());
  245. }
  246. /**
  247. * Make sure a response with some leading whitespace in the response body
  248. * does not get modified (see ZF-1924)
  249. *
  250. */
  251. public function testLeadingWhitespaceBody()
  252. {
  253. $message = file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'response_leadingws');
  254. $body = Zend_Http_Response::extractBody($message);
  255. $this->assertEquals($body, "\r\n\t \n\r\tx", 'Extracted body is not identical to expected body');
  256. }
  257. /**
  258. * Test that parsing a multibyte-encoded chunked response works.
  259. *
  260. * This can potentially fail on different PHP environments - for example
  261. * when mbstring.func_overload is set to overload strlen().
  262. *
  263. */
  264. public function testMultibyteChunkedResponse()
  265. {
  266. $md5 = 'f734924685f92b243c8580848cadc560';
  267. $response = Zend_Http_Response::fromString($this->readResponse('response_multibyte_body'));
  268. $this->assertEquals($md5, md5($response->getBody()));
  269. }
  270. /**
  271. * Test that headers are properly set when passed to the constructor as an associative array
  272. *
  273. * @group ZF-10277
  274. */
  275. public function testConstructorWithHeadersAssocArray()
  276. {
  277. $response = new Zend_Http_Response(200, array(
  278. 'content-type' => 'text/plain',
  279. 'x-foo' => 'bar:baz'
  280. ));
  281. $this->assertEquals('text/plain', $response->getHeader('content-type'));
  282. $this->assertEquals('bar:baz', $response->getHeader('x-foo'));
  283. }
  284. /**
  285. * Test that headers are properly parsed when passed to the constructor as an indexed array
  286. *
  287. * @link http://framework.zend.com/issues/browse/ZF-10277
  288. * @group ZF-10277
  289. */
  290. public function testConstructorWithHeadersIndexedArrayZF10277()
  291. {
  292. $response = new Zend_Http_Response(200, array(
  293. 'content-type: text/plain',
  294. 'x-foo: bar:baz'
  295. ));
  296. $this->assertEquals('text/plain', $response->getHeader('content-type'));
  297. $this->assertEquals('bar:baz', $response->getHeader('x-foo'));
  298. }
  299. /**
  300. * Test that headers are properly parsed when passed to the constructor as
  301. * an indexed array with no whitespace after the ':' sign
  302. *
  303. * @link http://framework.zend.com/issues/browse/ZF-10277
  304. * @group ZF-10277
  305. */
  306. public function testConstructorWithHeadersIndexedArrayNoWhitespace()
  307. {
  308. $response = new Zend_Http_Response(200, array(
  309. 'content-type:text/plain',
  310. 'x-foo:bar:baz'
  311. ));
  312. $this->assertEquals('text/plain', $response->getHeader('content-type'));
  313. $this->assertEquals('bar:baz', $response->getHeader('x-foo'));
  314. }
  315. /**
  316. * Helper function: read test response from file
  317. *
  318. * @param string $response
  319. * @return string
  320. */
  321. protected function readResponse($response)
  322. {
  323. $message = file_get_contents(
  324. dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $response
  325. );
  326. // Line endings are sometimes an issue inside the canned responses; the
  327. // following is a negative lookbehind assertion, and replaces any \n
  328. // not preceded by \r with the sequence \r\n, ensuring that the message
  329. // is well-formed.
  330. return preg_replace("#(?<!\r)\n#", "\r\n", $message);
  331. }
  332. public function invalidResponseHeaders()
  333. {
  334. return array(
  335. 'bad-status-line' => array("HTTP/1.0a 200 OK\r\nHost: example.com\r\n\r\nMessage Body"),
  336. 'nl-in-header' => array("HTTP/1.1 200 OK\r\nHost: example.\ncom\r\n\r\nMessage Body"),
  337. 'cr-in-header' => array("HTTP/1.1 200 OK\r\nHost: example.\rcom\r\n\r\nMessage Body"),
  338. 'bad-continuation' => array("HTTP/1.1 200 OK\r\nHost: example.\r\ncom\r\n\r\nMessage Body"),
  339. 'no-status-nl-in-header' => array("Host: example.\ncom\r\n\r\nMessage Body"),
  340. 'no-status-cr-in-header' => array("Host: example.\rcom\r\n\r\nMessage Body"),
  341. 'no-status-bad-continuation' => array("Host: example.\r\ncom\r\n\r\nMessage Body"),
  342. );
  343. }
  344. /**
  345. * @group ZF2015-04
  346. * @dataProvider invalidResponseHeaders
  347. */
  348. public function testExtractHeadersRaisesExceptionWhenDetectingCRLFInjection($message)
  349. {
  350. $this->setExpectedException('Zend_Http_Exception', 'Invalid');
  351. Zend_Http_Response::extractHeaders($message);
  352. }
  353. /**
  354. * @group 587
  355. */
  356. public function testExtractHeadersShouldAllowAnyValidHttpHeaderToken()
  357. {
  358. $response = $this->readResponse('response_587');
  359. $headers = Zend_Http_Response::extractHeaders($response);
  360. $this->assertArrayHasKey('zipi.step', $headers);
  361. $this->assertEquals(0, $headers['zipi.step']);
  362. }
  363. /**
  364. * @group 587
  365. */
  366. public function testExtractHeadersShouldAllowHeadersWithEmptyValues()
  367. {
  368. $response = $this->readResponse('response_587_empty');
  369. $headers = Zend_Http_Response::extractHeaders($response);
  370. $this->assertArrayHasKey('imagetoolbar', $headers);
  371. $this->assertEmpty($headers['imagetoolbar']);
  372. }
  373. /**
  374. * @group 587
  375. */
  376. public function testExtractHeadersShouldAllowHeadersWithMissingValues()
  377. {
  378. $response = $this->readResponse('response_587_null');
  379. $headers = Zend_Http_Response::extractHeaders($response);
  380. $this->assertArrayHasKey('imagetoolbar', $headers);
  381. $this->assertEmpty($headers['imagetoolbar']);
  382. }
  383. }