CGI-Easy-SendFile

 view release on metacpan or  search on metacpan

t/send_file.t  view on Meta::CPAN

use warnings;
use strict;
use Test::More;
use CGI::Easy::Headers;
use CGI::Easy::Util qw( date_http );

use CGI::Easy::SendFile qw( send_file );

plan tests=>50;


my $r = { ENV => {} };
my $h = CGI::Easy::Headers->new();
my ($data, $wait);
my %wait_h = (
    'Status'                => '200 OK',
    'Date'                  => q{},
    'Set-Cookie'            => [],
    'Accept-Ranges'         => 'bytes',
    'Content-Type'          => 'application/x-download',
);
my $data_dynamic = 'Test file';
my $file_dynamic = \$data_dynamic;
my $data_real    = do { seek DATA, 0, 0; join q{}, <DATA> };
my $file_real    = $0;


# default

$h = CGI::Easy::Headers->new();
$data = send_file($r, $h, $file_dynamic);
is ${$data}, $data_dynamic, 'default dynamic';
is_deeply $h, { %wait_h,
    'Expires'               => 'Sat, 01 Jan 2000 00:00:00 GMT',
    'Content-Disposition'   => 'attachment',
    'Content-Length'        => length $data_dynamic,
};

$h = CGI::Easy::Headers->new();
$data = send_file($r, $h, $file_real);
is ${$data}, $data_real, 'default real';
is_deeply $h, { %wait_h,
    'Expires'               => 'Sat, 01 Jan 2000 00:00:00 GMT',
    'Content-Disposition'   => 'attachment',
    'Content-Length'        => length $data_real,
};

# cache

$h = CGI::Easy::Headers->new();
$h->{Expires} = 'Sat, 01 Jan 2000 00:00:00 GMT',
$data = send_file($r, $h, $file_dynamic, {
    type    => 'image/png',
    cache   => 1,
    inline  => 1,
});
is ${$data}, $data_dynamic, 'image/cache/inline dynamic';
is_deeply $h, { %wait_h,
    'Content-Type'          => 'image/png',
    'Content-Length'        => length $data_dynamic,
};

$h = CGI::Easy::Headers->new();
$h->{Expires} = 'Sat, 01 Jan 2000 00:00:00 GMT',
$data = send_file($r, $h, $file_real, {
    type    => 'image/png',
    cache   => 1,
    inline  => 1,
});
is ${$data}, $data_real, 'image/cache/inline real';
is_deeply $h, { %wait_h,
    'Content-Type'          => 'image/png',
    'Content-Length'        => length $data_real,
    'Last-Modified'         => date_http((stat $file_real)[9]),
};

$r->{ENV} = { HTTP_IF_MODIFIED_SINCE => 'Sat, 01 Jan 2000 00:00:00 GMT' };
$h = CGI::Easy::Headers->new();
$h->{Expires} = 'Sat, 01 Jan 2000 00:00:00 GMT',
$data = send_file($r, $h, $file_real, {
    type    => 'image/png',
    cache   => 1,
    inline  => 1,
});
is ${$data}, $data_real, 'image/cache/inline real (ifmod in past)';
is_deeply $h, { %wait_h,
    'Content-Type'          => 'image/png',
    'Content-Length'        => length $data_real,
    'Last-Modified'         => date_http((stat $file_real)[9]),
};

$r->{ENV} = { HTTP_IF_MODIFIED_SINCE => date_http((stat $file_real)[9]) };
$h = CGI::Easy::Headers->new();
$h->{Expires} = 'Sat, 01 Jan 2000 00:00:00 GMT',
$data = send_file($r, $h, $file_real, {
    type    => 'image/png',
    cache   => 1,
    inline  => 1,
});
is ${$data}, q{}, 'image/cache/inline real (ifmod current)';
is_deeply $h, {
    Status                  => '304 Not Modified',
    'Content-Type'          => 'text/html; charset=utf-8',  # XXX?
    'Set-Cookie'            => [],
    Date                    => q{},
};

$h = CGI::Easy::Headers->new();
$h->{Expires} = 'Sat, 01 Jan 2000 00:00:00 GMT',
$data = send_file($r, $h, $file_real, {
    type    => 'image/png',
    inline  => 1,
});
is ${$data}, $data_real, 'image/inline real (ifmod current)';
is_deeply $h, { %wait_h,
    'Expires'               => 'Sat, 01 Jan 2000 00:00:00 GMT',
    'Content-Type'          => 'image/png',
    'Content-Length'        => length $data_real,
};

$r->{ENV} = { HTTP_IF_MODIFIED_SINCE => 'Tue, 01 Jan 2030 00:00:00 GMT' };
$h = CGI::Easy::Headers->new();
$h->{Expires} = 'Sat, 01 Jan 2000 00:00:00 GMT',
$data = send_file($r, $h, $file_real, {
    type    => 'image/png',
    cache   => 1,
    inline  => 1,
});
is ${$data}, $data_real, 'image/cache/inline real (ifmod in future)';
is_deeply $h, { %wait_h,
    'Content-Type'          => 'image/png',
    'Content-Length'        => length $data_real,
    'Last-Modified'         => date_http((stat $file_real)[9]),
};

# range

$r->{ENV} = {};
$h = CGI::Easy::Headers->new();
$h->{Expires} = 'Sat, 01 Jan 2000 00:00:00 GMT',
$data = send_file($r, $h, $file_dynamic, {
    range   => 1,
});
is ${$data}, $data_dynamic, 'range (no HTTP_RANGE)';
is_deeply $h, { %wait_h,
    Expires                 => 'Sat, 01 Jan 2000 00:00:00 GMT',
    'Content-Disposition'   => 'attachment',
    'Content-Length'        => length $data_dynamic,
};

$r->{ENV} = { HTTP_RANGE => 'bytes=0-' };
$h = CGI::Easy::Headers->new();
$h->{Expires} = 'Sat, 01 Jan 2000 00:00:00 GMT',
$data = send_file($r, $h, $file_dynamic, {
    range   => 1,
});
is ${$data}, $data_dynamic, 'range 0- (full)';
is_deeply $h, { %wait_h,
    Expires                 => 'Sat, 01 Jan 2000 00:00:00 GMT',
    'Content-Disposition'   => 'attachment',
    'Content-Length'        => length $data_dynamic,
};

$r->{ENV} = { HTTP_RANGE => 'bytes=1-' };



( run in 0.576 second using v1.01-cache-2.11-cpan-39bf76dae61 )