ResponseTest.php 12 KB

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