ResponseTransformerTest.php 11 KB

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