Core\Test\SentRequest::header
header($name) returns the value of one header of a request that your program sent in a test. You get the request from Core\Test::sentHttp(), after Core\Test::answerHttp has set up a fake web service.
Upper and lower case letters in the name do not matter, the same as in HTTP. header("Authorization") and header("authorization") return the same value. If the request has no header with that name, the result is null. A test uses this to check a token, a content type or a language that the program sent.
The examples below show how to read one header, what a missing header returns, and a test that checks every request of an API client carries its token.
Parameters
$namestringThe header name. Upper and lower case letters do not matter, soauthorizationandAuthorizationfind the same header.
Return value
Returns ?string. The header value. If the request has no header with this name, the result is null.
Examples
Read one header
<?nvs// Shows how to read one header of a request your program sent.
use Core\Http\Client;use Core\Test;use Core\Test\SentRequest;
Test::answerHttp('https://api.example.com/profile', 200);Client::get('https://api.example.com/profile', { headers: ['Accept-Language' => 'de'] });
SentRequest $request = Test::sentHttp()[0];
// This prints `de`.echo $request->header('Accept-Language') ?? 'none', "\n";
// Upper and lower case letters in the name do not matter. This prints `de` again.echo $request->header('accept-language') ?? 'none', "\n";dedeA header that is missing
<?nvs// Shows what `header` returns for a header the request does not have.
use Core\Http\Client;use Core\Test;use Core\Test\SentRequest;
Test::answerHttp('https://api.example.com/status', 200);Client::get('https://api.example.com/status');
SentRequest $request = Test::sentHttp()[0];
// The program sent no `Authorization` header, so the result is `null`.// This prints `no token`.echo $request->header('Authorization') ?? 'no token', "\n";
// `Test::assertNull` checks the same thing in a test.Test::assertNull($request->header('Authorization'));echo "checked\n";no tokencheckedTest that every request has a token
<?nvs// Shows how to test that an API client sends its token with every request.
use Core\Http\Client;use Core\Test;use Core\Test\SentRequest;
class InvoiceClient{ public function constructor(private string $token) {}
// Reads one invoice. public function get(uint $id): int { return Client::get('https://billing.example.com/invoices/' . $id, { headers: ['Authorization' => 'Bearer ' . $this->token], })->status(); }
// Marks one invoice as paid. public function pay(uint $id): int { return Client::post('https://billing.example.com/invoices/' . $id . '/pay', { headers: ['Authorization' => 'Bearer ' . $this->token], })->status(); }}
Test::answerHttp('https://billing.example.com/invoices/*', 200);InvoiceClient $client = new InvoiceClient('t-555');$client->get(10);$client->pay(10);
// A request without the token fails on the real service.// The test checks the header of every request.foreach (Test::sentHttp() as SentRequest $request) { Test::assertSame($request->header('Authorization'), 'Bearer t-555');}
// This prints `2 requests had the token`.echo Core\Arr::count(Test::sentHttp()), " requests had the token\n";2 requests had the token