ResponseTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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->assertNull($response->json);
  92. $data = json_encode([]);
  93. $response = new Response([], $data);
  94. $this->assertTrue(is_array($response->json));
  95. $data = json_encode(null);
  96. $response = new Response([], $data);
  97. $this->assertNull($response->json);
  98. $data = json_encode(false);
  99. $response = new Response([], $data);
  100. $this->assertFalse($response->json);
  101. $data = json_encode('');
  102. $response = new Response([], $data);
  103. $this->assertSame('', $response->json);
  104. }
  105. /**
  106. * Test accessor for xml
  107. *
  108. * @return void
  109. */
  110. public function testBodyXml()
  111. {
  112. $data = <<<XML
  113. <?xml version="1.0" encoding="utf-8"?>
  114. <root>
  115. <test>Test</test>
  116. </root>
  117. XML;
  118. $response = new Response([], $data);
  119. $this->assertTrue(isset($response->xml));
  120. $this->assertEquals('Test', (string)$response->xml->test);
  121. $data = '';
  122. $response = new Response([], $data);
  123. $this->assertFalse(isset($response->xml));
  124. }
  125. /**
  126. * Test isOk()
  127. *
  128. * @return void
  129. */
  130. public function testIsOk()
  131. {
  132. $headers = [
  133. 'HTTP/1.1 200 OK',
  134. 'Content-Type: text/html'
  135. ];
  136. $response = new Response($headers, 'ok');
  137. $this->assertTrue($response->isOk());
  138. $headers = [
  139. 'HTTP/1.1 201 Created',
  140. 'Content-Type: text/html'
  141. ];
  142. $response = new Response($headers, 'ok');
  143. $this->assertTrue($response->isOk());
  144. $headers = [
  145. 'HTTP/1.1 202 Accepted',
  146. 'Content-Type: text/html'
  147. ];
  148. $response = new Response($headers, 'ok');
  149. $this->assertTrue($response->isOk());
  150. $headers = [
  151. 'HTTP/1.1 301 Moved Permanently',
  152. 'Content-Type: text/html'
  153. ];
  154. $response = new Response($headers, '');
  155. $this->assertFalse($response->isOk());
  156. $headers = [
  157. 'HTTP/1.0 404 Not Found',
  158. 'Content-Type: text/html'
  159. ];
  160. $response = new Response($headers, '');
  161. $this->assertFalse($response->isOk());
  162. }
  163. /**
  164. * Test isRedirect()
  165. *
  166. * @return void
  167. */
  168. public function testIsRedirect()
  169. {
  170. $headers = [
  171. 'HTTP/1.1 200 OK',
  172. 'Content-Type: text/html'
  173. ];
  174. $response = new Response($headers, 'ok');
  175. $this->assertFalse($response->isRedirect());
  176. $headers = [
  177. 'HTTP/1.1 301 Moved Permanently',
  178. 'Location: /',
  179. 'Content-Type: text/html'
  180. ];
  181. $response = new Response($headers, '');
  182. $this->assertTrue($response->isRedirect());
  183. $headers = [
  184. 'HTTP/1.0 404 Not Found',
  185. 'Content-Type: text/html'
  186. ];
  187. $response = new Response($headers, '');
  188. $this->assertFalse($response->isRedirect());
  189. }
  190. /**
  191. * Test parsing / getting cookies.
  192. *
  193. * @return void
  194. */
  195. public function testCookie()
  196. {
  197. $headers = [
  198. 'HTTP/1.0 200 Ok',
  199. 'Set-Cookie: test=value',
  200. 'Set-Cookie: session=123abc',
  201. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  202. ];
  203. $response = new Response($headers, '');
  204. $this->assertEquals('value', $response->cookie('test'));
  205. $this->assertEquals('123abc', $response->cookie('session'));
  206. $this->assertEquals('soon', $response->cookie('expiring'));
  207. $result = $response->cookie('expiring', true);
  208. $this->assertTrue($result['httponly']);
  209. $this->assertTrue($result['secure']);
  210. $this->assertEquals(
  211. 'Wed, 09-Jun-2021 10:18:14 GMT',
  212. $result['expires']
  213. );
  214. $this->assertEquals('/', $result['path']);
  215. $result = $response->header('set-cookie');
  216. $this->assertCount(3, $result, 'Should be an array.');
  217. $this->assertTrue(isset($response->cookies));
  218. $this->assertEquals(
  219. 'soon',
  220. $response->cookies['expiring']['value']
  221. );
  222. }
  223. /**
  224. * Test statusCode()
  225. *
  226. * @return void
  227. */
  228. public function testStatusCode()
  229. {
  230. $headers = [
  231. 'HTTP/1.0 404 Not Found',
  232. 'Content-Type: text/html'
  233. ];
  234. $response = new Response($headers, '');
  235. $this->assertEquals(404, $response->statusCode());
  236. $this->assertEquals(404, $response->code);
  237. $this->assertTrue(isset($response->code));
  238. }
  239. /**
  240. * Test reading the encoding out.
  241. *
  242. * @return void
  243. */
  244. public function testEncoding()
  245. {
  246. $headers = [
  247. 'HTTP/1.0 200 Ok',
  248. ];
  249. $response = new Response($headers, '');
  250. $this->assertNull($response->encoding());
  251. $headers = [
  252. 'HTTP/1.0 200 Ok',
  253. 'Content-Type: text/html'
  254. ];
  255. $response = new Response($headers, '');
  256. $this->assertNull($response->encoding());
  257. $headers = [
  258. 'HTTP/1.0 200 Ok',
  259. 'Content-Type: text/html; charset="UTF-8"'
  260. ];
  261. $response = new Response($headers, '');
  262. $this->assertEquals('UTF-8', $response->encoding());
  263. $headers = [
  264. 'HTTP/1.0 200 Ok',
  265. "Content-Type: text/html; charset='ISO-8859-1'"
  266. ];
  267. $response = new Response($headers, '');
  268. $this->assertEquals('ISO-8859-1', $response->encoding());
  269. }
  270. /**
  271. * Test that gzip responses are automatically decompressed.
  272. *
  273. * @return void
  274. */
  275. public function testAutoDecodeGzipBody()
  276. {
  277. $headers = [
  278. 'HTTP/1.0 200 OK',
  279. 'Content-Encoding: gzip',
  280. 'Content-Length: 32',
  281. 'Content-Type: text/html; charset=UTF-8'
  282. ];
  283. $body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA=');
  284. $response = new Response($headers, $body);
  285. $this->assertEquals('Hello world!', $response->body);
  286. }
  287. }