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   1223 use strict;
  1         1  
  1         28  
4 1     1   3 use warnings;
  1         1  
  1         29  
5              
6 1     1   4 use base 'Mojolicious::Plugin';
  1         2  
  1         88  
7              
8 1     1   4 use Mojo::ByteStream;
  1         1  
  1         165  
9              
10             sub register {
11 1     1 1 50 my ($self, $app) = @_;
12              
13             $app->hook(
14             after_dispatch => sub {
15 3     3   24502 my $self = shift;
16              
17 3 100       10 return unless $self->req->method eq 'GET';
18              
19 2         26 my $body = $self->res->body;
20 2 50       39 return unless defined $body;
21              
22 2         14 my $our_etag = Mojo::ByteStream->new($body)->md5_sum;
23 2         29 $self->res->headers->header('ETag' => $our_etag);
24              
25 2         70 my $browser_etag = $self->req->headers->header('If-None-Match');
26 2 100 66     40 return unless $browser_etag && $browser_etag eq $our_etag;
27              
28 1         9 $self->res->code(304);
29 1         10 $self->res->body('');
30             }
31 1         10 );
32             }
33              
34             1;