Browse files

Do not set the "hl" cookie if the response is an error, as the locale…

… may not be properly set, thus rendering the cookie useless
  • Loading branch information...
1 parent c1be05b commit da5feb2a61fbb435ff6cba94c3d14bab8550789c @acasademont acasademont committed Jul 17, 2012
Showing with 17 additions and 2 deletions.
  1. +3 −2 EventListener/CookieSettingListener.php
  2. +14 −0 Tests/Functional/PrefixStrategyTest.php
View
5 EventListener/CookieSettingListener.php
@@ -35,7 +35,8 @@ public function __construct($cookieName, $cookieLifetime, $cookiePath, $cookieDo
public function onKernelResponse(FilterResponseEvent $event)
{
- if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
+ //Check if the current response contains an error. If it does, do not set the cookie as the Locale may not be properly set
+ if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType() || !($event->getResponse()->isSuccessful() || $event->getResponse()->isRedirection())) {
return;
}
@@ -46,4 +47,4 @@ public function onKernelResponse(FilterResponseEvent $event)
$event->getResponse()->headers->setCookie(new Cookie($this->cookieName, $request->getLocale(), time() + $this->cookieLifetime, $this->cookiePath, $this->cookieDomain, $this->cookieSecure, $this->cookieHttponly));
}
}
-}
+}
View
14 Tests/Functional/PrefixStrategyTest.php
@@ -57,4 +57,18 @@ public function testLanguageCookieIsSet()
$this->assertSame(2, count($cookies));
$this->assertSame('de', $cookies[0]->getValue());
}
+
+ public function testNoCookieOnError()
+ {
+ $client = $this->createClient(array('config' => 'strategy_prefix.yml'));
+ $client->insulate();
+
+ $client->request('GET', '/nonexistent');
+
+ $response = $client->getResponse();
+ $this->assertTrue($response->isClientError(), (string) $response);
+
+ $cookies = $response->headers->getCookies();
+ $this->assertSame(1, count($cookies));
+ }
}

0 comments on commit da5feb2

Please sign in to comment.