ResponseEmitterTest.php 9.9 KB

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