Skip to content

Commit 7abddd0

Browse files
committed
Fix SessionListener without session in request
1 parent ab7b2d9 commit 7abddd0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function onKernelRequest(RequestEvent $event)
6868

6969
public function onKernelResponse(ResponseEvent $event)
7070
{
71-
if (!$event->isMainRequest()) {
71+
if (!$event->isMainRequest() || (!$this->container->has('initialized_session') && !$event->getRequest()->hasSession())) {
7272
return;
7373
}
7474

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

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

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

0 commit comments

Comments
 (0)