ResponseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 203 Non-Authoritative Information',
  195. 'Content-Type: text/html'
  196. ];
  197. $response = new Response($headers, 'ok');
  198. $this->assertTrue($response->isOk());
  199. $headers = [
  200. 'HTTP/1.1 204 No Content',
  201. 'Content-Type: text/html'
  202. ];
  203. $response = new Response($headers, 'ok');
  204. $this->assertTrue($response->isOk());
  205. $headers = [
  206. 'HTTP/1.1 301 Moved Permanently',
  207. 'Content-Type: text/html'
  208. ];
  209. $response = new Response($headers, '');
  210. $this->assertFalse($response->isOk());
  211. $headers = [
  212. 'HTTP/1.0 404 Not Found',
  213. 'Content-Type: text/html'
  214. ];
  215. $response = new Response($headers, '');
  216. $this->assertFalse($response->isOk());
  217. }
  218. /**
  219. * Test isRedirect()
  220. *
  221. * @return void
  222. */
  223. public function testIsRedirect()
  224. {
  225. $headers = [
  226. 'HTTP/1.1 200 OK',
  227. 'Content-Type: text/html'
  228. ];
  229. $response = new Response($headers, 'ok');
  230. $this->assertFalse($response->isRedirect());
  231. $headers = [
  232. 'HTTP/1.1 301 Moved Permanently',
  233. 'Location: /',
  234. 'Content-Type: text/html'
  235. ];
  236. $response = new Response($headers, '');
  237. $this->assertTrue($response->isRedirect());
  238. $headers = [
  239. 'HTTP/1.0 404 Not Found',
  240. 'Content-Type: text/html'
  241. ];
  242. $response = new Response($headers, '');
  243. $this->assertFalse($response->isRedirect());
  244. }
  245. /**
  246. * Test parsing / getting cookies.
  247. *
  248. * @group deprecated
  249. * @return void
  250. */
  251. public function testCookie()
  252. {
  253. $this->deprecated(function () {
  254. $headers = [
  255. 'HTTP/1.0 200 Ok',
  256. 'Set-Cookie: test=value',
  257. 'Set-Cookie: session=123abc',
  258. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  259. ];
  260. $response = new Response($headers, '');
  261. $this->assertEquals('value', $response->cookie('test'));
  262. $this->assertEquals('123abc', $response->cookie('session'));
  263. $this->assertEquals('soon', $response->cookie('expiring'));
  264. $result = $response->cookie('expiring', true);
  265. $this->assertTrue($result['httponly']);
  266. $this->assertTrue($result['secure']);
  267. $this->assertEquals(
  268. 'Wed, 09-Jun-2021 10:18:14 GMT',
  269. $result['expires']
  270. );
  271. $this->assertEquals('/', $result['path']);
  272. $result = $response->header('set-cookie');
  273. $this->assertCount(3, $result, 'Should be an array.');
  274. $this->assertTrue(isset($response->cookies));
  275. $this->assertEquals(
  276. 'soon',
  277. $response->cookies['expiring']['value']
  278. );
  279. });
  280. }
  281. /**
  282. * Test accessing cookies through the PSR7-like methods
  283. *
  284. * @return void
  285. */
  286. public function testGetCookies()
  287. {
  288. $headers = [
  289. 'HTTP/1.0 200 Ok',
  290. 'Set-Cookie: test=value',
  291. 'Set-Cookie: session=123abc',
  292. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  293. ];
  294. $response = new Response($headers, '');
  295. $this->assertNull($response->getCookie('undef'));
  296. $this->assertEquals('value', $response->getCookie('test'));
  297. $this->assertEquals('soon', $response->getCookie('expiring'));
  298. $result = $response->getCookieData('expiring');
  299. $this->assertEquals('soon', $result['value']);
  300. $this->assertTrue($result['httponly']);
  301. $this->assertTrue($result['secure']);
  302. $this->assertEquals(
  303. 'Wed, 09-Jun-2021 10:18:14 GMT',
  304. $result['expires']
  305. );
  306. $this->assertEquals('/', $result['path']);
  307. $result = $response->getCookies();
  308. $this->assertCount(3, $result);
  309. $this->assertArrayHasKey('test', $result);
  310. $this->assertArrayHasKey('session', $result);
  311. $this->assertArrayHasKey('expiring', $result);
  312. }
  313. /**
  314. * Test accessing cookie collection
  315. *
  316. * @return void
  317. */
  318. public function testGetCookieCollection()
  319. {
  320. $headers = [
  321. 'HTTP/1.0 200 Ok',
  322. 'Set-Cookie: test=value',
  323. 'Set-Cookie: session=123abc',
  324. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  325. ];
  326. $response = new Response($headers, '');
  327. $cookies = $response->getCookieCollection();
  328. $this->assertInstanceOf(CookieCollection::class, $cookies);
  329. $this->assertTrue($cookies->has('test'));
  330. $this->assertTrue($cookies->has('session'));
  331. $this->assertTrue($cookies->has('expiring'));
  332. $this->assertSame('123abc', $cookies->get('session')->getValue());
  333. }
  334. /**
  335. * Test statusCode()
  336. *
  337. * @return void
  338. */
  339. public function testGetStatusCode()
  340. {
  341. $headers = [
  342. 'HTTP/1.0 404 Not Found',
  343. 'Content-Type: text/html'
  344. ];
  345. $response = new Response($headers, '');
  346. $this->assertSame(404, $response->getStatusCode());
  347. $this->assertSame(404, $response->code);
  348. $this->assertTrue(isset($response->code));
  349. }
  350. /**
  351. * Test statusCode()
  352. *
  353. * @group deprecated
  354. * @return void
  355. */
  356. public function testStatusCode()
  357. {
  358. $this->deprecated(function () {
  359. $headers = [
  360. 'HTTP/1.0 404 Not Found',
  361. 'Content-Type: text/html'
  362. ];
  363. $response = new Response($headers, '');
  364. $this->assertSame(404, $response->statusCode());
  365. $this->assertSame(404, $response->code);
  366. $this->assertTrue(isset($response->code));
  367. });
  368. }
  369. /**
  370. * Test reading the encoding out.
  371. *
  372. * @return void
  373. */
  374. public function testGetEncoding()
  375. {
  376. $headers = [
  377. 'HTTP/1.0 200 Ok',
  378. ];
  379. $response = new Response($headers, '');
  380. $this->assertNull($response->getEncoding());
  381. $headers = [
  382. 'HTTP/1.0 200 Ok',
  383. 'Content-Type: text/html'
  384. ];
  385. $response = new Response($headers, '');
  386. $this->assertNull($response->getEncoding());
  387. $headers = [
  388. 'HTTP/1.0 200 Ok',
  389. 'Content-Type: text/html; charset="UTF-8"'
  390. ];
  391. $response = new Response($headers, '');
  392. $this->assertEquals('UTF-8', $response->getEncoding());
  393. $headers = [
  394. 'HTTP/1.0 200 Ok',
  395. "Content-Type: text/html; charset='ISO-8859-1'"
  396. ];
  397. $response = new Response($headers, '');
  398. $this->assertEquals('ISO-8859-1', $response->getEncoding());
  399. }
  400. /**
  401. * Test reading the encoding out.
  402. *
  403. * @group deprecated
  404. * @return void
  405. */
  406. public function testEncoding()
  407. {
  408. $this->deprecated(function () {
  409. $headers = [
  410. 'HTTP/1.0 200 Ok',
  411. ];
  412. $response = new Response($headers, '');
  413. $this->assertNull($response->encoding());
  414. $headers = [
  415. 'HTTP/1.0 200 Ok',
  416. 'Content-Type: text/html'
  417. ];
  418. $response = new Response($headers, '');
  419. $this->assertNull($response->encoding());
  420. $headers = [
  421. 'HTTP/1.0 200 Ok',
  422. 'Content-Type: text/html; charset="UTF-8"'
  423. ];
  424. $response = new Response($headers, '');
  425. $this->assertEquals('UTF-8', $response->encoding());
  426. $headers = [
  427. 'HTTP/1.0 200 Ok',
  428. "Content-Type: text/html; charset='ISO-8859-1'"
  429. ];
  430. $response = new Response($headers, '');
  431. $this->assertEquals('ISO-8859-1', $response->encoding());
  432. });
  433. }
  434. /**
  435. * Test that gzip responses are automatically decompressed.
  436. *
  437. * @return void
  438. */
  439. public function testAutoDecodeGzipBody()
  440. {
  441. $headers = [
  442. 'HTTP/1.0 200 OK',
  443. 'Content-Encoding: gzip',
  444. 'Content-Length: 32',
  445. 'Content-Type: text/html; charset=UTF-8'
  446. ];
  447. $body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA=');
  448. $response = new Response($headers, $body);
  449. $this->assertEquals('Hello world!', $response->body);
  450. }
  451. }