ResponseTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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->statusCode());
  63. $this->assertEquals('1.0', $response->version());
  64. $this->assertEquals(
  65. 'text/html;charset="UTF-8"',
  66. $response->header('content-type')
  67. );
  68. $this->assertEquals(
  69. 'Tue, 25 Dec 2012 04:43:47 GMT',
  70. $response->header('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->version());
  82. $this->assertSame(200, $response->statusCode());
  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. * @return void
  237. */
  238. public function testCookie()
  239. {
  240. $headers = [
  241. 'HTTP/1.0 200 Ok',
  242. 'Set-Cookie: test=value',
  243. 'Set-Cookie: session=123abc',
  244. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  245. ];
  246. $response = new Response($headers, '');
  247. $this->assertEquals('value', $response->cookie('test'));
  248. $this->assertEquals('123abc', $response->cookie('session'));
  249. $this->assertEquals('soon', $response->cookie('expiring'));
  250. $result = $response->cookie('expiring', true);
  251. $this->assertTrue($result['httponly']);
  252. $this->assertTrue($result['secure']);
  253. $this->assertEquals(
  254. 'Wed, 09-Jun-2021 10:18:14 GMT',
  255. $result['expires']
  256. );
  257. $this->assertEquals('/', $result['path']);
  258. $result = $response->header('set-cookie');
  259. $this->assertCount(3, $result, 'Should be an array.');
  260. $this->assertTrue(isset($response->cookies));
  261. $this->assertEquals(
  262. 'soon',
  263. $response->cookies['expiring']['value']
  264. );
  265. }
  266. /**
  267. * Test accessing cookies through the PSR7-like methods
  268. *
  269. * @return void
  270. */
  271. public function testGetCookies()
  272. {
  273. $headers = [
  274. 'HTTP/1.0 200 Ok',
  275. 'Set-Cookie: test=value',
  276. 'Set-Cookie: session=123abc',
  277. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  278. ];
  279. $response = new Response($headers, '');
  280. $this->assertNull($response->getCookie('undef'));
  281. $this->assertEquals('value', $response->getCookie('test'));
  282. $this->assertEquals('soon', $response->getCookie('expiring'));
  283. $result = $response->getCookieData('expiring');
  284. $this->assertEquals('soon', $result['value']);
  285. $this->assertTrue($result['httponly']);
  286. $this->assertTrue($result['secure']);
  287. $this->assertEquals(
  288. 'Wed, 09-Jun-2021 10:18:14 GMT',
  289. $result['expires']
  290. );
  291. $this->assertEquals('/', $result['path']);
  292. $result = $response->getCookies();
  293. $this->assertCount(3, $result);
  294. $this->assertArrayHasKey('test', $result);
  295. $this->assertArrayHasKey('session', $result);
  296. $this->assertArrayHasKey('expiring', $result);
  297. }
  298. /**
  299. * Test accessing cookie collection
  300. *
  301. * @return void
  302. */
  303. public function testGetCookieCollection()
  304. {
  305. $headers = [
  306. 'HTTP/1.0 200 Ok',
  307. 'Set-Cookie: test=value',
  308. 'Set-Cookie: session=123abc',
  309. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  310. ];
  311. $response = new Response($headers, '');
  312. $cookies = $response->getCookieCollection();
  313. $this->assertInstanceOf(CookieCollection::class, $cookies);
  314. $this->assertTrue($cookies->has('test'));
  315. $this->assertTrue($cookies->has('session'));
  316. $this->assertTrue($cookies->has('expiring'));
  317. $this->assertSame('123abc', $cookies->get('session')->getValue());
  318. }
  319. /**
  320. * Test statusCode()
  321. *
  322. * @return void
  323. */
  324. public function testStatusCode()
  325. {
  326. $headers = [
  327. 'HTTP/1.0 404 Not Found',
  328. 'Content-Type: text/html'
  329. ];
  330. $response = new Response($headers, '');
  331. $this->assertSame(404, $response->statusCode());
  332. $this->assertSame(404, $response->getStatusCode());
  333. $this->assertSame(404, $response->code);
  334. $this->assertTrue(isset($response->code));
  335. }
  336. /**
  337. * Test reading the encoding out.
  338. *
  339. * @return void
  340. */
  341. public function testEncoding()
  342. {
  343. $headers = [
  344. 'HTTP/1.0 200 Ok',
  345. ];
  346. $response = new Response($headers, '');
  347. $this->assertNull($response->encoding());
  348. $headers = [
  349. 'HTTP/1.0 200 Ok',
  350. 'Content-Type: text/html'
  351. ];
  352. $response = new Response($headers, '');
  353. $this->assertNull($response->getEncoding());
  354. $this->assertNull($response->encoding());
  355. $headers = [
  356. 'HTTP/1.0 200 Ok',
  357. 'Content-Type: text/html; charset="UTF-8"'
  358. ];
  359. $response = new Response($headers, '');
  360. $this->assertEquals('UTF-8', $response->getEncoding());
  361. $this->assertEquals('UTF-8', $response->encoding());
  362. $headers = [
  363. 'HTTP/1.0 200 Ok',
  364. "Content-Type: text/html; charset='ISO-8859-1'"
  365. ];
  366. $response = new Response($headers, '');
  367. $this->assertEquals('ISO-8859-1', $response->getEncoding());
  368. $this->assertEquals('ISO-8859-1', $response->encoding());
  369. }
  370. /**
  371. * Test that gzip responses are automatically decompressed.
  372. *
  373. * @return void
  374. */
  375. public function testAutoDecodeGzipBody()
  376. {
  377. $headers = [
  378. 'HTTP/1.0 200 OK',
  379. 'Content-Encoding: gzip',
  380. 'Content-Length: 32',
  381. 'Content-Type: text/html; charset=UTF-8'
  382. ];
  383. $body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA=');
  384. $response = new Response($headers, $body);
  385. $this->assertEquals('Hello world!', $response->body);
  386. }
  387. }