|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpKernel\Tests\EventListener; |
| 13 | + |
| 14 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 15 | +use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener; |
| 18 | +use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
| 19 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 20 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 21 | + |
| 22 | +class ValidateRequestListenerTest extends \PHPUnit_Framework_TestCase |
| 23 | +{ |
| 24 | + public function testListenerThrowsWhenMasterRequestHasInconsistentClientIps() |
| 25 | + { |
| 26 | + $dispatcher = new EventDispatcher(); |
| 27 | + $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); |
| 28 | + $listener = new ValidateRequestListener(); |
| 29 | + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
| 30 | + $request->method('getClientIps') |
| 31 | + ->will($this->throwException(new ConflictingHeadersException())); |
| 32 | + |
| 33 | + $dispatcher->addListener(KernelEvents::REQUEST, array($listener, 'onKernelRequest')); |
| 34 | + $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); |
| 35 | + |
| 36 | + $this->setExpectedException('Symfony\Component\HttpKernel\Exception\BadRequestHttpException'); |
| 37 | + $dispatcher->dispatch(KernelEvents::REQUEST, $event); |
| 38 | + } |
| 39 | + |
| 40 | + public function testListenerDoesNothingOnValidRequests() |
| 41 | + { |
| 42 | + $dispatcher = new EventDispatcher(); |
| 43 | + $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); |
| 44 | + $listener = new ValidateRequestListener(); |
| 45 | + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
| 46 | + $request->method('getClientIps') |
| 47 | + ->willReturn(array('127.0.0.1')); |
| 48 | + |
| 49 | + $dispatcher->addListener(KernelEvents::REQUEST, array($listener, 'onKernelRequest')); |
| 50 | + $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); |
| 51 | + $dispatcher->dispatch(KernelEvents::REQUEST, $event); |
| 52 | + } |
| 53 | + |
| 54 | + public function testListenerDoesNothingOnSubrequests() |
| 55 | + { |
| 56 | + $dispatcher = new EventDispatcher(); |
| 57 | + $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); |
| 58 | + $listener = new ValidateRequestListener(); |
| 59 | + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
| 60 | + $request->method('getClientIps') |
| 61 | + ->will($this->throwException(new ConflictingHeadersException())); |
| 62 | + |
| 63 | + $dispatcher->addListener(KernelEvents::REQUEST, array($listener, 'onKernelRequest')); |
| 64 | + $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST); |
| 65 | + $dispatcher->dispatch(KernelEvents::REQUEST, $event); |
| 66 | + } |
| 67 | +} |
0 commit comments