ResponseEmitterTest.php 11 KB

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