bug #31213 [WebProfilerBundle] Intercept redirections only for HTML format (javiereguiluz)

This PR was merged into the 3.4 branch.

Discussion
----------

[WebProfilerBundle] Intercept redirections only for HTML format

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #31168
| License       | MIT
| Doc PR        | -

This applies the solution proposed by @bpolaszek in #31168.

Commits
-------

418678823b Intercept redirections only for HTML format
This commit is contained in:
Fabien Potencier 2019-04-24 09:02:22 +02:00
commit d98f7833f7
2 changed files with 15 additions and 2 deletions

View File

@ -87,7 +87,7 @@ class WebDebugToolbarListener implements EventSubscriberInterface
return;
}
if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects) {
if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects && 'html' === $request->getRequestFormat()) {
$session = $request->getSession();
if (null !== $session && $session->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
// keep current flashes for one more request if using AutoExpireFlashBag

View File

@ -58,7 +58,7 @@ class WebDebugToolbarListenerTest extends TestCase
/**
* @dataProvider provideRedirects
*/
public function testRedirectionIsIntercepted($statusCode, $hasSession)
public function testHtmlRedirectionIsIntercepted($statusCode, $hasSession)
{
$response = new Response('Some content', $statusCode);
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
@ -71,6 +71,19 @@ class WebDebugToolbarListenerTest extends TestCase
$this->assertEquals('Redirection', $response->getContent());
}
public function testNonHtmlRedirectionIsNotIntercepted()
{
$response = new Response('Some content', '301');
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'json', true), HttpKernelInterface::MASTER_REQUEST, $response);
$listener = new WebDebugToolbarListener($this->getTwigMock('Redirection'), true);
$listener->onKernelResponse($event);
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals('Some content', $response->getContent());
}
public function testToolbarIsInjected()
{
$response = new Response('<html><head></head><body></body></html>');