Navigation Menu

Skip to content

Commit

Permalink
Update Response to use CookieCollection internally.
Browse files Browse the repository at this point in the history
This change should be completely backwards compatible and enable the new
cookie collection to be used.
  • Loading branch information
markstory committed Apr 7, 2017
1 parent 5547d37 commit bf81b15
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 22 deletions.
60 changes: 47 additions & 13 deletions src/Http/Response.php
Expand Up @@ -16,6 +16,8 @@

use Cake\Core\Configure;
use Cake\Filesystem\File;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
use Cake\Log\Log;
use Cake\Network\CorsBuilder;
use Cake\Network\Exception\NotFoundException;
Expand Down Expand Up @@ -389,11 +391,11 @@ class Response implements ResponseInterface
protected $_cacheDirectives = [];

/**
* Holds cookies to be sent to the client
* Collection of cookies to send to the client
*
* @var array
* @var Cake\Http\Cookie\CookieCollection
*/
protected $_cookies = [];
protected $_cookies = null;

/**
* Reason Phrase
Expand Down Expand Up @@ -459,6 +461,7 @@ public function __construct(array $options = [])
$this->_contentType = $this->resolveType($options['type']);
}
$this->_setContentType();
$this->_cookies = new CookieCollection();
}

/**
Expand Down Expand Up @@ -1928,15 +1931,15 @@ public function __toString()
public function cookie($options = null)
{
if ($options === null) {
return $this->_cookies;
return $this->getCookies();
}

if (is_string($options)) {
if (!isset($this->_cookies[$options])) {
if (!$this->_cookies->has($options)) {
return null;
}

return $this->_cookies[$options];
return $this->_cookies->get($options)->toArrayResponse();
}

$defaults = [
Expand All @@ -1949,8 +1952,16 @@ public function cookie($options = null)
'httpOnly' => false
];
$options += $defaults;

$this->_cookies[$options['name']] = $options;
$cookie = new Cookie(
$options['name'],
$options['value'],
$options['expire'],
$options['path'],
$options['domain'],
$options['secure'],
$options['httpOnly']
);
$this->_cookies = $this->_cookies->add($cookie);
}

/**
Expand Down Expand Up @@ -1994,10 +2005,18 @@ public function withCookie($name, $data = '')
'httpOnly' => false
];
$data += $defaults;
$data['name'] = $name;
$cookie = new Cookie(
$name,
$data['value'],
$data['expire'],
$data['path'],
$data['domain'],
$data['secure'],
$data['httpOnly']
);

$new = clone $this;
$new->_cookies[$name] = $data;
$new->_cookies = $new->_cookies->add($cookie);

return $new;
}
Expand All @@ -2013,11 +2032,11 @@ public function withCookie($name, $data = '')
*/
public function getCookie($name)
{
if (isset($this->_cookies[$name])) {
return $this->_cookies[$name];
if (!$this->_cookies->has($name)) {
return null;
}

return null;
return $this->_cookies->get($name)->toArrayResponse();
}

/**
Expand All @@ -2028,6 +2047,21 @@ public function getCookie($name)
* @return array
*/
public function getCookies()
{
$out = [];
foreach ($this->_cookies as $cookie) {
$out[$cookie->getName()] = $cookie->toArrayResponse();
}

return $out;
}

/**
* Get the CookieCollection from the response
*
* @return \Cake\Http\Cookie\CookieCollection
*/
public function getCookieCollection()
{
return $this->_cookies;
}
Expand Down
36 changes: 27 additions & 9 deletions tests/TestCase/Http/ResponseTest.php
Expand Up @@ -16,6 +16,8 @@

include_once CORE_TEST_CASES . DS . 'Http' . DS . 'server_mocks.php';

use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Network\Exception\NotFoundException;
Expand Down Expand Up @@ -1332,7 +1334,8 @@ public function testCookieSettings()
'path' => '/',
'domain' => '',
'secure' => false,
'httpOnly' => false];
'httpOnly' => false
];
$result = $response->cookie('CakeTestCookie[Testing]');
$this->assertEquals($expected, $result);

Expand Down Expand Up @@ -1478,13 +1481,6 @@ public function testWithCookieArray()
public function testGetCookies()
{
$response = new Response();
$cookie = [
'name' => 'ignored key',
'value' => '[a,b,c]',
'expire' => 1000,
'path' => '/test',
'secure' => true
];
$new = $response->withCookie('testing', 'a')
->withCookie('test2', ['value' => 'b', 'path' => '/test', 'secure' => true]);
$expected = [
Expand All @@ -1510,6 +1506,28 @@ public function testGetCookies()
$this->assertEquals($expected, $new->getCookies());
}

/**
* Test getCookieCollection() as array data
*
* @return void
*/
public function testGetCookieCollection()
{
$response = new Response();
$new = $response->withCookie('testing', 'a')
->withCookie('test2', ['value' => 'b', 'path' => '/test', 'secure' => true]);
$cookies = $response->getCookieCollection();
$this->assertInstanceOf(CookieCollection::class, $cookies);
$this->assertCount(0, $cookies, 'Original response not mutated');

$cookies = $new->getCookieCollection();
$this->assertInstanceOf(CookieCollection::class, $cookies);
$this->assertCount(2, $cookies);

$this->assertTrue($cookies->has('testing'));
$this->assertTrue($cookies->has('test2'));
}

/**
* Test CORS
*
Expand Down Expand Up @@ -3010,7 +3028,7 @@ public function testDebugInfo()
],
'file' => null,
'fileRange' => [],
'cookies' => [],
'cookies' => new CookieCollection(),
'cacheDirectives' => [],
'body' => ''
];
Expand Down

0 comments on commit bf81b15

Please sign in to comment.