ResponseTransformerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. * For full copyright and license information, please see the LICENSE.txt
  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.3.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http;
  16. use Cake\Http\Response as CakeResponse;
  17. use Cake\Http\ResponseTransformer;
  18. use Cake\TestSuite\TestCase;
  19. use Zend\Diactoros\Response as PsrResponse;
  20. use Zend\Diactoros\Stream;
  21. /**
  22. * Test case for the response transformer.
  23. *
  24. * @group deprecated
  25. */
  26. class ResponseTransformerTest extends TestCase
  27. {
  28. /**
  29. * server used in testing
  30. *
  31. * @var array
  32. */
  33. protected $server;
  34. /**
  35. * Old error level
  36. *
  37. * @var int
  38. */
  39. protected $errorLevel;
  40. /**
  41. * setup
  42. *
  43. * @return void
  44. */
  45. public function setUp()
  46. {
  47. parent::setUp();
  48. $this->server = $_SERVER;
  49. $this->errorLevel = error_reporting(E_ALL ^ E_USER_DEPRECATED);
  50. }
  51. /**
  52. * teardown
  53. *
  54. * @return void
  55. */
  56. public function tearDown()
  57. {
  58. parent::tearDown();
  59. $_SERVER = $this->server;
  60. error_reporting($this->errorLevel);
  61. unset($this->errorLevel);
  62. }
  63. /**
  64. * Test conversion getting the right class type.
  65. *
  66. * @return void
  67. */
  68. public function testToCakeCorrectType()
  69. {
  70. $psr = new PsrResponse('php://memory', 401, []);
  71. $result = ResponseTransformer::toCake($psr);
  72. $this->assertInstanceOf('Cake\Http\Response', $result);
  73. }
  74. /**
  75. * Test conversion getting the status code
  76. *
  77. * @return void
  78. */
  79. public function testToCakeStatusCode()
  80. {
  81. $psr = new PsrResponse('php://memory', 401, []);
  82. $result = ResponseTransformer::toCake($psr);
  83. $this->assertSame(401, $result->statusCode());
  84. $psr = new PsrResponse('php://memory', 200, []);
  85. $result = ResponseTransformer::toCake($psr);
  86. $this->assertSame(200, $result->statusCode());
  87. }
  88. /**
  89. * Test conversion getting headers.
  90. *
  91. * @return void
  92. */
  93. public function testToCakeHeaders()
  94. {
  95. $psr = new PsrResponse('php://memory', 200, ['X-testing' => 'value']);
  96. $result = ResponseTransformer::toCake($psr);
  97. $expected = [
  98. 'Content-Type' => 'text/html; charset=UTF-8',
  99. 'X-testing' => 'value',
  100. ];
  101. $this->assertSame($expected, $result->header());
  102. }
  103. /**
  104. * Test conversion getting headers.
  105. *
  106. * @return void
  107. */
  108. public function testToCakeHeaderMultiple()
  109. {
  110. $psr = new PsrResponse('php://memory', 200, ['X-testing' => ['value', 'value2']]);
  111. $result = ResponseTransformer::toCake($psr);
  112. $expected = [
  113. 'Content-Type' => 'text/html; charset=UTF-8',
  114. 'X-testing' => ['value', 'value2'],
  115. ];
  116. $this->assertSame($expected, $result->header());
  117. }
  118. /**
  119. * Test conversion getting the body.
  120. *
  121. * @return void
  122. */
  123. public function testToCakeBody()
  124. {
  125. $psr = new PsrResponse('php://memory', 200, ['X-testing' => ['value', 'value2']]);
  126. $psr->getBody()->write('A message for you');
  127. $result = ResponseTransformer::toCake($psr);
  128. $this->assertSame('A message for you', $result->body());
  129. }
  130. /**
  131. * Test conversion with a file body.
  132. *
  133. * @return void
  134. */
  135. public function testToCakeFileStream()
  136. {
  137. $stream = new Stream('file://' . __FILE__, 'rb');
  138. $psr = new PsrResponse($stream, 200, ['Content-Disposition' => 'attachment; filename="test.php"']);
  139. $result = ResponseTransformer::toCake($psr);
  140. $this->assertNotEmpty($result->getFile(), 'Should convert file responses.');
  141. $headers = $result->header();
  142. $this->assertArrayHasKey('Content-Length', $headers);
  143. $this->assertArrayHasKey('Content-Disposition', $headers);
  144. $this->assertArrayHasKey('Content-Transfer-Encoding', $headers);
  145. $this->assertArrayHasKey('Accept-Ranges', $headers);
  146. $this->assertEquals('attachment; filename="test.php"', $headers['Content-Disposition']);
  147. }
  148. /**
  149. * Test conversion getting cookies.
  150. *
  151. * @return void
  152. */
  153. public function testToCakeCookies()
  154. {
  155. $cookies = [
  156. 'remember_me=1";"1',
  157. 'forever=yes; Expires=Wed, 13 Jan 2021 12:30:40 GMT; Path=/some/path; Domain=example.com; HttpOnly; Secure',
  158. ];
  159. $psr = new PsrResponse('php://memory', 200, ['Set-Cookie' => $cookies]);
  160. $result = ResponseTransformer::toCake($psr);
  161. $expected = [
  162. 'name' => 'remember_me',
  163. 'value' => '1";"1',
  164. 'path' => '/',
  165. 'domain' => '',
  166. 'expire' => 0,
  167. 'secure' => false,
  168. 'httpOnly' => false,
  169. 'samesite' => null,
  170. ];
  171. $this->assertEquals($expected, $result->cookie('remember_me'));
  172. $expected = [
  173. 'name' => 'forever',
  174. 'value' => 'yes',
  175. 'path' => '/some/path',
  176. 'domain' => 'example.com',
  177. 'expire' => 1610541040,
  178. 'secure' => true,
  179. 'httpOnly' => true,
  180. 'samesite' => null,
  181. ];
  182. $this->assertEquals($expected, $result->cookie('forever'));
  183. }
  184. /**
  185. * Test conversion setting the status code.
  186. *
  187. * @return void
  188. */
  189. public function testToPsrStatusCode()
  190. {
  191. $cake = new CakeResponse(['status' => 403]);
  192. $result = ResponseTransformer::toPsr($cake);
  193. $this->assertSame(403, $result->getStatusCode());
  194. }
  195. /**
  196. * Test conversion setting cookies
  197. *
  198. * @return void
  199. */
  200. public function testToPsrCookieSimple()
  201. {
  202. $cake = new CakeResponse(['status' => 200]);
  203. $cake->cookie([
  204. 'name' => 'remember_me',
  205. 'value' => 1,
  206. ]);
  207. $result = ResponseTransformer::toPsr($cake);
  208. $this->assertEquals('remember_me=1; Path=/', $result->getHeader('Set-Cookie')[0]);
  209. }
  210. /**
  211. * Test conversion setting multiple cookies
  212. *
  213. * @return void
  214. */
  215. public function testToPsrCookieMultiple()
  216. {
  217. $cake = new CakeResponse(['status' => 200]);
  218. $cake->cookie([
  219. 'name' => 'remember_me',
  220. 'value' => 1,
  221. ]);
  222. $cake->cookie([
  223. 'name' => 'forever',
  224. 'value' => 2,
  225. ]);
  226. $result = ResponseTransformer::toPsr($cake);
  227. $this->assertEquals('remember_me=1; Path=/', $result->getHeader('Set-Cookie')[0]);
  228. $this->assertEquals('forever=2; Path=/', $result->getHeader('Set-Cookie')[1]);
  229. }
  230. /**
  231. * Test conversion setting cookie attributes
  232. *
  233. * @return void
  234. */
  235. public function testToPsrCookieAttributes()
  236. {
  237. $cake = new CakeResponse(['status' => 200]);
  238. $cake->cookie([
  239. 'name' => 'remember_me',
  240. 'value' => '1 1',
  241. 'path' => '/some/path',
  242. 'domain' => 'example.com',
  243. 'expire' => strtotime('2021-01-13 12:30:40'),
  244. 'secure' => true,
  245. 'httpOnly' => true,
  246. ]);
  247. $result = ResponseTransformer::toPsr($cake);
  248. $this->assertEquals(
  249. 'remember_me=1+1; Expires=Wed, 13 Jan 2021 12:30:40 GMT; Path=/some/path; Domain=example.com; HttpOnly; Secure',
  250. $result->getHeader('Set-Cookie')[0],
  251. 'Cookie attributes should exist, and name/value should be encoded'
  252. );
  253. }
  254. /**
  255. * Test conversion setting the content-type.
  256. *
  257. * @return void
  258. */
  259. public function testToPsrContentType()
  260. {
  261. $cake = new CakeResponse();
  262. $cake->type('html');
  263. $cake->charset('utf-8');
  264. $result = ResponseTransformer::toPsr($cake);
  265. $this->assertSame('text/html; charset=utf-8', $result->getHeaderLine('Content-Type'));
  266. }
  267. /**
  268. * Test conversion omitting content-type on 304 and 204 status codes
  269. *
  270. * @return void
  271. */
  272. public function testToPsrContentTypeCharsetIsTypeSpecific()
  273. {
  274. $cake = new CakeResponse();
  275. $cake->charset('utf-8');
  276. $cake->type('text/html');
  277. $result = ResponseTransformer::toPsr($cake);
  278. $this->assertSame('text/html; charset=utf-8', $result->getHeaderLine('Content-Type'));
  279. $cake->type('application/octet-stream');
  280. $result = ResponseTransformer::toPsr($cake);
  281. $this->assertSame('application/octet-stream', $result->getHeaderLine('Content-Type'));
  282. }
  283. /**
  284. * Test conversion setting headers.
  285. *
  286. * @return void
  287. */
  288. public function testToPsrHeaders()
  289. {
  290. $cake = new CakeResponse(['status' => 403]);
  291. $cake->header([
  292. 'X-testing' => ['one', 'two'],
  293. 'Location' => 'http://example.com/testing',
  294. ]);
  295. $result = ResponseTransformer::toPsr($cake);
  296. $expected = [
  297. 'Content-Type' => ['text/html; charset=UTF-8'],
  298. 'X-testing' => ['one', 'two'],
  299. 'Location' => ['http://example.com/testing'],
  300. ];
  301. $this->assertSame($expected, $result->getHeaders());
  302. }
  303. /**
  304. * Test conversion setting a string body.
  305. *
  306. * @return void
  307. */
  308. public function testToPsrBodyString()
  309. {
  310. $cake = new CakeResponse(['status' => 403, 'body' => 'A response for you']);
  311. $result = ResponseTransformer::toPsr($cake);
  312. $this->assertSame($cake->body(), '' . $result->getBody());
  313. }
  314. /**
  315. * Test conversion setting a callable body.
  316. *
  317. * @return void
  318. */
  319. public function testToPsrBodyCallable()
  320. {
  321. $cake = new CakeResponse(['status' => 200]);
  322. $cake->body(function () {
  323. return 'callback response';
  324. });
  325. $result = ResponseTransformer::toPsr($cake);
  326. $this->assertSame('callback response', '' . $result->getBody());
  327. }
  328. /**
  329. * Test conversion setting a file body.
  330. *
  331. * @return void
  332. */
  333. public function testToPsrBodyFileResponse()
  334. {
  335. $cake = $this->getMockBuilder('Cake\Http\Response')
  336. ->setMethods(['_clearBuffer'])
  337. ->getMock();
  338. $cake->file(__FILE__, ['name' => 'some-file.php', 'download' => true]);
  339. $result = ResponseTransformer::toPsr($cake);
  340. $this->assertEquals(
  341. 'attachment; filename="some-file.php"',
  342. $result->getHeaderLine('Content-Disposition')
  343. );
  344. $this->assertEquals(
  345. 'binary',
  346. $result->getHeaderLine('Content-Transfer-Encoding')
  347. );
  348. $this->assertEquals(
  349. 'bytes',
  350. $result->getHeaderLine('Accept-Ranges')
  351. );
  352. $this->assertContains('<?php', '' . $result->getBody());
  353. }
  354. /**
  355. * Test conversion setting a file body with range headers
  356. *
  357. * @return void
  358. */
  359. public function testToPsrBodyFileResponseFileRange()
  360. {
  361. $_SERVER['HTTP_RANGE'] = 'bytes=10-20';
  362. $cake = $this->getMockBuilder('Cake\Http\Response')
  363. ->setMethods(['_clearBuffer'])
  364. ->getMock();
  365. $path = TEST_APP . 'webroot/css/cake.generic.css';
  366. $cake->file($path, ['name' => 'test-asset.css', 'download' => true]);
  367. $result = ResponseTransformer::toPsr($cake);
  368. $this->assertEquals(
  369. 'bytes 10-20/15641',
  370. $result->getHeaderLine('Content-Range'),
  371. 'Content-Range header missing'
  372. );
  373. }
  374. }