ResponseTransformerTest.php 9.6 KB

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