Skip to content

Commit

Permalink
[HttpKernel] Fix SessionListener without session in request #46268
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Morcinek committed May 6, 2022
1 parent b1e5131 commit 473f4dd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion EventListener/AbstractSessionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function onKernelResponse(FilterResponseEvent $event)
// Always remove the internal header if present
$response->headers->remove(self::NO_AUTO_CACHE_CONTROL_HEADER);

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

Expand Down
18 changes: 18 additions & 0 deletions Tests/EventListener/SessionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ public function testUninitializedSession()
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
}

public function testUninitializedSessionWithoutInitializedSession()
{
$kernel = $this->createMock(HttpKernelInterface::class);
$response = new Response();
$response->setSharedMaxAge(60);
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');

$container = new ServiceLocator([]);

$listener = new SessionListener($container);
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MASTER_REQUEST, $response));
$this->assertFalse($response->headers->has('Expires'));
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
$this->assertFalse($response->headers->hasCacheControlDirective('must-revalidate'));
$this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
}

public function testSurrogateMasterRequestIsPublic()
{
$session = $this->createMock(Session::class);
Expand Down

0 comments on commit 473f4dd

Please sign in to comment.