ServerTest.php 5.8 KB

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