Bio-DB-Big
view release on metacpan or search on metacpan
t/05remote.t view on Meta::CPAN
=head1 LICENSE
Copyright [2015-2017] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=cut
use strict;
use warnings;
use Test::More;
use Test::Fake::HTTPD;
use Test::Output;
use Test::Exception;
use Bio::DB::Big;
use FindBin '$Bin';
use bytes qw//;
note 'This code implements a basic HTTP range system otherwise tests do not work properly';
my $return_file = sub {
my ($file, $range_request, $seek, $length) = @_;
my $target = "${Bin}/data/${file}";
open my $fh, '<', $target or die "Cannot open '${target}' file: $!";
binmode $fh;
my $file_contents;
if($range_request) {
seek($fh, $seek, 0);
read($fh, $file_contents, $length);
}
else {
local $/ = undef;
$file_contents = <$fh>;
}
close $fh;
# Get file stats
my @stat = stat $target;
my $total_size = $stat[7];
return (\$file_contents, $total_size);
};
delete $ENV{HTTP_PROXY} if $ENV{HTTP_PROXY};
delete $ENV{http_proxy} if $ENV{http_proxy};
my $get_server = sub {
my $httpd = Test::Fake::HTTPD->new();
$httpd->run(sub {
my $req = shift;
my $content = \q{};
my $total_size = 0;
my $code = '404';
my %headers = (
'Content-Type' => 'application/octet-stream'
);
my $range = $req->header('range');
my ($range_request, $start, $end, $length) = (0,0,0,0);
if($range =~ /bytes=(\d+)-(\d+)/) {
$range_request = 1;
$start = $1;
$end = $2;
$length = ($end - $start)+1;
}
if($req->uri() eq '/test.bw') {
($content, $total_size) = $return_file->('test.bw', $range_request, $start, $length);
$code = '200';
}
elsif($req->uri() eq '/test.bb') {
($content, $total_size) = $return_file->('test.bb', $range_request, $start, $length);
$code = '200';
}
elsif($req->uri() eq '/moved/test.bw') {
# warn 'asked about this!';
$code = '302';
$headers{'Location'} = '/test.bw';
}
if($start && $code eq '200') {
$code = '206'; # change to partial content
my $content_length = bytes::length($$content);
my $actual_end = $start + $content_length;
$headers{'Content-Length'} = $content_length;
$headers{'Content-Range'} = "bytes $start-$actual_end/$total_size";
}
return [
$code,
[%headers],
[ $$content ]
];
});
ok(defined $httpd, 'Got a web server');
note( sprintf "You can connect to your server at %s.\n", $httpd->host_port );
return $httpd;
};
Bio::DB::Big->init();
subtest 'Testing opening remote BigWig file' => sub {
my $httpd = $get_server->();
my $url_root = $httpd->endpoint;
( run in 0.564 second using v1.01-cache-2.11-cpan-483215c6ad5 )