From 36b067cf9e5b7da5e579edd49002f8e3de251db0 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 8 Dec 2016 21:54:44 -0500 Subject: [PATCH] Add withCookie() & getCookie() These methods provide cookie methods that better match the PSR7 conventions. --- src/Network/Response.php | 48 ++++++++++++++++ tests/TestCase/Network/ResponseTest.php | 74 +++++++++++++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/src/Network/Response.php b/src/Network/Response.php index fccc9b30fdb..63d14c466aa 100644 --- a/src/Network/Response.php +++ b/src/Network/Response.php @@ -1912,6 +1912,7 @@ public function __toString() * @param array|null $options Either null to get all cookies, string for a specific cookie * or array to set cookie. * @return mixed + * @deprecated 3.4.0 Use getCookie() and withCookie() instead. */ public function cookie($options = null) { @@ -1941,6 +1942,53 @@ public function cookie($options = null) $this->_cookies[$options['name']] = $options; } + /** + * Create a new response with a cookie set. + * + * @param string $name The name of the cookie to set. + * @param array|string $data Either a string value, or an array of cookie data. + * @return static + */ + public function withCookie($name, $data = '') + { + if (!is_array($data)) { + $data = ['value' => $data]; + } + $defaults = [ + 'value' => '', + 'expire' => 0, + 'path' => '/', + 'domain' => '', + 'secure' => false, + 'httpOnly' => false + ]; + $data += $defaults; + $data['name'] = $name; + + $new = clone $this; + $new->_cookies[$name] = $data; + + return $new; + } + + /** + * Read a single cookie from the response. + * + * This method provides read access to pending cookies. It will + * not read the `Set-Cookie` header if set. + * + * @param string $name The cookie name you want to read. + * @return array|null Either the cookie data or null + */ + public function getCookie($name) + { + if (isset($this->_cookies[$name])) { + return $this->_cookies[$name]; + } + + return null; + } + /** * Setup access for origin and methods on cross origin requests * diff --git a/tests/TestCase/Network/ResponseTest.php b/tests/TestCase/Network/ResponseTest.php index 492866b0ad7..6020ee70f7f 100644 --- a/tests/TestCase/Network/ResponseTest.php +++ b/tests/TestCase/Network/ResponseTest.php @@ -1359,6 +1359,80 @@ public function testCookieSettings() $this->assertEquals($expected, $result); } + /** + * Test setting cookies with no value + * + * @return void + */ + public function testWithCookieEmpty() + { + $response = new Response(); + $new = $response->withCookie('testing'); + $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate'); + + $expected = [ + 'name' => 'testing', + 'value' => '', + 'expire' => 0, + 'path' => '/', + 'domain' => '', + 'secure' => false, + 'httpOnly' => false]; + $result = $new->getCookie('testing'); + $this->assertEquals($expected, $result); + } + + /** + * Test setting cookies with scalar values + * + * @return void + */ + public function testWithCookieScalar() + { + $response = new Response(); + $new = $response->withCookie('testing', 'abc123'); + $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate'); + $this->assertEquals('abc123', $new->getCookie('testing')['value']); + + $new = $response->withCookie('testing', 99); + $this->assertEquals(99, $new->getCookie('testing')['value']); + + $new = $response->withCookie('testing', false); + $this->assertFalse($new->getCookie('testing')['value']); + + $new = $response->withCookie('testing', true); + $this->assertTrue($new->getCookie('testing')['value']); + } + + /** + * Test withCookie() and array data. + * + * @return void + */ + public function testWithCookieArray() + { + $response = new Response(); + $cookie = [ + 'name' => 'ignored key', + 'value' => '[a,b,c]', + 'expire' => 1000, + 'path' => '/test', + 'secure' => true + ]; + $new = $response->withCookie('testing', $cookie); + $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate'); + $expected = [ + 'name' => 'testing', + 'value' => '[a,b,c]', + 'expire' => 1000, + 'path' => '/test', + 'domain' => '', + 'secure' => true, + 'httpOnly' => false + ]; + $this->assertEquals($expected, $new->getCookie('testing')); + } + /** * Test CORS *