1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
use strict;
use Test::More;
use lib 't';
use MockFurl;
use Catmandu::Importer::getJSON;
sub test_importer(@) { ##no critic
my ($url, $requests, $content, $expect, $msg) = @_;
my $importer = Catmandu::Importer::getJSON->new(
file => \ do { join "\n", map { $_->[0] } @$requests },
client => MockFurl::new( content => $content ),
);
$importer->url($url) if defined $url;
$expect = [ map { $expect } @$requests ] if ref $expect ne 'ARRAY';
is_deeply $importer->to_array, $expect, $msg;
is_deeply $importer->client->urls, [ map { $_->[1] } @$requests ];
}
my @requests = (
[ '{ } ' => 'http://example.org/' ],
[ '{"q":"&"}' => 'http://example.org/?q=%26' ],
[ '/path?q=%20 ' => 'http://example.org/path?q=%20' ],
);
test_importer 'http://example.org/', \@requests,
'{"x":"\u2603"}' => {x=>"\x{2603}"},
'URI';
test_importer
'http://example.{tdl}/{?foo}{?bar}',
[
[ 'http://example.org' => 'http://example.org' ],
[ '{"tdl":"com"}' => 'http://example.com/' ],
[ '{"tdl":"com","bar":"doz"}' => 'http://example.com/?bar=doz' ],
],
'{}' => { },
'URI::Template';
is_deeply(Catmandu::Importer::getJSON->new(
client => MockFurl::new( content => '{"hello":"World"}' ),
from => 'http://example.org',
)->to_array, [{hello=>"World"}], '--from');
is_deeply(Catmandu::Importer::getJSON->new(
dry => 1, url => 'http://example.{tdl}/',
file => \'{"tdl":"org"}'
)->to_array, [{url=>"http://example.org/"}],'--dry');
test_importer undef,
[ ["http://example.info" => "http://example.info" ] ],
'[{"n":1},{"n":2}]' => [{"n"=>1},{"n"=>2}],
'JSON array response';
my $importer = Catmandu::Importer::getJSON->new(
client => MockFurl::new( content => '[{"n":1},{"n":2}]' ),
from => 'http://example.org',
);
{
my $res = $importer->to_array;
is_deeply $res->[0], {n => 1}, 'array response 1/2';
is_deeply $res->[1], { n => 2}, 'array response 2/2';
}
{
my $warning; local $SIG{__WARN__} = sub { $warning = shift };
is_deeply(Catmandu::Importer::getJSON->new(
file => \"x\n\nhttp://example.org/",
dry => 1,
warn => 1,
)->to_array, [{ url => "http://example.org/" }]);
is $warning, "failed to construct URL: x\n", "warning";
}
done_testing;
|