Skip to content

Commit bf81b15

Browse files
committed
Update Response to use CookieCollection internally.
This change should be completely backwards compatible and enable the new cookie collection to be used.
1 parent 5547d37 commit bf81b15

File tree

2 files changed

+74
-22
lines changed

2 files changed

+74
-22
lines changed

src/Http/Response.php

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
use Cake\Core\Configure;
1818
use Cake\Filesystem\File;
19+
use Cake\Http\Cookie\Cookie;
20+
use Cake\Http\Cookie\CookieCollection;
1921
use Cake\Log\Log;
2022
use Cake\Network\CorsBuilder;
2123
use Cake\Network\Exception\NotFoundException;
@@ -389,11 +391,11 @@ class Response implements ResponseInterface
389391
protected $_cacheDirectives = [];
390392

391393
/**
392-
* Holds cookies to be sent to the client
394+
* Collection of cookies to send to the client
393395
*
394-
* @var array
396+
* @var Cake\Http\Cookie\CookieCollection
395397
*/
396-
protected $_cookies = [];
398+
protected $_cookies = null;
397399

398400
/**
399401
* Reason Phrase
@@ -459,6 +461,7 @@ public function __construct(array $options = [])
459461
$this->_contentType = $this->resolveType($options['type']);
460462
}
461463
$this->_setContentType();
464+
$this->_cookies = new CookieCollection();
462465
}
463466

464467
/**
@@ -1928,15 +1931,15 @@ public function __toString()
19281931
public function cookie($options = null)
19291932
{
19301933
if ($options === null) {
1931-
return $this->_cookies;
1934+
return $this->getCookies();
19321935
}
19331936

19341937
if (is_string($options)) {
1935-
if (!isset($this->_cookies[$options])) {
1938+
if (!$this->_cookies->has($options)) {
19361939
return null;
19371940
}
19381941

1939-
return $this->_cookies[$options];
1942+
return $this->_cookies->get($options)->toArrayResponse();
19401943
}
19411944

19421945
$defaults = [
@@ -1949,8 +1952,16 @@ public function cookie($options = null)
19491952
'httpOnly' => false
19501953
];
19511954
$options += $defaults;
1952-
1953-
$this->_cookies[$options['name']] = $options;
1955+
$cookie = new Cookie(
1956+
$options['name'],
1957+
$options['value'],
1958+
$options['expire'],
1959+
$options['path'],
1960+
$options['domain'],
1961+
$options['secure'],
1962+
$options['httpOnly']
1963+
);
1964+
$this->_cookies = $this->_cookies->add($cookie);
19541965
}
19551966

19561967
/**
@@ -1994,10 +2005,18 @@ public function withCookie($name, $data = '')
19942005
'httpOnly' => false
19952006
];
19962007
$data += $defaults;
1997-
$data['name'] = $name;
2008+
$cookie = new Cookie(
2009+
$name,
2010+
$data['value'],
2011+
$data['expire'],
2012+
$data['path'],
2013+
$data['domain'],
2014+
$data['secure'],
2015+
$data['httpOnly']
2016+
);
19982017

19992018
$new = clone $this;
2000-
$new->_cookies[$name] = $data;
2019+
$new->_cookies = $new->_cookies->add($cookie);
20012020

20022021
return $new;
20032022
}
@@ -2013,11 +2032,11 @@ public function withCookie($name, $data = '')
20132032
*/
20142033
public function getCookie($name)
20152034
{
2016-
if (isset($this->_cookies[$name])) {
2017-
return $this->_cookies[$name];
2035+
if (!$this->_cookies->has($name)) {
2036+
return null;
20182037
}
20192038

2020-
return null;
2039+
return $this->_cookies->get($name)->toArrayResponse();
20212040
}
20222041

20232042
/**
@@ -2028,6 +2047,21 @@ public function getCookie($name)
20282047
* @return array
20292048
*/
20302049
public function getCookies()
2050+
{
2051+
$out = [];
2052+
foreach ($this->_cookies as $cookie) {
2053+
$out[$cookie->getName()] = $cookie->toArrayResponse();
2054+
}
2055+
2056+
return $out;
2057+
}
2058+
2059+
/**
2060+
* Get the CookieCollection from the response
2061+
*
2062+
* @return \Cake\Http\Cookie\CookieCollection
2063+
*/
2064+
public function getCookieCollection()
20312065
{
20322066
return $this->_cookies;
20332067
}

tests/TestCase/Http/ResponseTest.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

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

19+
use Cake\Http\Cookie\Cookie;
20+
use Cake\Http\Cookie\CookieCollection;
1921
use Cake\Http\Response;
2022
use Cake\Http\ServerRequest;
2123
use Cake\Network\Exception\NotFoundException;
@@ -1332,7 +1334,8 @@ public function testCookieSettings()
13321334
'path' => '/',
13331335
'domain' => '',
13341336
'secure' => false,
1335-
'httpOnly' => false];
1337+
'httpOnly' => false
1338+
];
13361339
$result = $response->cookie('CakeTestCookie[Testing]');
13371340
$this->assertEquals($expected, $result);
13381341

@@ -1478,13 +1481,6 @@ public function testWithCookieArray()
14781481
public function testGetCookies()
14791482
{
14801483
$response = new Response();
1481-
$cookie = [
1482-
'name' => 'ignored key',
1483-
'value' => '[a,b,c]',
1484-
'expire' => 1000,
1485-
'path' => '/test',
1486-
'secure' => true
1487-
];
14881484
$new = $response->withCookie('testing', 'a')
14891485
->withCookie('test2', ['value' => 'b', 'path' => '/test', 'secure' => true]);
14901486
$expected = [
@@ -1510,6 +1506,28 @@ public function testGetCookies()
15101506
$this->assertEquals($expected, $new->getCookies());
15111507
}
15121508

1509+
/**
1510+
* Test getCookieCollection() as array data
1511+
*
1512+
* @return void
1513+
*/
1514+
public function testGetCookieCollection()
1515+
{
1516+
$response = new Response();
1517+
$new = $response->withCookie('testing', 'a')
1518+
->withCookie('test2', ['value' => 'b', 'path' => '/test', 'secure' => true]);
1519+
$cookies = $response->getCookieCollection();
1520+
$this->assertInstanceOf(CookieCollection::class, $cookies);
1521+
$this->assertCount(0, $cookies, 'Original response not mutated');
1522+
1523+
$cookies = $new->getCookieCollection();
1524+
$this->assertInstanceOf(CookieCollection::class, $cookies);
1525+
$this->assertCount(2, $cookies);
1526+
1527+
$this->assertTrue($cookies->has('testing'));
1528+
$this->assertTrue($cookies->has('test2'));
1529+
}
1530+
15131531
/**
15141532
* Test CORS
15151533
*
@@ -3010,7 +3028,7 @@ public function testDebugInfo()
30103028
],
30113029
'file' => null,
30123030
'fileRange' => [],
3013-
'cookies' => [],
3031+
'cookies' => new CookieCollection(),
30143032
'cacheDirectives' => [],
30153033
'body' => ''
30163034
];

0 commit comments

Comments
 (0)