File: apache2-registry.t

package info (click to toggle)
libplack-perl 0.9989-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,556 kB
  • sloc: perl: 6,890; python: 6; makefile: 2
file content (111 lines) | stat: -rw-r--r-- 2,694 bytes parent folder | download | duplicates (3)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use strict;
use warnings;
use File::Path;
use Test::TCP;
use LWP::UserAgent;
use HTTP::Request::Common;
use Test::More;

plan skip_all => "TEST_APACHE2 is not set"
    unless $ENV{TEST_APACHE2};

# Note: you need to load 64bit lib to test Apache2 on OS X 10.5 or later

test_tcp(
    client => sub {
        my $port = shift;

        my $ua = LWP::UserAgent->new;
        my $call = sub {
            my $req = shift;
            $req->uri->port($port);
            return $ua->request($req);
        };

        my $res1 = $call->( GET 'http://127.0.0.1/psgi-bin/app.psgi' );
        note $res1->content;
        is $res1->header('X-Script-Name'), '/psgi-bin/app.psgi';
        is $res1->header('X-Path-Info')  , '';

        my $res2 = $call->(
            GET 'http://127.0.0.1/psgi-bin/deep/app.psgi/deeply'
        );
        note $res2->content;
        is $res2->header('X-Script-Name'), '/psgi-bin/deep/app.psgi';
        is $res2->header('X-Path-Info')  , '/deeply';

        my $res3 = $call->( GET 'http://127.0.0.1/psgi-bin/404.psgi' );
        note $res3->content;
        is $res3->code, 404;

        my $res4 = $call->( GET 'http://127.0.0.1/psgi-bin/dead.psgi' );
        note $res4->content;
        is $res4->code, 500;
    },
    server => sub {
        my $port = shift;
        run_httpd($port);
    },
);

done_testing();

sub run_httpd {
    my $port = shift;

    my $tmpdir = $ENV{APACHE2_TMP_DIR} || File::Temp::tempdir( CLEANUP => 1 );

    mkpath( "$tmpdir/psgi-bin" );
    write_file("$tmpdir/psgi-bin/app.psgi", _render_psgi());
    mkpath( "$tmpdir/psgi-bin/deep" );
    write_file("$tmpdir/psgi-bin/deep/app.psgi", _render_psgi());
    write_file("$tmpdir/psgi-bin/dead.psgi", _render_dead_psgi());
    write_file("$tmpdir/httpd.conf", _render_conf($tmpdir, $port));

    exec "httpd -X -D FOREGROUND -f $tmpdir/httpd.conf";
}

sub write_file {
    my($path, $content) = @_;

    open my $out, ">", $path or die "$path: $!";
    print $out $content;
}

sub _render_psgi {
    return <<'EOF';
sub {
    my $env = shift;
    [200, [
        'Content-Type' => 'text/plain',
        'X-Script-Name' => $env->{SCRIPT_NAME},
        'X-Path-Info'   => $env->{PATH_INFO},
    ], ['OK']]
}
EOF
}

sub _render_dead_psgi {
    return <<'EOF';
die 'What's happen?';
EOF
}

sub _render_conf {
    my ($tmpdir, $port) = @_;
    <<"END";
LoadModule perl_module libexec/apache2/mod_perl.so
ServerRoot $tmpdir
DocumentRoot $tmpdir
PidFile $tmpdir/httpd.pid
LockFile $tmpdir/httpd.lock
ErrorLog $tmpdir/error_log
Listen $port

PerlModule Plack::Handler::Apache2::Registry;
<Location /psgi-bin>
SetHandler modperl
PerlHandler Plack::Handler::Apache2::Registry
</Location>
END
}