PageRenderTime 65ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Http/ResponseTest.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 390 lines | 230 code | 66 blank | 94 comment | 0 complexity | ee013db5f22389062105a40540bd8139 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: ResponseTest.php 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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 wer 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. $res = Zend_Http_Response::fromString($response_text);
  70. $this->assertEquals('deflate', $res->getHeader('Content-encoding'));
  71. $this->assertEquals('d82c87e3d5888db0193a3fb12396e616', md5($res->getBody()));
  72. $this->assertEquals('c830dd74bb502443cf12514c185ff174', md5($res->getRawBody()));
  73. }
  74. public function testChunkedResponse ()
  75. {
  76. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_chunked');
  77. $res = Zend_Http_Response::fromString($response_text);
  78. $this->assertEquals('chunked', $res->getHeader('Transfer-encoding'));
  79. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  80. $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getRawBody()));
  81. }
  82. public function testChunkedResponseCaseInsensitiveZF5438()
  83. {
  84. $response_text = file_get_contents(dirname(__FILE__) . '/_files/response_chunked_case');
  85. $res = Zend_Http_Response::fromString($response_text);
  86. $this->assertEquals('chunked', strtolower($res->getHeader('Transfer-encoding')));
  87. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  88. $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getRawBody()));
  89. }
  90. public function testLineBreaksCompatibility()
  91. {
  92. $response_text_lf = $this->readResponse('response_lfonly');
  93. $res_lf = Zend_Http_Response::fromString($response_text_lf);
  94. $response_text_crlf = $this->readResponse('response_crlf');
  95. $res_crlf = Zend_Http_Response::fromString($response_text_crlf);
  96. $this->assertEquals($res_lf->getHeadersAsString(true), $res_crlf->getHeadersAsString(true), 'Responses headers do not match');
  97. $this->assertEquals($res_lf->getBody(), $res_crlf->getBody(), 'Response bodies do not match');
  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 convertion to string does not match original string');
  174. $this->assertEquals(strtolower($response_str), strtolower((string)$response), 'Response convertion 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 'Unkown'
  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->assertType('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. $body = Zend_Http_Response::extractBody($this->readResponse('response_leadingws'));
  254. $this->assertEquals($body, "\r\n\t \n\r\tx", 'Extracted body is not identical to expected body');
  255. }
  256. /**
  257. * Test that parsing a multibyte-encoded chunked response works.
  258. *
  259. * This can potentially fail on different PHP environments - for example
  260. * when mbstring.func_overload is set to overload strlen().
  261. *
  262. */
  263. public function testMultibyteChunkedResponse()
  264. {
  265. $md5 = 'ab952f1617d0e28724932401f2d3c6ae';
  266. $response = Zend_Http_Response::fromString($this->readResponse('response_multibyte_body'));
  267. $this->assertEquals($md5, md5($response->getBody()));
  268. }
  269. /**
  270. * Test that headers are properly set when passed to the constructor as an associative array
  271. *
  272. * @group ZF-10277
  273. */
  274. public function testConstructorWithHeadersAssocArray()
  275. {
  276. $response = new Zend_Http_Response(200, array(
  277. 'content-type' => 'text/plain',
  278. 'x-foo' => 'bar:baz'
  279. ));
  280. $this->assertEquals('text/plain', $response->getHeader('content-type'));
  281. $this->assertEquals('bar:baz', $response->getHeader('x-foo'));
  282. }
  283. /**
  284. * Test that headers are properly parsed when passed to the constructor as an indexed array
  285. *
  286. * @link http://framework.zend.com/issues/browse/ZF-10277
  287. * @group ZF-10277
  288. */
  289. public function testConstructorWithHeadersIndexedArrayZF10277()
  290. {
  291. $response = new Zend_Http_Response(200, array(
  292. 'content-type: text/plain',
  293. 'x-foo: bar:baz'
  294. ));
  295. $this->assertEquals('text/plain', $response->getHeader('content-type'));
  296. $this->assertEquals('bar:baz', $response->getHeader('x-foo'));
  297. }
  298. /**
  299. * Test that headers are properly parsed when passed to the constructor as
  300. * an indexed array with no whitespace after the ':' sign
  301. *
  302. * @link http://framework.zend.com/issues/browse/ZF-10277
  303. * @group ZF-10277
  304. */
  305. public function testConstructorWithHeadersIndexedArrayNoWhitespace()
  306. {
  307. $response = new Zend_Http_Response(200, array(
  308. 'content-type:text/plain',
  309. 'x-foo:bar:baz'
  310. ));
  311. $this->assertEquals('text/plain', $response->getHeader('content-type'));
  312. $this->assertEquals('bar:baz', $response->getHeader('x-foo'));
  313. }
  314. /**
  315. * Helper function: read test response from file
  316. *
  317. * @param string $response
  318. * @return string
  319. */
  320. protected function readResponse($response)
  321. {
  322. return file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $response);
  323. }
  324. }