ServerTest.php 9.0 KB

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