File Coverage

blib/lib/Bootylicious/Plugin/HttpCache.pm
Criterion Covered Total %
statement 24 24 100.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 6 6 100.0
pod 1 1 100.0
total 38 40 95.0


line stmt bran cond sub pod time code
1             package Bootylicious::Plugin::HttpCache;
2              
3 1     1   12663 use strict;
  1         3  
  1         160  
4 1     1   8 use warnings;
  1         2  
  1         60  
5              
6 1     1   7 use base 'Mojolicious::Plugin';
  1         2  
  1         166  
7              
8 1     1   9 use Mojo::ByteStream;
  1         2  
  1         364  
9              
10             sub register {
11 1     1 1 64 my ($self, $app) = @_;
12              
13             $app->hook(
14             after_dispatch => sub {
15 3     3   94579 my $self = shift;
16              
17 3 100       21 return unless $self->req->method eq 'GET';
18              
19 2         68 my $body = $self->res->body;
20 2 50       77 return unless defined $body;
21              
22 2         23 my $our_etag = Mojo::ByteStream->new($body)->md5_sum;
23 2         74 $self->res->headers->header('ETag' => $our_etag);
24              
25 2         111 my $browser_etag = $self->req->headers->header('If-None-Match');
26 2 100 66     144 return unless $browser_etag && $browser_etag eq $our_etag;
27              
28 1         21 $self->res->code(304);
29 1         18 $self->res->body('');
30             }
31 1         16 );
32             }
33              
34             1;