File: static.t

package info (click to toggle)
libplack-perl 1.0048-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,480 kB
  • sloc: perl: 5,288; python: 7; makefile: 4; javascript: 1
file content (97 lines) | stat: -rw-r--r-- 2,817 bytes parent folder | download | duplicates (7)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use strict;
use warnings;
use Test::More;
use Plack::Middleware::Static;
use Plack::Builder;
use Plack::Util;
use HTTP::Request::Common;
use HTTP::Response;
use Cwd;
use Plack::Test;

my $base = cwd;

Plack::MIME->add_type(".foo" => "text/x-fooo");

my $handler = builder {
    enable "Plack::Middleware::Static",
        path => sub { s!^/share/!!}, root => "share";
    enable "Plack::Middleware::Static",
        path => sub { s!^/more_share/!! if $_[1]->{PATH_INFO} =~ m!^/more_share/!  },
        root => "share";
    enable "Plack::Middleware::Static",
        path => sub { s!^/share-pass/!!}, root => "share", pass_through => 1;
    enable "Plack::Middleware::Static",
        path => qr{\.(t|PL|txt)$}i, root => '.';
    enable "Plack::Middleware::Static",
        path => qr{\.foo$}i, root => '.', 
        content_type => sub { substr Plack::MIME->mime_type($_[0]),0,-1  } ;
    sub {
        [200, ['Content-Type' => 'text/plain', 'Content-Length' => 2], ['ok']]
    };
};

my %test = (
    client => sub {
        my $cb  = shift;

        {
            my $path = "t/test.txt";
            my $res = $cb->(GET "http://localhost/$path");
            is $res->content_type, 'text/plain', 'ok case';
            like $res->content, qr/foo/;
            is -s $path, length($res->content);
            my $content = do { open my $fh, "<", $path; binmode $fh; join '', <$fh> };
            is $content, $res->content;
        }

        {
            my $res = $cb->(GET "http://localhost/..%2f..%2f..%2fetc%2fpasswd.t");
            is $res->code, 403;
        }

        {
            my $res = $cb->(GET "http://localhost/..%2fMakefile.PL");
            is $res->code, 403, 'directory traversal';
        }

        {
            my $res = $cb->(GET "http://localhost/foo/not_found.t");
            is $res->code, 404, 'not found';
            is $res->content, 'not found';
        }

        {
            my $res = $cb->(GET "http://localhost/share/face.jpg");
            is $res->content_type, 'image/jpeg';
        }

        {
            my $res = $cb->(GET "http://localhost/more_share/face.jpg");
            is $res->content_type, 'image/jpeg';
        }

        {
            my $res = $cb->(GET "http://localhost/share-pass/faceX.jpg");
            is $res->code, 200, 'pass through';
            is $res->content, 'ok';
        }

        {
            my $res = $cb->(GET "http://localhost/t/Plack-Middleware/static.txt");
            is $res->content_type, 'text/plain';
            my($ct, $charset) = $res->content_type;
            is $charset, 'charset=utf-8';
        }

        {
            my $res = $cb->(GET "http://localhost/t/Plack-Middleware/static.foo");
            is $res->content_type, 'text/x-foo';
        }
},
    app => $handler,
);

test_psgi %test;

done_testing;