4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\EventListener;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\EventDispatcher\EventDispatcher;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\RequestStack;
18 use Symfony\Component\HttpFoundation\Response;
19 use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
20 use Symfony\Component\HttpKernel\Controller\ControllerResolver;
21 use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
22 use Symfony\Component\HttpKernel\EventListener\RouterListener;
23 use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
24 use Symfony\Component\HttpKernel\HttpKernelInterface;
25 use Symfony\Component\HttpKernel\HttpKernel;
26 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
27 use Symfony\Component\Routing\Exception\NoConfigurationException;
28 use Symfony\Component\Routing\RequestContext;
30 class RouterListenerTest extends TestCase
32 private $requestStack;
34 protected function setUp()
36 $this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->disableOriginalConstructor()->getMock();
40 * @dataProvider getPortData
42 public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHttpPort, $expectedHttpsPort)
44 $urlMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')
45 ->disableOriginalConstructor()
47 $context = new RequestContext();
48 $context->setHttpPort($defaultHttpPort);
49 $context->setHttpsPort($defaultHttpsPort);
50 $urlMatcher->expects($this->any())
51 ->method('getContext')
52 ->will($this->returnValue($context));
54 $listener = new RouterListener($urlMatcher, $this->requestStack);
55 $event = $this->createGetResponseEventForUri($uri);
56 $listener->onKernelRequest($event);
58 $this->assertEquals($expectedHttpPort, $context->getHttpPort());
59 $this->assertEquals($expectedHttpsPort, $context->getHttpsPort());
60 $this->assertEquals(0 === strpos($uri, 'https') ? 'https' : 'http', $context->getScheme());
63 public function getPortData()
66 array(80, 443, 'http://localhost/', 80, 443),
67 array(80, 443, 'http://localhost:90/', 90, 443),
68 array(80, 443, 'https://localhost/', 80, 443),
69 array(80, 443, 'https://localhost:90/', 80, 90),
76 * @return GetResponseEvent
78 private function createGetResponseEventForUri($uri)
80 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
81 $request = Request::create($uri);
82 $request->attributes->set('_controller', null); // Prevents going in to routing process
84 return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
88 * @expectedException \InvalidArgumentException
90 public function testInvalidMatcher()
92 new RouterListener(new \stdClass(), $this->requestStack);
95 public function testRequestMatcher()
97 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
98 $request = Request::create('http://localhost/');
99 $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
101 $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
102 $requestMatcher->expects($this->once())
103 ->method('matchRequest')
104 ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
105 ->will($this->returnValue(array()));
107 $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
108 $listener->onKernelRequest($event);
111 public function testSubRequestWithDifferentMethod()
113 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
114 $request = Request::create('http://localhost/', 'post');
115 $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
117 $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
118 $requestMatcher->expects($this->any())
119 ->method('matchRequest')
120 ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
121 ->will($this->returnValue(array()));
123 $context = new RequestContext();
125 $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
126 $listener->onKernelRequest($event);
128 // sub-request with another HTTP method
129 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
130 $request = Request::create('http://localhost/', 'get');
131 $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST);
133 $listener->onKernelRequest($event);
135 $this->assertEquals('GET', $context->getMethod());
139 * @dataProvider getLoggingParameterData
141 public function testLoggingParameter($parameter, $log, $parameters)
143 $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
144 $requestMatcher->expects($this->once())
145 ->method('matchRequest')
146 ->will($this->returnValue($parameter));
148 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
149 $logger->expects($this->once())
151 ->with($this->equalTo($log), $this->equalTo($parameters));
153 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
154 $request = Request::create('http://localhost/');
156 $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext(), $logger);
157 $listener->onKernelRequest(new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST));
160 public function getLoggingParameterData()
163 array(array('_route' => 'foo'), 'Matched route "{route}".', array('route' => 'foo', 'route_parameters' => array('_route' => 'foo'), 'request_uri' => 'http://localhost/', 'method' => 'GET')),
164 array(array(), 'Matched route "{route}".', array('route' => 'n/a', 'route_parameters' => array(), 'request_uri' => 'http://localhost/', 'method' => 'GET')),
168 public function testWithBadRequest()
170 $requestStack = new RequestStack();
172 $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
173 $requestMatcher->expects($this->never())->method('matchRequest');
175 $dispatcher = new EventDispatcher();
176 $dispatcher->addSubscriber(new ValidateRequestListener());
177 $dispatcher->addSubscriber(new RouterListener($requestMatcher, $requestStack, new RequestContext()));
178 $dispatcher->addSubscriber(new ExceptionListener(function () {
179 return new Response('Exception handled', 400);
182 $kernel = new HttpKernel($dispatcher, new ControllerResolver(), $requestStack, new ArgumentResolver());
184 $request = Request::create('http://localhost/');
185 $request->headers->set('host', '###');
186 $response = $kernel->handle($request);
187 $this->assertSame(400, $response->getStatusCode());
190 public function testNoRoutingConfigurationResponse()
192 $requestStack = new RequestStack();
194 $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
196 ->expects($this->once())
197 ->method('matchRequest')
198 ->willThrowException(new NoConfigurationException())
201 $dispatcher = new EventDispatcher();
202 $dispatcher->addSubscriber(new RouterListener($requestMatcher, $requestStack, new RequestContext()));
204 $kernel = new HttpKernel($dispatcher, new ControllerResolver(), $requestStack, new ArgumentResolver());
206 $request = Request::create('http://localhost/');
207 $response = $kernel->handle($request);
208 $this->assertSame(404, $response->getStatusCode());
209 $this->assertContains('Welcome', $response->getContent());
213 * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
215 public function testRequestWithBadHost()
217 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
218 $request = Request::create('http://bad host %22/');
219 $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
221 $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
223 $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
224 $listener->onKernelRequest($event);