Skip to content

Core\Test\Response::headers

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

headers() returns every header of the answer to a request that Core\Test::request() sent. The result is an array<array<string>>. Each key is a header name in lower case. Each value is a list with one entry for each line of that header, in order.

Most headers have one line. Set-Cookie has one line for each cookie the program set. To check a whole cookie line, with attributes such as Path and HttpOnly, read headers()["set-cookie"].

These are the headers your program sends to a visitor. The first key is always content-type. The server also adds headers of its own, such as Content-Length and Date. headers() does not return those. A request that failed has no headers, so the result is an empty array.

The examples below show every header of a page, the lines of two cookies, and a request that fails.

Return value

Returns array<array<string>>. An array<array<string>>. Each key is a header name in lower case. Its value is a list with one entry for each line of that header, in order. Set-Cookie has one line for each cookie. A request that failed returns an empty array.

Examples

Every header of a page

<?nvs
// Shows every header that one page sends.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. It sets one header and writes a page.
Core\Response::setHeader('Cache-Control', 'no-store');
echo '<h1>Your basket</h1>';
} else {
var $response = Test::request(Method::Get, '/basket');
// Each name is in lower case, and each value is a list of lines.
// The page used `echo`, so the content type is HTML.
var $headers = $response->headers();
// This prints `{"content-type":["text/html; charset=utf-8"],"cache-control":["no-store"]}`.
echo Core\Json::encode($headers), "\n";
}
Output
{"content-type":["text/html; charset=utf-8"],"cache-control":["no-store"]}

The lines of two cookies

<?nvs
// Shows how to check the whole `Set-Cookie` line of each cookie.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. It sets two cookies.
Core\Response::addCookie('sid', 'abc123');
Core\Response::addCookie('lang', 'en', { httpOnly: false });
} else {
var $response = Test::request(Method::Get, '/login');
// There is one line for each cookie, in the order the program set them.
// This prints two lines. The second line has no `HttpOnly`.
foreach ($response->headers()['set-cookie'] as string $line) {
echo $line, "\n";
}
}
Output
sid=abc123; Path=/; Secure; HttpOnly; SameSite=Lax
lang=en; Path=/; Secure; SameSite=Lax

A request that fails

<?nvs
// Shows what `headers()` returns when the program throws an error while it answers.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. It sets a header, and then throws
// an error it does not catch.
Core\Response::setHeader('Cache-Control', 'no-store');
throw new RuntimeError('the database is not available');
} else {
var $response = Test::request(Method::Get, '/report');
// The server does not send the headers of a failed request, so the
// array is empty. This prints `500 0`.
echo $response->status(), ' ', Core\Arr::count($response->headers()), "\n";
}
Output
500 0