We’be been using a wonderful Perl web testing package HTTP::WebTest by Ilya Martynov for years . It’s quite old, but it aged gracefully except for one small problem: HTTP::WebTest’s API.pm module dies if an improper URI is passed along. This happens quite often, especially if HTTP::WebTest::Plugin::Click module is used - when, for example, no suitable URL or button is found in a result page to click on. The API’s run_test() method dies thus canceling the whole test sequence.

The fix is to use the following plug-in:

package APPDesign::WebTest::MuteBadURI;
use strict;
use URI;
use base qw(HTTP::WebTest::Plugin);

my $API_BAD_URI = "http://MISSING_HOSTNAME/";
my $MY_BAD_URI  = "http://0.0.0.0/";

sub prepare_request
{
  my $self = shift;

  if( $self->webtest->current_request and ($self->webtest->current_request->uri eq $API_BAD_URI) )
  {
    $self->webtest->current_request->base_uri($MY_BAD_URI);
  }
}

1;

It fools API.pm by replacing it’s bad URI marker with it’s own and allowing all subsequent tests in a chain to proceed and fail (that’s a bad URI) peacefully. In order to use the plug-in, add it to the list of required plug-ins on initialization, for example:

use HTTP::WebTest;
my %globalParams =
(
  relative_urls  => "yes",
  plugins        =>
  [
    "HTTP::WebTest::Plugin::Click",
    "APPDesign::WebTest::MuteBadURI",
  ],
  #... and so on
);

# Run the tests
my $webtest = new HTTP::WebTest;
$webtest->run_wtscript($ARGV[0], %globalParams);