ServerTest.php 5.7 KB

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