-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror.t
55 lines (44 loc) · 1.12 KB
/
mirror.t
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
use strict;
use warnings;
use Test::More;
use File::Temp qw( tempdir );
use Path::Class 0.24;
use Plack::Test;
use HTTP::Request::Common;
use Plack::Middleware::Mirror ();
my %requests = (
'/helper' => "rubber\nducky",
'/monkey/island.txt' => "I want to be\na mighty pirate."
);
plan tests => 4 * keys %requests;
my $dir = tempdir( CLEANUP => 1 );
my $app = Plack::Middleware::Mirror->wrap(
sub {
my ($env) = @_;
#diag explain $env;
return [ 200, [ 'Content-Type' => 'text/plain' ], [ $requests{ $env->{PATH_INFO} } ] ];
},
path => qr/./,
mirror_dir => $dir,
#debug => 1,
);
test_psgi $app, sub {
my ($cb) = @_;
while ( my ($path, $content) = each %requests ) {
my $res = $cb->(GET "http://localhost$path");
# basics
is $res->code, 200;
is $res->content, $content;
#diag explain [`find $dir`];
my $file = file($dir, split(/\//, $path));
ok( -e $file, 'file exists' );
is slurp( $file ), $content, 'file contains "downloaded" content';
}
};
sub slurp {
my ($file) = @_;
open(my $fh, '<', $file)
or die "Failed to open mirrored '$file'";
local $/;
return <$fh>;
}