Core\Test\Response::status
$response->status(): uintIn a test, Core\Test::request() sends a request to your own program. status() returns the HTTP status code of the response. The status code is the one the program set with Core\Response::setStatus. A method such as Core\Response::redirect also sets a code.
If the program set no code, the result is 200. If the program throws an error that it does not catch, the result is 500. The server sends the same codes to a real visitor. So a test can check an error page as well as a normal one.
Some requests are stopped by the server before your program runs, and a test gets the same code. A form post sent from another website gets 403, because the server blocks such posts. A body larger than the size limit gets 413. You set that limit in the [limits] block of nvs.toml.
The examples below show a request with no status and a status the program sets. The last one is a test that checks that an old address redirects to a new one.
Return value
Returns uint. The code the program set with Core\Response::setStatus. If the program set no code, the result is 200. If the program threw an error it did not catch, the result is 500, the same code the server sends. If the server stops the request before the program runs, the result is the code the server sends, such as 403 or 413.
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
No status means 200
<?nvs// Shows how to read the status code of the answer to one request.
use Core\Http\Method;use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) { // This part answers the request. It writes text and sets no status. echo 'Hello';} else { var $response = Test::request(Method::Get, '/hello');
// The program set no status, so `status()` returns 200. This prints `200`. echo $response->status(), "\n";}200A status the program sets
<?nvs// Shows that `status()` returns the code the program set with `Core\Response::setStatus`.
use Core\Http\Method;use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) { // This part answers the request. A new order gets the status 201 (Created). Core\Response::setStatus(201); echo 'order created';} else { var $response = Test::request(Method::Post, '/orders');
// This prints `201 order created`. echo $response->status(), ' ', $response->body(), "\n";}201 order createdTest that an old address redirects
<?nvs// Shows how to test that an old address sends the browser to the new one.
use Core\Http\Method;use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) { // This part answers each request. `/old-shop` redirects to `/shop`. // Every other path gets a normal page. var $path = Core\Request::path(); if ($path == '/old-shop') { Core\Response::redirect('/shop'); } else { echo 'the shop'; }} else { // This part is the test. A redirect has the status 303 by default. var $old = Test::request(Method::Get, '/old-shop'); Test::assertEquals($old->status(), 303);
var $new = Test::request(Method::Get, '/shop'); Test::assertEquals($new->status(), 200);
// Both checks passed, so this prints `the old address redirects`. echo "the old address redirects\n";}the old address redirects