File Coverage

blib/lib/Plack/Middleware/CookieMonster.pm
Criterion Covered Total %
statement 33 33 100.0
branch 5 6 83.3
condition 1 3 33.3
subroutine 7 7 100.0
pod 1 1 100.0
total 47 50 94.0


line stmt bran cond sub pod time code
1             package Plack::Middleware::CookieMonster;
2 1     1   96626 use strict;
  1         2  
  1         32  
3 1     1   4 use warnings;
  1         1  
  1         44  
4              
5             our $VERSION = '0.03';
6             $VERSION = eval $VERSION;
7              
8 1     1   5 use parent qw/ Plack::Middleware /;
  1         5  
  1         7  
9              
10 1     1   130 use Plack::Util::Accessor qw( cookie_names path );
  1         1  
  1         9  
11 1     1   588 use Plack::Request;
  1         29881  
  1         200  
12              
13              
14             sub call {
15 5     5 1 33773 my ( $self, $env ) = @_;
16              
17 5         19 my $res = $self->app->( $env );
18              
19 5 50 33     45405 if ( $env->{ 'plack.stacktrace.html' } && $res->[ 0 ] == 500 ) {
20 5         20 my @cookies = $self->_get_cookie_names( $env );
21 5         11 my $path = '';
22 5 100       14 if ( defined $self->path ) {
23 2         12 $path = sprintf 'path=%s; ', $self->path;
24             }
25 5         39 my $fmt = sprintf '%%s=deleted; %sExpires=Sat, 01-May-1971 04:30:01 GMT', $path;
26 5         10 foreach my $cookie ( @cookies ) {
27 5         4 push @{ $res->[ 1 ] }, 'Set-Cookie', sprintf $fmt, $cookie;
  5         22  
28             }
29             }
30              
31 5         34 return $res;
32             }
33              
34             sub _get_cookie_names {
35 15     15   4075 my ( $self, $env ) = @_;
36              
37 15         63 my $sent = Plack::Request->new( $env )->cookies;
38              
39 15 100       712 if ( my $cookie_names = $self->cookie_names ) {
40 10         58 return grep { $sent->{ $_ } } @$cookie_names;
  14         80  
41             }
42             else {
43 5         109 return keys %$sent;
44             }
45             }
46              
47             1;
48              
49             __END__