The LWP way:
use LWP::UserAgent::Mockable;
BEGIN {
# Options: record|playback|passthrough
$ENV{LWP_UA_MOCK} ||= 'playback';
$ENV{LWP_UA_MOCK_FILE} ||= "$0-lwp-mock.out";
}
END {
LWP::UserAgent::Mockable->finished();
}
# You might be able to normalise the requests using the callbacks
The Mojo way:
use Mojo::UserAgent::Mockable;
my $mock_ua = Mojo::UserAgent::Mockable->new(
mode => $ENV{MOJO_UA_MOCK},
file => "$0-mojo-ua-mock.out",
# ignore_body => 1,
# or:
request_normalizer => sub {
my ( $req, $recorded_req ) = @_;
my ($time) = $recorded_req->body =~ m{};
my ($hash) = $recorded_req->body =~ m{
my $body = $req->body;
$body =~ s{}{};
$body =~ s{
$req->body($body);
},
);
END {
$mock_ua->save if $ENV{MOJO_UA_MOCK} eq 'record';
}
ok my $t = Test::Mojo->new("My::App");
$t->app->mock( useragent => sub { $mock_ua } );
# Configure the proxy in record/passthrough mode only
if ( $MODE =~ /record|passthrough/ ) {
local $ENV{HTTPS_PROXY} = $t->app->config->{http_proxy};
local $ENV{HTTP_PROXY} = $t->app->config->{http_proxy};
my $proxy = Mojo::UserAgent::Proxy->new;
$proxy->detect;
$mock_ua->proxy( $proxy );
}