Skip to content
Permalink
Browse files

Response withoutCookie

  • Loading branch information...
Joris Vaesen
Joris Vaesen committed Aug 2, 2017
1 parent 51f9ab8 commit 862c12fa68f182f1fa12e4928ed8ba43fbad3448
Showing with 108 additions and 0 deletions.
  1. +61 −0 src/Http/Response.php
  2. +47 −0 tests/TestCase/Http/ResponseTest.php
@@ -19,6 +19,7 @@
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;
@@ -2040,6 +2041,66 @@ public function withCookie($name, $data = '')
return $new;
}
/**
* 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.
*
@@ -1491,6 +1491,53 @@ public function testWithCookieObject()
$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.
*

0 comments on commit 862c12f

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