Skip to content

Commit 7481550

Browse files
authored
Merge pull request #548 from ismailcherri/cookie-not-written-with-redirect
[Bug] Flash messages are overwritten with redirects
2 parents 0e3ecaa + 2f6d0e1 commit 7481550

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

src/EventListener/FlashMessageListener.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,21 @@ public function onKernelResponse(FlashMessageResponseEvent $event)
8787
return;
8888
}
8989

90+
$response = $event->getResponse();
91+
92+
// If the response is a redirect, we should wait until the final response
93+
// is reached
94+
if ($response->isRedirect()) {
95+
return;
96+
}
97+
9098
$flashBag = $this->session->getFlashBag();
9199
$flashes = $flashBag->all();
92100

93101
if (empty($flashes)) {
94102
return;
95103
}
96104

97-
$response = $event->getResponse();
98-
99105
$cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
100106
$host = (null === $this->options['host']) ? '' : $this->options['host'];
101107
if (isset($cookies[$host][$this->options['path']][$this->options['name']])) {

tests/Functional/EventListener/FlashMessageListenerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,36 @@ public function testFlashMessageCookieIsSet()
4949
$this->fail('Cookie flash_cookie_name not found in the cookie response header: '.implode(',', $cookies));
5050
}
5151
}
52+
53+
public function testFlashMessageCookieIsSetOnRedirect()
54+
{
55+
$client = static::createClient();
56+
$client->followRedirects(true);
57+
$client->setMaxRedirects(2);
58+
$session = static::$kernel->getContainer()->get('session');
59+
60+
$client->request('GET', '/flash-redirect');
61+
$this->assertFalse($session->isStarted());
62+
$response = $client->getResponse();
63+
$cookies = $response->headers->getCookies();
64+
$this->assertGreaterThanOrEqual(1, $cookies, implode(',', $cookies));
65+
66+
$found = false;
67+
foreach ($cookies as $cookie) {
68+
/** @var Cookie $cookie */
69+
if ('flash_cookie_name' !== $cookie->getName()) {
70+
continue;
71+
}
72+
73+
$this->assertEquals('/', $cookie->getPath());
74+
$this->assertNull($cookie->getDomain());
75+
$this->assertTrue($cookie->isSecure());
76+
$this->assertJsonStringEqualsJsonString(json_encode(['notice' => ['Flash Message!', 'Flash Message!']]), $cookie->getValue());
77+
$found = true;
78+
}
79+
80+
if (!$found) {
81+
$this->fail('Cookie flash_cookie_name not found in the cookie response header: '.implode(',', $cookies));
82+
}
83+
}
5284
}

tests/Functional/Fixtures/Controller/FlashMessageController.php

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

1414
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1515
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16+
use Symfony\Component\HttpFoundation\RedirectResponse;
1617
use Symfony\Component\HttpFoundation\Response;
1718

1819
if (!\class_exists(AbstractController::class)) {
@@ -30,4 +31,14 @@ public function flashAction()
3031

3132
return new Response('flash');
3233
}
34+
35+
public function flashRedirectAction()
36+
{
37+
$this->addFlash(
38+
'notice',
39+
'Flash Message!'
40+
);
41+
42+
return new RedirectResponse('/flash');
43+
}
3344
}

tests/Functional/Fixtures/app/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,9 @@ test_flash:
5656
path: /flash
5757
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\FlashMessageController::flashAction }
5858

59+
test_flash_redirect:
60+
path: /flash-redirect
61+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\FlashMessageController::flashRedirectAction }
62+
5963
user_context_hash:
6064
path: /secured_area/_fos_user_context_hash

tests/Functional/Fixtures/app/config/routing_41.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,9 @@ test_flash:
5656
path: /flash
5757
controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\FlashMessageController::flashAction
5858

59+
test_flash_redirect:
60+
path: /flash-redirect
61+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\FlashMessageController::flashRedirectAction }
62+
5963
user_context_hash:
6064
path: /secured_area/_fos_user_context_hash

0 commit comments

Comments
 (0)