Core\Test\Response::bytes
$response->bytes(): bytesbytes() returns the body of the response to a request that Core\Test::request() sent. The result has the type bytes, which is raw binary data. So it works for every body: text, a PDF, an image, or a file sent with Core\Response::sendFile. If the program wrote nothing, the result is empty bytes.
body() returns the same body as a string. It throws a RuntimeError when the body is not valid UTF-8. Use bytes() for such a body. Core\Test::assertEquals compares it with the bytes you expect.
A request fails when the program throws an error that it does not catch. Then the result is empty bytes, because the server sends no body for a failed request. A real visitor gets the same empty body. A HEAD request has no body either, so the result is also empty bytes.
You can call bytes() as often as you like. Each call returns the same bytes.
The examples below show how to read an image, how to compare a download with the file you expect, and what a failed request returns.
Return value
Returns bytes. Every byte of the body, in order. The bytes do not have to be valid UTF-8, so this works for a PDF, an image or a file sent with Core\Response::sendFile. If the program wrote nothing, or threw an error it did not catch, the result is empty bytes.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Test::request has new options: clientIp, scheme, cookies, and the body options json, form, multipart and contentType of Core\Http\Client. The response is the one a server sends. A write from another site gets 403, and a HEAD request gets no body. |
Examples
Read an image
<?nvs// Shows how to read a body that is not text, such as the start of a PNG image.
use Core\Http\Method;use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) { // This part answers the request with the first eight bytes of a PNG file. Core\Response::sendBytes(Core\Encoding::fromHex('89504e470d0a1a0a'), 'image/png');} else { var $response = Test::request(Method::Get, '/logo.png');
// `bytes()` returns every byte of the body. // This prints `89504e470d0a1a0a`. echo Core\Encoding::toHex($response->bytes()), "\n";
// The byte `89` is not valid UTF-8, so `body()` throws an error. try { echo $response->body(), "\n"; } catch (RuntimeError $notText) { echo "body() throws an error\n"; }}89504e470d0a1a0abody() throws an errorCompare a download
<?nvs// Shows how to test that a download sends exactly the file you expect.
use Core\Http\Method;use Core\Test;
final class Invoice{ // The file that the shop sends. A real shop would create a PDF here. public static function file(): bytes { return Core\Encoding::fromHex('255044462d312e370affd8'); }}
if (Core\Env::context() == Core\Env::Context::Request) { Core\Response::sendBytes(Invoice::file(), 'application/pdf');} else { var $response = Test::request(Method::Get, '/invoice/1042'); Test::assertEquals($response->header('Content-Type'), 'application/pdf');
// `==` compares two `bytes` values byte for byte. Test::assertTrue($response->bytes() == Invoice::file());
// Both checks passed, so this prints `the invoice is the expected file`. echo "the invoice is the expected file\n";}the invoice is the expected fileA request that fails
<?nvs// Shows what `bytes()` 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) { // The program writes some text, and then throws an error it does not catch. echo 'The first half of the file'; throw new RuntimeError('the disk is full');} else { var $response = Test::request(Method::Get, '/export');
// The server sends no body for a failed request, so `bytes()` returns // empty `bytes`. This prints `0`. echo Core\Bytes::length($response->bytes()), "\n";
// The status of a failed request is 500. This prints `500`. echo $response->status(), "\n";}0500