Skip to content

Commit

Permalink
Fix SessionListener without session in request
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Dec 23, 2021
1 parent ab7b2d9 commit 7abddd0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function onKernelRequest(RequestEvent $event)

public function onKernelResponse(ResponseEvent $event)
{
if (!$event->isMainRequest()) {
if (!$event->isMainRequest() || (!$this->container->has('initialized_session') && !$event->getRequest()->hasSession())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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 testSurrogateMainRequestIsPublic()
{
$session = $this->createMock(Session::class);
Expand Down

0 comments on commit 7abddd0

Please sign in to comment.