Mojo-UserAgent-Mockable

 view release on metacpan or  search on metacpan

t/record_playback.t  view on Meta::CPAN

# NB this test is also run from t/canonicalize_portably_*

use 5.014;

use File::Temp;
use FindBin qw($Bin);
use lib qq{$Bin/lib};
use Mojo::UserAgent::Mockable;
use RandomOrgQuota qw/check_quota/;
use Test::Most;
use Test::Mojo;

my $ver;
eval { 
    require IO::Socket::SSL; 
    $ver = $IO::Socket::SSL::VERSION; 
    1;
} or plan skip_all => 'IO::Socket::SSL not installed';

plan skip_all => qq{Minimum version of IO::Socket::SSL is 1.94 for this test, but you have $ver} if $ver < 1.94;


my $TEST_FILE_DIR = qq{$Bin/files};
my $COUNT         = 5;
my $MIN           = 0;
my $MAX           = 1e9;
my $COLS          = 1;
my $BASE          = 10;

my %args = @_;

my $dir = File::Temp->newdir;

my $url = Mojo::URL->new(q{https://www.random.org/integers/})->query(
    num    => $COUNT,
    min    => $MIN,
    max    => $MAX,
    col    => $COLS,
    base   => $BASE,
    format => 'plain',
);
my $app = $args{'app'};

my $output_file = qq{$dir/output.json};

my $transaction_count = 10;
plan skip_all => 'Random.org quota exceeded' unless check_quota($transaction_count);

my %cookies;
my $cookie_count = 0;
# Record the interchange
my ( @results, @transactions );
{    # Look! Scoping braces!
    my $mock = Mojo::UserAgent::Mockable->new( mode => 'record', file => $output_file );
    $mock->transactor->name('kit.peters@broadbean.com');

    for ( 1 .. $transaction_count ) {
        push @transactions, $mock->get( $url->clone->query( [ quux => int rand 1e9 ] ));
    }

    for my $cookie (@{$mock->cookie_jar->all}) {
        $cookie_count++;
        my $domain = $cookie->domain;
        my $name = $cookie->name;
        BAIL_OUT(qq{Duplicate cookie "$name" for domain "$domain"}) if ($cookies{$domain}{$name}); 
        $cookies{$domain}{$name} = $cookie;
    }
    @results = map { [ split /\n/, $_->res->text ] } @transactions;

    plan skip_all => 'Remote not responding properly'
        unless ref $results[0] eq 'ARRAY' && scalar @{ $results[0] } == $COUNT;
    $mock->save;
}

subtest "Check that the file was recorded in a nice clean format" => sub {
    open(my $mocks_fh, '<', $output_file) || BAIL_OUT('Output file does not exist');
    foreach my $expected (
        '[',
        '   {',
        '      "class" : "Mojo::Transaction::HTTP",',



( run in 1.340 second using v1.01-cache-2.11-cpan-39bf76dae61 )