PageRenderTime 25ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Http/ResponseTest.php

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