ServerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_once __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 run where the protocol is invalid
  108. *
  109. * @return void
  110. */
  111. public function testRunInvalidProtocol()
  112. {
  113. $_SERVER['SERVER_PROTOCOL'] = 'HTTP/onclick 1=1';
  114. $app = new MiddlewareApplication($this->config);
  115. $server = new Server($app);
  116. $res = $server->run();
  117. $this->assertEquals(400, $res->getStatusCode());
  118. $this->assertEquals('text/plain', $res->getHeaderLine('content-type'));
  119. $this->assertEquals('Bad Request', '' . $res->getBody());
  120. }
  121. /**
  122. * Test an application failing to build middleware properly
  123. *
  124. * @expectedException RuntimeException
  125. * @expectedExceptionMessage The application `middleware` method
  126. */
  127. public function testRunWithApplicationNotMakingMiddleware()
  128. {
  129. $app = new InvalidMiddlewareApplication($this->config);
  130. $server = new Server($app);
  131. $server->run();
  132. }
  133. /**
  134. * Test middleware being invoked.
  135. *
  136. * @return void
  137. */
  138. public function testRunMultipleMiddlewareSuccess()
  139. {
  140. $app = new MiddlewareApplication($this->config);
  141. $server = new Server($app);
  142. $res = $server->run();
  143. $this->assertSame('first', $res->getHeaderLine('X-First'));
  144. $this->assertSame('second', $res->getHeaderLine('X-Second'));
  145. }
  146. /**
  147. * Test middleware not creating a response.
  148. *
  149. * @expectedException RuntimeException
  150. * @expectedExceptionMessage Application did not create a response. Got "Not a response" instead.
  151. */
  152. public function testRunMiddlewareNoResponse()
  153. {
  154. $app = new BadResponseApplication($this->config);
  155. $server = new Server($app);
  156. $server->run();
  157. }
  158. /**
  159. * Test that emit invokes the appropriate methods on the emitter.
  160. *
  161. * @return void
  162. */
  163. public function testEmit()
  164. {
  165. $response = new Response('php://memory', 200, ['x-testing' => 'source header']);
  166. $final = $response
  167. ->withHeader('X-First', 'first')
  168. ->withHeader('X-Second', 'second');
  169. $emitter = $this->getMockBuilder('Zend\Diactoros\Response\EmitterInterface')->getMock();
  170. $emitter->expects($this->once())
  171. ->method('emit')
  172. ->with($final);
  173. $app = new MiddlewareApplication($this->config);
  174. $server = new Server($app);
  175. $server->emit($server->run(null, $response), $emitter);
  176. }
  177. /**
  178. * Test that emit invokes the appropriate methods on the emitter.
  179. *
  180. * @return void
  181. */
  182. public function testEmitCallbackStream()
  183. {
  184. $response = new Response('php://memory', 200, ['x-testing' => 'source header']);
  185. $response = $response->withBody(new CallbackStream(function () {
  186. echo 'body content';
  187. }));
  188. $app = new MiddlewareApplication($this->config);
  189. $server = new Server($app);
  190. ob_start();
  191. $server->emit($response);
  192. $result = ob_get_clean();
  193. $this->assertEquals('body content', $result);
  194. }
  195. /**
  196. * Ensure that the Server.buildMiddleware event is fired.
  197. *
  198. * @return void
  199. */
  200. public function testBuildMiddlewareEvent()
  201. {
  202. $app = new MiddlewareApplication($this->config);
  203. $server = new Server($app);
  204. $this->called = false;
  205. $server->eventManager()->on('Server.buildMiddleware', function ($event, $middleware) {
  206. $this->assertInstanceOf('Cake\Http\MiddlewareQueue', $middleware);
  207. $middleware->add(function ($req, $res, $next) {
  208. $this->called = true;
  209. return $next($req, $res);
  210. });
  211. $this->middleware = $middleware;
  212. });
  213. $server->run();
  214. $this->assertTrue($this->called, 'Middleware added in the event was not triggered.');
  215. $this->assertInstanceOf('Closure', $this->middleware->get(3), '2nd last middleware is a closure');
  216. $this->assertSame($app, $this->middleware->get(4), 'Last middleware is an app instance');
  217. }
  218. }