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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
use strict;
use warnings;
use Test::More 0.96;
use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
use Test::Fatal;
use Plack::Test;
use HTTP::Request::Common;
use Plack::Middleware::LogErrors;
# given an $app, apply middleware to mutate $env->{$key} to $value.
sub apply_mw
{
my ($app, %mutations) = @_;
sub {
my $env = shift;
@{$env}{keys %mutations} = values %mutations;
$app->($env);
};
}
# a boring app that prints to psgi.errors and psgix.logger
sub app
{
sub {
my $env = shift;
$env->{'psgix.logger'}->({
level => 'info',
message => 'PATH=' . $env->{PATH_INFO} . "\n",
});
$env->{'psgi.errors'}->print("oh noes!\n");
[ 200, [], [ 'hello' ] ];
};
}
subtest 'logger not something we know how to use (not a coderef)' => sub
{
like(
exception {
Plack::Middleware::LogErrors->wrap(app,
logger => bless({}, 'MyLogger'),
);
},
qr/^'logger' is not a coderef!/,
'bad logger parameter',
);
};
# this is easy to occur if the middleware is applied in the wrong order -
# instead of silently falling back to psgi.errors (which means the application
# was a no-op), we'd rather know that we screwed up - ergo die.
subtest 'no psgix.logger configured' => sub
{
my $app = Plack::Middleware::LogErrors->wrap(app);
my $psgi_errors;
$app = apply_mw(
$app,
'psgi.errors' => do { open my $io, '>>', \$psgi_errors; $io },
# no psgix.logger
);
test_psgi $app, sub {
my $res = shift->(GET '/hello');
is($psgi_errors, undef, 'we died before the app is called');
is($res->code, 500, 'response code');
like($res->content, qr/^no psgix\.logger in \$env; cannot map psgi\.errors to it!/, 'content');
};
};
subtest 'default case: psgix.logger configured; no logger override' => sub
{
my $app = Plack::Middleware::LogErrors->wrap(app);
my $psgi_errors;
my $psgix_log;
$app = apply_mw(
$app,
'psgi.errors' => do { open my $io, '>>', \$psgi_errors; $io },
'psgix.logger' => sub {
my $args = shift;
$psgix_log .= $args->{level} . ' -- ' . $args->{message};
}
);
test_psgi $app, sub {
my $res = shift->(GET '/hello');
is($psgi_errors, undef, 'original psgi.errors got no content');
is($psgix_log, "info -- PATH=/hello\nerror -- oh noes!\n", 'logger got log message and error');
is($res->code, 200, 'response code');
is($res->content, 'hello', 'content');
};
};
subtest 'psgix.logger configured, but we override error logger to something else' => sub
{
my $error_logger;
my $app = Plack::Middleware::LogErrors->wrap(app,
logger => sub {
my $args = shift;
$error_logger .= $args->{level} . ' -- ' . $args->{message};
}
);
my $psgi_errors;
my $psgix_log;
$app = apply_mw(
$app,
'psgi.errors' => do { open my $io, '>>', \$psgi_errors; $io },
'psgix.logger' => sub {
my $args = shift;
$psgix_log .= $args->{level} . ' -- ' . $args->{message};
}
);
test_psgi $app, sub {
my $res = shift->(GET '/hello');
is($psgi_errors, undef, 'original psgi.errors got no content');
is($psgix_log, "info -- PATH=/hello\n", 'psgix.logger got normal log message');
is($error_logger, "error -- oh noes!\n", 'custom error logger got error');
is($res->code, 200, 'response code');
is($res->content, 'hello', 'content');
};
};
done_testing;
|