Core\Test::sentHttp
Core\Test::sentHttp(): array<Core\Test\SentRequest>Core\Test::sentHttp() returns every HTTP request your program sent to a fake web service. You create the fake service with Core\Test::answerHttp. The list is in the order the requests were sent, and the first request comes first. Each item is a Core\Test\SentRequest.
A SentRequest has four methods. method() returns the HTTP method, such as Core\Http\Method::Post. url() returns the whole URL the program used. header($name) returns one header, or null if the request did not have it. The name can use upper or lower case letters. body() returns the body as bytes. Reading the list does not clear it. Before your program sends anything, the list is empty.
The examples below show how to read one request, how the list grows with each request, and a test of an API client that must send a token.
Return value
Returns array<Core\Test\SentRequest>. One Core\Test\SentRequest for each request, in the order the program sent them. The list is empty when the program sent nothing. Nothing in a request is tainted, because the program wrote all of it.
Examples
Read what was sent
<?nvs// Shows how to read the request your program sent to a fake web service.
use Core\Http\Client;use Core\Http\Method;use Core\Test;use Core\Test\SentRequest;
Test::answerHttp('https://api.example.com/notes', 201);
Client::post('https://api.example.com/notes', { json: ['title' => 'Shopping'] });
// `sentHttp()` returns a list with one item for each request.// Here the list has one item.SentRequest $request = Test::sentHttp()[0];
// This prints `POST https://api.example.com/notes`.echo $request->method() == Method::Post ? 'POST ' : 'other ', $request->url(), "\n";
// `header` reads one header of the request. This prints `application/json`.echo $request->header('content-type') ?? 'no content type', "\n";
// `body()` returns bytes. This prints `{"title":"Shopping"}`.echo $request->body() as string, "\n";POST https://api.example.com/notesapplication/json{"title":"Shopping"}One item for each request
<?nvs// Shows that the list has one item for each request, in the order they were sent.
use Core\Http\Client;use Core\Test;use Core\Test\SentRequest;
// A URL that ends in `*` answers every URL that starts with the same text.Test::answerHttp('https://api.example.com/users/*', 200, { json: ['name' => 'Ada'] });
// Before the program sends a request, the list is empty. This prints `0`.echo Core\Arr::count(Test::sentHttp()), "\n";
Client::get('https://api.example.com/users/7');Client::get('https://api.example.com/users/12');Client::delete('https://api.example.com/users/7');
// `url()` is the whole URL the program used, not the URL with `*`.// This prints the three URLs, the oldest first.foreach (Test::sentHttp() as SentRequest $request) { echo $request->url(), "\n";}0https://api.example.com/users/7https://api.example.com/users/12https://api.example.com/users/7A client that sends a token
<?nvs// Shows how to test that an API client sends the right token and the right order.
use Core\Http\Client;use Core\Test;use Core\Test\SentRequest;
class OrderClient{ // Sends one order to the shop service, with the token in a header. public static function place(string $token, string $sku, int $count): int { var $response = Client::post('https://shop.example.com/orders', { headers: ['authorization' => 'Bearer ' . $token], json: ['sku' => $sku, 'count' => $count], }); return $response->status(); }}
Test::answerHttp('https://shop.example.com/orders', 201);OrderClient::place('t-123', 'mug', 2);
// The test checks the request, not only the reply.// `header` finds the header with any mix of upper and lower case letters.Test::assertCount(Test::sentHttp(), 1);SentRequest $request = Test::sentHttp()[0];Test::assertSame($request->header('Authorization'), 'Bearer t-123');Test::assertSame($request->body() as string, '{"sku":"mug","count":2}');
// This prints `the order was sent with the token`.echo "the order was sent with the token\n";the order was sent with the token