ServerTest.php 10 KB

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