ResponseTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 reading with PSR7 methods.
  24. *
  25. * @return void
  26. */
  27. public function testHeaderParsingPsr7()
  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, 'winner!');
  35. $this->assertEquals('1.0', $response->getProtocolVersion());
  36. $this->assertEquals(200, $response->getStatusCode());
  37. $this->assertEquals('OK', $response->getReasonPhrase());
  38. $this->assertEquals(
  39. 'text/html;charset="UTF-8"',
  40. $response->getHeaderLine('content-type')
  41. );
  42. $this->assertEquals(
  43. 'Tue, 25 Dec 2012 04:43:47 GMT',
  44. $response->getHeaderLine('Date')
  45. );
  46. $this->assertEquals('winner!', '' . $response->getBody());
  47. }
  48. /**
  49. * Test parsing headers and capturing content
  50. *
  51. * @return void
  52. */
  53. public function testHeaderParsing()
  54. {
  55. $headers = [
  56. 'HTTP/1.0 200 OK',
  57. 'Content-Type : text/html;charset="UTF-8"',
  58. 'date: Tue, 25 Dec 2012 04:43:47 GMT',
  59. ];
  60. $response = new Response($headers, 'ok');
  61. $this->assertEquals(200, $response->statusCode());
  62. $this->assertEquals('1.0', $response->version());
  63. $this->assertEquals(
  64. 'text/html;charset="UTF-8"',
  65. $response->header('content-type')
  66. );
  67. $this->assertEquals(
  68. 'Tue, 25 Dec 2012 04:43:47 GMT',
  69. $response->header('Date')
  70. );
  71. $this->assertEquals(
  72. 'text/html;charset="UTF-8"',
  73. $response->headers['Content-Type']
  74. );
  75. $this->assertTrue(isset($response->headers));
  76. $headers = [
  77. 'HTTP/1.0 200',
  78. ];
  79. $response = new Response($headers, 'ok');
  80. $this->assertEquals('1.0', $response->version());
  81. $this->assertEquals(200, $response->statusCode());
  82. }
  83. /**
  84. * Test body()
  85. *
  86. * @return void
  87. */
  88. public function testBody()
  89. {
  90. $data = [
  91. 'property' => 'value'
  92. ];
  93. $encoded = json_encode($data);
  94. $response = new Response([], $encoded);
  95. $result = $response->body('json_decode');
  96. $this->assertEquals($data['property'], $result->property);
  97. $this->assertEquals($encoded, $response->body());
  98. $this->assertEquals($encoded, $response->body);
  99. $this->assertTrue(isset($response->body));
  100. }
  101. /**
  102. * Test accessor for json
  103. *
  104. * @return void
  105. */
  106. public function testBodyJson()
  107. {
  108. $data = [
  109. 'property' => 'value'
  110. ];
  111. $encoded = json_encode($data);
  112. $response = new Response([], $encoded);
  113. $this->assertTrue(isset($response->json));
  114. $this->assertEquals($data['property'], $response->json['property']);
  115. $data = '';
  116. $response = new Response([], $data);
  117. $this->assertNull($response->json);
  118. $data = json_encode([]);
  119. $response = new Response([], $data);
  120. $this->assertTrue(is_array($response->json));
  121. $data = json_encode(null);
  122. $response = new Response([], $data);
  123. $this->assertNull($response->json);
  124. $data = json_encode(false);
  125. $response = new Response([], $data);
  126. $this->assertFalse($response->json);
  127. $data = json_encode('');
  128. $response = new Response([], $data);
  129. $this->assertSame('', $response->json);
  130. }
  131. /**
  132. * Test accessor for json
  133. *
  134. * @return void
  135. */
  136. public function testBodyJsonPsr7()
  137. {
  138. $this->markTestIncomplete();
  139. }
  140. /**
  141. * Test accessor for xml
  142. *
  143. * @return void
  144. */
  145. public function testBodyXml()
  146. {
  147. $data = <<<XML
  148. <?xml version="1.0" encoding="utf-8"?>
  149. <root>
  150. <test>Test</test>
  151. </root>
  152. XML;
  153. $response = new Response([], $data);
  154. $this->assertTrue(isset($response->xml));
  155. $this->assertEquals('Test', (string)$response->xml->test);
  156. $data = '';
  157. $response = new Response([], $data);
  158. $this->assertFalse(isset($response->xml));
  159. }
  160. /**
  161. * Test isOk()
  162. *
  163. * @return void
  164. */
  165. public function testIsOk()
  166. {
  167. $headers = [
  168. 'HTTP/1.1 200 OK',
  169. 'Content-Type: text/html'
  170. ];
  171. $response = new Response($headers, 'ok');
  172. $this->assertTrue($response->isOk());
  173. $headers = [
  174. 'HTTP/1.1 201 Created',
  175. 'Content-Type: text/html'
  176. ];
  177. $response = new Response($headers, 'ok');
  178. $this->assertTrue($response->isOk());
  179. $headers = [
  180. 'HTTP/1.1 202 Accepted',
  181. 'Content-Type: text/html'
  182. ];
  183. $response = new Response($headers, 'ok');
  184. $this->assertTrue($response->isOk());
  185. $headers = [
  186. 'HTTP/1.1 301 Moved Permanently',
  187. 'Content-Type: text/html'
  188. ];
  189. $response = new Response($headers, '');
  190. $this->assertFalse($response->isOk());
  191. $headers = [
  192. 'HTTP/1.0 404 Not Found',
  193. 'Content-Type: text/html'
  194. ];
  195. $response = new Response($headers, '');
  196. $this->assertFalse($response->isOk());
  197. }
  198. /**
  199. * Test isRedirect()
  200. *
  201. * @return void
  202. */
  203. public function testIsRedirect()
  204. {
  205. $headers = [
  206. 'HTTP/1.1 200 OK',
  207. 'Content-Type: text/html'
  208. ];
  209. $response = new Response($headers, 'ok');
  210. $this->assertFalse($response->isRedirect());
  211. $headers = [
  212. 'HTTP/1.1 301 Moved Permanently',
  213. 'Location: /',
  214. 'Content-Type: text/html'
  215. ];
  216. $response = new Response($headers, '');
  217. $this->assertTrue($response->isRedirect());
  218. $headers = [
  219. 'HTTP/1.0 404 Not Found',
  220. 'Content-Type: text/html'
  221. ];
  222. $response = new Response($headers, '');
  223. $this->assertFalse($response->isRedirect());
  224. }
  225. /**
  226. * Test parsing / getting cookies.
  227. *
  228. * @return void
  229. */
  230. public function testCookie()
  231. {
  232. $headers = [
  233. 'HTTP/1.0 200 Ok',
  234. 'Set-Cookie: test=value',
  235. 'Set-Cookie: session=123abc',
  236. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  237. ];
  238. $response = new Response($headers, '');
  239. $this->assertEquals('value', $response->cookie('test'));
  240. $this->assertEquals('123abc', $response->cookie('session'));
  241. $this->assertEquals('soon', $response->cookie('expiring'));
  242. $result = $response->cookie('expiring', true);
  243. $this->assertTrue($result['httponly']);
  244. $this->assertTrue($result['secure']);
  245. $this->assertEquals(
  246. 'Wed, 09-Jun-2021 10:18:14 GMT',
  247. $result['expires']
  248. );
  249. $this->assertEquals('/', $result['path']);
  250. $result = $response->header('set-cookie');
  251. $this->assertCount(3, $result, 'Should be an array.');
  252. $this->assertTrue(isset($response->cookies));
  253. $this->assertEquals(
  254. 'soon',
  255. $response->cookies['expiring']['value']
  256. );
  257. }
  258. /**
  259. * Test accessing cookies set through the PSR7 interface.
  260. *
  261. * @return void
  262. */
  263. public function testCookiesPsr7()
  264. {
  265. $this->markTestIncomplete();
  266. }
  267. /**
  268. * Test statusCode()
  269. *
  270. * @return void
  271. */
  272. public function testStatusCode()
  273. {
  274. $headers = [
  275. 'HTTP/1.0 404 Not Found',
  276. 'Content-Type: text/html'
  277. ];
  278. $response = new Response($headers, '');
  279. $this->assertEquals(404, $response->statusCode());
  280. $this->assertEquals(404, $response->getStatusCode());
  281. $this->assertEquals(404, $response->code);
  282. $this->assertTrue(isset($response->code));
  283. }
  284. /**
  285. * Test reading the encoding out.
  286. *
  287. * @return void
  288. */
  289. public function testEncoding()
  290. {
  291. $headers = [
  292. 'HTTP/1.0 200 Ok',
  293. ];
  294. $response = new Response($headers, '');
  295. $this->assertNull($response->encoding());
  296. $headers = [
  297. 'HTTP/1.0 200 Ok',
  298. 'Content-Type: text/html'
  299. ];
  300. $response = new Response($headers, '');
  301. $this->assertNull($response->getEncoding());
  302. $this->assertNull($response->encoding());
  303. $headers = [
  304. 'HTTP/1.0 200 Ok',
  305. 'Content-Type: text/html; charset="UTF-8"'
  306. ];
  307. $response = new Response($headers, '');
  308. $this->assertEquals('UTF-8', $response->getEncoding());
  309. $this->assertEquals('UTF-8', $response->encoding());
  310. $headers = [
  311. 'HTTP/1.0 200 Ok',
  312. "Content-Type: text/html; charset='ISO-8859-1'"
  313. ];
  314. $response = new Response($headers, '');
  315. $this->assertEquals('ISO-8859-1', $response->getEncoding());
  316. $this->assertEquals('ISO-8859-1', $response->encoding());
  317. }
  318. /**
  319. * Test that gzip responses are automatically decompressed.
  320. *
  321. * @return void
  322. */
  323. public function testAutoDecodeGzipBody()
  324. {
  325. $headers = [
  326. 'HTTP/1.0 200 OK',
  327. 'Content-Encoding: gzip',
  328. 'Content-Length: 32',
  329. 'Content-Type: text/html; charset=UTF-8'
  330. ];
  331. $body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA=');
  332. $response = new Response($headers, $body);
  333. $this->assertEquals('Hello world!', $response->body);
  334. }
  335. }