No Description

HttpKernelTest.php 14KB

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