ServerTest.php 7.6 KB

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