ResponseTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Network\Http;
  15. use Cake\Network\Http\Response;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * HTTP response test.
  19. */
  20. class ResponseTest extends TestCase
  21. {
  22. /**
  23. * Test parsing headers and capturing content
  24. *
  25. * @return void
  26. */
  27. public function testHeaderParsing()
  28. {
  29. $headers = [
  30. 'HTTP/1.0 200 OK',
  31. 'Content-Type : text/html;charset="UTF-8"',
  32. 'date: Tue, 25 Dec 2012 04:43:47 GMT',
  33. ];
  34. $response = new Response($headers, 'ok');
  35. $this->assertEquals('1.0', $response->version());
  36. $this->assertEquals(200, $response->statusCode());
  37. $this->assertEquals(
  38. 'text/html;charset="UTF-8"',
  39. $response->header('content-type')
  40. );
  41. $this->assertEquals(
  42. 'Tue, 25 Dec 2012 04:43:47 GMT',
  43. $response->header('Date')
  44. );
  45. $this->assertEquals(
  46. 'text/html;charset="UTF-8"',
  47. $response->headers['Content-Type']
  48. );
  49. $this->assertTrue(isset($response->headers));
  50. $headers = [
  51. 'HTTP/1.0 200',
  52. ];
  53. $response = new Response($headers, 'ok');
  54. $this->assertEquals('1.0', $response->version());
  55. $this->assertEquals(200, $response->statusCode());
  56. }
  57. /**
  58. * Test body()
  59. *
  60. * @return void
  61. */
  62. public function testBody()
  63. {
  64. $data = [
  65. 'property' => 'value'
  66. ];
  67. $encoded = json_encode($data);
  68. $response = new Response([], $encoded);
  69. $result = $response->body('json_decode');
  70. $this->assertEquals($data['property'], $result->property);
  71. $this->assertEquals($encoded, $response->body());
  72. $this->assertEquals($encoded, $response->body);
  73. $this->assertTrue(isset($response->body));
  74. }
  75. /**
  76. * Test accessor for json
  77. *
  78. * @return void
  79. */
  80. public function testBodyJson()
  81. {
  82. $data = [
  83. 'property' => 'value'
  84. ];
  85. $encoded = json_encode($data);
  86. $response = new Response([], $encoded);
  87. $this->assertTrue(isset($response->json));
  88. $this->assertEquals($data['property'], $response->json['property']);
  89. $data = '';
  90. $response = new Response([], $data);
  91. $this->assertFalse(isset($response->json));
  92. }
  93. /**
  94. * Test accessor for xml
  95. *
  96. * @return void
  97. */
  98. public function testBodyXml()
  99. {
  100. $data = <<<XML
  101. <?xml version="1.0" encoding="utf-8"?>
  102. <root>
  103. <test>Test</test>
  104. </root>
  105. XML;
  106. $response = new Response([], $data);
  107. $this->assertTrue(isset($response->xml));
  108. $this->assertEquals('Test', (string)$response->xml->test);
  109. $data = '';
  110. $response = new Response([], $data);
  111. $this->assertFalse(isset($response->xml));
  112. }
  113. /**
  114. * Test isOk()
  115. *
  116. * @return void
  117. */
  118. public function testIsOk()
  119. {
  120. $headers = [
  121. 'HTTP/1.1 200 OK',
  122. 'Content-Type: text/html'
  123. ];
  124. $response = new Response($headers, 'ok');
  125. $this->assertTrue($response->isOk());
  126. $headers = [
  127. 'HTTP/1.1 201 Created',
  128. 'Content-Type: text/html'
  129. ];
  130. $response = new Response($headers, 'ok');
  131. $this->assertTrue($response->isOk());
  132. $headers = [
  133. 'HTTP/1.1 202 Accepted',
  134. 'Content-Type: text/html'
  135. ];
  136. $response = new Response($headers, 'ok');
  137. $this->assertTrue($response->isOk());
  138. $headers = [
  139. 'HTTP/1.1 301 Moved Permanently',
  140. 'Content-Type: text/html'
  141. ];
  142. $response = new Response($headers, '');
  143. $this->assertFalse($response->isOk());
  144. $headers = [
  145. 'HTTP/1.0 404 Not Found',
  146. 'Content-Type: text/html'
  147. ];
  148. $response = new Response($headers, '');
  149. $this->assertFalse($response->isOk());
  150. }
  151. /**
  152. * Test isRedirect()
  153. *
  154. * @return void
  155. */
  156. public function testIsRedirect()
  157. {
  158. $headers = [
  159. 'HTTP/1.1 200 OK',
  160. 'Content-Type: text/html'
  161. ];
  162. $response = new Response($headers, 'ok');
  163. $this->assertFalse($response->isRedirect());
  164. $headers = [
  165. 'HTTP/1.1 301 Moved Permanently',
  166. 'Location: /',
  167. 'Content-Type: text/html'
  168. ];
  169. $response = new Response($headers, '');
  170. $this->assertTrue($response->isRedirect());
  171. $headers = [
  172. 'HTTP/1.0 404 Not Found',
  173. 'Content-Type: text/html'
  174. ];
  175. $response = new Response($headers, '');
  176. $this->assertFalse($response->isRedirect());
  177. }
  178. /**
  179. * Test parsing / getting cookies.
  180. *
  181. * @return void
  182. */
  183. public function testCookie()
  184. {
  185. $headers = [
  186. 'HTTP/1.0 200 Ok',
  187. 'Set-Cookie: test=value',
  188. 'Set-Cookie: session=123abc',
  189. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  190. ];
  191. $response = new Response($headers, '');
  192. $this->assertEquals('value', $response->cookie('test'));
  193. $this->assertEquals('123abc', $response->cookie('session'));
  194. $this->assertEquals('soon', $response->cookie('expiring'));
  195. $result = $response->cookie('expiring', true);
  196. $this->assertTrue($result['httponly']);
  197. $this->assertTrue($result['secure']);
  198. $this->assertEquals(
  199. 'Wed, 09-Jun-2021 10:18:14 GMT',
  200. $result['expires']
  201. );
  202. $this->assertEquals('/', $result['path']);
  203. $result = $response->header('set-cookie');
  204. $this->assertCount(3, $result, 'Should be an array.');
  205. $this->assertTrue(isset($response->cookies));
  206. $this->assertEquals(
  207. 'soon',
  208. $response->cookies['expiring']['value']
  209. );
  210. }
  211. /**
  212. * Test statusCode()
  213. *
  214. * @return void
  215. */
  216. public function testStatusCode()
  217. {
  218. $headers = [
  219. 'HTTP/1.0 404 Not Found',
  220. 'Content-Type: text/html'
  221. ];
  222. $response = new Response($headers, '');
  223. $this->assertEquals(404, $response->statusCode());
  224. $this->assertEquals(404, $response->code);
  225. $this->assertTrue(isset($response->code));
  226. }
  227. /**
  228. * Test reading the encoding out.
  229. *
  230. * @return void
  231. */
  232. public function testEncoding()
  233. {
  234. $headers = [
  235. 'HTTP/1.0 200 Ok',
  236. ];
  237. $response = new Response($headers, '');
  238. $this->assertNull($response->encoding());
  239. $headers = [
  240. 'HTTP/1.0 200 Ok',
  241. 'Content-Type: text/html'
  242. ];
  243. $response = new Response($headers, '');
  244. $this->assertNull($response->encoding());
  245. $headers = [
  246. 'HTTP/1.0 200 Ok',
  247. 'Content-Type: text/html; charset="UTF-8"'
  248. ];
  249. $response = new Response($headers, '');
  250. $this->assertEquals('UTF-8', $response->encoding());
  251. $headers = [
  252. 'HTTP/1.0 200 Ok',
  253. "Content-Type: text/html; charset='ISO-8859-1'"
  254. ];
  255. $response = new Response($headers, '');
  256. $this->assertEquals('ISO-8859-1', $response->encoding());
  257. }
  258. /**
  259. * Test that gzip responses are automatically decompressed.
  260. *
  261. * @return void
  262. */
  263. public function testAutoDecodeGzipBody()
  264. {
  265. $headers = [
  266. 'HTTP/1.0 200 OK',
  267. 'Content-Encoding: gzip',
  268. 'Content-Length: 32',
  269. 'Content-Type: text/html; charset=UTF-8'
  270. ];
  271. $body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA=');
  272. $response = new Response($headers, $body);
  273. $this->assertEquals('Hello world!', $response->body);
  274. }
  275. }