ServerTest.php 9.2 KB

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