Skip to content

Core\Test::serverUrl

Core\Test::serverUrl(): ?string

Core\Test::serverUrl() returns the address of a real web server for one test. A test gets this server when it is declared with #[Test(server: true)]. The server runs on your own computer, on a free port that the operating system chooses. The address looks like http://127.0.0.1:52341. It has no / at the end, so you can add a path to it directly.

The server answers each request by running your program, the same way a real server does. Each request uses the test's clock and the environment variables that env: sets in the #[Test] attribute. Core\Test::logs returns what these requests log, and Core\Test::sentMail returns the mail they send. Use serverUrl when a test needs a real network connection, for example to test an HTTP client. For most tests, Core\Test::request is simpler and faster.

Everywhere else, serverUrl() returns null. This includes a test without server: true and a program started with nvs run.

The examples below show the null result, a test that calls its own server, and a test of a small health check client.

Return value

Returns ?string. An address such as http://127.0.0.1:52341, with no / at the end, so you can add a path directly. The result is null everywhere else, which includes a test without server: true and a program started with nvs run.

Changelog

VersionChange
0.0.6A request to the test server uses the test's clock and its env: variables. What the request logs or sends as mail is in Core\Test::logs and Core\Test::sentMail.

Examples

No server outside a server test

<?nvs
// Shows that `Core\Test::serverUrl` returns `null` when no test server is running.
use Core\Test;
// This program runs with `nvs run`, not in a test with `server: true`.
// So there is no server, and this prints `no server`.
echo Test::serverUrl() ?? 'no server', "\n";
// Code that needs the address can check for `null` first.
// This prints `this check needs a test with server: true`.
?string $url = Test::serverUrl();
if ($url == null) {
echo "this check needs a test with server: true\n";
} else {
echo 'the server is at ', $url, "\n";
}
Output
no server
this check needs a test with server: true

A test that calls its own server

<?nvs
// Shows how a test sends a real HTTP request to its own program.
use Core\Http\Client;
use Core\Test;
class PingTest
{
// `server: true` starts a server for this test only.
#[Test(server: true)]
public function itAnswersOverTheNetwork(): void
{
string $base = Test::serverUrl() ?? '';
// The address has no `/` at the end, so the path is added directly.
var $response = Client::get($base . '/ping');
Test::assertEquals($response->status(), 200);
Test::assertSame($response->text(), 'pong');
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
// The server runs this file for each request, and this part writes the answer.
var $path = Core\Request::path();
echo $path == '/ping' ? 'pong' : 'unknown page';
} else {
// `nvs test` runs the test above. Under `nvs run` there is no server,
// so this prints `no server`.
echo Test::serverUrl() ?? 'no server', "\n";
}
Output
no server

Test a health check client

<?nvs
// Shows how to test an HTTP client class against a real server that your test starts.
use Core\Http\Client;
use Core\Test;
class HealthCheck
{
// Returns "up" when the page answers with the status 200, and "down" otherwise.
public static function check(string $url): string
{
var $response = Client::get($url);
return $response->status() == 200 ? 'up' : 'down';
}
}
class HealthCheckTest
{
#[Test(server: true)]
public function itSeesAWorkingPageAndABrokenOne(): void
{
string $base = Test::serverUrl() ?? '';
Test::assertSame(HealthCheck::check($base . '/health'), 'up');
Test::assertSame(HealthCheck::check($base . '/broken'), 'down');
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
// The server runs this file for each request. `/health` works,
// and every other page answers with the status 503.
var $path = Core\Request::path();
if ($path == '/health') {
echo 'ok';
} else {
Core\Response::setStatus(503);
echo 'failing';
}
} else {
// `nvs test` runs the test above. Under `nvs run` there is no server,
// so this prints `no server`.
echo Test::serverUrl() ?? 'no server', "\n";
}
Output
no server