ResponseEmitterTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.5
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase;
  16. use Cake\Http\CallbackStream;
  17. use Cake\Http\ResponseEmitter;
  18. use Cake\TestSuite\TestCase;
  19. use Zend\Diactoros\Response;
  20. use Zend\Diactoros\ServerRequestFactory;
  21. use Zend\Diactoros\Stream;
  22. require_once __DIR__ . '/server_mocks.php';
  23. /**
  24. * Response emitter test.
  25. */
  26. class ResponseEmitterTest extends TestCase
  27. {
  28. protected $emitter;
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $GLOBALS['mockedHeaders'] = $GLOBALS['mockedCookies'] = [];
  33. $this->emitter = new ResponseEmitter();
  34. }
  35. /**
  36. * Test emitting simple responses.
  37. *
  38. * @return void
  39. */
  40. public function testEmitResponseSimple()
  41. {
  42. $response = (new Response())
  43. ->withStatus(201)
  44. ->withHeader('Content-Type', 'text/html')
  45. ->withHeader('Location', 'http://example.com/cake/1');
  46. $response->getBody()->write('It worked');
  47. ob_start();
  48. $this->emitter->emit($response);
  49. $out = ob_get_clean();
  50. $this->assertEquals('It worked', $out);
  51. $expected = [
  52. 'HTTP/1.1 201 Created',
  53. 'Content-Type: text/html',
  54. 'Location: http://example.com/cake/1'
  55. ];
  56. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  57. }
  58. /**
  59. * Test emitting responses with cookies
  60. *
  61. * @return void
  62. */
  63. public function testEmitResponseCookies()
  64. {
  65. $response = (new Response())
  66. ->withAddedHeader('Set-Cookie', "simple=val;\tSecure")
  67. ->withAddedHeader('Set-Cookie', 'people=jim,jack,jonny";";Path=/accounts')
  68. ->withAddedHeader('Set-Cookie', 'google=not=nice;Path=/accounts; HttpOnly')
  69. ->withAddedHeader('Set-Cookie', 'a=b; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Domain=www.example.com;')
  70. ->withHeader('Content-Type', 'text/plain');
  71. $response->getBody()->write('ok');
  72. ob_start();
  73. $this->emitter->emit($response);
  74. $out = ob_get_clean();
  75. $this->assertEquals('ok', $out);
  76. $expected = [
  77. 'HTTP/1.1 200 OK',
  78. 'Content-Type: text/plain'
  79. ];
  80. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  81. $expected = [
  82. [
  83. 'name' => 'simple',
  84. 'value' => 'val',
  85. 'path' => '',
  86. 'expire' => 0,
  87. 'domain' => '',
  88. 'secure' => true,
  89. 'httponly' => false
  90. ],
  91. [
  92. 'name' => 'people',
  93. 'value' => 'jim,jack,jonny";"',
  94. 'path' => '/accounts',
  95. 'expire' => 0,
  96. 'domain' => '',
  97. 'secure' => false,
  98. 'httponly' => false
  99. ],
  100. [
  101. 'name' => 'google',
  102. 'value' => 'not=nice',
  103. 'path' => '/accounts',
  104. 'expire' => 0,
  105. 'domain' => '',
  106. 'secure' => false,
  107. 'httponly' => true
  108. ],
  109. [
  110. 'name' => 'a',
  111. 'value' => 'b',
  112. 'path' => '',
  113. 'expire' => 1610576581,
  114. 'domain' => 'www.example.com',
  115. 'secure' => false,
  116. 'httponly' => false
  117. ],
  118. ];
  119. $this->assertEquals($expected, $GLOBALS['mockedCookies']);
  120. }
  121. /**
  122. * Test emitting responses using callback streams.
  123. *
  124. * We use callback streams for closure based responses.
  125. *
  126. * @return void
  127. */
  128. public function testEmitResponseCallbackStream()
  129. {
  130. $stream = new CallbackStream(function () {
  131. echo 'It worked';
  132. });
  133. $response = (new Response())
  134. ->withStatus(201)
  135. ->withBody($stream)
  136. ->withHeader('Content-Type', 'text/plain');
  137. ob_start();
  138. $this->emitter->emit($response);
  139. $out = ob_get_clean();
  140. $this->assertEquals('It worked', $out);
  141. $expected = [
  142. 'HTTP/1.1 201 Created',
  143. 'Content-Type: text/plain',
  144. ];
  145. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  146. }
  147. /**
  148. * Test valid body ranges.
  149. *
  150. * @return void
  151. */
  152. public function testEmitResponseBodyRange()
  153. {
  154. $response = (new Response())
  155. ->withHeader('Content-Type', 'text/plain')
  156. ->withHeader('Content-Range', 'bytes 1-4/9');
  157. $response->getBody()->write('It worked');
  158. ob_start();
  159. $this->emitter->emit($response);
  160. $out = ob_get_clean();
  161. $this->assertEquals('t wo', $out);
  162. $expected = [
  163. 'HTTP/1.1 200 OK',
  164. 'Content-Type: text/plain',
  165. 'Content-Range: bytes 1-4/9',
  166. ];
  167. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  168. }
  169. /**
  170. * Test valid body ranges.
  171. *
  172. * @return void
  173. */
  174. public function testEmitResponseBodyRangeComplete()
  175. {
  176. $response = (new Response())
  177. ->withHeader('Content-Type', 'text/plain')
  178. ->withHeader('Content-Range', 'bytes 0-20/9');
  179. $response->getBody()->write('It worked');
  180. ob_start();
  181. $this->emitter->emit($response, 2);
  182. $out = ob_get_clean();
  183. $this->assertEquals('It worked', $out);
  184. }
  185. /**
  186. * Test out of bounds body ranges.
  187. *
  188. * @return void
  189. */
  190. public function testEmitResponseBodyRangeOverflow()
  191. {
  192. $response = (new Response())
  193. ->withHeader('Content-Type', 'text/plain')
  194. ->withHeader('Content-Range', 'bytes 5-20/9');
  195. $response->getBody()->write('It worked');
  196. ob_start();
  197. $this->emitter->emit($response);
  198. $out = ob_get_clean();
  199. $this->assertEquals('rked', $out);
  200. }
  201. /**
  202. * Test malformed content-range header
  203. *
  204. * @return void
  205. */
  206. public function testEmitResponseBodyRangeMalformed()
  207. {
  208. $response = (new Response())
  209. ->withHeader('Content-Type', 'text/plain')
  210. ->withHeader('Content-Range', 'bytes 9-ba/a');
  211. $response->getBody()->write('It worked');
  212. ob_start();
  213. $this->emitter->emit($response);
  214. $out = ob_get_clean();
  215. $this->assertEquals('It worked', $out);
  216. }
  217. /**
  218. * Test callback streams returning content and ranges
  219. *
  220. * @return void
  221. */
  222. public function testEmitResponseBodyRangeCallbackStream()
  223. {
  224. $stream = new CallbackStream(function () {
  225. return 'It worked';
  226. });
  227. $response = (new Response())
  228. ->withStatus(201)
  229. ->withBody($stream)
  230. ->withHeader('Content-Range', 'bytes 1-4/9')
  231. ->withHeader('Content-Type', 'text/plain');
  232. ob_start();
  233. $this->emitter->emit($response);
  234. $out = ob_get_clean();
  235. $this->assertEquals('t wo', $out);
  236. $expected = [
  237. 'HTTP/1.1 201 Created',
  238. 'Content-Range: bytes 1-4/9',
  239. 'Content-Type: text/plain',
  240. ];
  241. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  242. }
  243. }