ServerTest.php 5.8 KB

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