ServerTest.php 6.5 KB

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