Skip to content

Commit 473f4dd

Browse files
author
Eduard Morcinek
committed
[HttpKernel] Fix SessionListener without session in request #46268
1 parent b1e5131 commit 473f4dd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

EventListener/AbstractSessionListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function onKernelResponse(FilterResponseEvent $event)
7474
// Always remove the internal header if present
7575
$response->headers->remove(self::NO_AUTO_CACHE_CONTROL_HEADER);
7676

77-
if (!$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : $event->getRequest()->getSession()) {
77+
if (!$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : ($event->getRequest()->hasSession() ? $event->getRequest()->getSession() : null)) {
7878
return;
7979
}
8080

Tests/EventListener/SessionListenerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ public function testUninitializedSession()
139139
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
140140
}
141141

142+
public function testUninitializedSessionWithoutInitializedSession()
143+
{
144+
$kernel = $this->createMock(HttpKernelInterface::class);
145+
$response = new Response();
146+
$response->setSharedMaxAge(60);
147+
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
148+
149+
$container = new ServiceLocator([]);
150+
151+
$listener = new SessionListener($container);
152+
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MASTER_REQUEST, $response));
153+
$this->assertFalse($response->headers->has('Expires'));
154+
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
155+
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
156+
$this->assertFalse($response->headers->hasCacheControlDirective('must-revalidate'));
157+
$this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
158+
}
159+
142160
public function testSurrogateMasterRequestIsPublic()
143161
{
144162
$session = $this->createMock(Session::class);

0 commit comments

Comments
 (0)