ResponseEmitterTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. ->withCookie(new Cookie('samesite', 'val', null, '/', '', true, false, 'Lax'))
  104. ->withAddedHeader('Set-Cookie', 'google=not=nice;Path=/accounts; HttpOnly')
  105. ->withHeader('Content-Type', 'text/plain');
  106. $response->getBody()->write('ok');
  107. ob_start();
  108. $this->emitter->emit($response);
  109. $out = ob_get_clean();
  110. $this->assertEquals('ok', $out);
  111. $expected = [
  112. 'HTTP/1.1 200 OK',
  113. 'Content-Type: text/plain',
  114. ];
  115. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  116. $expected = [
  117. [
  118. 'name' => 'simple',
  119. 'value' => 'val',
  120. 'path' => '/',
  121. 'expire' => 0,
  122. 'domain' => '',
  123. 'secure' => true,
  124. 'httponly' => false,
  125. ],
  126. [
  127. 'name' => 'samesite',
  128. 'value' => 'val',
  129. 'path' => '/',
  130. 'expire' => 0,
  131. 'domain' => '',
  132. 'secure' => true,
  133. 'httponly' => false,
  134. 'samesite' => 'Lax',
  135. ],
  136. [
  137. 'name' => 'google',
  138. 'value' => 'not=nice',
  139. 'path' => '/accounts',
  140. 'expire' => 0,
  141. 'domain' => '',
  142. 'secure' => false,
  143. 'httponly' => true,
  144. ],
  145. ];
  146. if (PHP_VERSION_ID < 70300) {
  147. $expected[1]['path'] = '/; SameSite=Lax';
  148. unset($expected[1]['samesite']);
  149. } else {
  150. $expected[0]['samesite'] = null;
  151. $expected[2]['samesite'] = null;
  152. }
  153. $this->assertEquals($expected, $GLOBALS['mockedCookies']);
  154. }
  155. /**
  156. * Test emitting responses with cookies
  157. *
  158. * @return void
  159. */
  160. public function testEmitResponseCookies()
  161. {
  162. $response = (new Response())
  163. ->withAddedHeader('Set-Cookie', "simple=val;\tSecure")
  164. ->withAddedHeader('Set-Cookie', 'people=jim,jack,jonny";";Path=/accounts')
  165. ->withAddedHeader('Set-Cookie', 'google=not=nice;Path=/accounts; HttpOnly')
  166. ->withAddedHeader('Set-Cookie', 'a=b; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Domain=www.example.com;')
  167. ->withAddedHeader('Set-Cookie', 'list%5B%5D=a%20b%20c')
  168. ->withAddedHeader('Set-Cookie', "samesite=val;Path=/;SameSite=None")
  169. ->withHeader('Content-Type', 'text/plain');
  170. $response->getBody()->write('ok');
  171. ob_start();
  172. $this->emitter->emit($response);
  173. $out = ob_get_clean();
  174. $this->assertEquals('ok', $out);
  175. $expected = [
  176. 'HTTP/1.1 200 OK',
  177. 'Content-Type: text/plain',
  178. ];
  179. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  180. $expected = [
  181. [
  182. 'name' => 'simple',
  183. 'value' => 'val',
  184. 'path' => '',
  185. 'expire' => 0,
  186. 'domain' => '',
  187. 'secure' => true,
  188. 'httponly' => false,
  189. ],
  190. [
  191. 'name' => 'people',
  192. 'value' => 'jim,jack,jonny";"',
  193. 'path' => '/accounts',
  194. 'expire' => 0,
  195. 'domain' => '',
  196. 'secure' => false,
  197. 'httponly' => false,
  198. ],
  199. [
  200. 'name' => 'google',
  201. 'value' => 'not=nice',
  202. 'path' => '/accounts',
  203. 'expire' => 0,
  204. 'domain' => '',
  205. 'secure' => false,
  206. 'httponly' => true,
  207. ],
  208. [
  209. 'name' => 'a',
  210. 'value' => 'b',
  211. 'path' => '',
  212. 'expire' => 1610576581,
  213. 'domain' => 'www.example.com',
  214. 'secure' => false,
  215. 'httponly' => false,
  216. ],
  217. [
  218. 'name' => 'list[]',
  219. 'value' => 'a b c',
  220. 'path' => '',
  221. 'expire' => 0,
  222. 'domain' => '',
  223. 'secure' => false,
  224. 'httponly' => false,
  225. ],
  226. [
  227. 'name' => 'samesite',
  228. 'value' => 'val',
  229. 'path' => '/',
  230. 'expire' => 0,
  231. 'domain' => '',
  232. 'secure' => false,
  233. 'httponly' => false,
  234. ],
  235. ];
  236. if (PHP_VERSION_ID < 70300) {
  237. $expected[5]['path'] = '/; SameSite=None';
  238. } else {
  239. foreach ($expected as &$val) {
  240. $val['samesite'] = null;
  241. }
  242. $expected[5]['samesite'] = 'None';
  243. }
  244. $this->assertEquals($expected, $GLOBALS['mockedCookies']);
  245. }
  246. /**
  247. * Test emitting responses using callback streams.
  248. *
  249. * We use callback streams for closure based responses.
  250. *
  251. * @return void
  252. */
  253. public function testEmitResponseCallbackStream()
  254. {
  255. $stream = new CallbackStream(function () {
  256. echo 'It worked';
  257. });
  258. $response = (new Response())
  259. ->withStatus(201)
  260. ->withBody($stream)
  261. ->withHeader('Content-Type', 'text/plain');
  262. ob_start();
  263. $this->emitter->emit($response);
  264. $out = ob_get_clean();
  265. $this->assertEquals('It worked', $out);
  266. $expected = [
  267. 'HTTP/1.1 201 Created',
  268. 'Content-Type: text/plain',
  269. ];
  270. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  271. }
  272. /**
  273. * Test valid body ranges.
  274. *
  275. * @return void
  276. */
  277. public function testEmitResponseBodyRange()
  278. {
  279. $response = (new Response())
  280. ->withHeader('Content-Type', 'text/plain')
  281. ->withHeader('Content-Range', 'bytes 1-4/9');
  282. $response->getBody()->write('It worked');
  283. ob_start();
  284. $this->emitter->emit($response);
  285. $out = ob_get_clean();
  286. $this->assertEquals('t wo', $out);
  287. $expected = [
  288. 'HTTP/1.1 200 OK',
  289. 'Content-Type: text/plain',
  290. 'Content-Range: bytes 1-4/9',
  291. ];
  292. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  293. }
  294. /**
  295. * Test valid body ranges.
  296. *
  297. * @return void
  298. */
  299. public function testEmitResponseBodyRangeComplete()
  300. {
  301. $response = (new Response())
  302. ->withHeader('Content-Type', 'text/plain')
  303. ->withHeader('Content-Range', 'bytes 0-20/9');
  304. $response->getBody()->write('It worked');
  305. ob_start();
  306. $this->emitter->emit($response, 2);
  307. $out = ob_get_clean();
  308. $this->assertEquals('It worked', $out);
  309. }
  310. /**
  311. * Test out of bounds body ranges.
  312. *
  313. * @return void
  314. */
  315. public function testEmitResponseBodyRangeOverflow()
  316. {
  317. $response = (new Response())
  318. ->withHeader('Content-Type', 'text/plain')
  319. ->withHeader('Content-Range', 'bytes 5-20/9');
  320. $response->getBody()->write('It worked');
  321. ob_start();
  322. $this->emitter->emit($response);
  323. $out = ob_get_clean();
  324. $this->assertEquals('rked', $out);
  325. }
  326. /**
  327. * Test malformed content-range header
  328. *
  329. * @return void
  330. */
  331. public function testEmitResponseBodyRangeMalformed()
  332. {
  333. $response = (new Response())
  334. ->withHeader('Content-Type', 'text/plain')
  335. ->withHeader('Content-Range', 'bytes 9-ba/a');
  336. $response->getBody()->write('It worked');
  337. ob_start();
  338. $this->emitter->emit($response);
  339. $out = ob_get_clean();
  340. $this->assertEquals('It worked', $out);
  341. }
  342. /**
  343. * Test callback streams returning content and ranges
  344. *
  345. * @return void
  346. */
  347. public function testEmitResponseBodyRangeCallbackStream()
  348. {
  349. $stream = new CallbackStream(function () {
  350. return 'It worked';
  351. });
  352. $response = (new Response())
  353. ->withStatus(201)
  354. ->withBody($stream)
  355. ->withHeader('Content-Range', 'bytes 1-4/9')
  356. ->withHeader('Content-Type', 'text/plain');
  357. ob_start();
  358. $this->emitter->emit($response);
  359. $out = ob_get_clean();
  360. $this->assertEquals('t wo', $out);
  361. $expected = [
  362. 'HTTP/1.1 201 Created',
  363. 'Content-Range: bytes 1-4/9',
  364. 'Content-Type: text/plain',
  365. ];
  366. $this->assertEquals($expected, $GLOBALS['mockedHeaders']);
  367. }
  368. }