HttpKernelTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
  15. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  16. use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
  17. use Symfony\Component\HttpKernel\HttpKernel;
  18. use Symfony\Component\HttpKernel\HttpKernelInterface;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  21. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\EventDispatcher\EventDispatcher;
  26. class HttpKernelTest extends TestCase
  27. {
  28. /**
  29. * @expectedException \RuntimeException
  30. */
  31. public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrue()
  32. {
  33. $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); });
  34. $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
  35. }
  36. /**
  37. * @expectedException \RuntimeException
  38. */
  39. public function testHandleWhenControllerThrowsAnExceptionAndCatchIsFalseAndNoListenerIsRegistered()
  40. {
  41. $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); });
  42. $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
  43. }
  44. public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithAHandlingListener()
  45. {
  46. $dispatcher = new EventDispatcher();
  47. $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
  48. $event->setResponse(new Response($event->getException()->getMessage()));
  49. });
  50. $kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException('foo'); });
  51. $response = $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
  52. $this->assertEquals('500', $response->getStatusCode());
  53. $this->assertEquals('foo', $response->getContent());
  54. }
  55. public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithANonHandlingListener()
  56. {
  57. $exception = new \RuntimeException();
  58. $dispatcher = new EventDispatcher();
  59. $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
  60. // should set a response, but does not
  61. });
  62. $kernel = $this->getHttpKernel($dispatcher, function () use ($exception) { throw $exception; });
  63. try {
  64. $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
  65. $this->fail('LogicException expected');
  66. } catch (\RuntimeException $e) {
  67. $this->assertSame($exception, $e);
  68. }
  69. }
  70. public function testHandleExceptionWithARedirectionResponse()
  71. {
  72. $dispatcher = new EventDispatcher();
  73. $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
  74. $event->setResponse(new RedirectResponse('/login', 301));
  75. });
  76. $kernel = $this->getHttpKernel($dispatcher, function () { throw new AccessDeniedHttpException(); });
  77. $response = $kernel->handle(new Request());
  78. $this->assertEquals('301', $response->getStatusCode());
  79. $this->assertEquals('/login', $response->headers->get('Location'));
  80. }
  81. public function testHandleHttpException()
  82. {
  83. $dispatcher = new EventDispatcher();
  84. $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
  85. $event->setResponse(new Response($event->getException()->getMessage()));
  86. });
  87. $kernel = $this->getHttpKernel($dispatcher, function () { throw new MethodNotAllowedHttpException(array('POST')); });
  88. $response = $kernel->handle(new Request());
  89. $this->assertEquals('405', $response->getStatusCode());
  90. $this->assertEquals('POST', $response->headers->get('Allow'));
  91. }
  92. /**
  93. * @dataProvider getStatusCodes
  94. */
  95. public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
  96. {
  97. $dispatcher = new EventDispatcher();
  98. $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) use ($responseStatusCode, $expectedStatusCode) {
  99. $event->setResponse(new Response('', $responseStatusCode, array('X-Status-Code' => $expectedStatusCode)));
  100. });
  101. $kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException(); });
  102. $response = $kernel->handle(new Request());
  103. $this->assertEquals($expectedStatusCode, $response->getStatusCode());
  104. $this->assertFalse($response->headers->has('X-Status-Code'));
  105. }
  106. public function getStatusCodes()
  107. {
  108. return array(
  109. array(200, 404),
  110. array(404, 200),
  111. array(301, 200),
  112. array(500, 200),
  113. );
  114. }
  115. public function testHandleWhenAListenerReturnsAResponse()
  116. {
  117. $dispatcher = new EventDispatcher();
  118. $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
  119. $event->setResponse(new Response('hello'));
  120. });
  121. $kernel = $this->getHttpKernel($dispatcher);
  122. $this->assertEquals('hello', $kernel->handle(new Request())->getContent());
  123. }
  124. /**
  125. * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  126. */
  127. public function testHandleWhenNoControllerIsFound()
  128. {
  129. $dispatcher = new EventDispatcher();
  130. $kernel = $this->getHttpKernel($dispatcher, false);
  131. $kernel->handle(new Request());
  132. }
  133. public function testHandleWhenTheControllerIsAClosure()
  134. {
  135. $response = new Response('foo');
  136. $dispatcher = new EventDispatcher();
  137. $kernel = $this->getHttpKernel($dispatcher, function () use ($response) { return $response; });
  138. $this->assertSame($response, $kernel->handle(new Request()));
  139. }
  140. public function testHandleWhenTheControllerIsAnObjectWithInvoke()
  141. {
  142. $dispatcher = new EventDispatcher();
  143. $kernel = $this->getHttpKernel($dispatcher, new Controller());
  144. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  145. }
  146. public function testHandleWhenTheControllerIsAFunction()
  147. {
  148. $dispatcher = new EventDispatcher();
  149. $kernel = $this->getHttpKernel($dispatcher, 'Symfony\Component\HttpKernel\Tests\controller_func');
  150. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  151. }
  152. public function testHandleWhenTheControllerIsAnArray()
  153. {
  154. $dispatcher = new EventDispatcher();
  155. $kernel = $this->getHttpKernel($dispatcher, array(new Controller(), 'controller'));
  156. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  157. }
  158. public function testHandleWhenTheControllerIsAStaticArray()
  159. {
  160. $dispatcher = new EventDispatcher();
  161. $kernel = $this->getHttpKernel($dispatcher, array('Symfony\Component\HttpKernel\Tests\Controller', 'staticcontroller'));
  162. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  163. }
  164. /**
  165. * @expectedException \LogicException
  166. */
  167. public function testHandleWhenTheControllerDoesNotReturnAResponse()
  168. {
  169. $dispatcher = new EventDispatcher();
  170. $kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
  171. $kernel->handle(new Request());
  172. }
  173. public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()
  174. {
  175. $dispatcher = new EventDispatcher();
  176. $dispatcher->addListener(KernelEvents::VIEW, function ($event) {
  177. $event->setResponse(new Response($event->getControllerResult()));
  178. });
  179. $kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
  180. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  181. }
  182. public function testHandleWithAResponseListener()
  183. {
  184. $dispatcher = new EventDispatcher();
  185. $dispatcher->addListener(KernelEvents::RESPONSE, function ($event) {
  186. $event->setResponse(new Response('foo'));
  187. });
  188. $kernel = $this->getHttpKernel($dispatcher);
  189. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  190. }
  191. public function testHandleAllowChangingControllerArguments()
  192. {
  193. $dispatcher = new EventDispatcher();
  194. $dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function (FilterControllerArgumentsEvent $event) {
  195. $event->setArguments(array('foo'));
  196. });
  197. $kernel = $this->getHttpKernel($dispatcher, function ($content) { return new Response($content); });
  198. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  199. }
  200. public function testHandleAllowChangingControllerAndArguments()
  201. {
  202. $dispatcher = new EventDispatcher();
  203. $dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function (FilterControllerArgumentsEvent $event) {
  204. $oldController = $event->getController();
  205. $oldArguments = $event->getArguments();
  206. $newController = function ($id) use ($oldController, $oldArguments) {
  207. $response = call_user_func_array($oldController, $oldArguments);
  208. $response->headers->set('X-Id', $id);
  209. return $response;
  210. };
  211. $event->setController($newController);
  212. $event->setArguments(array('bar'));
  213. });
  214. $kernel = $this->getHttpKernel($dispatcher, function ($content) { return new Response($content); }, null, array('foo'));
  215. $this->assertResponseEquals(new Response('foo', 200, array('X-Id' => 'bar')), $kernel->handle(new Request()));
  216. }
  217. public function testTerminate()
  218. {
  219. $dispatcher = new EventDispatcher();
  220. $kernel = $this->getHttpKernel($dispatcher);
  221. $dispatcher->addListener(KernelEvents::TERMINATE, function ($event) use (&$called, &$capturedKernel, &$capturedRequest, &$capturedResponse) {
  222. $called = true;
  223. $capturedKernel = $event->getKernel();
  224. $capturedRequest = $event->getRequest();
  225. $capturedResponse = $event->getResponse();
  226. });
  227. $kernel->terminate($request = Request::create('/'), $response = new Response());
  228. $this->assertTrue($called);
  229. $this->assertEquals($kernel, $capturedKernel);
  230. $this->assertEquals($request, $capturedRequest);
  231. $this->assertEquals($response, $capturedResponse);
  232. }
  233. public function testVerifyRequestStackPushPopDuringHandle()
  234. {
  235. $request = new Request();
  236. $stack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->setMethods(array('push', 'pop'))->getMock();
  237. $stack->expects($this->at(0))->method('push')->with($this->equalTo($request));
  238. $stack->expects($this->at(1))->method('pop');
  239. $dispatcher = new EventDispatcher();
  240. $kernel = $this->getHttpKernel($dispatcher, null, $stack);
  241. $kernel->handle($request, HttpKernelInterface::MASTER_REQUEST);
  242. }
  243. /**
  244. * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
  245. */
  246. public function testInconsistentClientIpsOnMasterRequests()
  247. {
  248. $request = new Request();
  249. $request->setTrustedProxies(array('1.1.1.1'));
  250. $request->server->set('REMOTE_ADDR', '1.1.1.1');
  251. $request->headers->set('FORWARDED', '2.2.2.2');
  252. $request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
  253. $dispatcher = new EventDispatcher();
  254. $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
  255. $event->getRequest()->getClientIp();
  256. });
  257. $kernel = $this->getHttpKernel($dispatcher);
  258. $kernel->handle($request, $kernel::MASTER_REQUEST, false);
  259. }
  260. private function getHttpKernel(EventDispatcherInterface $eventDispatcher, $controller = null, RequestStack $requestStack = null, array $arguments = array())
  261. {
  262. if (null === $controller) {
  263. $controller = function () { return new Response('Hello'); };
  264. }
  265. $controllerResolver = $this->getMockBuilder(ControllerResolverInterface::class)->getMock();
  266. $controllerResolver
  267. ->expects($this->any())
  268. ->method('getController')
  269. ->will($this->returnValue($controller));
  270. $argumentResolver = $this->getMockBuilder(ArgumentResolverInterface::class)->getMock();
  271. $argumentResolver
  272. ->expects($this->any())
  273. ->method('getArguments')
  274. ->will($this->returnValue($arguments));
  275. return new HttpKernel($eventDispatcher, $controllerResolver, $requestStack, $argumentResolver);
  276. }
  277. private function assertResponseEquals(Response $expected, Response $actual)
  278. {
  279. $expected->setDate($actual->getDate());
  280. $this->assertEquals($expected, $actual);
  281. }
  282. }
  283. class Controller
  284. {
  285. public function __invoke()
  286. {
  287. return new Response('foo');
  288. }
  289. public function controller()
  290. {
  291. return new Response('foo');
  292. }
  293. public static function staticController()
  294. {
  295. return new Response('foo');
  296. }
  297. }
  298. function controller_func()
  299. {
  300. return new Response('foo');
  301. }