8000 [hackday] Added tests trying to reproduce bug #11663, throwing AccessDeniedHttpException in kernel request listener by adlpz · Pull Request #12709 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[hackday] Added tests trying to reproduce bug #11663, throwing AccessDeniedHttpException in kernel request listener #12709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added tests trying to reproduce bug #11663, throwing AccessDeniedHttp…
…Exception in kernel request listener
  • Loading branch information
adlpz committed Nov 29, 2014
commit 571ee0dc7c375bc7e529372530d8ae20adb192ad
< E6CA td id="diff-fd319dfda9fb25cc02132421b16403197583f85c8a2a4ac879931f1332db6f4fL122" data-line-number="122" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@

namespace Symfony\Component\HttpKernel\Tests\EventListener;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -40,6 +44,32 @@ public function testConstruct()
$this->assertSame('foo', $_controller->getValue($l));
}

public function testHandleHttpExceptionThrownInListener()
{
// store the current error_log, and disable it temporarily
$errorLog = ini_set('error_log', file_exists('/dev/null') ? '/dev/null' : 'nul');

$listener = new ExceptionListener('foo');

$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber($listener);
$dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
throw new HttpException(1337);
});

$kernel = new HttpKernel($dispatcher, $this->getTestResolver());

try {
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST);
$this->fail('HttpKernel::handle is expected to throw HttpException');
} catch (HttpException $exception) {
$this->assertEquals(1337, $exception->getStatusCode());
}

// restore the old error_log
ini_set('error_log', $errorLog);
}

/**
* @dataProvider provider
*/
Expand Down Expand Up @@ -119,6 +149,21 @@ public function testSubRequestFormat()
$response = $event->getResponse();
$this->assertEquals('xml', $response->getContent());
}

protected function getTestResolver()
{
$controller = function () { return new Response('foo'); };

$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
$resolver->expects($this->any())
->method('getController')
->will($this->returnValue($controller));
$resolver->expects($this->any())
->method('getArguments')
->will($this->returnValue(array()));

return $resolver;
}
}

class TestLogger extends Logger implements DebugLoggerInterface
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpKernel\Tests;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
Expand Down Expand Up @@ -71,6 +72,25 @@ public function testHandleExceptionWithARedirectionResponse()
$this->assertEquals('/login', $response->headers->get('Location'));
}

public function testHandleWhenAnAccessDeniedHttpExceptionIsThrownByAListener()
{
$dispatcher = new EventDispatcher();

$dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
throw new AccessDeniedHttpException();
});

$dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) {
$event->setResponse(new Response('foo', $event->getException()->getStatusCode()));
});

$kernel = new HttpKernel($dispatcher, $this->getResolver(function() { return new Response(); }));
$response = $kernel->handle(new Request());

$this->assertEquals('foo', $response->getContent());
$this->assertEquals('403', $response->getStatusCode());
}

public function testHandleHttpException()
{
$dispatcher = new EventDispatcher();
Expand Down
0