ServerTest.php 6.5 KB

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