Fetch-Image

 view release on metacpan or  search on metacpan

lib/Fetch/Image.pm  view on Meta::CPAN

}

# returns a HTTP::Response for a HTTP HEAD request
sub _head{
    my ( $self, $ua, $url ) = @_;

    my $head = $ua->head( $url );

    $head->is_error && Exception::Simple->throw("transfer error");

    exists( $self->{'config'}->{'allowed_types'}->{ $head->header('content-type') } )
        || Exception::Simple->throw("invalid content-type");

    if (
        $head->header('content-length')
        && ( $head->header('content-length') > $self->{'config'}->{'max_filesize'} )
    ){
    #file too big
        Exception::Simple->throw("filesize exceeded");
    }

    return $head;

t/fetch_image.t  view on Meta::CPAN

my $fetcher_config = {
    'max_filesize' => '51200',
    'user_agent' => 'mozilla firefox yo',
};

my $fetcher = new_ok('Fetch::Image' => [$fetcher_config], 'fetcher');

# text file
throws_ok{
    $fetcher->fetch( 'http://www.ietf.org/rfc/rfc3751.txt' );
} qr/invalid content-type/, 'not an image';

# large file (50k+)
throws_ok{
    $fetcher->fetch( 'http://i.zdnet.com/blogs/nasa-small.bmp' );
} qr/filesize exceeded/, 'file too large';

# invalid url
throws_ok{
    $fetcher->fetch( 'something un urlish' );
} qr/invalid url/, 'invalid url';

t/fetch_image.t  view on Meta::CPAN

# test allowed_types
{
    my $fetcher_config = {
        'allowed_types' => {
            'image/jpeg' => 1,
        },
    };
    my $fetcher = new_ok('Fetch::Image' => [$fetcher_config], 'fetcher');
    throws_ok{
        $fetcher->fetch('http://www.google.com/images/srpr/logo4w.png');
    } qr/invalid content-type/, 'invalid content-type';
}

done_testing;



( run in 0.915 second using v1.01-cache-2.11-cpan-524268b4103 )