ResponseTransformerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. ];
  170. $this->assertEquals($expected, $result->cookie('remember_me'));
  171. $expected = [
  172. 'name' => 'forever',
  173. 'value' => 'yes',
  174. 'path' => '/some/path',
  175. 'domain' => 'example.com',
  176. 'expire' => 1610541040,
  177. 'secure' => true,
  178. 'httpOnly' => true,
  179. ];
  180. $this->assertEquals($expected, $result->cookie('forever'));
  181. }
  182. /**
  183. * Test conversion setting the status code.
  184. *
  185. * @return void
  186. */
  187. public function testToPsrStatusCode()
  188. {
  189. $cake = new CakeResponse(['status' => 403]);
  190. $result = ResponseTransformer::toPsr($cake);
  191. $this->assertSame(403, $result->getStatusCode());
  192. }
  193. /**
  194. * Test conversion setting cookies
  195. *
  196. * @return void
  197. */
  198. public function testToPsrCookieSimple()
  199. {
  200. $cake = new CakeResponse(['status' => 200]);
  201. $cake->cookie([
  202. 'name' => 'remember_me',
  203. 'value' => 1
  204. ]);
  205. $result = ResponseTransformer::toPsr($cake);
  206. $this->assertEquals('remember_me=1; Path=/', $result->getHeader('Set-Cookie')[0]);
  207. }
  208. /**
  209. * Test conversion setting multiple cookies
  210. *
  211. * @return void
  212. */
  213. public function testToPsrCookieMultiple()
  214. {
  215. $cake = new CakeResponse(['status' => 200]);
  216. $cake->cookie([
  217. 'name' => 'remember_me',
  218. 'value' => 1
  219. ]);
  220. $cake->cookie([
  221. 'name' => 'forever',
  222. 'value' => 2
  223. ]);
  224. $result = ResponseTransformer::toPsr($cake);
  225. $this->assertEquals('remember_me=1; Path=/', $result->getHeader('Set-Cookie')[0]);
  226. $this->assertEquals('forever=2; Path=/', $result->getHeader('Set-Cookie')[1]);
  227. }
  228. /**
  229. * Test conversion setting cookie attributes
  230. *
  231. * @return void
  232. */
  233. public function testToPsrCookieAttributes()
  234. {
  235. $cake = new CakeResponse(['status' => 200]);
  236. $cake->cookie([
  237. 'name' => 'remember_me',
  238. 'value' => '1 1',
  239. 'path' => '/some/path',
  240. 'domain' => 'example.com',
  241. 'expire' => strtotime('2021-01-13 12:30:40'),
  242. 'secure' => true,
  243. 'httpOnly' => true,
  244. ]);
  245. $result = ResponseTransformer::toPsr($cake);
  246. $this->assertEquals(
  247. 'remember_me=1+1; Expires=Wed, 13 Jan 2021 12:30:40 GMT; Path=/some/path; Domain=example.com; HttpOnly; Secure',
  248. $result->getHeader('Set-Cookie')[0],
  249. 'Cookie attributes should exist, and name/value should be encoded'
  250. );
  251. }
  252. /**
  253. * Test conversion setting the content-type.
  254. *
  255. * @return void
  256. */
  257. public function testToPsrContentType()
  258. {
  259. $cake = new CakeResponse();
  260. $cake->type('html');
  261. $cake->charset('utf-8');
  262. $result = ResponseTransformer::toPsr($cake);
  263. $this->assertSame('text/html; charset=utf-8', $result->getHeaderLine('Content-Type'));
  264. }
  265. /**
  266. * Test conversion omitting content-type on 304 and 204 status codes
  267. *
  268. * @return void
  269. */
  270. public function testToPsrContentTypeCharsetIsTypeSpecific()
  271. {
  272. $cake = new CakeResponse();
  273. $cake->charset('utf-8');
  274. $cake->type('text/html');
  275. $result = ResponseTransformer::toPsr($cake);
  276. $this->assertSame('text/html; charset=utf-8', $result->getHeaderLine('Content-Type'));
  277. $cake->type('application/octet-stream');
  278. $result = ResponseTransformer::toPsr($cake);
  279. $this->assertSame('application/octet-stream', $result->getHeaderLine('Content-Type'));
  280. $cake->type('application/json');
  281. $result = ResponseTransformer::toPsr($cake);
  282. $this->assertSame('application/json; charset=utf-8', $result->getHeaderLine('Content-Type'));
  283. }
  284. /**
  285. * Test conversion setting headers.
  286. *
  287. * @return void
  288. */
  289. public function testToPsrHeaders()
  290. {
  291. $cake = new CakeResponse(['status' => 403]);
  292. $cake->header([
  293. 'X-testing' => ['one', 'two'],
  294. 'Location' => 'http://example.com/testing'
  295. ]);
  296. $result = ResponseTransformer::toPsr($cake);
  297. $expected = [
  298. 'Content-Type' => ['text/html; charset=UTF-8'],
  299. 'X-testing' => ['one', 'two'],
  300. 'Location' => ['http://example.com/testing'],
  301. ];
  302. $this->assertSame($expected, $result->getHeaders());
  303. }
  304. /**
  305. * Test conversion setting a string body.
  306. *
  307. * @return void
  308. */
  309. public function testToPsrBodyString()
  310. {
  311. $cake = new CakeResponse(['status' => 403, 'body' => 'A response for you']);
  312. $result = ResponseTransformer::toPsr($cake);
  313. $this->assertSame($cake->body(), '' . $result->getBody());
  314. }
  315. /**
  316. * Test conversion setting a callable body.
  317. *
  318. * @return void
  319. */
  320. public function testToPsrBodyCallable()
  321. {
  322. $cake = new CakeResponse(['status' => 200]);
  323. $cake->body(function () {
  324. return 'callback response';
  325. });
  326. $result = ResponseTransformer::toPsr($cake);
  327. $this->assertSame('callback response', '' . $result->getBody());
  328. }
  329. /**
  330. * Test conversion setting a file body.
  331. *
  332. * @return void
  333. */
  334. public function testToPsrBodyFileResponse()
  335. {
  336. $cake = $this->getMockBuilder('Cake\Http\Response')
  337. ->setMethods(['_clearBuffer'])
  338. ->getMock();
  339. $cake->file(__FILE__, ['name' => 'some-file.php', 'download' => true]);
  340. $result = ResponseTransformer::toPsr($cake);
  341. $this->assertEquals(
  342. 'attachment; filename="some-file.php"',
  343. $result->getHeaderLine('Content-Disposition')
  344. );
  345. $this->assertEquals(
  346. 'binary',
  347. $result->getHeaderLine('Content-Transfer-Encoding')
  348. );
  349. $this->assertEquals(
  350. 'bytes',
  351. $result->getHeaderLine('Accept-Ranges')
  352. );
  353. $this->assertContains('<?php', '' . $result->getBody());
  354. }
  355. /**
  356. * Test conversion setting a file body with range headers
  357. *
  358. * @return void
  359. */
  360. public function testToPsrBodyFileResponseFileRange()
  361. {
  362. $_SERVER['HTTP_RANGE'] = 'bytes=10-20';
  363. $cake = $this->getMockBuilder('Cake\Http\Response')
  364. ->setMethods(['_clearBuffer'])
  365. ->getMock();
  366. $path = TEST_APP . 'webroot/css/cake.generic.css';
  367. $cake->file($path, ['name' => 'test-asset.css', 'download' => true]);
  368. $result = ResponseTransformer::toPsr($cake);
  369. $this->assertEquals(
  370. 'bytes 10-20/15641',
  371. $result->getHeaderLine('Content-Range'),
  372. 'Content-Range header missing'
  373. );
  374. }
  375. }