ResponseEmitterTest.php 8.6 KB

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