ResponseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Http\Client;
  15. use Cake\Http\Client\Response;
  16. use Cake\Http\Cookie\CookieCollection;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * HTTP response test.
  20. */
  21. class ResponseTest extends TestCase
  22. {
  23. /**
  24. * Test parsing headers and reading with PSR7 methods.
  25. *
  26. * @return void
  27. */
  28. public function testHeaderParsingPsr7()
  29. {
  30. $headers = [
  31. 'HTTP/1.0 200 OK',
  32. 'Content-Type : text/html;charset="UTF-8"',
  33. 'date: Tue, 25 Dec 2012 04:43:47 GMT',
  34. ];
  35. $response = new Response($headers, 'winner!');
  36. $this->assertEquals('1.0', $response->getProtocolVersion());
  37. $this->assertSame(200, $response->getStatusCode());
  38. $this->assertEquals('OK', $response->getReasonPhrase());
  39. $this->assertEquals(
  40. 'text/html;charset="UTF-8"',
  41. $response->getHeaderLine('content-type')
  42. );
  43. $this->assertEquals(
  44. 'Tue, 25 Dec 2012 04:43:47 GMT',
  45. $response->getHeaderLine('Date')
  46. );
  47. $this->assertEquals('winner!', '' . $response->getBody());
  48. }
  49. /**
  50. * Test parsing headers and capturing content
  51. *
  52. * @return void
  53. */
  54. public function testHeaderParsing()
  55. {
  56. $headers = [
  57. 'HTTP/1.0 200 OK',
  58. 'Content-Type : text/html;charset="UTF-8"',
  59. 'date: Tue, 25 Dec 2012 04:43:47 GMT',
  60. ];
  61. $response = new Response($headers, 'ok');
  62. $this->assertSame(200, $response->getStatusCode());
  63. $this->assertEquals('1.0', $response->getProtocolVersion());
  64. $this->assertEquals(
  65. 'text/html;charset="UTF-8"',
  66. $response->getHeaderLine('content-type')
  67. );
  68. $this->assertEquals(
  69. 'Tue, 25 Dec 2012 04:43:47 GMT',
  70. $response->getHeaderLine('Date')
  71. );
  72. $this->assertEquals(
  73. 'text/html;charset="UTF-8"',
  74. $response->headers['Content-Type']
  75. );
  76. $this->assertTrue(isset($response->headers));
  77. $headers = [
  78. 'HTTP/1.0 200',
  79. ];
  80. $response = new Response($headers, 'ok');
  81. $this->assertEquals('1.0', $response->getProtocolVersion());
  82. $this->assertSame(200, $response->getStatusCode());
  83. }
  84. /**
  85. * Test body()
  86. *
  87. * @return void
  88. */
  89. public function testBody()
  90. {
  91. $data = [
  92. 'property' => 'value'
  93. ];
  94. $encoded = json_encode($data);
  95. $response = new Response([], $encoded);
  96. $this->assertEquals($encoded, $response->getBody()->getContents());
  97. $this->assertEquals($encoded, $response->body());
  98. $result = $response->body('json_decode');
  99. $this->assertEquals($data['property'], $result->property);
  100. $this->assertEquals($encoded, $response->body);
  101. $this->assertTrue(isset($response->body));
  102. }
  103. /**
  104. * Test accessor for json
  105. *
  106. * @return void
  107. */
  108. public function testBodyJson()
  109. {
  110. $data = [
  111. 'property' => 'value'
  112. ];
  113. $encoded = json_encode($data);
  114. $response = new Response([], $encoded);
  115. $this->assertTrue(isset($response->json));
  116. $this->assertEquals($data['property'], $response->json['property']);
  117. $data = '';
  118. $response = new Response([], $data);
  119. $this->assertNull($response->json);
  120. $data = json_encode([]);
  121. $response = new Response([], $data);
  122. $this->assertInternalType('array', $response->json);
  123. $data = json_encode(null);
  124. $response = new Response([], $data);
  125. $this->assertNull($response->json);
  126. $data = json_encode(false);
  127. $response = new Response([], $data);
  128. $this->assertFalse($response->json);
  129. $data = json_encode('');
  130. $response = new Response([], $data);
  131. $this->assertSame('', $response->json);
  132. }
  133. /**
  134. * Test accessor for json when set with PSR7 methods.
  135. *
  136. * @return void
  137. */
  138. public function testBodyJsonPsr7()
  139. {
  140. $data = [
  141. 'property' => 'value'
  142. ];
  143. $encoded = json_encode($data);
  144. $response = new Response([], '');
  145. $response->getBody()->write($encoded);
  146. $this->assertEquals($data, $response->json);
  147. }
  148. /**
  149. * Test accessor for xml
  150. *
  151. * @return void
  152. */
  153. public function testBodyXml()
  154. {
  155. $data = <<<XML
  156. <?xml version="1.0" encoding="utf-8"?>
  157. <root>
  158. <test>Test</test>
  159. </root>
  160. XML;
  161. $response = new Response([], $data);
  162. $this->assertTrue(isset($response->xml));
  163. $this->assertEquals('Test', (string)$response->xml->test);
  164. $data = '';
  165. $response = new Response([], $data);
  166. $this->assertFalse(isset($response->xml));
  167. }
  168. /**
  169. * Test isOk()
  170. *
  171. * @return void
  172. */
  173. public function testIsOk()
  174. {
  175. $headers = [
  176. 'HTTP/1.1 200 OK',
  177. 'Content-Type: text/html'
  178. ];
  179. $response = new Response($headers, 'ok');
  180. $this->assertTrue($response->isOk());
  181. $headers = [
  182. 'HTTP/1.1 201 Created',
  183. 'Content-Type: text/html'
  184. ];
  185. $response = new Response($headers, 'ok');
  186. $this->assertTrue($response->isOk());
  187. $headers = [
  188. 'HTTP/1.1 202 Accepted',
  189. 'Content-Type: text/html'
  190. ];
  191. $response = new Response($headers, 'ok');
  192. $this->assertTrue($response->isOk());
  193. $headers = [
  194. 'HTTP/1.1 301 Moved Permanently',
  195. 'Content-Type: text/html'
  196. ];
  197. $response = new Response($headers, '');
  198. $this->assertFalse($response->isOk());
  199. $headers = [
  200. 'HTTP/1.0 404 Not Found',
  201. 'Content-Type: text/html'
  202. ];
  203. $response = new Response($headers, '');
  204. $this->assertFalse($response->isOk());
  205. }
  206. /**
  207. * Test isRedirect()
  208. *
  209. * @return void
  210. */
  211. public function testIsRedirect()
  212. {
  213. $headers = [
  214. 'HTTP/1.1 200 OK',
  215. 'Content-Type: text/html'
  216. ];
  217. $response = new Response($headers, 'ok');
  218. $this->assertFalse($response->isRedirect());
  219. $headers = [
  220. 'HTTP/1.1 301 Moved Permanently',
  221. 'Location: /',
  222. 'Content-Type: text/html'
  223. ];
  224. $response = new Response($headers, '');
  225. $this->assertTrue($response->isRedirect());
  226. $headers = [
  227. 'HTTP/1.0 404 Not Found',
  228. 'Content-Type: text/html'
  229. ];
  230. $response = new Response($headers, '');
  231. $this->assertFalse($response->isRedirect());
  232. }
  233. /**
  234. * Test parsing / getting cookies.
  235. *
  236. * @group deprecated
  237. * @return void
  238. */
  239. public function testCookie()
  240. {
  241. $this->deprecated(function () {
  242. $headers = [
  243. 'HTTP/1.0 200 Ok',
  244. 'Set-Cookie: test=value',
  245. 'Set-Cookie: session=123abc',
  246. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  247. ];
  248. $response = new Response($headers, '');
  249. $this->assertEquals('value', $response->cookie('test'));
  250. $this->assertEquals('123abc', $response->cookie('session'));
  251. $this->assertEquals('soon', $response->cookie('expiring'));
  252. $result = $response->cookie('expiring', true);
  253. $this->assertTrue($result['httponly']);
  254. $this->assertTrue($result['secure']);
  255. $this->assertEquals(
  256. 'Wed, 09-Jun-2021 10:18:14 GMT',
  257. $result['expires']
  258. );
  259. $this->assertEquals('/', $result['path']);
  260. $result = $response->header('set-cookie');
  261. $this->assertCount(3, $result, 'Should be an array.');
  262. $this->assertTrue(isset($response->cookies));
  263. $this->assertEquals(
  264. 'soon',
  265. $response->cookies['expiring']['value']
  266. );
  267. });
  268. }
  269. /**
  270. * Test accessing cookies through the PSR7-like methods
  271. *
  272. * @return void
  273. */
  274. public function testGetCookies()
  275. {
  276. $headers = [
  277. 'HTTP/1.0 200 Ok',
  278. 'Set-Cookie: test=value',
  279. 'Set-Cookie: session=123abc',
  280. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  281. ];
  282. $response = new Response($headers, '');
  283. $this->assertNull($response->getCookie('undef'));
  284. $this->assertEquals('value', $response->getCookie('test'));
  285. $this->assertEquals('soon', $response->getCookie('expiring'));
  286. $result = $response->getCookieData('expiring');
  287. $this->assertEquals('soon', $result['value']);
  288. $this->assertTrue($result['httponly']);
  289. $this->assertTrue($result['secure']);
  290. $this->assertEquals(
  291. 'Wed, 09-Jun-2021 10:18:14 GMT',
  292. $result['expires']
  293. );
  294. $this->assertEquals('/', $result['path']);
  295. $result = $response->getCookies();
  296. $this->assertCount(3, $result);
  297. $this->assertArrayHasKey('test', $result);
  298. $this->assertArrayHasKey('session', $result);
  299. $this->assertArrayHasKey('expiring', $result);
  300. }
  301. /**
  302. * Test accessing cookie collection
  303. *
  304. * @return void
  305. */
  306. public function testGetCookieCollection()
  307. {
  308. $headers = [
  309. 'HTTP/1.0 200 Ok',
  310. 'Set-Cookie: test=value',
  311. 'Set-Cookie: session=123abc',
  312. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  313. ];
  314. $response = new Response($headers, '');
  315. $cookies = $response->getCookieCollection();
  316. $this->assertInstanceOf(CookieCollection::class, $cookies);
  317. $this->assertTrue($cookies->has('test'));
  318. $this->assertTrue($cookies->has('session'));
  319. $this->assertTrue($cookies->has('expiring'));
  320. $this->assertSame('123abc', $cookies->get('session')->getValue());
  321. }
  322. /**
  323. * Test statusCode()
  324. *
  325. * @return void
  326. */
  327. public function testGetStatusCode()
  328. {
  329. $headers = [
  330. 'HTTP/1.0 404 Not Found',
  331. 'Content-Type: text/html'
  332. ];
  333. $response = new Response($headers, '');
  334. $this->assertSame(404, $response->getStatusCode());
  335. $this->assertSame(404, $response->code);
  336. $this->assertTrue(isset($response->code));
  337. }
  338. /**
  339. * Test statusCode()
  340. *
  341. * @group deprecated
  342. * @return void
  343. */
  344. public function testStatusCode()
  345. {
  346. $this->deprecated(function () {
  347. $headers = [
  348. 'HTTP/1.0 404 Not Found',
  349. 'Content-Type: text/html'
  350. ];
  351. $response = new Response($headers, '');
  352. $this->assertSame(404, $response->statusCode());
  353. $this->assertSame(404, $response->code);
  354. $this->assertTrue(isset($response->code));
  355. });
  356. }
  357. /**
  358. * Test reading the encoding out.
  359. *
  360. * @return void
  361. */
  362. public function testGetEncoding()
  363. {
  364. $headers = [
  365. 'HTTP/1.0 200 Ok',
  366. ];
  367. $response = new Response($headers, '');
  368. $this->assertNull($response->getEncoding());
  369. $headers = [
  370. 'HTTP/1.0 200 Ok',
  371. 'Content-Type: text/html'
  372. ];
  373. $response = new Response($headers, '');
  374. $this->assertNull($response->getEncoding());
  375. $headers = [
  376. 'HTTP/1.0 200 Ok',
  377. 'Content-Type: text/html; charset="UTF-8"'
  378. ];
  379. $response = new Response($headers, '');
  380. $this->assertEquals('UTF-8', $response->getEncoding());
  381. $headers = [
  382. 'HTTP/1.0 200 Ok',
  383. "Content-Type: text/html; charset='ISO-8859-1'"
  384. ];
  385. $response = new Response($headers, '');
  386. $this->assertEquals('ISO-8859-1', $response->getEncoding());
  387. }
  388. /**
  389. * Test reading the encoding out.
  390. *
  391. * @group deprecated
  392. * @return void
  393. */
  394. public function testEncoding()
  395. {
  396. $this->deprecated(function () {
  397. $headers = [
  398. 'HTTP/1.0 200 Ok',
  399. ];
  400. $response = new Response($headers, '');
  401. $this->assertNull($response->encoding());
  402. $headers = [
  403. 'HTTP/1.0 200 Ok',
  404. 'Content-Type: text/html'
  405. ];
  406. $response = new Response($headers, '');
  407. $this->assertNull($response->encoding());
  408. $headers = [
  409. 'HTTP/1.0 200 Ok',
  410. 'Content-Type: text/html; charset="UTF-8"'
  411. ];
  412. $response = new Response($headers, '');
  413. $this->assertEquals('UTF-8', $response->encoding());
  414. $headers = [
  415. 'HTTP/1.0 200 Ok',
  416. "Content-Type: text/html; charset='ISO-8859-1'"
  417. ];
  418. $response = new Response($headers, '');
  419. $this->assertEquals('ISO-8859-1', $response->encoding());
  420. });
  421. }
  422. /**
  423. * Test that gzip responses are automatically decompressed.
  424. *
  425. * @return void
  426. */
  427. public function testAutoDecodeGzipBody()
  428. {
  429. $headers = [
  430. 'HTTP/1.0 200 OK',
  431. 'Content-Encoding: gzip',
  432. 'Content-Length: 32',
  433. 'Content-Type: text/html; charset=UTF-8'
  434. ];
  435. $body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA=');
  436. $response = new Response($headers, $body);
  437. $this->assertEquals('Hello world!', $response->body);
  438. }
  439. }