Skip to content

Core\Test\Response::cookies

$response->cookies(): array<string>

cookies() returns the cookies that the program set in the answer to a request that Core\Test::request() sent. The result is an array<string>. Each key is a cookie name, and each value is the cookie value, exactly as the program set it.

The result has only the name and the value of each cookie. To check an attribute such as Path or HttpOnly, read the whole line from headers()["set-cookie"].

If the program set the same name twice, the result has the last value. This is the value a browser keeps. A cookie that the program deleted is in the result with an empty value. If the program set no cookie, the result is an empty array.

The examples below show how to test a login cookie, a cookie that is set twice, and a cookie that is deleted.

Return value

Returns array<string>. An array<string>. Each key is a cookie name and each value is the cookie value, exactly as the program set it. Nothing is decoded. Attributes such as Path and HttpOnly are not in the result, and headers()["set-cookie"] returns the whole lines. When the program set one name twice, the result has the last value. A cookie that the program deleted is in the result with an empty value. The array is empty when the program set no cookie.

Examples

Test a login cookie

<?nvs
// Shows how to test that a login page sets a session cookie.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. A login sets the `sid` cookie.
var $path = Core\Request::path();
if ($path == '/login') {
Core\Response::addCookie('sid', 'abc123');
}
} else {
// This part is the test.
var $login = Test::request(Method::Post, '/login');
// This prints `{"sid":"abc123"}`.
echo Core\Json::encode($login->cookies()), "\n";
// Another page sets no cookie, so the array is empty. This prints `0`.
var $home = Test::request(Method::Get, '/');
echo Core\Arr::count($home->cookies()), "\n";
}
Output
{"sid":"abc123"}
0

A cookie set twice

<?nvs
// Shows which value you get when the program sets one cookie twice.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. It sets `theme` twice.
Core\Response::addCookie('theme', 'light');
Core\Response::addCookie('lang', 'en');
Core\Response::addCookie('theme', 'dark');
} else {
var $response = Test::request(Method::Get, '/settings');
// A browser keeps the last value, and so does `cookies()`. This prints `dark`.
echo $response->cookies()['theme'], "\n";
// `headers()` still has all three lines. This prints `3`.
echo Core\Arr::count($response->headers()['set-cookie']), "\n";
}
Output
dark
3

A deleted cookie

<?nvs
// Shows how to test that a logout page deletes the session cookie.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. A cookie with an empty value and
// `maxAge: 0s` tells the browser to delete it.
Core\Response::addCookie('sid', '', { maxAge: 0s });
} else {
var $response = Test::request(Method::Post, '/logout');
// The deleted cookie is in the result with an empty value. This prints `[]`.
echo '[', $response->cookies()['sid'], "]\n";
// The whole line shows `Max-Age=0`. This prints `true`.
var $line = $response->headers()['set-cookie'][0];
echo Core\Str::contains($line, 'Max-Age=0') ? 'true' : 'false', "\n";
}
Output
[]
true