ResponseTest.php 12 KB

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