|
12 | 12 | namespace Symfony\Component\HttpKernel\Tests\EventListener;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 16 | +use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
15 | 17 | use Symfony\Component\HttpKernel\HttpKernelInterface;
|
16 | 18 | use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
|
| 19 | +use Symfony\Component\HttpKernel\KernelEvents; |
17 | 20 | use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
|
18 | 21 | use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
19 | 22 | use Symfony\Component\HttpFoundation\Request;
|
@@ -122,6 +125,32 @@ public function testSubRequestFormat()
|
122 | 125 | $response = $event->getResponse();
|
123 | 126 | $this->assertEquals('xml', $response->getContent());
|
124 | 127 | }
|
| 128 | + |
| 129 | + public function testCSPHeaderIsRemoved() |
| 130 | + { |
| 131 | + $dispatcher = new EventDispatcher(); |
| 132 | + $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(); |
| 133 | + $kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) { |
| 134 | + return new Response($request->getRequestFormat()); |
| 135 | + })); |
| 136 | + |
| 137 | + $listener = new ExceptionListener('foo', $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock()); |
| 138 | + |
| 139 | + $dispatcher->addSubscriber($listener); |
| 140 | + |
| 141 | + $request = Request::create('/'); |
| 142 | + $event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new \Exception('foo')); |
| 143 | + $dispatcher->dispatch(KernelEvents::EXCEPTION, $event); |
| 144 | + |
| 145 | + $response = new Response('', 200, array('content-security-policy' => "style-src 'self'")); |
| 146 | + $this->assertTrue($response->headers->has('content-security-policy')); |
| 147 | + |
| 148 | + $event = new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response); |
| 149 | + $dispatcher->dispatch(KernelEvents::RESPONSE, $event); |
| 150 | + |
| 151 | + $this->assertFalse($response->headers->has('content-security-policy'), 'CSP header has been removed'); |
| 152 | + $this->assertFalse($dispatcher->hasListeners(KernelEvents::RESPONSE), 'CSP removal listener has been removed'); |
| 153 | + } |
125 | 154 | }
|
126 | 155 |
|
127 | 156 | class TestLogger extends Logger implements DebugLoggerInterface
|
|
0 commit comments