8000 [HttpKernel] Make AbstractTestSessionListener compatible with CookieC… · symfony/symfony@f54d969 · GitHub
[go: up one dir, main page]

Skip to content

Commit f54d969

Browse files
thewilkybarkidnicolas-grekas
authored andcommitted
[HttpKernel] Make AbstractTestSessionListener compatible with CookieClearingLogoutHandler
1 parent b61c8fc commit f54d969

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
< 8000 /div>
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public function onKernelResponse(FilterResponseEvent $event)
7171

7272
if ($session instanceof Session ? !$session->isEmpty() || (null !== $this->sessionId && $session->getId() !== $this->sessionId) : $wasStarted) {
7373
$params = session_get_cookie_params();
74+
75+
foreach ($event->getResponse()->headers->getCookies() as $cookie) {
76+
if ($session->getName() === $cookie->getName() && $params['path'] === $cookie->getPath() && $params['domain'] == $cookie->getDomain()) {
77+
return;
78+
}
79+
}
80+
7481
$event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
7582
$this->sessionId = $session->getId();
7683
}

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ public function testEmptySessionWithNewSessionIdDoesSendCookie()
106106
$this->assertNotEmpty($response->headers->getCookies());
107107
}
108108

109+
/**
110+
* @dataProvider anotherCookieProvider
111+
*/
112+
public function testSessionWithNewSessionIdAndNewCookieDoesNotSendAnotherCookie($existing, array $expected)
113+
{
114+
$this->sessionHasBeenStarted();
115+
$this->sessionIsEmpty();
116+
$this->fixSessionId('456');
117+
118+
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
119+
$request = Request::create('/', 'GET', array(), array('MOCKSESSID' => '123'));
120+
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
121+
$this->listener->onKernelRequest($event);
122+
123+
$response = new Response('', 200, array('Set-Cookie' => $existing));
124+
125+
$response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
126+
127+
$this->assertSame($expected, $response->headers->get('Set-Cookie', null, false));
128+
}
129+
130+
public function anotherCookieProvider()
131+
{
132+
return array(
133+
'same' => array('MOCKSESSID=789; path=/', array('MOCKSESSID=789; path=/')),
134+
'different domain' => array('MOCKSESSID=789; path=/; domain=example.com', array('MOCKSESSID=789; path=/; domain=example.com', 'MOCKSESSID=456; path=/')),
135+
'different path' => array('MOCKSESSID=789; path=/foo', array('MOCKSESSID=789; path=/foo', 'MOCKSESSID=456; path=/')),
136+
);
137+
}
138+
109139
public function testUnstartedSessionIsNotSave()
110140
{
111141
$this->sessionHasNotBeenStarted();
@@ -123,10 +153,10 @@ public function testDoesNotImplementServiceSubscriberInterface()
123153
$this->assertFalse(is_subclass_of(TestSessionListener::class, ServiceSubscriberInterface::class, 'Implementing ServiceSubscriberInterface would create a dep on the DI component, which eg Silex cannot afford'));
124154
}
125155

126-
private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
156+
private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, Response $response = null)
127157
{
128158
$request->setSession($this->session);
129-
$response = new Response();
159+
$response = $response ?: new Response();
130160
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
131161
$event = new FilterResponseEvent($kernel, $request, $type, $response);
132162

0 commit comments

Comments
 (0)
0