ResponseEmitterTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. ->withAddedHeader('Set-Cookie', 'list%5B%5D=a%20b%20c')
  71. ->withHeader('Content-Type', 'text/plain');
  72. $response->getBody()->write('ok');
  73. ob_start();
  74. $this->emitter->emit($response);
  75. $out = ob_get_clean();
  76. $this->assertEquals('ok', $out);
  77. $expected = [
  78. 'HTTP/1.1 200 OK',
  79. 'Content-Type: text/plain'
  80. ];
  81. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  82. $expected = [
  83. [
  84. 'name' => 'simple',
  85. 'value' => 'val',
  86. 'path' => '',
  87. 'expire' => 0,
  88. 'domain' => '',
  89. 'secure' => true,
  90. 'httponly' => false
  91. ],
  92. [
  93. 'name' => 'people',
  94. 'value' => 'jim,jack,jonny";"',
  95. 'path' => '/accounts',
  96. 'expire' => 0,
  97. 'domain' => '',
  98. 'secure' => false,
  99. 'httponly' => false
  100. ],
  101. [
  102. 'name' => 'google',
  103. 'value' => 'not=nice',
  104. 'path' => '/accounts',
  105. 'expire' => 0,
  106. 'domain' => '',
  107. 'secure' => false,
  108. 'httponly' => true
  109. ],
  110. [
  111. 'name' => 'a',
  112. 'value' => 'b',
  113. 'path' => '',
  114. 'expire' => 1610576581,
  115. 'domain' => 'www.example.com',
  116. 'secure' => false,
  117. 'httponly' => false
  118. ],
  119. [
  120. 'name' => 'list[]',
  121. 'value' => 'a b c',
  122. 'path' => '',
  123. 'expire' => 0,
  124. 'domain' => '',
  125. 'secure' => false,
  126. 'httponly' => false
  127. ],
  128. ];
  129. $this->assertEquals($expected, $GLOBALS['mockedCookies']);
  130. }
  131. /**
  132. * Test emitting responses using callback streams.
  133. *
  134. * We use callback streams for closure based responses.
  135. *
  136. * @return void
  137. */
  138. public function testEmitResponseCallbackStream()
  139. {
  140. $stream = new CallbackStream(function () {
  141. echo 'It worked';
  142. });
  143. $response = (new Response())
  144. ->withStatus(201)
  145. ->withBody($stream)
  146. ->withHeader('Content-Type', 'text/plain');
  147. ob_start();
  148. $this->emitter->emit($response);
  149. $out = ob_get_clean();
  150. $this->assertEquals('It worked', $out);
  151. $expected = [
  152. 'HTTP/1.1 201 Created',
  153. 'Content-Type: text/plain',
  154. ];
  155. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  156. }
  157. /**
  158. * Test valid body ranges.
  159. *
  160. * @return void
  161. */
  162. public function testEmitResponseBodyRange()
  163. {
  164. $response = (new Response())
  165. ->withHeader('Content-Type', 'text/plain')
  166. ->withHeader('Content-Range', 'bytes 1-4/9');
  167. $response->getBody()->write('It worked');
  168. ob_start();
  169. $this->emitter->emit($response);
  170. $out = ob_get_clean();
  171. $this->assertEquals('t wo', $out);
  172. $expected = [
  173. 'HTTP/1.1 200 OK',
  174. 'Content-Type: text/plain',
  175. 'Content-Range: bytes 1-4/9',
  176. ];
  177. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  178. }
  179. /**
  180. * Test valid body ranges.
  181. *
  182. * @return void
  183. */
  184. public function testEmitResponseBodyRangeComplete()
  185. {
  186. $response = (new Response())
  187. ->withHeader('Content-Type', 'text/plain')
  188. ->withHeader('Content-Range', 'bytes 0-20/9');
  189. $response->getBody()->write('It worked');
  190. ob_start();
  191. $this->emitter->emit($response, 2);
  192. $out = ob_get_clean();
  193. $this->assertEquals('It worked', $out);
  194. }
  195. /**
  196. * Test out of bounds body ranges.
  197. *
  198. * @return void
  199. */
  200. public function testEmitResponseBodyRangeOverflow()
  201. {
  202. $response = (new Response())
  203. ->withHeader('Content-Type', 'text/plain')
  204. ->withHeader('Content-Range', 'bytes 5-20/9');
  205. $response->getBody()->write('It worked');
  206. ob_start();
  207. $this->emitter->emit($response);
  208. $out = ob_get_clean();
  209. $this->assertEquals('rked', $out);
  210. }
  211. /**
  212. * Test malformed content-range header
  213. *
  214. * @return void
  215. */
  216. public function testEmitResponseBodyRangeMalformed()
  217. {
  218. $response = (new Response())
  219. ->withHeader('Content-Type', 'text/plain')
  220. ->withHeader('Content-Range', 'bytes 9-ba/a');
  221. $response->getBody()->write('It worked');
  222. ob_start();
  223. $this->emitter->emit($response);
  224. $out = ob_get_clean();
  225. $this->assertEquals('It worked', $out);
  226. }
  227. /**
  228. * Test callback streams returning content and ranges
  229. *
  230. * @return void
  231. */
  232. public function testEmitResponseBodyRangeCallbackStream()
  233. {
  234. $stream = new CallbackStream(function () {
  235. return 'It worked';
  236. });
  237. $response = (new Response())
  238. ->withStatus(201)
  239. ->withBody($stream)
  240. ->withHeader('Content-Range', 'bytes 1-4/9')
  241. ->withHeader('Content-Type', 'text/plain');
  242. ob_start();
  243. $this->emitter->emit($response);
  244. $out = ob_get_clean();
  245. $this->assertEquals('t wo', $out);
  246. $expected = [
  247. 'HTTP/1.1 201 Created',
  248. 'Content-Range: bytes 1-4/9',
  249. 'Content-Type: text/plain',
  250. ];
  251. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  252. }
  253. }