Skip to content

Core\Test\SentRequest::url

$sentRequest->url(): string

url() returns the URL of one request that your program sent in a test. You get the request from Core\Test::sentHttp(), after Core\Test::answerHttp has set up a fake web service.

The result is the whole URL, exactly as the program wrote it. It includes the path and the query string. It is not the pattern you gave to Core\Test::answerHttp. When one pattern ending in * answers many URLs, url() still tells you which URL the program used. This lets a test check that the program built a path or a query string correctly.

The examples below show how to read one URL, the URLs behind one pattern, and a test that checks a search client encodes its search text.

Return value

Returns string. The whole URL, with its path and query string. It is not the pattern you gave to Core\Test::answerHttp, so a test can check the exact path the program used.

Examples

Read the url

<?nvs
// Shows how to read the URL of a request your program sent.
use Core\Http\Client;
use Core\Test;
// The `*` at the end answers every URL that starts with this text.
Test::answerHttp('https://api.example.com/weather*', 200, { json: ['temp' => 21] });
Client::get('https://api.example.com/weather?city=Oslo&days=3');
// `url()` returns the whole URL, with the query string.
// This prints `https://api.example.com/weather?city=Oslo&days=3`.
echo Test::sentHttp()[0]->url(), "\n";
Output
https://api.example.com/weather?city=Oslo&days=3

The url and not the pattern

<?nvs
// Shows that `url()` returns the URL the program used, and not the pattern of the fake service.
use Core\Http\Client;
use Core\Test;
use Core\Test\SentRequest;
// The `*` at the end answers every URL that starts with this text.
Test::answerHttp('https://api.example.com/users/*', 200);
Client::get('https://api.example.com/users/7');
Client::get('https://api.example.com/users/8/orders');
// This prints the two URLs the program used, one on each line.
foreach (Test::sentHttp() as SentRequest $request) {
echo $request->url(), "\n";
}
Output
https://api.example.com/users/7
https://api.example.com/users/8/orders

Test that a search encodes its query

<?nvs
// Shows how to test that a search client puts the search text into the URL correctly.
use Core\Http\Client;
use Core\Test;
class BookSearch
{
// Searches the book service for a title.
public static function find(string $title): int
{
string $query = Core\Uri::encodeComponent($title);
var $response = Client::get('https://books.example.com/search?q=' . $query);
return $response->status();
}
}
Test::answerHttp('https://books.example.com/search*', 200, { json: [] });
BookSearch::find('War & Peace');
// A space and a `&` must be encoded in a URL.
// The test checks the exact URL the client built.
Test::assertSame(Test::sentHttp()[0]->url(), 'https://books.example.com/search?q=War%20%26%20Peace');
// This prints `the search text was encoded`.
echo "the search text was encoded\n";
Output
the search text was encoded