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\HttpKernel\Event\GetResponseEvent;
18 use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
19 use Symfony\Component\HttpKernel\HttpKernelInterface;
20 use Symfony\Component\HttpKernel\KernelEvents;
22 class ValidateRequestListenerTest extends TestCase
24 protected function tearDown()
26 Request::setTrustedProxies(array(), -1);
30 * @expectedException \Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException
32 public function testListenerThrowsWhenMasterRequestHasInconsistentClientIps()
34 $dispatcher = new EventDispatcher();
35 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
37 $request = new Request();
38 $request->setTrustedProxies(array('1.1.1.1'), Request::HEADER_X_FORWARDED_FOR | Request::HEADER_FORWARDED);
39 $request->server->set('REMOTE_ADDR', '1.1.1.1');
40 $request->headers->set('FORWARDED', 'for=2.2.2.2');
41 $request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
43 $dispatcher->addListener(KernelEvents::REQUEST, array(new ValidateRequestListener(), 'onKernelRequest'));
44 $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
46 $dispatcher->dispatch(KernelEvents::REQUEST, $event);