Skip to content

Core\Test\Response::body

$response->body(): string

body() returns the body of the response as a string. Core\Test::request() sends a request to your own program in a test, and the program answers it. The body is everything the program wrote with echo, in order. When the program used a method such as Core\Response::sendJson, the body is what that method sent. If the program wrote nothing, the result is an empty string.

A request fails when the program throws an error that it does not catch. Then the result is also an empty string, 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 an empty string.

You can call body() as often as you like. Each call returns the same text.

The examples below show how to read a written page, what a failed request returns, and how to test the JSON that an API sends.

Return value

Returns string. Everything the program wrote with echo, in order. If the program wrote nothing, the result is an empty string. If the program threw an error it did not catch, the result is also an empty string. The result is empty for a HEAD request too. The server sends no body for a request that threw an error, or for HEAD.

Errors

  • RuntimeErrorThe body is not valid UTF-8, so it is not a string. Core\Response::sendBytes and Core\Response::sendFile can write such a body. Read it with bytes().

Changelog

VersionChange
0.0.5Core\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 what the page wrote

<?nvs
// Shows how to read the text that your program wrote for one request.
use Core\Http\Method;
use Core\Test;
// `Core\Test::request` runs this file again for the request. In that run,
// `Core\Env::context()` returns `Request`, and this part writes the answer.
if (Core\Env::context() == Core\Env::Context::Request) {
var $path = Core\Request::path();
echo 'Welcome. ';
echo 'You opened ', $path;
} else {
// From the command line, this part sends the request and reads the answer.
var $response = Test::request(Method::Get, '/start');
// `body()` returns everything the program wrote, in order.
// This prints `Welcome. You opened /start`.
echo $response->body(), "\n";
}
Output
Welcome. You opened /start

A request that fails

<?nvs
// Shows what `body()` 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 page';
throw new RuntimeError('the database is not available');
} else {
var $response = Test::request(Method::Get, '/report');
// The server sends no body for a failed request, so `body()` returns
// an empty string. This prints `[]`.
echo '[', $response->body(), "]\n";
// The status of a failed request is 500. This prints `500`.
echo $response->status(), "\n";
}
Output
[]
500

Test a json answer

<?nvs
// Shows how to test the JSON that a shop sends for one product.
use Core\Http\Method;
use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers each request. `Core\Response::sendJson` converts the
// array to JSON and sends it.
Core\Response::sendJson(['sku' => 'mug-7', 'name' => 'Blue mug']);
} else {
// This part is the test. It sends the request and checks the whole body.
var $response = Test::request(Method::Get, '/products/mug-7');
Test::assertEquals($response->status(), 200);
Test::assertSame($response->body(), "{\"sku\":\"mug-7\",\"name\":\"Blue mug\"}");
// Both checks passed, so this prints `the product JSON is correct`.
echo "the product JSON is correct\n";
}
Output
the product JSON is correct