ServerTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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\EventManager;
  19. use Cake\Http\BaseApplication;
  20. use Cake\Http\CallbackStream;
  21. use Cake\Http\MiddlewareQueue;
  22. use Cake\Http\Server;
  23. use Cake\TestSuite\TestCase;
  24. use InvalidArgumentException;
  25. use Laminas\Diactoros\Response;
  26. use Laminas\Diactoros\ServerRequestFactory;
  27. use RuntimeException;
  28. use TestApp\Http\BadResponseApplication;
  29. use TestApp\Http\InvalidMiddlewareApplication;
  30. use TestApp\Http\MiddlewareApplication;
  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('pluginMiddleware')
  120. ->with($this->isInstanceOf(MiddlewareQueue::class))
  121. ->will($this->returnCallback(function ($middleware) {
  122. return $middleware;
  123. }));
  124. $server = new Server($app);
  125. $res = $server->run($request, $response);
  126. $this->assertEquals(
  127. 'source header',
  128. $res->getHeaderLine('X-testing'),
  129. 'Input response is carried through out middleware'
  130. );
  131. $this->assertEquals(
  132. 'request header',
  133. $res->getHeaderLine('X-pass'),
  134. 'Request is used in middleware'
  135. );
  136. }
  137. /**
  138. * test run building a request from globals.
  139. *
  140. * @return void
  141. */
  142. public function testRunWithGlobals()
  143. {
  144. $_SERVER['HTTP_X_PASS'] = 'globalvalue';
  145. $app = new MiddlewareApplication($this->config);
  146. $server = new Server($app);
  147. $res = $server->run();
  148. $this->assertEquals(
  149. 'globalvalue',
  150. $res->getHeaderLine('X-pass'),
  151. 'Default request is made from server'
  152. );
  153. }
  154. /**
  155. * Test an application failing to build middleware properly
  156. *
  157. * @return void
  158. */
  159. public function testRunWithApplicationNotMakingMiddleware()
  160. {
  161. $this->expectException(RuntimeException::class);
  162. $this->expectExceptionMessage('The application `middleware` method');
  163. $app = new InvalidMiddlewareApplication($this->config);
  164. $server = new Server($app);
  165. $server->run();
  166. }
  167. /**
  168. * Test middleware being invoked.
  169. *
  170. * @return void
  171. */
  172. public function testRunMultipleMiddlewareSuccess()
  173. {
  174. $app = new MiddlewareApplication($this->config);
  175. $server = new Server($app);
  176. $res = $server->run();
  177. $this->assertSame('first', $res->getHeaderLine('X-First'));
  178. $this->assertSame('second', $res->getHeaderLine('X-Second'));
  179. }
  180. /**
  181. * Test middleware not creating a response.
  182. *
  183. * @return void
  184. */
  185. public function testRunMiddlewareNoResponse()
  186. {
  187. $this->expectException(RuntimeException::class);
  188. $this->expectExceptionMessage('Application did not create a response. Got "Not a response" instead.');
  189. $app = new BadResponseApplication($this->config);
  190. $server = new Server($app);
  191. $server->run();
  192. }
  193. /**
  194. * Test that emit invokes the appropriate methods on the emitter.
  195. *
  196. * @return void
  197. */
  198. public function testEmit()
  199. {
  200. $response = new Response('php://memory', 200, ['x-testing' => 'source header']);
  201. $final = $response
  202. ->withHeader('X-First', 'first')
  203. ->withHeader('X-Second', 'second');
  204. $emitter = $this->getMockBuilder('Laminas\Diactoros\Response\EmitterInterface')->getMock();
  205. $emitter->expects($this->once())
  206. ->method('emit')
  207. ->with($final);
  208. $app = new MiddlewareApplication($this->config);
  209. $server = new Server($app);
  210. $server->emit($server->run(null, $response), $emitter);
  211. }
  212. /**
  213. * Test that emit invokes the appropriate methods on the emitter.
  214. *
  215. * @return void
  216. */
  217. public function testEmitCallbackStream()
  218. {
  219. $GLOBALS['mockedHeadersSent'] = false;
  220. $response = new Response('php://memory', 200, ['x-testing' => 'source header']);
  221. $response = $response->withBody(new CallbackStream(function () {
  222. echo 'body content';
  223. }));
  224. $app = new MiddlewareApplication($this->config);
  225. $server = new Server($app);
  226. ob_start();
  227. $server->emit($response);
  228. $result = ob_get_clean();
  229. $this->assertEquals('body content', $result);
  230. }
  231. /**
  232. * Ensure that the Server.buildMiddleware event is fired.
  233. *
  234. * @return void
  235. */
  236. public function testBuildMiddlewareEvent()
  237. {
  238. $app = new MiddlewareApplication($this->config);
  239. $server = new Server($app);
  240. $this->called = false;
  241. $server->getEventManager()->on('Server.buildMiddleware', function (Event $event, $middleware) {
  242. $this->assertInstanceOf('Cake\Http\MiddlewareQueue', $middleware);
  243. $middleware->add(function ($req, $res, $next) {
  244. $this->called = true;
  245. return $next($req, $res);
  246. });
  247. $this->middleware = $middleware;
  248. });
  249. $server->run();
  250. $this->assertTrue($this->called, 'Middleware added in the event was not triggered.');
  251. $this->assertInstanceOf('Closure', $this->middleware->get(3), '2nd last middleware is a closure');
  252. $this->assertSame($app, $this->middleware->get(4), 'Last middleware is an app instance');
  253. }
  254. /**
  255. * test event manager proxies to the application.
  256. *
  257. * @return void
  258. */
  259. public function testEventManagerProxies()
  260. {
  261. $app = $this->getMockForAbstractClass(
  262. BaseApplication::class,
  263. [$this->config]
  264. );
  265. $server = new Server($app);
  266. $this->assertSame($app->getEventManager(), $server->getEventManager());
  267. }
  268. /**
  269. * test event manager cannot be set on applications without events.
  270. *
  271. * @return void
  272. */
  273. public function testGetEventManagerNonEventedApplication()
  274. {
  275. $app = $this->createMock(HttpApplicationInterface::class);
  276. $server = new Server($app);
  277. $this->assertSame(EventManager::instance(), $server->getEventManager());
  278. }
  279. /**
  280. * test event manager cannot be set on applications without events.
  281. *
  282. * @return void
  283. */
  284. public function testSetEventManagerNonEventedApplication()
  285. {
  286. $this->expectException(InvalidArgumentException::class);
  287. $app = $this->createMock(HttpApplicationInterface::class);
  288. $events = new EventManager();
  289. $server = new Server($app);
  290. $server->setEventManager($events);
  291. }
  292. /**
  293. * test deprecated method defined in interface
  294. *
  295. * @return void
  296. */
  297. public function testEventManagerCompat()
  298. {
  299. $this->deprecated(function () {
  300. $app = $this->createMock(HttpApplicationInterface::class);
  301. $server = new Server($app);
  302. $this->assertSame(EventManager::instance(), $server->eventManager());
  303. });
  304. }
  305. }