Permalink
Browse files

Merge pull request #55 from acasademont/no_cookie_on_error

Do not set the "hl" cookie if the response is an error, as the locale ma...
  • Loading branch information...
schmittjoh committed Nov 16, 2012
2 parents 9ac689e + da5feb2 commit 64070a8b8c0da7a1c411463778266caffedc7a12
Showing with 17 additions and 2 deletions.
  1. +3 −2 EventListener/CookieSettingListener.php
  2. +14 −0 Tests/Functional/PrefixStrategyTest.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));
}
}
-}
+}
@@ -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 64070a8

Please sign in to comment.