Skip to content

Commit c06b36d

Browse files
committed
address review
1 parent 1f9f809 commit c06b36d

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

system/HTTP/Response.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\HTTP;
1515

16+
use CodeIgniter\Cookie\Cookie;
1617
use CodeIgniter\HTTP\Exceptions\HTTPException;
1718

1819
/**
@@ -142,6 +143,8 @@ class Response extends Message implements ResponseInterface
142143
*/
143144
public function __construct()
144145
{
146+
Cookie::setDefaults(config('Cookie'));
147+
145148
$this->noCache()->setContentType('text/html');
146149
}
147150

system/HTTP/ResponseTrait.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,7 @@ public function getCSP(): ContentSecurityPolicy
822822
*/
823823
private function initializeCookieStore(): CookieStore
824824
{
825-
if ($this->cookieStore === null) {
826-
Cookie::setDefaults(config(CookieConfig::class));
827-
$this->cookieStore = new CookieStore([]);
828-
}
825+
$this->cookieStore ??= new CookieStore([]);
829826

830827
return $this->cookieStore;
831828
}

tests/system/HTTP/ResponseTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Config\Factories;
1717
use CodeIgniter\Config\Services;
18+
use CodeIgniter\Cookie\Cookie;
1819
use CodeIgniter\Cookie\CookieStore;
1920
use CodeIgniter\HTTP\Exceptions\HTTPException;
2021
use CodeIgniter\Superglobals;
@@ -753,4 +754,29 @@ public function testGetCookiesReturnsEmptyArrayWhenCookieStoreNotInitialized():
753754
'getCookies() must not instantiate the store when no cookies have been set.',
754755
);
755756
}
757+
758+
public function testGetCookieStillUsesSetCookieDefaultsWhenStoreNotInitialized(): void
759+
{
760+
$oldDefaults = Cookie::setDefaults();
761+
762+
config('Cookie')->prefix = 'test_';
763+
764+
try {
765+
$response = new Response();
766+
767+
$this->assertNull($this->getPrivateProperty($response, 'cookieStore'));
768+
769+
$response->setCookie('foo', 'bar');
770+
771+
$this->assertInstanceOf(CookieStore::class, $this->getPrivateProperty($response, 'cookieStore'));
772+
$this->assertTrue($response->hasCookie('foo'));
773+
774+
$cookie = $response->getCookie('foo');
775+
$this->assertSame('test_', $cookie->getPrefix());
776+
$this->assertSame('test_foo', $cookie->getPrefixedName());
777+
} finally {
778+
Cookie::setDefaults($oldDefaults);
779+
Factories::reset('config');
780+
}
781+
}
756782
}

0 commit comments

Comments
 (0)