Skip to content
This repository
Browse code

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...
commit 64070a8b8c0da7a1c411463778266caffedc7a12 2 parents 9ac689e + da5feb2
Johannes authored
5  EventListener/CookieSettingListener.php
@@ -35,7 +35,8 @@ public function __construct($cookieName, $cookieLifetime, $cookiePath, $cookieDo
35 35
 
36 36
     public function onKernelResponse(FilterResponseEvent $event)
37 37
     {
38  
-        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
  38
+        //Check if the current response contains an error. If it does, do not set the cookie as the Locale may not be properly set
  39
+        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType() || !($event->getResponse()->isSuccessful() || $event->getResponse()->isRedirection())) {
39 40
             return;
40 41
         }
41 42
 
@@ -46,4 +47,4 @@ public function onKernelResponse(FilterResponseEvent $event)
46 47
             $event->getResponse()->headers->setCookie(new Cookie($this->cookieName, $request->getLocale(), time() + $this->cookieLifetime, $this->cookiePath, $this->cookieDomain, $this->cookieSecure, $this->cookieHttponly));
47 48
         }
48 49
     }
49  
-}
  50
+}
14  Tests/Functional/PrefixStrategyTest.php
@@ -57,4 +57,18 @@ public function testLanguageCookieIsSet()
57 57
         $this->assertSame(2, count($cookies));
58 58
         $this->assertSame('de', $cookies[0]->getValue());
59 59
     }
  60
+
  61
+    public function testNoCookieOnError()
  62
+    {
  63
+        $client = $this->createClient(array('config' => 'strategy_prefix.yml'));
  64
+        $client->insulate();
  65
+
  66
+        $client->request('GET', '/nonexistent');
  67
+
  68
+        $response = $client->getResponse();
  69
+        $this->assertTrue($response->isClientError(), (string) $response);
  70
+
  71
+        $cookies = $response->headers->getCookies();
  72
+        $this->assertSame(1, count($cookies));
  73
+    }
60 74
 }

0 notes on commit 64070a8

Please sign in to comment.
Something went wrong with that request. Please try again.