PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/tests/ZendTest/Http/ResponseTest.php

https://bitbucket.org/pcelta/zf2
PHP | 359 lines | 251 code | 63 blank | 45 comment | 0 complexity | 910eee6e8f81be6ab968b7875dab31cf MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Http
  9. */
  10. namespace ZendTest\Http;
  11. use Zend\Http\Response;
  12. class ResponseTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testResponseFactoryFromStringCreatesValidResponse()
  15. {
  16. $string = 'HTTP/1.0 200 OK' . "\r\n\r\n" . 'Foo Bar';
  17. $response = Response::fromString($string);
  18. $this->assertEquals(200, $response->getStatusCode());
  19. $this->assertEquals('Foo Bar', $response->getContent());
  20. }
  21. public function testResponseCanRenderStatusLine()
  22. {
  23. $response = new Response;
  24. $response->setVersion(1.1);
  25. $response->setStatusCode(Response::STATUS_CODE_404);
  26. $this->assertEquals('HTTP/1.1 404 Not Found', $response->renderStatusLine());
  27. $response->setReasonPhrase('Foo Bar');
  28. $this->assertEquals('HTTP/1.1 404 Foo Bar', $response->renderStatusLine());
  29. }
  30. public function testResponseUsesHeadersContainerByDefault()
  31. {
  32. $response = new Response();
  33. $this->assertInstanceOf('Zend\Http\Headers', $response->getHeaders());
  34. }
  35. public function testRequestCanSetHeaders()
  36. {
  37. $response = new Response();
  38. $headers = new \Zend\Http\Headers();
  39. $ret = $response->setHeaders($headers);
  40. $this->assertInstanceOf('Zend\Http\Response', $ret);
  41. $this->assertSame($headers, $response->getHeaders());
  42. }
  43. public function testResponseCanSetStatusCode()
  44. {
  45. $response = new Response;
  46. $this->assertEquals(200, $response->getStatusCode());
  47. $response->setStatusCode('303');
  48. $this->assertEquals(303, $response->getStatusCode());
  49. }
  50. public function testResponseSetStatusCodeThrowsExceptionOnInvalidCode()
  51. {
  52. $response = new Response;
  53. $this->setExpectedException('Zend\Http\Exception\InvalidArgumentException', 'Invalid status code');
  54. $response->setStatusCode(606);
  55. }
  56. public function testResponseEndsAtStatusCode()
  57. {
  58. $string = 'HTTP/1.0 200' . "\r\n\r\n" . 'Foo Bar';
  59. $response = Response::fromString($string);
  60. $this->assertEquals(200, $response->getStatusCode());
  61. $this->assertEquals('Foo Bar', $response->getContent());
  62. }
  63. public function testResponseHasZeroLengthReasonPhrase()
  64. {
  65. // Space after status code is mandatory,
  66. // though, reason phrase can be empty.
  67. // @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
  68. $string = 'HTTP/1.0 200 ' . "\r\n\r\n" . 'Foo Bar';
  69. $response = Response::fromString($string);
  70. $this->assertEquals(200, $response->getStatusCode());
  71. $this->assertEquals('Foo Bar', $response->getContent());
  72. // Reason phrase would fallback to default reason phrase.
  73. $this->assertEquals('OK', $response->getReasonPhrase());
  74. }
  75. public function testGzipResponse ()
  76. {
  77. $response_text = file_get_contents(__DIR__ . '/_files/response_gzip');
  78. $res = Response::fromString($response_text);
  79. $this->assertEquals('gzip', $res->getHeaders()->get('Content-encoding')->getFieldValue());
  80. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  81. $this->assertEquals('f24dd075ba2ebfb3bf21270e3fdc5303', md5($res->getContent()));
  82. }
  83. public function testDeflateResponse ()
  84. {
  85. $response_text = file_get_contents(__DIR__ . '/_files/response_deflate');
  86. $res = Response::fromString($response_text);
  87. $this->assertEquals('deflate', $res->getHeaders()->get('Content-encoding')->getFieldValue());
  88. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  89. $this->assertEquals('ad62c21c3aa77b6a6f39600f6dd553b8', md5($res->getContent()));
  90. }
  91. /**
  92. * Make sure wer can handle non-RFC complient "deflate" responses.
  93. *
  94. * Unlike stanrdard 'deflate' response, those do not contain the zlib header
  95. * and trailer. Unfortunately some buggy servers (read: IIS) send those and
  96. * we need to support them.
  97. *
  98. * @link http://framework.zend.com/issues/browse/ZF-6040
  99. */
  100. public function testNonStandardDeflateResponseZF6040()
  101. {
  102. $this->markTestSkipped('Not correctly handling non-RFC complient "deflate" responses');
  103. $response_text = file_get_contents(__DIR__ . '/_files/response_deflate_iis');
  104. $res = Response::fromString($response_text);
  105. $this->assertEquals('deflate', $res->getHeaders()->get('Content-encoding')->getFieldValue());
  106. $this->assertEquals('d82c87e3d5888db0193a3fb12396e616', md5($res->getBody()));
  107. $this->assertEquals('c830dd74bb502443cf12514c185ff174', md5($res->getContent()));
  108. }
  109. public function testChunkedResponse ()
  110. {
  111. $response_text = file_get_contents(__DIR__ . '/_files/response_chunked');
  112. $res = Response::fromString($response_text);
  113. $this->assertEquals('chunked', $res->getHeaders()->get('Transfer-encoding')->getFieldValue());
  114. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  115. $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getContent()));
  116. }
  117. public function testChunkedResponseCaseInsensitiveZF5438()
  118. {
  119. $response_text = file_get_contents(__DIR__ . '/_files/response_chunked_case');
  120. $res = Response::fromString($response_text);
  121. $this->assertEquals('chunked', strtolower($res->getHeaders()->get('Transfer-encoding')->getFieldValue()));
  122. $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
  123. $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getContent()));
  124. }
  125. public function testLineBreaksCompatibility()
  126. {
  127. $response_text_lf = $this->readResponse('response_lfonly');
  128. $res_lf = Response::fromString($response_text_lf);
  129. $response_text_crlf = $this->readResponse('response_crlf');
  130. $res_crlf = Response::fromString($response_text_crlf);
  131. $this->assertEquals($res_lf->getHeaders()->toString(), $res_crlf->getHeaders()->toString(), 'Responses headers do not match');
  132. $this->markTestIncomplete('Something is fishy with the response bodies in the test responses');
  133. $this->assertEquals($res_lf->getBody(), $res_crlf->getBody(), 'Response bodies do not match');
  134. }
  135. public function test404IsClientErrorAndNotFound()
  136. {
  137. $response_text = $this->readResponse('response_404');
  138. $response = Response::fromString($response_text);
  139. $this->assertEquals(404, $response->getStatusCode(), 'Response code is expected to be 404, but it\'s not.');
  140. $this->assertTrue($response->isClientError(), 'Response is an error, but isClientError() returned false');
  141. $this->assertFalse($response->isForbidden(), 'Response is an error, but isForbidden() returned true');
  142. $this->assertFalse($response->isInformational(), 'Response is an error, but isInformational() returned true');
  143. $this->assertTrue($response->isNotFound(), 'Response is an error, but isNotFound() returned false');
  144. $this->assertFalse($response->isOk(), 'Response is an error, but isOk() returned true');
  145. $this->assertFalse($response->isServerError(), 'Response is an error, but isServerError() returned true');
  146. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  147. $this->assertFalse($response->isSuccess(), 'Response is an error, but isSuccess() returned true');
  148. }
  149. public function test500isError()
  150. {
  151. $response_text = $this->readResponse('response_500');
  152. $response = Response::fromString($response_text);
  153. $this->assertEquals(500, $response->getStatusCode(), 'Response code is expected to be 500, but it\'s not.');
  154. $this->assertFalse($response->isClientError(), 'Response is an error, but isClientError() returned true');
  155. $this->assertFalse($response->isForbidden(), 'Response is an error, but isForbidden() returned true');
  156. $this->assertFalse($response->isInformational(), 'Response is an error, but isInformational() returned true');
  157. $this->assertFalse($response->isNotFound(), 'Response is an error, but isNotFound() returned true');
  158. $this->assertFalse($response->isOk(), 'Response is an error, but isOk() returned true');
  159. $this->assertTrue($response->isServerError(), 'Response is an error, but isServerError() returned false');
  160. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  161. $this->assertFalse($response->isSuccess(), 'Response is an error, but isSuccess() returned true');
  162. }
  163. /**
  164. * @group ZF-5520
  165. */
  166. public function test302LocationHeaderMatches()
  167. {
  168. $headerName = 'Location';
  169. $headerValue = 'http://www.google.com/ig?hl=en';
  170. $response = Response::fromString($this->readResponse('response_302'));
  171. $responseIis = Response::fromString($this->readResponse('response_302_iis'));
  172. $this->assertEquals($headerValue, $response->getHeaders()->get($headerName)->getFieldValue());
  173. $this->assertEquals($headerValue, $responseIis->getHeaders()->get($headerName)->getFieldValue());
  174. }
  175. public function test300isRedirect()
  176. {
  177. $response = Response::fromString($this->readResponse('response_302'));
  178. $this->assertEquals(302, $response->getStatusCode(), 'Response code is expected to be 302, but it\'s not.');
  179. $this->assertFalse($response->isClientError(), 'Response is an error, but isClientError() returned true');
  180. $this->assertFalse($response->isForbidden(), 'Response is an error, but isForbidden() returned true');
  181. $this->assertFalse($response->isInformational(), 'Response is an error, but isInformational() returned true');
  182. $this->assertFalse($response->isNotFound(), 'Response is an error, but isNotFound() returned true');
  183. $this->assertFalse($response->isOk(), 'Response is an error, but isOk() returned true');
  184. $this->assertFalse($response->isServerError(), 'Response is an error, but isServerError() returned true');
  185. $this->assertTrue($response->isRedirect(), 'Response is an error, but isRedirect() returned false');
  186. $this->assertFalse($response->isSuccess(), 'Response is an error, but isSuccess() returned true');
  187. }
  188. public function test200Ok()
  189. {
  190. $response = Response::fromString($this->readResponse('response_deflate'));
  191. $this->assertEquals(200, $response->getStatusCode(), 'Response code is expected to be 200, but it\'s not.');
  192. $this->assertFalse($response->isClientError(), 'Response is an error, but isClientError() returned true');
  193. $this->assertFalse($response->isForbidden(), 'Response is an error, but isForbidden() returned true');
  194. $this->assertFalse($response->isInformational(), 'Response is an error, but isInformational() returned true');
  195. $this->assertFalse($response->isNotFound(), 'Response is an error, but isNotFound() returned true');
  196. $this->assertTrue($response->isOk(), 'Response is an error, but isOk() returned false');
  197. $this->assertFalse($response->isServerError(), 'Response is an error, but isServerError() returned true');
  198. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  199. $this->assertTrue($response->isSuccess(), 'Response is an error, but isSuccess() returned false');
  200. }
  201. public function test100Continue()
  202. {
  203. $this->markTestIncomplete();
  204. }
  205. public function testAutoMessageSet()
  206. {
  207. $response = Response::fromString($this->readResponse('response_403_nomessage'));
  208. $this->assertEquals(403, $response->getStatusCode(), 'Response status is expected to be 403, but it isn\'t');
  209. $this->assertEquals('Forbidden', $response->getReasonPhrase(), 'Response is 403, but message is not "Forbidden" as expected');
  210. // While we're here, make sure it's classified as error...
  211. $this->assertTrue($response->isClientError(), 'Response is an error, but isClientError() returned false');
  212. $this->assertTrue($response->isForbidden(), 'Response is an error, but isForbidden() returned false');
  213. $this->assertFalse($response->isInformational(), 'Response is an error, but isInformational() returned true');
  214. $this->assertFalse($response->isNotFound(), 'Response is an error, but isNotFound() returned true');
  215. $this->assertFalse($response->isOk(), 'Response is an error, but isOk() returned true');
  216. $this->assertFalse($response->isServerError(), 'Response is an error, but isServerError() returned true');
  217. $this->assertFalse($response->isRedirect(), 'Response is an error, but isRedirect() returned true');
  218. $this->assertFalse($response->isSuccess(), 'Response is an error, but isSuccess() returned true');
  219. }
  220. public function testToString()
  221. {
  222. $response_str = $this->readResponse('response_404');
  223. $response = Response::fromString($response_str);
  224. $this->assertEquals(strtolower(str_replace("\n", "\r\n", $response_str)), strtolower($response->toString()), 'Response convertion to string does not match original string');
  225. $this->assertEquals(strtolower(str_replace("\n", "\r\n", $response_str)), strtolower((string)$response), 'Response convertion to string does not match original string');
  226. }
  227. public function testToStringGzip()
  228. {
  229. $response_str = $this->readResponse('response_gzip');
  230. $response = Response::fromString($response_str);
  231. $this->assertEquals(strtolower($response_str), strtolower($response->toString()), 'Response convertion to string does not match original string');
  232. $this->assertEquals(strtolower($response_str), strtolower((string)$response), 'Response convertion to string does not match original string');
  233. }
  234. public function testGetHeaders()
  235. {
  236. $response = Response::fromString($this->readResponse('response_deflate'));
  237. $headers = $response->getHeaders();
  238. $this->assertEquals(8, count($headers), 'Header count is not as expected');
  239. $this->assertEquals('Apache', $headers->get('Server')->getFieldValue(), 'Server header is not as expected');
  240. $this->assertEquals('deflate', $headers->get('Content-encoding')->getFieldValue(), 'Content-type header is not as expected');
  241. }
  242. public function testGetVersion()
  243. {
  244. $response = Response::fromString($this->readResponse('response_chunked'));
  245. $this->assertEquals(1.1, $response->getVersion(), 'Version is expected to be 1.1');
  246. }
  247. public function testUnknownCode()
  248. {
  249. $response_str = $this->readResponse('response_unknown');
  250. $this->setExpectedException('InvalidArgumentException', 'Invalid status code provided: "550"');
  251. $response = Response::fromString($response_str);
  252. }
  253. public function testMultilineHeader()
  254. {
  255. $response = Response::fromString($this->readResponse('response_multiline_header'));
  256. // Make sure we got the corrent no. of headers
  257. $this->assertEquals(6, count($response->getHeaders()), 'Header count is expected to be 6');
  258. // Check header integrity
  259. $this->assertEquals('timeout=15,max=100', $response->getHeaders()->get('keep-alive')->getFieldValue());
  260. $this->assertEquals('text/html;charset=iso-8859-1', $response->getHeaders()->get('content-type')->getFieldValue());
  261. }
  262. /**
  263. * Make sure a response with some leading whitespace in the response body
  264. * does not get modified (see ZF-1924)
  265. *
  266. */
  267. public function testLeadingWhitespaceBody()
  268. {
  269. $response = Response::fromString($this->readResponse('response_leadingws'));
  270. $this->assertEquals($response->getContent(), "\r\n\t \n\r\tx", 'Extracted body is not identical to expected body');
  271. }
  272. /**
  273. * Test that parsing a multibyte-encoded chunked response works.
  274. *
  275. * This can potentially fail on different PHP environments - for example
  276. * when mbstring.func_overload is set to overload strlen().
  277. *
  278. */
  279. public function testMultibyteChunkedResponse()
  280. {
  281. $this->markTestSkipped('Looks like the headers are split with \n and the body with \r\n');
  282. $md5 = 'ab952f1617d0e28724932401f2d3c6ae';
  283. $response = Response::fromString($this->readResponse('response_multibyte_body'));
  284. $this->assertEquals($md5, md5($response->getBody()));
  285. }
  286. /**
  287. * Helper function: read test response from file
  288. *
  289. * @param string $response
  290. * @return string
  291. */
  292. protected function readResponse($response)
  293. {
  294. return file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . $response);
  295. }
  296. }