ServerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase;
  16. use Cake\Event\Event;
  17. use Cake\Http\CallbackStream;
  18. use Cake\Http\MiddlewareQueue;
  19. use Cake\Http\Server;
  20. use Cake\TestSuite\TestCase;
  21. use TestApp\Http\BadResponseApplication;
  22. use TestApp\Http\InvalidMiddlewareApplication;
  23. use TestApp\Http\MiddlewareApplication;
  24. use Zend\Diactoros\Response;
  25. use Zend\Diactoros\ServerRequestFactory;
  26. require_once __DIR__ . '/server_mocks.php';
  27. /**
  28. * Server test case
  29. */
  30. class ServerTest extends TestCase
  31. {
  32. /**
  33. * Setup
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. parent::setUp();
  40. $this->server = $_SERVER;
  41. $this->config = dirname(dirname(__DIR__));
  42. $GLOBALS['mockedHeaders'] = [];
  43. $GLOBALS['mockedHeadersSent'] = true;
  44. }
  45. /**
  46. * Teardown
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. $_SERVER = $this->server;
  54. unset($GLOBALS['mockedHeadersSent']);
  55. }
  56. /**
  57. * test get/set on the app
  58. *
  59. * @return void
  60. */
  61. public function testAppGetSet()
  62. {
  63. $app = $this->getMockBuilder('Cake\Http\BaseApplication')
  64. ->setConstructorArgs([$this->config])
  65. ->getMock();
  66. $server = new Server($app);
  67. $this->assertSame($app, $server->getApp($app));
  68. }
  69. /**
  70. * test run building a response
  71. *
  72. * @return void
  73. */
  74. public function testRunWithRequestAndResponse()
  75. {
  76. $response = new Response('php://memory', 200, ['X-testing' => 'source header']);
  77. $request = ServerRequestFactory::fromGlobals();
  78. $request = $request->withHeader('X-pass', 'request header');
  79. $app = new MiddlewareApplication($this->config);
  80. $server = new Server($app);
  81. $res = $server->run($request, $response);
  82. $this->assertEquals(
  83. 'source header',
  84. $res->getHeaderLine('X-testing'),
  85. 'Input response is carried through out middleware'
  86. );
  87. $this->assertEquals(
  88. 'request header',
  89. $res->getHeaderLine('X-pass'),
  90. 'Request is used in middleware'
  91. );
  92. }
  93. /**
  94. * test run calling plugin hooks
  95. *
  96. * @return void
  97. */
  98. public function testRunCallingPluginHooks()
  99. {
  100. $response = new Response('php://memory', 200, ['X-testing' => 'source header']);
  101. $request = ServerRequestFactory::fromGlobals();
  102. $request = $request->withHeader('X-pass', 'request header');
  103. $app = $this->getMockBuilder(MiddlewareApplication::class)
  104. ->setMethods(['pluginBootstrap', 'pluginMiddleware'])
  105. ->setConstructorArgs([$this->config])
  106. ->getMock();
  107. $app->expects($this->once())
  108. ->method('pluginBootstrap');
  109. $app->expects($this->once())
  110. ->method('pluginMiddleware')
  111. ->with($this->isInstanceOf(MiddlewareQueue::class))
  112. ->will($this->returnCallback(function ($middleware) {
  113. return $middleware;
  114. }));
  115. $server = new Server($app);
  116. $res = $server->run($request, $response);
  117. $this->assertEquals(
  118. 'source header',
  119. $res->getHeaderLine('X-testing'),
  120. 'Input response is carried through out middleware'
  121. );
  122. $this->assertEquals(
  123. 'request header',
  124. $res->getHeaderLine('X-pass'),
  125. 'Request is used in middleware'
  126. );
  127. }
  128. /**
  129. * test run building a request from globals.
  130. *
  131. * @return void
  132. */
  133. public function testRunWithGlobals()
  134. {
  135. $_SERVER['HTTP_X_PASS'] = 'globalvalue';
  136. $app = new MiddlewareApplication($this->config);
  137. $server = new Server($app);
  138. $res = $server->run();
  139. $this->assertEquals(
  140. 'globalvalue',
  141. $res->getHeaderLine('X-pass'),
  142. 'Default request is made from server'
  143. );
  144. }
  145. /**
  146. * Test an application failing to build middleware properly
  147. *
  148. */
  149. public function testRunWithApplicationNotMakingMiddleware()
  150. {
  151. $this->expectException(\RuntimeException::class);
  152. $this->expectExceptionMessage('The application `middleware` method');
  153. $app = new InvalidMiddlewareApplication($this->config);
  154. $server = new Server($app);
  155. $server->run();
  156. }
  157. /**
  158. * Test middleware being invoked.
  159. *
  160. * @return void
  161. */
  162. public function testRunMultipleMiddlewareSuccess()
  163. {
  164. $app = new MiddlewareApplication($this->config);
  165. $server = new Server($app);
  166. $res = $server->run();
  167. $this->assertSame('first', $res->getHeaderLine('X-First'));
  168. $this->assertSame('second', $res->getHeaderLine('X-Second'));
  169. }
  170. /**
  171. * Test middleware not creating a response.
  172. *
  173. */
  174. public function testRunMiddlewareNoResponse()
  175. {
  176. $this->expectException(\RuntimeException::class);
  177. $this->expectExceptionMessage('Application did not create a response. Got "Not a response" instead.');
  178. $app = new BadResponseApplication($this->config);
  179. $server = new Server($app);
  180. $server->run();
  181. }
  182. /**
  183. * Test that emit invokes the appropriate methods on the emitter.
  184. *
  185. * @return void
  186. */
  187. public function testEmit()
  188. {
  189. $response = new Response('php://memory', 200, ['x-testing' => 'source header']);
  190. $final = $response
  191. ->withHeader('X-First', 'first')
  192. ->withHeader('X-Second', 'second');
  193. $emitter = $this->getMockBuilder('Zend\Diactoros\Response\EmitterInterface')->getMock();
  194. $emitter->expects($this->once())
  195. ->method('emit')
  196. ->with($final);
  197. $app = new MiddlewareApplication($this->config);
  198. $server = new Server($app);
  199. $server->emit($server->run(null, $response), $emitter);
  200. }
  201. /**
  202. * Test that emit invokes the appropriate methods on the emitter.
  203. *
  204. * @return void
  205. */
  206. public function testEmitCallbackStream()
  207. {
  208. $GLOBALS['mockedHeadersSent'] = false;
  209. $response = new Response('php://memory', 200, ['x-testing' => 'source header']);
  210. $response = $response->withBody(new CallbackStream(function () {
  211. echo 'body content';
  212. }));
  213. $app = new MiddlewareApplication($this->config);
  214. $server = new Server($app);
  215. ob_start();
  216. $server->emit($response);
  217. $result = ob_get_clean();
  218. $this->assertEquals('body content', $result);
  219. }
  220. /**
  221. * Ensure that the Server.buildMiddleware event is fired.
  222. *
  223. * @return void
  224. */
  225. public function testBuildMiddlewareEvent()
  226. {
  227. $app = new MiddlewareApplication($this->config);
  228. $server = new Server($app);
  229. $this->called = false;
  230. $server->getEventManager()->on('Server.buildMiddleware', function (Event $event, $middleware) {
  231. $this->assertInstanceOf('Cake\Http\MiddlewareQueue', $middleware);
  232. $middleware->add(function ($req, $res, $next) {
  233. $this->called = true;
  234. return $next($req, $res);
  235. });
  236. $this->middleware = $middleware;
  237. });
  238. $server->run();
  239. $this->assertTrue($this->called, 'Middleware added in the event was not triggered.');
  240. $this->assertInstanceOf('Closure', $this->middleware->get(3), '2nd last middleware is a closure');
  241. $this->assertSame($app, $this->middleware->get(4), 'Last middleware is an app instance');
  242. }
  243. }