From 89ed756462fd4660acfd8ad1c2224dffa0fccf3e Mon Sep 17 00:00:00 2001 From: David Maicher Date: Mon, 25 Jun 2018 20:31:52 +0200 Subject: [PATCH] failing test to reproduce session problem --- .../TestBundle/Controller/SessionController.php | 8 ++++++++ .../TestBundle/Resources/config/routing.yml | 4 ++++ .../Tests/Functional/SessionTest.php | 16 ++++++++++++++++ .../Component/HttpFoundation/Session/Session.php | 2 -- .../HttpFoundation/Session/SessionBagProxy.php | 5 +++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php index a1d91e7929..0c82d8cc73 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php @@ -43,6 +43,14 @@ class SessionController implements ContainerAwareInterface return new Response(sprintf('Welcome back %s, nice to meet you.', $name)); } + public function cacheableAction() + { + $response = new Response('all good'); + $response->setSharedMaxAge(100); + + return $response; + } + public function logoutAction(Request $request) { $request->getSession()->invalidate(); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml index 11b85a86d3..923204ab0f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml @@ -2,6 +2,10 @@ session_welcome: path: /session defaults: { _controller: TestBundle:Session:welcome } +session_cacheable: + path: /cacheable + defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::cacheableAction } + session_welcome_name: path: /session/{name} defaults: { _controller: TestBundle:Session:welcome } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php index 166d8a3391..2e1634220c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php @@ -126,6 +126,22 @@ class SessionTest extends WebTestCase $this->assertContains('Welcome back client2, nice to meet you.', $crawler2->text()); } + /** + * @dataProvider getConfigs + */ + public function testCorrectCacheControlHeadersForCacheableAction($config, $insulate) + { + $client = $this->createClient(array('test_case' => 'Session', 'root_config' => $config)); + if ($insulate) { + $client->insulate(); + } + + $client->request('GET', '/cacheable'); + + $response = $client->getResponse(); + $this->assertSame('public, s-maxage=100', $response->headers->get('cache-control')); + } + public function getConfigs() { return array( diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 31da54c73e..c0978d552f 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -54,8 +54,6 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable */ public function start() { - ++$this->usageIndex; - return $this->storage->start(); } diff --git a/src/Symfony/Component/HttpFoundation/Session/SessionBagProxy.php b/src/Symfony/Component/HttpFoundation/Session/SessionBagProxy.php index d6b242d4ee..3504bdfe7b 100644 --- a/src/Symfony/Component/HttpFoundation/Session/SessionBagProxy.php +++ b/src/Symfony/Component/HttpFoundation/Session/SessionBagProxy.php @@ -44,6 +44,11 @@ final class SessionBagProxy implements SessionBagInterface */ public function isEmpty() { + if (!isset($this->data[$this->bag->getStorageKey()])) { + return true; + } + ++$this->usageIndex; + return empty($this->data[$this->bag->getStorageKey()]); }