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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#!/usr/bin/env perl
use strict;
use warnings;
use v5.10.1;
use Test::More;
use Test::Exception;
use Test::LWP::UserAgent;
use HTTP::Response;
my $pkg;
BEGIN {
$pkg = 'Catmandu::Importer';
use_ok $pkg;
}
require_ok $pkg;
{
package T::Importer;
use Moo;
with $pkg;
sub generator {
my ($self) = @_;
sub {
state $fh = $self->fh;
my $name = $self->fh->getline;
return defined $name ? {"hello" => $name} : undef;
};
}
package T::DataPathImporter;
use Moo;
with $pkg;
sub generator {
my ($self) = @_;
sub {
state $data = [
{abc => [{a => 1}, {b => 2}, {c => 3}]},
{abc => [{d => 4}, {e => 5}, {f => 6}]}
];
return shift @$data;
};
}
}
my $i = T::Importer->new;
ok $i->does('Catmandu::Iterable');
$i = T::Importer->new(file => \"World");
is_deeply $i->to_array, [{hello => "World"}], 'import from string reference';
$i = T::Importer->new(file => \"Hello\nWorld");
is join('', $i->fh->getlines), "Hello\nWorld", "import all";
$i = T::Importer->new(file => "missing");
throws_ok {$i->fh->getlines} "Catmandu::BadArg",
"throws an error if file doesn't exist";
$i = T::DataPathImporter->new;
is_deeply $i->to_array,
[
{abc => [{a => 1}, {b => 2}, {c => 3}]},
{abc => [{d => 4}, {e => 5}, {f => 6}]}
];
$i = T::DataPathImporter->new(data_path => 'abc');
is_deeply $i->to_array,
[[{a => 1}, {b => 2}, {c => 3}], [{d => 4}, {e => 5}, {f => 6}]];
$i = T::DataPathImporter->new(data_path => 'abc.*');
is_deeply $i->to_array,
[{a => 1}, {b => 2}, {c => 3}, {d => 4}, {e => 5}, {f => 6}];
$i = T::Importer->new(user_agent => user_agent(), file => 'http://demo.org/');
is join('', $i->fh->getlines), "test123", "read from http (file)";
$i = T::Importer->new(
user_agent => user_agent(),
file => 'http://demo.org/{id}',
variables => {id => 1234}
);
is $i->file, "http://demo.org/1234";
is join('', $i->fh->getlines), "test1234",
"read from http (file + variables)";
$i = T::Importer->new(
user_agent => user_agent(),
file => 'http://demo.org/{1},{2},{3}',
variables => [qw(red green blue)]
);
is $i->file, "http://demo.org/red,green,blue";
is join('', $i->fh->getlines), "RED-GREEN-BLUE",
"read from http (file + variables list)";
$i = T::Importer->new(
user_agent => user_agent(),
file => 'http://demo.org/{1},{2},{3}',
variables => "red,green,blue"
);
is $i->file, "http://demo.org/red,green,blue";
is join('', $i->fh->getlines), "RED-GREEN-BLUE",
"read from http (file + variables list)";
$i = T::Importer->new(
user_agent => user_agent(),
file => 'http://demo.org/post',
http_method => 'POST',
http_body => '=={id}==',
variables => {id => 1234}
);
is $i->file, "http://demo.org/post";
is join('', $i->fh->getlines), "POST",
"read from http (file + variables list + post request)";
$i = T::Importer->new(
user_agent => user_agent(),
file => 'http://demo.org/post',
http_method => 'POST',
http_body => '=={id}==',
variables => "red,green,blue"
);
is $i->file, "http://demo.org/post";
is join('', $i->fh->getlines), "POST",
"read from http (file + variables list + post request)";
$i = T::Importer->new(
user_agent => user_agent(),
file => 'http://demo.org/not-exsists',
http_method => 'POST',
http_body => '=={id}==',
variables => "red,green,blue"
);
throws_ok {$i->fh->getlines} 'Catmandu::HTTPError',
"throws an error on non-existing pages";
$i = T::Importer->new(file => 'http://demo.org');
is ref($i->_http_client), 'LWP::UserAgent', 'Got a real client';
done_testing;
sub user_agent {
my $ua = Test::LWP::UserAgent->new(agent => 'Test/1.0');
$ua->map_response(
qr{^http://demo.org/$},
HTTP::Response->new(
'200', 'OK', ['Content-Type' => 'text/plain'], 'test123'
)
);
$ua->map_response(
qr{^http://demo.org/1234$},
HTTP::Response->new(
'200', 'OK', ['Content-Type' => 'text/plain'], 'test1234'
)
);
$ua->map_response(
qr{^http://demo.org/red,green,blue$},
HTTP::Response->new(
'200', 'OK',
['Content-Type' => 'text/plain'], 'RED-GREEN-BLUE'
)
);
$ua->map_response(
qr{^http://demo.org/post$},
HTTP::Response->new(
'200', 'OK', ['Content-Type' => 'text/plain'], 'POST'
)
);
$ua;
}
|