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. /**
  20. * HTTP response test.
  21. */
  22. class ResponseTest extends TestCase
  23. {
  24. /**
  25. * Test parsing headers and reading with PSR7 methods.
  26. */
  27. public function testHeaderParsingPsr7(): void
  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->assertSame('1.0', $response->getProtocolVersion());
  36. $this->assertSame(200, $response->getStatusCode());
  37. $this->assertSame('OK', $response->getReasonPhrase());
  38. $this->assertSame(
  39. 'text/html;charset="UTF-8"',
  40. $response->getHeaderLine('content-type')
  41. );
  42. $this->assertSame(
  43. 'Tue, 25 Dec 2012 04:43:47 GMT',
  44. $response->getHeaderLine('Date')
  45. );
  46. $this->assertSame('winner!', '' . $response->getBody());
  47. }
  48. /**
  49. * Test parsing headers and capturing content
  50. */
  51. public function testHeaderParsing(): void
  52. {
  53. $headers = [
  54. 'HTTP/1.0 200 OK',
  55. 'Content-Type : text/html;charset="UTF-8"',
  56. 'date: Tue, 25 Dec 2012 04:43:47 GMT',
  57. ];
  58. $response = new Response($headers, 'ok');
  59. $this->assertSame(200, $response->getStatusCode());
  60. $this->assertSame('1.0', $response->getProtocolVersion());
  61. $this->assertSame(
  62. 'text/html;charset="UTF-8"',
  63. $response->getHeaderLine('content-type')
  64. );
  65. $this->assertSame(
  66. 'Tue, 25 Dec 2012 04:43:47 GMT',
  67. $response->getHeaderLine('Date')
  68. );
  69. $this->assertSame(
  70. 'text/html;charset="UTF-8"',
  71. $response->getHeaderLine('Content-Type')
  72. );
  73. $headers = [
  74. 'HTTP/1.0 200',
  75. ];
  76. $response = new Response($headers, 'ok');
  77. $this->assertSame('1.0', $response->getProtocolVersion());
  78. $this->assertSame(200, $response->getStatusCode());
  79. }
  80. /**
  81. * Test getStringBody()
  82. */
  83. public function getStringBody(): void
  84. {
  85. $response = new Response([], 'string');
  86. $this->assertSame('string', $response->getStringBody());
  87. }
  88. /**
  89. * Test accessor for JSON
  90. */
  91. public function testBodyJson(): void
  92. {
  93. $data = [
  94. 'property' => 'value',
  95. ];
  96. $encoded = json_encode($data);
  97. $response = new Response([], $encoded);
  98. $this->assertSame($data['property'], $response->getJson()['property']);
  99. $data = '';
  100. $response = new Response([], $data);
  101. $this->assertNull($response->getJson());
  102. $data = json_encode([]);
  103. $response = new Response([], $data);
  104. $this->assertIsArray($response->getJson());
  105. $data = json_encode(null);
  106. $response = new Response([], $data);
  107. $this->assertNull($response->getJson());
  108. $data = json_encode(false);
  109. $response = new Response([], $data);
  110. $this->assertFalse($response->getJson());
  111. $data = json_encode('');
  112. $response = new Response([], $data);
  113. $this->assertSame('', $response->getJson());
  114. }
  115. /**
  116. * Test accessor for JSON when set with PSR7 methods.
  117. */
  118. public function testBodyJsonPsr7(): void
  119. {
  120. $data = [
  121. 'property' => 'value',
  122. ];
  123. $encoded = json_encode($data);
  124. $response = new Response([], '');
  125. $response->getBody()->write($encoded);
  126. $this->assertEquals($data, $response->getJson());
  127. }
  128. /**
  129. * Test accessor for XML
  130. */
  131. public function testBodyXml(): void
  132. {
  133. $data = <<<XML
  134. <?xml version="1.0" encoding="utf-8"?>
  135. <root>
  136. <test>Test</test>
  137. </root>
  138. XML;
  139. $response = new Response([], $data);
  140. $this->assertSame('Test', (string)$response->getXml()->test);
  141. $data = '';
  142. $response = new Response([], $data);
  143. $this->assertNull($response->getXml());
  144. }
  145. /**
  146. * Test isOk()
  147. */
  148. public function testIsOk(): void
  149. {
  150. $headers = [
  151. 'HTTP/1.1 200 OK',
  152. 'Content-Type: text/html',
  153. ];
  154. $response = new Response($headers, 'ok');
  155. $this->assertTrue($response->isOk());
  156. $headers = [
  157. 'HTTP/1.1 201 Created',
  158. 'Content-Type: text/html',
  159. ];
  160. $response = new Response($headers, 'ok');
  161. $this->assertTrue($response->isOk());
  162. $headers = [
  163. 'HTTP/1.1 202 Accepted',
  164. 'Content-Type: text/html',
  165. ];
  166. $response = new Response($headers, 'ok');
  167. $this->assertTrue($response->isOk());
  168. $headers = [
  169. 'HTTP/1.1 203 Non-Authoritative Information',
  170. 'Content-Type: text/html',
  171. ];
  172. $response = new Response($headers, 'ok');
  173. $this->assertTrue($response->isOk());
  174. $headers = [
  175. 'HTTP/1.1 204 No Content',
  176. 'Content-Type: text/html',
  177. ];
  178. $response = new Response($headers, 'ok');
  179. $this->assertTrue($response->isOk());
  180. $headers = [
  181. 'HTTP/1.1 301 Moved Permanently',
  182. 'Content-Type: text/html',
  183. ];
  184. $response = new Response($headers, '');
  185. $this->assertTrue($response->isOk());
  186. $headers = [
  187. 'HTTP/1.0 404 Not Found',
  188. 'Content-Type: text/html',
  189. ];
  190. $response = new Response($headers, '');
  191. $this->assertFalse($response->isOk());
  192. }
  193. /**
  194. * provider for isSuccess.
  195. *
  196. * @return array
  197. */
  198. public static function isSuccessProvider(): array
  199. {
  200. return [
  201. [
  202. true,
  203. new Response([
  204. 'HTTP/1.1 200 OK',
  205. 'Content-Type: text/html',
  206. ], 'ok'),
  207. ],
  208. [
  209. true,
  210. new Response([
  211. 'HTTP/1.1 201 Created',
  212. 'Content-Type: text/html',
  213. ], 'ok'),
  214. ],
  215. [
  216. true,
  217. new Response([
  218. 'HTTP/1.1 202 Accepted',
  219. 'Content-Type: text/html',
  220. ], 'ok'),
  221. ],
  222. [
  223. true,
  224. new Response([
  225. 'HTTP/1.1 203 Non-Authoritative Information',
  226. 'Content-Type: text/html',
  227. ], 'ok'),
  228. ],
  229. [
  230. true,
  231. new Response([
  232. 'HTTP/1.1 204 No Content',
  233. 'Content-Type: text/html',
  234. ], ''),
  235. ],
  236. [
  237. false,
  238. new Response([
  239. 'HTTP/1.1 301 Moved Permanently',
  240. 'Content-Type: text/html',
  241. ], ''),
  242. ],
  243. [
  244. false,
  245. new Response([
  246. 'HTTP/1.0 404 Not Found',
  247. 'Content-Type: text/html',
  248. ], ''),
  249. ],
  250. ];
  251. }
  252. /**
  253. * Test isSuccess()
  254. *
  255. * @dataProvider isSuccessProvider
  256. */
  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. }