Skip to content
Permalink
Browse files

Add withCookie() & getCookie()

These methods provide cookie methods that better match the PSR7
conventions.
  • Loading branch information...
markstory committed Dec 9, 2016
1 parent 089c85c commit 36b067cf9e5b7da5e579edd49002f8e3de251db0
Showing with 122 additions and 0 deletions.
  1. +48 −0 src/Network/Response.php
  2. +74 −0 tests/TestCase/Network/ResponseTest.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
*
@@ -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
*

0 comments on commit 36b067c

Please sign in to comment.
You can’t perform that action at this time.