ResponseTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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\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. $result = $response->body('json_decode');
  97. $this->assertEquals($data['property'], $result->property);
  98. $this->assertEquals($encoded, $response->body());
  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 accessing cookie collection
  299. *
  300. * @return void
  301. */
  302. public function testGetCookieCollection()
  303. {
  304. $headers = [
  305. 'HTTP/1.0 200 Ok',
  306. 'Set-Cookie: test=value',
  307. 'Set-Cookie: session=123abc',
  308. 'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
  309. ];
  310. $response = new Response($headers, '');
  311. $cookies = $response->getCookieCollection();
  312. $this->assertInstanceOf(CookieCollection::class, $cookies);
  313. $this->assertTrue($cookies->has('test'));
  314. $this->assertTrue($cookies->has('session'));
  315. $this->assertTrue($cookies->has('expiring'));
  316. $this->assertSame('123abc', $cookies->get('session')->getValue());
  317. }
  318. /**
  319. * Test statusCode()
  320. *
  321. * @return void
  322. */
  323. public function testStatusCode()
  324. {
  325. $headers = [
  326. 'HTTP/1.0 404 Not Found',
  327. 'Content-Type: text/html'
  328. ];
  329. $response = new Response($headers, '');
  330. $this->assertSame(404, $response->statusCode());
  331. $this->assertSame(404, $response->getStatusCode());
  332. $this->assertSame(404, $response->code);
  333. $this->assertTrue(isset($response->code));
  334. }
  335. /**
  336. * Test reading the encoding out.
  337. *
  338. * @return void
  339. */
  340. public function testEncoding()
  341. {
  342. $headers = [
  343. 'HTTP/1.0 200 Ok',
  344. ];
  345. $response = new Response($headers, '');
  346. $this->assertNull($response->encoding());
  347. $headers = [
  348. 'HTTP/1.0 200 Ok',
  349. 'Content-Type: text/html'
  350. ];
  351. $response = new Response($headers, '');
  352. $this->assertNull($response->getEncoding());
  353. $this->assertNull($response->encoding());
  354. $headers = [
  355. 'HTTP/1.0 200 Ok',
  356. 'Content-Type: text/html; charset="UTF-8"'
  357. ];
  358. $response = new Response($headers, '');
  359. $this->assertEquals('UTF-8', $response->getEncoding());
  360. $this->assertEquals('UTF-8', $response->encoding());
  361. $headers = [
  362. 'HTTP/1.0 200 Ok',
  363. "Content-Type: text/html; charset='ISO-8859-1'"
  364. ];
  365. $response = new Response($headers, '');
  366. $this->assertEquals('ISO-8859-1', $response->getEncoding());
  367. $this->assertEquals('ISO-8859-1', $response->encoding());
  368. }
  369. /**
  370. * Test that gzip responses are automatically decompressed.
  371. *
  372. * @return void
  373. */
  374. public function testAutoDecodeGzipBody()
  375. {
  376. $headers = [
  377. 'HTTP/1.0 200 OK',
  378. 'Content-Encoding: gzip',
  379. 'Content-Length: 32',
  380. 'Content-Type: text/html; charset=UTF-8'
  381. ];
  382. $body = base64_decode('H4sIAAAAAAAAA/NIzcnJVyjPL8pJUQQAlRmFGwwAAAA=');
  383. $response = new Response($headers, $body);
  384. $this->assertEquals('Hello world!', $response->body);
  385. }
  386. }