Question

I’m writing a bunch of test for my Mojolicious app and I’d like to use the json_is assertion to check the output returned by the app. The problem is that the app returns some absolute URLs like this one:

http://localhost:56144/foo

…and the TCP port is random, so I don’t know what to check the output against. Is there a way to find out the root app URL? Or maybe a different way to write the tests?

Was it helpful?

Solution 2

After some digging in the sources I have found out that I can pull the server URL from the user agent:

my $t = Test::Mojo->new('MyAppName');
diag $t->ua->server->url; # http://localhost:59475/

OTHER TIPS

If I understand your question, you can check your random url something like this:

use Test::More;
use Test::Mojo;
use Mojo::URL;

my $t = Test::Mojo->new('MyApp');

$t->post_ok('/search.json')->status_is(200);
# suppose that result something like this {"url":"http://random_domain.ru:1234/foo/bar"}
my $params = $t->tx->res->json;
my $url = Mojo::URL->new($params->{url});
is($url->path, '/foo/bar', 'test url path');
like($url->port, qr/^\d+$/, 'test port');
is($url->scheme, 'http', 'test scheme');

Just to confuse stuff further, if you a user agent inside your app, you'll find that the app's user agent is seperate from the Test::Mojo's user agent:

   my $t = Test::Mojo->new('MyApp');
   isnt($t->ua->server->url, $t->app->ua->server->url);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top