From: Andreas Marienborg Date: Thu, 28 Feb 2013 13:59:54 +0000 (+0700) Subject: Add test for logging via psgi env X-Git-Tag: 5.90030~19^2~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=2d5279cd7e404e0c4f6d4fc985414f8d0cd9022b Add test for logging via psgi env --- diff --git a/t/lib/TestApp/Controller/Log.pm b/t/lib/TestApp/Controller/Log.pm new file mode 100644 index 0000000..1a8cf0a --- /dev/null +++ b/t/lib/TestApp/Controller/Log.pm @@ -0,0 +1,14 @@ +package TestApp::Controller::Log; + +use strict; +use base 'Catalyst::Controller'; + +sub debug :Local { + my ( $self, $c ) = @_; + $c->log->debug('debug'); + $c->res->body( 'logged' ); +} + + +1; + diff --git a/t/psgi-log.t b/t/psgi-log.t new file mode 100644 index 0000000..ce52bab --- /dev/null +++ b/t/psgi-log.t @@ -0,0 +1,88 @@ +use strict; +use warnings; +no warnings 'once'; +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Test::More; + +use File::Spec; +use File::Temp qw/ tempdir /; + +use TestApp; + +use Plack::Builder; +use Plack::Test; +use HTTP::Request::Common; + +{ + package MockHandle; + use Moose; + + has 'log' => (is => 'ro', isa => 'ArrayRef', traits => ['Array'], default => sub { [] }, + handles => { + 'logs' => 'elements', + 'print' => 'push', + } + ); + + no Moose; +} + + +subtest "psgi.errors" => sub{ + + my $handle = MockHandle->new(); + my $app = builder { + + enable sub { + my $app = shift; + sub { + my $env = shift; + $env->{'psgi.errors'} = $handle; + my $res = $app->($env); + return $res; + }; + }; + TestApp->psgi_app; + }; + + + test_psgi $app, sub { + my $cb = shift; + my $res = $cb->(GET "/log/debug"); + my @logs = $handle->logs; + is(scalar(@logs), 1, "one event output"); + like($logs[0], qr/debug$/, "event matches test data"); + }; +}; + +subtest "psgix.logger" => sub { + + my @logs; + my $logger = sub { + push(@logs, @_); + }; + my $app = builder { + enable sub { + my $app = shift; + sub { + my $env = shift; + $env->{'psgix.logger'} = $logger; + $app->($env); + }; + }; + TestApp->psgi_app; + }; + + test_psgi $app, sub { + my $cb = shift; + my $res = $cb->(GET "/log/debug"); + is(scalar(@logs), 1, "one event logged"); + is_deeply($logs[0], { level => 'debug', message => "debug" }, "right stuff"); + }; +}; + + + +done_testing;