ResponseTransformerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 setting the status code.
  112. *
  113. * @return void
  114. */
  115. public function testToPsrStatusCode()
  116. {
  117. $cake = new CakeResponse(['status' => 403]);
  118. $result = ResponseTransformer::toPsr($cake);
  119. $this->assertSame(403, $result->getStatusCode());
  120. }
  121. /**
  122. * Test conversion setting the content-type.
  123. *
  124. * @return void
  125. */
  126. public function testToPsrContentType()
  127. {
  128. $cake = new CakeResponse();
  129. $cake->type('js');
  130. $result = ResponseTransformer::toPsr($cake);
  131. $this->assertSame('application/javascript', $result->getHeaderLine('Content-Type'));
  132. }
  133. /**
  134. * Test conversion setting headers.
  135. *
  136. * @return void
  137. */
  138. public function testToPsrHeaders()
  139. {
  140. $cake = new CakeResponse(['status' => 403]);
  141. $cake->header([
  142. 'X-testing' => ['one', 'two'],
  143. 'Location' => 'http://example.com/testing'
  144. ]);
  145. $result = ResponseTransformer::toPsr($cake);
  146. $expected = [
  147. 'X-testing' => ['one', 'two'],
  148. 'Location' => ['http://example.com/testing'],
  149. 'Content-Type' => ['text/html'],
  150. ];
  151. $this->assertSame($expected, $result->getHeaders());
  152. }
  153. /**
  154. * Test conversion setting a string body.
  155. *
  156. * @return void
  157. */
  158. public function testToPsrBodyString()
  159. {
  160. $cake = new CakeResponse(['status' => 403, 'body' => 'A response for you']);
  161. $result = ResponseTransformer::toPsr($cake);
  162. $this->assertSame($cake->body(), '' . $result->getBody());
  163. }
  164. /**
  165. * Test conversion setting a callable body.
  166. *
  167. * @return void
  168. */
  169. public function testToPsrBodyCallable()
  170. {
  171. $cake = new CakeResponse(['status' => 200]);
  172. $cake->body(function () {
  173. return 'callback response';
  174. });
  175. $result = ResponseTransformer::toPsr($cake);
  176. $this->assertSame('callback response', '' . $result->getBody());
  177. }
  178. /**
  179. * Test conversion setting a file body.
  180. *
  181. * @return void
  182. */
  183. public function testToPsrBodyFileResponse()
  184. {
  185. $cake = $this->getMock('Cake\Network\Response', ['_clearBuffer']);
  186. $cake->file(__FILE__, ['name' => 'some-file.php', 'download' => true]);
  187. $result = ResponseTransformer::toPsr($cake);
  188. $this->assertEquals(
  189. 'attachment; filename="some-file.php"',
  190. $result->getHeaderLine('Content-Disposition')
  191. );
  192. $this->assertEquals(
  193. 'binary',
  194. $result->getHeaderLine('Content-Transfer-Encoding')
  195. );
  196. $this->assertEquals(
  197. 'bytes',
  198. $result->getHeaderLine('Accept-Ranges')
  199. );
  200. $this->assertContains('<?php', '' . $result->getBody());
  201. }
  202. /**
  203. * Test conversion setting a file body with range headers
  204. *
  205. * @return void
  206. */
  207. public function testToPsrBodyFileResponseFileRange()
  208. {
  209. $_SERVER['HTTP_RANGE'] = 'bytes=10-20';
  210. $cake = $this->getMock('Cake\Network\Response', ['_clearBuffer']);
  211. $path = TEST_APP . 'webroot/css/cake.generic.css';
  212. $cake->file($path, ['name' => 'test-asset.css', 'download' => true]);
  213. $result = ResponseTransformer::toPsr($cake);
  214. $this->assertEquals(
  215. 'bytes 10-20/15640',
  216. $result->getHeaderLine('Content-Range'),
  217. 'Content-Range header missing'
  218. );
  219. }
  220. }