Skip to content

Commit

Permalink
Add withCookie() & getCookie()
Browse files Browse the repository at this point in the history
These methods provide cookie methods that better match the PSR7
conventions.
  • Loading branch information
markstory committed Dec 9, 2016
1 parent 089c85c commit 36b067c
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Network/Response.php
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
*
Expand Down
74 changes: 74 additions & 0 deletions tests/TestCase/Network/ResponseTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 36b067c

Please sign in to comment.