HttpKernelTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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\EventDispatcher;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
  19. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  20. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  21. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  22. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  23. use Symfony\Component\HttpKernel\Exception\ControllerDoesNotReturnResponseException;
  24. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  25. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  26. use Symfony\Component\HttpKernel\HttpKernel;
  27. use Symfony\Component\HttpKernel\HttpKernelInterface;
  28. use Symfony\Component\HttpKernel\KernelEvents;
  29. class HttpKernelTest extends TestCase
  30. {
  31. public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrue()
  32. {
  33. $this->expectException(\RuntimeException::class);
  34. $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); });
  35. $kernel->handle(new Request(), HttpKernelInterface::MAIN_REQUEST, true);
  36. }
  37. public function testRequestStackIsNotBrokenWhenControllerThrowsAnExceptionAndCatchIsTrue()
  38. {
  39. $requestStack = new RequestStack();
  40. $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); }, $requestStack);
  41. try {
  42. $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
  43. } catch (\Throwable $exception) {
  44. }
  45. self::assertNull($requestStack->getCurrentRequest());
  46. }
  47. public function testRequestStackIsNotBrokenWhenControllerThrowsAnExceptionAndCatchIsFalse()
  48. {
  49. $requestStack = new RequestStack();
  50. $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); }, $requestStack);
  51. try {
  52. $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
  53. } catch (\Throwable $exception) {
  54. }
  55. self::assertNull($requestStack->getCurrentRequest());
  56. }
  57. public function testRequestStackIsNotBrokenWhenControllerThrowsAnThrowable()
  58. {
  59. $requestStack = new RequestStack();
  60. $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \Error(); }, $requestStack);
  61. try {
  62. $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
  63. } catch (\Throwable $exception) {
  64. }
  65. self::assertNull($requestStack->getCurrentRequest());
  66. }
  67. public function testHandleWhenControllerThrowsAnExceptionAndCatchIsFalseAndNoListenerIsRegistered()
  68. {
  69. $this->expectException(\RuntimeException::class);
  70. $kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); });
  71. $kernel->handle(new Request(), HttpKernelInterface::MAIN_REQUEST, false);
  72. }
  73. public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithAHandlingListener()
  74. {
  75. $dispatcher = new EventDispatcher();
  76. $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
  77. $event->setResponse(new Response($event->getThrowable()->getMessage()));
  78. });
  79. $kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException('foo'); });
  80. $response = $kernel->handle(new Request(), HttpKernelInterface::MAIN_REQUEST, true);
  81. $this->assertEquals('500', $response->getStatusCode());
  82. $this->assertEquals('foo', $response->getContent());
  83. }
  84. public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithANonHandlingListener()
  85. {
  86. $exception = new \RuntimeException();
  87. $dispatcher = new EventDispatcher();
  88. $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
  89. // should set a response, but does not
  90. });
  91. $kernel = $this->getHttpKernel($dispatcher, function () use ($exception) { throw $exception; });
  92. try {
  93. $kernel->handle(new Request(), HttpKernelInterface::MAIN_REQUEST, true);
  94. $this->fail('LogicException expected');
  95. } catch (\RuntimeException $e) {
  96. $this->assertSame($exception, $e);
  97. }
  98. }
  99. public function testHandleExceptionWithARedirectionResponse()
  100. {
  101. $dispatcher = new EventDispatcher();
  102. $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
  103. $event->setResponse(new RedirectResponse('/login', 301));
  104. });
  105. $kernel = $this->getHttpKernel($dispatcher, function () { throw new AccessDeniedHttpException(); });
  106. $response = $kernel->handle(new Request());
  107. $this->assertEquals('301', $response->getStatusCode());
  108. $this->assertEquals('/login', $response->headers->get('Location'));
  109. }
  110. public function testHandleHttpException()
  111. {
  112. $dispatcher = new EventDispatcher();
  113. $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
  114. $event->setResponse(new Response($event->getThrowable()->getMessage()));
  115. });
  116. $kernel = $this->getHttpKernel($dispatcher, function () { throw new MethodNotAllowedHttpException(['POST']); });
  117. $response = $kernel->handle(new Request());
  118. $this->assertEquals('405', $response->getStatusCode());
  119. $this->assertEquals('POST', $response->headers->get('Allow'));
  120. }
  121. public function getStatusCodes()
  122. {
  123. return [
  124. [200, 404],
  125. [404, 200],
  126. [301, 200],
  127. [500, 200],
  128. ];
  129. }
  130. /**
  131. * @dataProvider getSpecificStatusCodes
  132. */
  133. public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($expectedStatusCode)
  134. {
  135. $dispatcher = new EventDispatcher();
  136. $dispatcher->addListener(KernelEvents::EXCEPTION, function (ExceptionEvent $event) use ($expectedStatusCode) {
  137. $event->allowCustomResponseCode();
  138. $event->setResponse(new Response('', $expectedStatusCode));
  139. });
  140. $kernel = $this->getHttpKernel($dispatcher, function () { throw new \RuntimeException(); });
  141. $response = $kernel->handle(new Request());
  142. $this->assertEquals($expectedStatusCode, $response->getStatusCode());
  143. }
  144. public static function getSpecificStatusCodes()
  145. {
  146. return [
  147. [200],
  148. [302],
  149. [403],
  150. ];
  151. }
  152. public function testHandleWhenAListenerReturnsAResponse()
  153. {
  154. $dispatcher = new EventDispatcher();
  155. $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
  156. $event->setResponse(new Response('hello'));
  157. });
  158. $kernel = $this->getHttpKernel($dispatcher);
  159. $this->assertEquals('hello', $kernel->handle(new Request())->getContent());
  160. }
  161. public function testHandleWhenNoControllerIsFound()
  162. {
  163. $this->expectException(NotFoundHttpException::class);
  164. $dispatcher = new EventDispatcher();
  165. $kernel = $this->getHttpKernel($dispatcher, false);
  166. $kernel->handle(new Request());
  167. }
  168. public function testHandleWhenTheControllerIsAClosure()
  169. {
  170. $response = new Response('foo');
  171. $dispatcher = new EventDispatcher();
  172. $kernel = $this->getHttpKernel($dispatcher, function () use ($response) { return $response; });
  173. $this->assertSame($response, $kernel->handle(new Request()));
  174. }
  175. public function testHandleWhenTheControllerIsAnObjectWithInvoke()
  176. {
  177. $dispatcher = new EventDispatcher();
  178. $kernel = $this->getHttpKernel($dispatcher, new TestController());
  179. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  180. }
  181. public function testHandleWhenTheControllerIsAFunction()
  182. {
  183. $dispatcher = new EventDispatcher();
  184. $kernel = $this->getHttpKernel($dispatcher, 'Symfony\Component\HttpKernel\Tests\controller_func');
  185. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  186. }
  187. public function testHandleWhenTheControllerIsAnArray()
  188. {
  189. $dispatcher = new EventDispatcher();
  190. $kernel = $this->getHttpKernel($dispatcher, [new TestController(), 'controller']);
  191. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  192. }
  193. public function testHandleWhenTheControllerIsAStaticArray()
  194. {
  195. $dispatcher = new EventDispatcher();
  196. $kernel = $this->getHttpKernel($dispatcher, ['Symfony\Component\HttpKernel\Tests\TestController', 'staticcontroller']);
  197. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  198. }
  199. public function testHandleWhenTheControllerDoesNotReturnAResponse()
  200. {
  201. $dispatcher = new EventDispatcher();
  202. $kernel = $this->getHttpKernel($dispatcher, function () {});
  203. try {
  204. $kernel->handle(new Request());
  205. $this->fail('The kernel should throw an exception.');
  206. } catch (ControllerDoesNotReturnResponseException $e) {
  207. $first = $e->getTrace()[0];
  208. // `file` index the array starting at 0, and __FILE__ starts at 1
  209. $line = file($first['file'])[$first['line'] - 2];
  210. $this->assertStringContainsString('// call controller', $line);
  211. }
  212. }
  213. public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()
  214. {
  215. $dispatcher = new EventDispatcher();
  216. $dispatcher->addListener(KernelEvents::VIEW, function ($event) {
  217. $event->setResponse(new Response($event->getControllerResult()));
  218. });
  219. $kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
  220. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  221. }
  222. public function testHandleWithAResponseListener()
  223. {
  224. $dispatcher = new EventDispatcher();
  225. $dispatcher->addListener(KernelEvents::RESPONSE, function ($event) {
  226. $event->setResponse(new Response('foo'));
  227. });
  228. $kernel = $this->getHttpKernel($dispatcher);
  229. $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
  230. }
  231. public function testHandleAllowChangingControllerArguments()
  232. {
  233. $dispatcher = new EventDispatcher();
  234. $dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function ($event) {
  235. $event->setArguments(['foo']);
  236. });
  237. $kernel = $this->getHttpKernel($dispatcher, function ($content) { return new Response($content); });
  238. $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
  239. }
  240. public function testHandleAllowChangingControllerAndArguments()
  241. {
  242. $dispatcher = new EventDispatcher();
  243. $dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function ($event) {
  244. $oldController = $event->getController();
  245. $oldArguments = $event->getArguments();
  246. $newController = function ($id) use ($oldController, $oldArguments) {
  247. $response = $oldController(...$oldArguments);
  248. $response->headers->set('X-Id', $id);
  249. return $response;
  250. };
  251. $event->setController($newController);
  252. $event->setArguments(['bar']);
  253. });
  254. $kernel = $this->getHttpKernel($dispatcher, function ($content) { return new Response($content); }, null, ['foo']);
  255. $this->assertResponseEquals(new Response('foo', 200, ['X-Id' => 'bar']), $kernel->handle(new Request()));
  256. }
  257. public function testTerminate()
  258. {
  259. $dispatcher = new EventDispatcher();
  260. $kernel = $this->getHttpKernel($dispatcher);
  261. $dispatcher->addListener(KernelEvents::TERMINATE, function ($event) use (&$called, &$capturedKernel, &$capturedRequest, &$capturedResponse) {
  262. $called = true;
  263. $capturedKernel = $event->getKernel();
  264. $capturedRequest = $event->getRequest();
  265. $capturedResponse = $event->getResponse();
  266. });
  267. $kernel->terminate($request = Request::create('/'), $response = new Response());
  268. $this->assertTrue($called);
  269. $this->assertEquals($kernel, $capturedKernel);
  270. $this->assertEquals($request, $capturedRequest);
  271. $this->assertEquals($response, $capturedResponse);
  272. }
  273. public function testTerminateWithException()
  274. {
  275. $dispatcher = new EventDispatcher();
  276. $requestStack = new RequestStack();
  277. $kernel = $this->getHttpKernel($dispatcher, null, $requestStack);
  278. $dispatcher->addListener(KernelEvents::EXCEPTION, function (ExceptionEvent $event) use (&$capturedRequest, $requestStack) {
  279. $capturedRequest = $requestStack->getCurrentRequest();
  280. $event->setResponse(new Response());
  281. });
  282. $kernel->terminateWithException(new \Exception('boo'), $request = Request::create('/'));
  283. $this->assertSame($request, $capturedRequest);
  284. $this->assertNull($requestStack->getCurrentRequest());
  285. }
  286. public function testVerifyRequestStackPushPopDuringHandle()
  287. {
  288. $request = new Request();
  289. $stack = $this->getMockBuilder(RequestStack::class)->onlyMethods(['push', 'pop'])->getMock();
  290. $stack->expects($this->once())->method('push')->with($this->equalTo($request));
  291. $stack->expects($this->once())->method('pop');
  292. $dispatcher = new EventDispatcher();
  293. $kernel = $this->getHttpKernel($dispatcher, null, $stack);
  294. $kernel->handle($request, HttpKernelInterface::MAIN_REQUEST);
  295. }
  296. public function testInconsistentClientIpsOnMainRequests()
  297. {
  298. $this->expectException(BadRequestHttpException::class);
  299. $request = new Request();
  300. $request->setTrustedProxies(['1.1.1.1'], Request::HEADER_X_FORWARDED_FOR | Request::HEADER_FORWARDED);
  301. $request->server->set('REMOTE_ADDR', '1.1.1.1');
  302. $request->headers->set('FORWARDED', 'for=2.2.2.2');
  303. $request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
  304. $dispatcher = new EventDispatcher();
  305. $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
  306. $event->getRequest()->getClientIp();
  307. });
  308. $kernel = $this->getHttpKernel($dispatcher);
  309. $kernel->handle($request, $kernel::MAIN_REQUEST, false);
  310. Request::setTrustedProxies([], -1);
  311. }
  312. private function getHttpKernel(EventDispatcherInterface $eventDispatcher, $controller = null, RequestStack $requestStack = null, array $arguments = [])
  313. {
  314. if (null === $controller) {
  315. $controller = function () { return new Response('Hello'); };
  316. }
  317. $controllerResolver = $this->createMock(ControllerResolverInterface::class);
  318. $controllerResolver
  319. ->expects($this->any())
  320. ->method('getController')
  321. ->willReturn($controller);
  322. $argumentResolver = $this->createMock(ArgumentResolverInterface::class);
  323. $argumentResolver
  324. ->expects($this->any())
  325. ->method('getArguments')
  326. ->willReturn($arguments);
  327. return new HttpKernel($eventDispatcher, $controllerResolver, $requestStack, $argumentResolver);
  328. }
  329. private function assertResponseEquals(Response $expected, Response $actual)
  330. {
  331. $expected->setDate($actual->getDate());
  332. $this->assertEquals($expected, $actual);
  333. }
  334. }
  335. class TestController
  336. {
  337. public function __invoke()
  338. {
  339. return new Response('foo');
  340. }
  341. public function controller()
  342. {
  343. return new Response('foo');
  344. }
  345. public static function staticController()
  346. {
  347. return new Response('foo');
  348. }
  349. }
  350. function controller_func()
  351. {
  352. return new Response('foo');
  353. }