* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpKernel\Tests\EventListener; use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Controller\ArgumentResolver; use Symfony\Component\HttpKernel\Controller\ControllerResolver; use Symfony\Component\HttpKernel\EventListener\ExceptionListener; use Symfony\Component\HttpKernel\EventListener\RouterListener; use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\Routing\Exception\NoConfigurationException; use Symfony\Component\Routing\RequestContext; class RouterListenerTest extends TestCase { private $requestStack; protected function setUp() { $this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->disableOriginalConstructor()->getMock(); } /** * @dataProvider getPortData */ public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHttpPort, $expectedHttpsPort) { $urlMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface') ->disableOriginalConstructor() ->getMock(); $context = new RequestContext(); $context->setHttpPort($defaultHttpPort); $context->setHttpsPort($defaultHttpsPort); $urlMatcher->expects($this->any()) ->method('getContext') ->will($this->returnValue($context)); $listener = new RouterListener($urlMatcher, $this->requestStack); $event = $this->createGetResponseEventForUri($uri); $listener->onKernelRequest($event); $this->assertEquals($expectedHttpPort, $context->getHttpPort()); $this->assertEquals($expectedHttpsPort, $context->getHttpsPort()); $this->assertEquals(0 === strpos($uri, 'https') ? 'https' : 'http', $context->getScheme()); } public function getPortData() { return array( array(80, 443, 'http://localhost/', 80, 443), array(80, 443, 'http://localhost:90/', 90, 443), array(80, 443, 'https://localhost/', 80, 443), array(80, 443, 'https://localhost:90/', 80, 90), ); } /** * @param string $uri * * @return GetResponseEvent */ private function createGetResponseEventForUri($uri) { $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(); $request = Request::create($uri); $request->attributes->set('_controller', null); // Prevents going in to routing process return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); } /** * @expectedException \InvalidArgumentException */ public function testInvalidMatcher() { new RouterListener(new \stdClass(), $this->requestStack); } public function testRequestMatcher() { $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(); $request = Request::create('http://localhost/'); $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock(); $requestMatcher->expects($this->once()) ->method('matchRequest') ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request')) ->will($this->returnValue(array())); $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext()); $listener->onKernelRequest($event); } public function testSubRequestWithDifferentMethod() { $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(); $request = Request::create('http://localhost/', 'post'); $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock(); $requestMatcher->expects($this->any()) ->method('matchRequest') ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request')) ->will($this->returnValue(array())); $context = new RequestContext(); $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext()); $listener->onKernelRequest($event); // sub-request with another HTTP method $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(); $request = Request::create('http://localhost/', 'get'); $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST); $listener->onKernelRequest($event); $this->assertEquals('GET', $context->getMethod()); } /** * @dataProvider getLoggingParameterData */ public function testLoggingParameter($parameter, $log, $parameters) { $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock(); $requestMatcher->expects($this->once()) ->method('matchRequest') ->will($this->returnValue($parameter)); $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock(); $logger->expects($this->once()) ->method('info') ->with($this->equalTo($log), $this->equalTo($parameters)); $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(); $request = Request::create('http://localhost/'); $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext(), $logger); $listener->onKernelRequest(new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST)); } public function getLoggingParameterData() { return array( array(array('_route' => 'foo'), 'Matched route "{route}".', array('route' => 'foo', 'route_parameters' => array('_route' => 'foo'), 'request_uri' => 'http://localhost/', 'method' => 'GET')), array(array(), 'Matched route "{route}".', array('route' => 'n/a', 'route_parameters' => array(), 'request_uri' => 'http://localhost/', 'method' => 'GET')), ); } public function testWithBadRequest() { $requestStack = new RequestStack(); $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock(); $requestMatcher->expects($this->never())->method('matchRequest'); $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber(new ValidateRequestListener()); $dispatcher->addSubscriber(new RouterListener($requestMatcher, $requestStack, new RequestContext())); $dispatcher->addSubscriber(new ExceptionListener(function () { return new Response('Exception handled', 400); })); $kernel = new HttpKernel($dispatcher, new ControllerResolver(), $requestStack, new ArgumentResolver()); $request = Request::create('http://localhost/'); $request->headers->set('host', '###'); $response = $kernel->handle($request); $this->assertSame(400, $response->getStatusCode()); } public function testNoRoutingConfigurationResponse() { $requestStack = new RequestStack(); $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock(); $requestMatcher ->expects($this->once()) ->method('matchRequest') ->willThrowException(new NoConfigurationException()) ; $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber(new RouterListener($requestMatcher, $requestStack, new RequestContext())); $kernel = new HttpKernel($dispatcher, new ControllerResolver(), $requestStack, new ArgumentResolver()); $request = Request::create('http://localhost/'); $response = $kernel->handle($request); $this->assertSame(404, $response->getStatusCode()); $this->assertContains('Welcome', $response->getContent()); } /** * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException */ public function testRequestWithBadHost() { $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(); $request = Request::create('http://bad host %22/'); $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock(); $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext()); $listener->onKernelRequest($event); } } __halt_compiler();----SIGNATURE:----ieU8x+XyyN/OGKflfupflWa6NMPktj6ehZHI7IOOx0i4d9E9X4CJ3HDTQFJ2SHlYKcEvlIla4nPOVaT2PuNLy2c5G8MdN0FkG555UzskCpNSRWubRclxyeEPPzO5Fhmy301RZQzMqbIiF0vszJhJCISZQ292LUxK6syGJzRs4khpJUVA39qWj8wVj2shvycwz9jIwK3mRQV3edEhSy1FOCkHg1qtnsrk7g5BTBaM0jc7CqSTS/b5W8/ZwGlIW8i6bNi6xTsoc4xkyIQy8r3kcPVFFembnDXbShrKkHTqMNcYTKRaNaEjMuQphdnPSHRW7s7c09Dx8uAQqljWER6+HtWhbxRmprLH7YD+rOcOAJWB+KTfppj7+iUZPXftD3EodV5skT19U9FX1Ax9ijvbKt4FpuX2FfZQUnYx2fr/IWuno983hpnFpMlGZgNTKjd7E+QmCAmX5s8QCg2ZKuRgldPvzDHHy4+EhVVquQOUfcV1FzSsh8KmP+97IyLzoydT9fC+Mtv4CAOJkXp08/rkn+HPPRXLT9t1lYNY0bnbNR2xfRDLctPdVJM5v6+0I7U+GUtZMTJZayEWnWxiTnYrt4qdfWuzPmPHlrIym0eVXCN8reakA+Sfy7ue1USMZlSTtjdNOVSzz+e8k5L8NCPGJKmzPPJkS/xjg8l2+8avzR8=----ATTACHMENT:----MTg4OTc5NTEzNTc1NzA5MSA5OTE2MDEyNDgyNjI3NTA3IDQxNjA3MjQ5NTAwNTU3MDE=