Mojo-UserAgent-Mockable
view release on metacpan or search on metacpan
t/serializer/store_retrieve.t view on Meta::CPAN
my $cols = $c->req->param('cols') || 1;
my @nums;
for ( 0 .. ( $count - 1 ) ) {
my $number = ( int rand( $max - $min ) ) + $min;
push @nums, $number;
}
$c->render( text => join qq{\n}, @nums );
};
}
my $serializer = Mojo::UserAgent::Mockable::Serializer->new;
subtest 'Victoria and Albert Museum' => sub {
my $dir = File::Temp->newdir;
my $output_file = qq{$dir/victoria_and_albert.json};
my @transactions;
push @transactions, Mojo::UserAgent->new->get(q{https://www.vam.ac.uk/api/json/museumobject/?limit=1});
my $result = $transactions[0]->res->json;
plan skip_all => 'Museum API not responding properly' unless ref $result eq 'HASH' && $result->{'meta'};
plan skip_all => 'No records returned' unless @{$result->{'records'}};
my $object_number = $result->{'records'}[0]{'fields'}{'object_number'};
push @transactions, Mojo::UserAgent->new->get(qq{https://www.vam.ac.uk/api/json/museumobject/$object_number});
my $museum_object = $transactions[1]->res->json;
plan skip_all => 'Museum object not retrieved properly' unless @{$museum_object} && keys %{$museum_object->[0]};
test_transactions($output_file, @transactions);
};
subtest 'Local App' => sub {
my $dir = File::Temp->newdir;
my $output_file = qq{$dir/local_app.json};
my $app = LocalApp::app;
my $ua = Mojo::UserAgent->new;
$ua->server->app($app);
my $url = $ua->server->url->clone->path('/records');
my @transactions = $ua->get($url);
my $records = $transactions[0]->res->json;
my $record_id = $records->{'records'}[0]{'id'};
push @transactions, $ua->get($url->clone->path(qq{/record/$record_id}));
my $record = $transactions[1]->res->json;
BAIL_OUT('Local app did not serve records correctly') unless $transactions[1]->res->json->[0]{'author'} eq 'Tommy Tutone';
test_transactions($output_file, @transactions);
};
subtest 'random.org' => sub {
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;
plan skip_all => 'Random.org quota exceeded' unless check_quota();
my $dir = File::Temp->newdir;
my $output_file = qq{$dir/random_org.json};
my $url = Mojo::URL->new( q{https://www.random.org/integers/} )->query(
num => 5,
min => 0,
max => 1e9,
col => 1,
base => 10,
format => 'plain',
);
my $ua = Mojo::UserAgent->new;
my @transactions = ($ua->get($url), $ua->get($url));
test_transactions($output_file, @transactions);
};
subtest 'URL bits' => sub {
my $dir = File::Temp->newdir;
my $output_file = qq{$dir/local_random.json};
my $app = LocalRandomApp::app;
my $ua = Mojo::UserAgent->new;
$ua->server->app($app);
my $url = Mojo::URL->new( $ua->server->url('https')->clone->path('/integers') )->query(
num => 5,
min => 0,
max => 1e9,
col => 1,
base => 10,
format => 'plain',
)->userinfo('nobody:nohow');
my @transactions = ($ua->get($url), $ua->get($url));
test_transactions($output_file, @transactions);
};
done_testing;
sub test_transactions {
my ($output_file, @transactions) = @_;
lives_ok { $serializer->store($output_file, @transactions) } q{serialize() did not die};
my $serialized = path($output_file)->slurp_raw;
is_valid_json($serialized, q{Serializer outputs valid JSON});
my $decoded = Mojo::JSON::decode_json($serialized);
is ref $decoded, 'ARRAY', q{Transactions serialized as array};
for (0 .. $#transactions) {
for my $key (qw/request response/) {
ok defined($decoded->[$_]{$key}), qq{Key "$key" defined in serial data};
for my $subkey (qw/class body/) {
ok defined($decoded->[$_]{$key}{$subkey}), qq{Key "$subkey" defined in "$key" data};
}
( run in 0.661 second using v1.01-cache-2.11-cpan-39bf76dae61 )