Commit 62d0733e authored by David Golden's avatar David Golden

add tests for mirror()

parent 3757896c
......@@ -2,7 +2,7 @@ Release notes for HTTP-Tiny
{{$NEXT}}
- Add a few tests for the get() method
- Add a few tests for the get() and mirror() methods
0.004 2010-12-15 22:53:59 EST5EDT
......
#!perl
use strict;
use warnings;
use Test::More 0.88;
use t::Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case
set_socket_source sort_headers $CRLF $LF];
use HTTP::Tiny;
use File::Temp qw/tempdir/;
BEGIN { monkey_patch() }
my $tempdir = tempdir( TMPDIR => 1, CLEANUP => 1 );
my $tempfile = $tempdir . "/tempfile.txt";
my %response_codes = (
'new.txt' => '200',
'modified.txt' => '200',
'not-modified.txt' => '304',
'missing.txt' => '404',
);
my $known_epoch = 760233600;
my $day = 24*3600;
my %timestamp = (
'modified.txt' => $known_epoch - 2 * $day,
'not-modified.txt' => $known_epoch - 2 * $day,
);
for my $file ( dir_list("t/cases", qr/^mirror/ ) ) {
unlink $tempfile;
my $data = do { local (@ARGV,$/) = $file; <> };
my ($params, $expect_req, $give_res) = split /--+\n/, $data;
# cleanup source data
my $version = HTTP::Tiny->VERSION || 0;
$expect_req =~ s{VERSION}{$version};
s{\n}{$CRLF}g for ($expect_req, $give_res);
# figure out what request to make
my $case = parse_case($params);
my $url = $case->{url}->[0];
my %options;
my %headers;
for my $line ( @{ $case->{headers} } ) {
my ($k,$v) = ($line =~ m{^([^:]+): (.*)$}g);
$headers{$k} = $v;
}
$options{headers} = \%headers if %headers;
# maybe create a file
(my $url_basename = $url) =~ s{.*/}{};
if ( my $mtime = $timestamp{$url_basename} ) {
open my $fh, ">", $tempfile;
close $fh;
utime $mtime, $mtime, $tempfile;
}
# setup mocking and test
my $res_fh = tmpfile($give_res);
my $req_fh = tmpfile();
my $http = HTTP::Tiny->new;
set_socket_source($req_fh, $res_fh);
my @call_args = %options ? ($url, $tempfile, \%options) : ($url, $tempfile);
my $response = $http->mirror(@call_args);
my $got_req = slurp($req_fh);
my $label = "get on $url";
$label .= " (@{[keys %options]})" if %options;
is( sort_headers($got_req), sort_headers($expect_req), "$label request" );
my $rc = $response_codes{$url_basename};
if ( $rc eq '200' ) {
ok( $response->{success}, "$label success flag true" );
is( $response->{status}, $rc, "$label response code $rc" );
ok( -e $tempfile, "$label file created" );
}
elsif ( $rc eq '304' ) {
ok( $response->{success}, "$label success flag true" );
is( $response->{status}, $rc, "$label response code $rc" );
is( (stat($tempfile))[9], $timestamp{$url_basename},
"$label file not overwritten" );
}
else {
ok( ! $response->{success}, "$label success flag false" );
is( $response->{status}, $rc, "$label response code $rc" )
or diag $response->{content};
ok( ! -e $tempfile, "$label file not created" );
}
}
done_testing;
......@@ -55,7 +55,7 @@ sub dir_list {
$filter ||= qr/./;
my $d = IO::Dir->new($dir)
or return;
return map { "$dir/$_" } grep { /$filter/ } grep { /^[^.]/ } $d->read;
return map { "$dir/$_" } sort grep { /$filter/ } grep { /^[^.]/ } $d->read;
}
sub slurp (*) {
......
url
http://example.com/modified.txt
----------
GET /modified.txt HTTP/1.1
Host: example.com
Connection: close
If-Modified-Since: Tue, 01 Feb 1994 00:00:00 GMT
User-Agent: HTTP-Tiny/VERSION
----------
HTTP/1.1 200 OK
Date: Thu, 03 Feb 1994 00:00:00 GMT
Last-Modified: Wed, 02 Feb 1994 00:00:00 GMT
Content-Type: text/plain
Content-Length: 42
abcdefghijklmnopqrstuvwxyz1234567890abcdef
url
http://example.com/not-modified.txt
----------
GET /not-modified.txt HTTP/1.1
Host: example.com
Connection: close
If-Modified-Since: Tue, 01 Feb 1994 00:00:00 GMT
User-Agent: HTTP-Tiny/VERSION
----------
HTTP/1.1 304 Not Modified
Date: Thu, 03 Feb 1994 00:00:00 GMT
Last-Modified: Tue, 01 Feb 1994 00:00:00 GMT
url
http://example.com/new.txt
----------
GET /new.txt HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
----------
HTTP/1.1 200 OK
Date: Thu, 03 Feb 1994 00:00:00 GMT
Last-Modified: Tue, 01 Feb 1994 00:00:00 GMT
Content-Type: text/plain
Content-Length: 42
abcdefghijklmnopqrstuvwxyz1234567890abcdef
url
http://example.com/missing.txt
----------
GET /missing.txt HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
----------
HTTP/1.1 404 Not Found
Date: Thu, 03 Feb 1994 00:00:00 GMT
Content-Length: 0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment