-
Notifications
You must be signed in to change notification settings - Fork 2
/
handle.t
42 lines (36 loc) · 1.17 KB
/
handle.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
use Mojo::Base qw{ -strict };
use Mojolicious::Lite;
use File::Basename;
use File::Spec;
use Encode ();
my $dir = dirname(__FILE__);
plugin 'DirectoryServer', root => $dir, handler => sub {
my ($c, $path) = @_;
$c->render( data => $path, format => 'txt' ) if (-f $path);
};
use Test::More tests => 3;
use Test::Mojo;
my $t = Test::Mojo->new;
$t->get_ok('/')->status_is(200);
my $location_is = sub {
my ($t, $regex, $desc) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
return $t->success(like($t->tx->res->headers->location, $regex));
};
use File::Basename;
subtest 'entries' => sub {
my $dh = DirHandle->new($dir);
while ( defined( my $ent = $dh->read ) ) {
$ent = Encode::decode_utf8($ent);
next if $ent eq '.' or $ent eq '..';
my $path = File::Spec->catdir( $dir, $ent );
if (-f $path) {
$t->get_ok("/$ent")->status_is(200)->content_is( Encode::encode_utf8($path) );
}
elsif (-d $path) {
$t->get_ok("/$ent")->status_is(302)->$location_is(qr|/$ent/$|);
$t->get_ok("/$ent/")->status_is(200)->content_like( qr/Parent Directory/ );
}
else { ok 0 }
}
}