Skip to content

Testing a page

Core\Test::request sends one request to your program. It uses your routes and your code, but no network and no server. It returns a Core\Test\Response that you can check.

<?nvs
// This program tests two pages of a shop without starting a server.
use Core\Http\Method;
use Core\Test;
class Shop
{
#[Core\Route(path: '/products/{id}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function product(uint $id): void
{
echo 'product ', $id;
}
}
// The gate checks each request before a route method runs, and it writes the error pages.
class Gate implements Core\Router\Gate
{
public function allows(Core\Router\Match $route): bool
{
return $route->accessAs<Core\Audience>() == Core\Audience::Public;
}
public function refused(Core\Router\Refusal $reason, ?Core\Router\Match $route): void
{
echo 'not found';
}
public function failed(?Core\Router\Match $route, ?Throwable $error): void
{
echo 'something went wrong';
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
// This part answers a request. A path with no route gets the status 404.
Core\Router::dispatch(new Gate());
} else {
// From the command line, this part runs. It sends two requests and checks each response.
var $found = Test::request(Method::Get, '/products/7');
Test::assertEquals($found->status(), 200);
Test::assertSame($found->body(), 'product 7');
var $missing = Test::request(Method::Get, '/basket');
Test::assertEquals($missing->status(), 404);
echo "both pages return the right status\n";
}
product 7

This program has both parts in one file, so you can run it as it is. In your own project, the page code is in your entry file. The entry file is the file the server runs for each request, such as public/index.nvs. The test is a #[Test] method in a class under src/ or in your tests/ folder. Run nvs test . from your project folder. Then the test and the entry file are compiled together, and the test can send requests to your pages.

Core\Test\Response has a method for each part of the response:

  • status() returns the status code, such as 200 or 404.
  • body() returns the body as a string.
  • header("content-type") returns one header.
  • cookies() returns the cookies the page set.
  • jsonAs<Order>() reads a JSON body into an instance of your class.
  • error() returns the class and message of the error that stopped the request. It is null when the request did not fail.

A request can send a body. You write it with the same options as Core\Http\Client:

  • json: sends a value as JSON. The content type is application/json.
  • form: sends the fields of a form. Core\Request::formField reads them.
  • multipart: sends fields and files. Core\Request::files reads the files.
  • body: sends a string or bytes as they are. contentType: gives its content type.

A request can have only one of body, json, form and multipart. If you give two, the compiler shows an error, as it does for Core\Http\Client. For example, Test::request(Method::Post, '/orders', {json: ['count' => 2]}) sends the JSON body {"count":2}.

The client address, the scheme and cookies

Section titled “The client address, the scheme and cookies”
  • clientIp: sets the address of the client. Core\Request::clientIp() returns it. Without it, the result is null.
  • scheme: is "http" or "https". The default is "http". Browsers send a cookie whose name starts with __Host- only over https. So your program reads such a cookie only when the scheme is "https".
  • cookies: sends cookies, such as ['session' => 'abc'].
  • A host header in headers: sets the host of the request.

Each call is a new request. The test does not keep cookies between two calls. To send a cookie again, copy it from cookies() of the last response into the next call.

A test gets the response that a real server sends:

  • A form post that another website forged gets the status 403. The server makes the same check.
  • A body over the size limit gets the status 413. The limit is set in the [limits] block of nvs.toml.
  • A HEAD request gets the headers and no body.
  • A request that throws an error gets the status 500. error() returns the error, such as RuntimeError: no stock.

Your program can start work that runs after the response is sent, with Core\Task::runAfterResponse. Core\Test::request returns only when that work has ended. So the test can check what the work wrote. If that work fails, the test fails.

Read the examples in Reference: Core\Test::request.

Some tests need a real server, for example a test of a WebSocket. A test with #[Test(server: true)] starts a local server for that test. Core\Test::serverUrl returns its address.

Read more in Reference: Core\Test\Response.