Browse Source

Response withoutCookie

Joris Vaesen 8 years ago
parent
commit
862c12fa68
2 changed files with 108 additions and 0 deletions
  1. 61 0
      src/Http/Response.php
  2. 47 0
      tests/TestCase/Http/ResponseTest.php

+ 61 - 0
src/Http/Response.php

@@ -19,6 +19,7 @@ use Cake\Filesystem\File;
 use Cake\Http\Cookie\Cookie;
 use Cake\Http\Cookie\CookieCollection;
 use Cake\Http\Cookie\CookieInterface;
+use Cake\I18n\Time;
 use Cake\Log\Log;
 use Cake\Network\CorsBuilder;
 use Cake\Network\Exception\NotFoundException;
@@ -2041,6 +2042,66 @@ class Response implements ResponseInterface
     }
 
     /**
+     * Create a new response with an expired cookie set.
+     *
+     * ### Options
+     *
+     * - `name`: The Cookie name
+     * - `value`: Value of the cookie
+     * - `expire`: Time the cookie expires in
+     * - `path`: Path the cookie applies to
+     * - `domain`: Domain the cookie is for.
+     * - `secure`: Is the cookie https?
+     * - `httpOnly`: Is the cookie available in the client?
+     *
+     * ### Examples
+     *
+     * ```
+     * // set scalar value with defaults
+     * $response = $response->withoutCookie('remember_me');
+     *
+     * // customize cookie attributes
+     * $response = $response->withoutCookie('remember_me', ['path' => '/login']);
+     *
+     * // add a cookie object
+     * $response = $response->withoutCookie(new Cookie('remember_me', 'deleted'));
+     * ```
+     *
+     * @param string|\Cake\Http\Cookie\Cookie $name The name of the cookie to expire, or a cookie object
+     * @param array $options An array of cookie options.
+     * @return static
+     */
+    public function withoutCookie($name, $options = [])
+    {
+        if ($name instanceof Cookie) {
+            $cookie = $name->withExpiry(new Time(1));
+        } else {
+            $options += [
+                'value' => '',
+                'path' => '/',
+                'domain' => '',
+                'secure' => false,
+                'httpOnly' => false
+            ];
+
+            $cookie = new Cookie(
+                $name,
+                $options['value'],
+                new Time(1),
+                $options['path'],
+                $options['domain'],
+                $options['secure'],
+                $options['httpOnly']
+            );
+        }
+
+        $new = clone $this;
+        $new->_cookies = $new->_cookies->add($cookie);
+
+        return $new;
+    }
+
+    /**
      * Read a single cookie from the response.
      *
      * This method provides read access to pending cookies. It will

+ 47 - 0
tests/TestCase/Http/ResponseTest.php

@@ -1491,6 +1491,53 @@ class ResponseTest extends TestCase
         $this->assertSame($cookie, $new->getCookieCollection()->get('yay'));
     }
 
+    public function testWithoutCookieScalar()
+    {
+        $response = new Response();
+        $response = $response->withCookie('testing', 'abc123');
+        $this->assertEquals('abc123', $response->getCookie('testing')['value']);
+
+        $new = $response->withoutCookie('testing');
+
+        $this->assertNull($response->getCookie('testing')['expire']);
+        $this->assertEquals('1', $new->getCookie('testing')['expire']);
+    }
+
+    public function testWithoutCookieOptions()
+    {
+        $options = [
+            'name' => 'testing',
+            'value' => 'abc123',
+            'domain' => 'cakephp.org',
+            'path' => '/custompath/',
+            'secure' => true,
+            'httpOnly' => true,
+            'expire' => (string)strtotime('+14 days'),
+        ];
+
+        $response = new Response();
+        $response = $response->withCookie('testing', $options);
+        $this->assertEquals($options, $response->getCookie('testing'));
+
+        $new = $response->withoutCookie('testing', $options);
+
+        $this->assertEquals($options['expire'], $response->getCookie('testing')['expire']);
+        $this->assertEquals('1', $new->getCookie('testing')['expire']);
+    }
+
+    public function testWithoutCookieObject()
+    {
+        $response = new Response();
+        $cookie = new Cookie('yay', 'a value');
+        $response = $response->withCookie($cookie);
+        $this->assertEquals('a value', $response->getCookie('yay')['value']);
+
+        $new = $response->withoutCookie($cookie);
+
+        $this->assertNull($response->getCookie('yay')['expire']);
+        $this->assertEquals('1', $new->getCookie('yay')['expire']);
+    }
+
     /**
      * Test getCookies() and array data.
      *