ResponseEmitterTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.5
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase;
  16. use Cake\Http\CallbackStream;
  17. use Cake\Http\Cookie\Cookie;
  18. use Cake\Http\Response;
  19. use Cake\Http\ResponseEmitter;
  20. use Cake\TestSuite\TestCase;
  21. require_once __DIR__ . '/server_mocks.php';
  22. /**
  23. * Response emitter test.
  24. */
  25. class ResponseEmitterTest extends TestCase
  26. {
  27. protected $emitter;
  28. /**
  29. * setup
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $GLOBALS['mockedHeadersSent'] = false;
  37. $GLOBALS['mockedHeaders'] = $GLOBALS['mockedCookies'] = [];
  38. $this->emitter = new ResponseEmitter();
  39. }
  40. /**
  41. * teardown
  42. *
  43. * @return void
  44. */
  45. public function tearDown()
  46. {
  47. parent::tearDown();
  48. unset($GLOBALS['mockedHeadersSent']);
  49. }
  50. /**
  51. * Test emitting simple responses.
  52. *
  53. * @return void
  54. */
  55. public function testEmitResponseSimple()
  56. {
  57. $response = (new Response())
  58. ->withStatus(201)
  59. ->withHeader('Content-Type', 'text/html')
  60. ->withHeader('Location', 'http://example.com/cake/1');
  61. $response->getBody()->write('It worked');
  62. ob_start();
  63. $this->emitter->emit($response);
  64. $out = ob_get_clean();
  65. $this->assertEquals('It worked', $out);
  66. $expected = [
  67. 'HTTP/1.1 201 Created',
  68. 'Content-Type: text/html',
  69. 'Location: http://example.com/cake/1',
  70. ];
  71. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  72. }
  73. /**
  74. * Test emitting a no-content response
  75. *
  76. * @return void
  77. */
  78. public function testEmitNoContentResponse()
  79. {
  80. $response = (new Response())
  81. ->withHeader('X-testing', 'value')
  82. ->withStatus(204);
  83. $response->getBody()->write('It worked');
  84. ob_start();
  85. $this->emitter->emit($response);
  86. $out = ob_get_clean();
  87. $this->assertEquals('', $out);
  88. $expected = [
  89. 'HTTP/1.1 204 No Content',
  90. 'X-testing: value',
  91. ];
  92. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  93. }
  94. /**
  95. * Test emitting responses with array cookes
  96. *
  97. * @return void
  98. */
  99. public function testEmitResponseArrayCookies()
  100. {
  101. $response = (new Response())
  102. ->withCookie(new Cookie('simple', 'val', null, '/', '', true))
  103. ->withAddedHeader('Set-Cookie', 'google=not=nice;Path=/accounts; HttpOnly')
  104. ->withHeader('Content-Type', 'text/plain');
  105. $response->getBody()->write('ok');
  106. ob_start();
  107. $this->emitter->emit($response);
  108. $out = ob_get_clean();
  109. $this->assertEquals('ok', $out);
  110. $expected = [
  111. 'HTTP/1.1 200 OK',
  112. 'Content-Type: text/plain',
  113. ];
  114. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  115. $expected = [
  116. [
  117. 'name' => 'simple',
  118. 'value' => 'val',
  119. 'path' => '/',
  120. 'expire' => 0,
  121. 'domain' => '',
  122. 'secure' => true,
  123. 'httponly' => false,
  124. ],
  125. [
  126. 'name' => 'google',
  127. 'value' => 'not=nice',
  128. 'path' => '/accounts',
  129. 'expire' => 0,
  130. 'domain' => '',
  131. 'secure' => false,
  132. 'httponly' => true,
  133. ],
  134. ];
  135. $this->assertEquals($expected, $GLOBALS['mockedCookies']);
  136. }
  137. /**
  138. * Test emitting responses with cookies
  139. *
  140. * @return void
  141. */
  142. public function testEmitResponseCookies()
  143. {
  144. $response = (new Response())
  145. ->withAddedHeader('Set-Cookie', "simple=val;\tSecure")
  146. ->withAddedHeader('Set-Cookie', 'people=jim,jack,jonny";";Path=/accounts')
  147. ->withAddedHeader('Set-Cookie', 'google=not=nice;Path=/accounts; HttpOnly')
  148. ->withAddedHeader('Set-Cookie', 'a=b; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Domain=www.example.com;')
  149. ->withAddedHeader('Set-Cookie', 'list%5B%5D=a%20b%20c')
  150. ->withHeader('Content-Type', 'text/plain');
  151. $response->getBody()->write('ok');
  152. ob_start();
  153. $this->emitter->emit($response);
  154. $out = ob_get_clean();
  155. $this->assertEquals('ok', $out);
  156. $expected = [
  157. 'HTTP/1.1 200 OK',
  158. 'Content-Type: text/plain',
  159. ];
  160. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  161. $expected = [
  162. [
  163. 'name' => 'simple',
  164. 'value' => 'val',
  165. 'path' => '',
  166. 'expire' => 0,
  167. 'domain' => '',
  168. 'secure' => true,
  169. 'httponly' => false,
  170. ],
  171. [
  172. 'name' => 'people',
  173. 'value' => 'jim,jack,jonny";"',
  174. 'path' => '/accounts',
  175. 'expire' => 0,
  176. 'domain' => '',
  177. 'secure' => false,
  178. 'httponly' => false,
  179. ],
  180. [
  181. 'name' => 'google',
  182. 'value' => 'not=nice',
  183. 'path' => '/accounts',
  184. 'expire' => 0,
  185. 'domain' => '',
  186. 'secure' => false,
  187. 'httponly' => true,
  188. ],
  189. [
  190. 'name' => 'a',
  191. 'value' => 'b',
  192. 'path' => '',
  193. 'expire' => 1610576581,
  194. 'domain' => 'www.example.com',
  195. 'secure' => false,
  196. 'httponly' => false,
  197. ],
  198. [
  199. 'name' => 'list[]',
  200. 'value' => 'a b c',
  201. 'path' => '',
  202. 'expire' => 0,
  203. 'domain' => '',
  204. 'secure' => false,
  205. 'httponly' => false,
  206. ],
  207. ];
  208. $this->assertEquals($expected, $GLOBALS['mockedCookies']);
  209. }
  210. /**
  211. * Test emitting responses using callback streams.
  212. *
  213. * We use callback streams for closure based responses.
  214. *
  215. * @return void
  216. */
  217. public function testEmitResponseCallbackStream()
  218. {
  219. $stream = new CallbackStream(function () {
  220. echo 'It worked';
  221. });
  222. $response = (new Response())
  223. ->withStatus(201)
  224. ->withBody($stream)
  225. ->withHeader('Content-Type', 'text/plain');
  226. ob_start();
  227. $this->emitter->emit($response);
  228. $out = ob_get_clean();
  229. $this->assertEquals('It worked', $out);
  230. $expected = [
  231. 'HTTP/1.1 201 Created',
  232. 'Content-Type: text/plain',
  233. ];
  234. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  235. }
  236. /**
  237. * Test valid body ranges.
  238. *
  239. * @return void
  240. */
  241. public function testEmitResponseBodyRange()
  242. {
  243. $response = (new Response())
  244. ->withHeader('Content-Type', 'text/plain')
  245. ->withHeader('Content-Range', 'bytes 1-4/9');
  246. $response->getBody()->write('It worked');
  247. ob_start();
  248. $this->emitter->emit($response);
  249. $out = ob_get_clean();
  250. $this->assertEquals('t wo', $out);
  251. $expected = [
  252. 'HTTP/1.1 200 OK',
  253. 'Content-Type: text/plain',
  254. 'Content-Range: bytes 1-4/9',
  255. ];
  256. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  257. }
  258. /**
  259. * Test valid body ranges.
  260. *
  261. * @return void
  262. */
  263. public function testEmitResponseBodyRangeComplete()
  264. {
  265. $response = (new Response())
  266. ->withHeader('Content-Type', 'text/plain')
  267. ->withHeader('Content-Range', 'bytes 0-20/9');
  268. $response->getBody()->write('It worked');
  269. ob_start();
  270. $this->emitter->emit($response, 2);
  271. $out = ob_get_clean();
  272. $this->assertEquals('It worked', $out);
  273. }
  274. /**
  275. * Test out of bounds body ranges.
  276. *
  277. * @return void
  278. */
  279. public function testEmitResponseBodyRangeOverflow()
  280. {
  281. $response = (new Response())
  282. ->withHeader('Content-Type', 'text/plain')
  283. ->withHeader('Content-Range', 'bytes 5-20/9');
  284. $response->getBody()->write('It worked');
  285. ob_start();
  286. $this->emitter->emit($response);
  287. $out = ob_get_clean();
  288. $this->assertEquals('rked', $out);
  289. }
  290. /**
  291. * Test malformed content-range header
  292. *
  293. * @return void
  294. */
  295. public function testEmitResponseBodyRangeMalformed()
  296. {
  297. $response = (new Response())
  298. ->withHeader('Content-Type', 'text/plain')
  299. ->withHeader('Content-Range', 'bytes 9-ba/a');
  300. $response->getBody()->write('It worked');
  301. ob_start();
  302. $this->emitter->emit($response);
  303. $out = ob_get_clean();
  304. $this->assertEquals('It worked', $out);
  305. }
  306. /**
  307. * Test callback streams returning content and ranges
  308. *
  309. * @return void
  310. */
  311. public function testEmitResponseBodyRangeCallbackStream()
  312. {
  313. $stream = new CallbackStream(function () {
  314. return 'It worked';
  315. });
  316. $response = (new Response())
  317. ->withStatus(201)
  318. ->withBody($stream)
  319. ->withHeader('Content-Range', 'bytes 1-4/9')
  320. ->withHeader('Content-Type', 'text/plain');
  321. ob_start();
  322. $this->emitter->emit($response);
  323. $out = ob_get_clean();
  324. $this->assertEquals('t wo', $out);
  325. $expected = [
  326. 'HTTP/1.1 201 Created',
  327. 'Content-Range: bytes 1-4/9',
  328. 'Content-Type: text/plain',
  329. ];
  330. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  331. }
  332. }