ResponseTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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\Http\Client\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 when set with PSR7 methods.
  133. *
  134. * @return void
  135. */
  136. public function testBodyJsonPsr7()
  137. {
  138. $data = [
  139. 'property' => 'value'
  140. ];
  141. $encoded = json_encode($data);
  142. $response = new Response([], '');
  143. $response->getBody()->write($encoded);
  144. $this->assertEquals($data, $response->json);
  145. }
  146. /**
  147. * Test accessor for xml
  148. *
  149. * @return void
  150. */
  151. public function testBodyXml()
  152. {
  153. $data = <<<XML
  154. <?xml version="1.0" encoding="utf-8"?>
  155. <root>
  156. <test>Test</test>
  157. </root>
  158. XML;
  159. $response = new Response([], $data);
  160. $this->assertTrue(isset($response->xml));
  161. $this->assertEquals('Test', (string)$response->xml->test);
  162. $data = '';
  163. $response = new Response([], $data);
  164. $this->assertFalse(isset($response->xml));
  165. }
  166. /**
  167. * Test isOk()
  168. *
  169. * @return void
  170. */
  171. public function testIsOk()
  172. {
  173. $headers = [
  174. 'HTTP/1.1 200 OK',
  175. 'Content-Type: text/html'
  176. ];
  177. $response = new Response($headers, 'ok');
  178. $this->assertTrue($response->isOk());
  179. $headers = [
  180. 'HTTP/1.1 201 Created',
  181. 'Content-Type: text/html'
  182. ];
  183. $response = new Response($headers, 'ok');
  184. $this->assertTrue($response->isOk());
  185. $headers = [
  186. 'HTTP/1.1 202 Accepted',
  187. 'Content-Type: text/html'
  188. ];
  189. $response = new Response($headers, 'ok');
  190. $this->assertTrue($response->isOk());
  191. $headers = [
  192. 'HTTP/1.1 301 Moved Permanently',
  193. 'Content-Type: text/html'
  194. ];
  195. $response = new Response($headers, '');
  196. $this->assertFalse($response->isOk());
  197. $headers = [
  198. 'HTTP/1.0 404 Not Found',
  199. 'Content-Type: text/html'
  200. ];
  201. $response = new Response($headers, '');
  202. $this->assertFalse($response->isOk());
  203. }
  204. /**
  205. * Test isRedirect()
  206. *
  207. * @return void
  208. */
  209. public function testIsRedirect()
  210. {
  211. $headers = [
  212. 'HTTP/1.1 200 OK',
  213. 'Content-Type: text/html'
  214. ];
  215. $response = new Response($headers, 'ok');
  216. $this->assertFalse($response->isRedirect());
  217. $headers = [
  218. 'HTTP/1.1 301 Moved Permanently',
  219. 'Location: /',
  220. 'Content-Type: text/html'
  221. ];
  222. $response = new Response($headers, '');
  223. $this->assertTrue($response->isRedirect());
  224. $headers = [
  225. 'HTTP/1.0 404 Not Found',
  226. 'Content-Type: text/html'
  227. ];
  228. $response = new Response($headers, '');
  229. $this->assertFalse($response->isRedirect());
  230. }
  231. /**
  232. * Test parsing / getting cookies.
  233. *
  234. * @return void
  235. */
  236. public function testCookie()
  237. {
  238. $headers = [
  239. 'HTTP/1.0 200 Ok',
  240. 'Set-Cookie: test=value',
  241. 'Set-Cookie: session=123abc',
  242. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  243. ];
  244. $response = new Response($headers, '');
  245. $this->assertEquals('value', $response->cookie('test'));
  246. $this->assertEquals('123abc', $response->cookie('session'));
  247. $this->assertEquals('soon', $response->cookie('expiring'));
  248. $result = $response->cookie('expiring', true);
  249. $this->assertTrue($result['httponly']);
  250. $this->assertTrue($result['secure']);
  251. $this->assertEquals(
  252. 'Wed, 09-Jun-2021 10:18:14 GMT',
  253. $result['expires']
  254. );
  255. $this->assertEquals('/', $result['path']);
  256. $result = $response->header('set-cookie');
  257. $this->assertCount(3, $result, 'Should be an array.');
  258. $this->assertTrue(isset($response->cookies));
  259. $this->assertEquals(
  260. 'soon',
  261. $response->cookies['expiring']['value']
  262. );
  263. }
  264. /**
  265. * Test accessing cookies through the PSR7-like methods
  266. *
  267. * @return void
  268. */
  269. public function testGetCookies()
  270. {
  271. $headers = [
  272. 'HTTP/1.0 200 Ok',
  273. 'Set-Cookie: test=value',
  274. 'Set-Cookie: session=123abc',
  275. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  276. ];
  277. $response = new Response($headers, '');
  278. $this->assertNull($response->getCookie('undef'));
  279. $this->assertEquals('value', $response->getCookie('test'));
  280. $this->assertEquals('soon', $response->getCookie('expiring'));
  281. $result = $response->getCookieData('expiring');
  282. $this->assertEquals('soon', $result['value']);
  283. $this->assertTrue($result['httponly']);
  284. $this->assertTrue($result['secure']);
  285. $this->assertEquals(
  286. 'Wed, 09-Jun-2021 10:18:14 GMT',
  287. $result['expires']
  288. );
  289. $this->assertEquals('/', $result['path']);
  290. $result = $response->getCookies();
  291. $this->assertCount(3, $result);
  292. $this->assertArrayHasKey('test', $result);
  293. $this->assertArrayHasKey('session', $result);
  294. $this->assertArrayHasKey('expiring', $result);
  295. }
  296. /**
  297. * Test statusCode()
  298. *
  299. * @return void
  300. */
  301. public function testStatusCode()
  302. {
  303. $headers = [
  304. 'HTTP/1.0 404 Not Found',
  305. 'Content-Type: text/html'
  306. ];
  307. $response = new Response($headers, '');
  308. $this->assertEquals(404, $response->statusCode());
  309. $this->assertEquals(404, $response->getStatusCode());
  310. $this->assertEquals(404, $response->code);
  311. $this->assertTrue(isset($response->code));
  312. }
  313. /**
  314. * Test reading the encoding out.
  315. *
  316. * @return void
  317. */
  318. public function testEncoding()
  319. {
  320. $headers = [
  321. 'HTTP/1.0 200 Ok',
  322. ];
  323. $response = new Response($headers, '');
  324. $this->assertNull($response->encoding());
  325. $headers = [
  326. 'HTTP/1.0 200 Ok',
  327. 'Content-Type: text/html'
  328. ];
  329. $response = new Response($headers, '');
  330. $this->assertNull($response->getEncoding());
  331. $this->assertNull($response->encoding());
  332. $headers = [
  333. 'HTTP/1.0 200 Ok',
  334. 'Content-Type: text/html; charset="UTF-8"'
  335. ];
  336. $response = new Response($headers, '');
  337. $this->assertEquals('UTF-8', $response->getEncoding());
  338. $this->assertEquals('UTF-8', $response->encoding());
  339. $headers = [
  340. 'HTTP/1.0 200 Ok',
  341. "Content-Type: text/html; charset='ISO-8859-1'"
  342. ];
  343. $response = new Response($headers, '');
  344. $this->assertEquals('ISO-8859-1', $response->getEncoding());
  345. $this->assertEquals('ISO-8859-1', $response->encoding());
  346. }
  347. /**
  348. * Test that gzip responses are automatically decompressed.
  349. *
  350. * @return void
  351. */
  352. public function testAutoDecodeGzipBody()
  353. {
  354. $headers = [
  355. 'HTTP/1.0 200 OK',
  356. 'Content-Encoding: gzip',
  357. 'Content-Length: 32',
  358. 'Content-Type: text/html; charset=UTF-8'
  359. ];
  360. $body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA=');
  361. $response = new Response($headers, $body);
  362. $this->assertEquals('Hello world!', $response->body);
  363. }
  364. }