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

Skip to content

Commit f3d49f7

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

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

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

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

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

14+
use Symfony\Component\EventDispatcher\EventDispatcher;
15+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
16+
use Symfony\Component\HttpKernel\Exception\HttpException;
17+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18+
use Symfony\Component\HttpKernel\HttpKernel;
1419
use Symfony\Component\HttpKernel\HttpKernelInterface;
1520
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
21+
use Symfony\Component\HttpKernel\KernelEvents;
1622
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
1723
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1824
use Symfony\Component\HttpFoundation\Request;
@@ -40,6 +46,32 @@ public function testConstruct()
4046
$this->assertSame('foo', $_controller->getValue($l));
4147
}
4248

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

124171
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