1use strict;
2use warnings;
3use lib '.';
4use Test::Base;
5
6eval q{ use MIME::Types };
7plan skip_all => "MIME::Types is not installed" if $@;
8eval q{ use Path::Class };
9plan skip_all => "Path::Class is not installed" if $@;
10eval q( { package foo; use Any::Moose;use Any::Moose 'X::Types::Path::Class' } );
11plan skip_all => "Mo[ou]seX::Types::Path::Class is not installed" if $@;
12
13plan tests => 8 * (blocks() - 1);
14
15use HTTP::Engine;
16use HTTP::Engine::Middleware;
17use HTTP::Engine::Response;
18use HTTP::Request;
19
20sub run_tests {
21    my($block, $mw) = @_;
22
23    my $request = HTTP::Request->new(
24        GET => $block->uri
25    );
26
27    my $response = HTTP::Engine->new(
28        interface => {
29            module          => 'Test',
30            request_handler => $mw->handler( sub { HTTP::Engine::Response->new( body => 'dynamic' ) } ),
31        },
32    )->run($request);
33
34    is $response->code, $block->code, 'status code';
35    is $response->content_type, $block->content_type, 'content type';
36    my $body = $block->body;
37    like $response->content, qr/$body/, 'body';
38}
39
40run {
41    my $block = shift;
42
43    if ($block->name !~ /without directory_index/) {
44        my @config = (
45            'HTTP::Engine::Middleware::Static' => {
46                docroot         => Path::Class::Dir->new('t', 'htdocs'),
47                directory_index => 'index.html',
48                is_404_handler  => 0,
49                regexp          => qr{/.*},
50            },
51        );
52
53        my $mw = HTTP::Engine::Middleware->new;
54        $mw->install(@config);
55        ok scalar(@{ $mw->middlewares }), 'firast instance';
56
57        run_tests($block, $mw);
58    }
59
60    if ($block->name !~ /with directory_index/) {
61        my @config = (
62            'HTTP::Engine::Middleware::Static' => {
63                docroot         => Path::Class::Dir->new('t', 'htdocs'),
64                is_404_handler  => 0,
65                regexp          => qr{/.*},
66            },
67        );
68
69        my $mw = HTTP::Engine::Middleware->new;
70        $mw->install(@config);
71        ok scalar(@{ $mw->middlewares }), 'firast instance';
72
73        run_tests($block, $mw);
74    }
75};
76
77
78__END__
79
80=== directory index (with directory_index)
81--- uri: http://localhost/
82--- content_type: text/html
83--- body: index page
84--- code: 200
85
86=== directory index (without directory_index)
87--- uri: http://localhost/
88--- content_type: text/html
89--- body: dynamic
90--- code: 200
91
92=== directory doesn't have directory_index handled by dynamic
93--- uri: http://localhost/css/
94--- content_type: text/html
95--- body: dynamic
96--- code: 200
97
98=== normal file
99--- uri: http://localhost/css/mobile.css
100--- content_type: text/css
101--- body: .mobile { display: none; }
102--- code: 200
103
104=== normal file doesn't exists
105--- uri: http://localhost/css/not_found.css
106--- content_type: text/html
107--- body: dynamic
108--- code: 200
109
110=== normal file doesn't exists
111--- uri: http://localhost/other_path.txt
112--- content_type: text/html
113--- body: dynamic
114--- code: 200
115
116=== directory doesn't exists
117--- uri: http://localhost/directory/does/not/exists
118--- content_type: text/html
119--- body: dynamic
120--- code: 200
121
122