ServerTest.php 6.7 KB

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