8000 Added tests trying to reproduce bug #11663, throwing AccessDeniedHttp… · adlpz/symfony@571ee0d · GitHub
[go: up one dir, main page]

Skip to content

Commit 571ee0d

Browse files
committed
Added tests trying to reproduce bug symfony#11663, throwing AccessDeniedHttpException in kernel request listener
1 parent d277c16 commit 571ee0d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests\EventListener;
1313

14+
use Symfony\Component\EventDispatcher\EventDispatcher;
15+
use Symfony\Component\HttpKernel\Exception\HttpException;
16+
use Symfony\Component\HttpKernel\HttpKernel;
1417
use Symfony\Component\HttpKernel\HttpKernelInterface;
1518
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
19+
use Symfony\Component\HttpKernel\KernelEvents;
1620
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
1721
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1822
use Symfony\Component\HttpFoundation\Request;
@@ -40,6 +44,32 @@ public function testConstruct()
4044
$this->assertSame('foo', $_controller->getValue($l));
4145
}
4246

47+
public function testHandleHttpExceptionThrownInListener()
48+
{
49+
// store the current error_log, and disable it temporarily
50+
$errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');
51+
52+
$listener = new ExceptionListener('foo');
53+
54+
$dispatcher = new EventDispatcher();
55+
$dispatcher->addSubscriber($listener);
56+
$dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
57+
throw new HttpException(1337);
58+
});
59+
60+
$kernel = new HttpKernel($dispatcher, $this->getTestResolver());
61+
62+
try {
63+
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST);
64+
$this->fail('HttpKernel::handle is expected to throw HttpException');
65+
} catch (HttpException $exception) {
66+
$this->assertEquals(1337, $exception->getStatusCode());
67+
}
68+
69+
// restore the old error_log
70+
ini_set('error_log', $errorLog);
71+
}
72+
4373
/**
4474
* @dataProvider provider
4575
*/
@@ -119,6 +149,21 @@ public function testSubRequestFormat()
119149
$response = $event->getResponse();
120150
$this->assertEquals('xml', $response->getContent());
121151
}
152+
153+
protected function getTestResolver()
154+
{
155+
$controller = function () { return new Response('foo'); };
156+
157+
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
158+
$resolver->expects($this->any())
159+
->method('getController')
160+
->will($this->returnValue($controller));
161+
$resolver->expects($this->any())
162+
->method('getArguments')
163+
->will($this->returnValue(array()));
164+
165+
return $resolver;
166+
}
122167
}
123168

124169
class TestLogger extends Logger implements DebugLoggerInterface

src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests;
1313

14+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1415
use Symfony\Component\HttpKernel\HttpKernel;
1516
use Symfony\Component\HttpKernel\HttpKernelInterface;
1617
use Symfony\Component\HttpKernel\KernelEvents;
@@ -71,6 +72,25 @@ public function testHandleExceptionWithARedirectionResponse()
7172
$this->assertEquals('/login', $response->headers->get('Location'));
7273
}
7374

75+
public function testHandleWhenAnAccessDeniedHttpExceptionIsThrownByAListener()
76+
{
77+
$dispatcher = new EventDispatcher();
78+
79+
$dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
80+
throw new AccessDeniedHttpException();
81+
});
82+
83+
$dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) {
84+
$event->setResponse(new Response('foo', $event->getException()->getStatusCode()));
85+
});
86+
87+
$kernel = new HttpKernel($dispatcher, $this->getResolver(function() { return new Response(); }));
88+
$response = $kernel->handle(new Request());
89+
90+
$this->assertEquals('foo', $response->getContent());
91+
$this->assertEquals('403', $response->getStatusCode());
92+
}
93+
7494
public function testHandleHttpException()
7595
{
7696
$dispatcher = new EventDispatcher();

0 commit comments

Comments
 (0)
0