Search code examples
perltestingmojolicious

Testing URL generation with Test::Mojo


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?


Solution

  • 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/