about summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2011-01-21 11:18:15 +0100
committerfranck cuny <franck@lumberjaph.net>2011-01-21 11:18:15 +0100
commit85173eb97566c5531f047098d0b0b6209b51c25d (patch)
tree1eb1158c2906a80005eb1e5f8d39e0d6338e77b7
parentChecking in changes prior to tagging of version 0.01. Changelog diff is: (diff)
downloadplack-middleware-etag-85173eb97566c5531f047098d0b0b6209b51c25d.tar.gz
add cache-control and tests
-rw-r--r--lib/Plack/Middleware/ETag.pm32
-rw-r--r--t/01_basic.t38
2 files changed, 68 insertions, 2 deletions
diff --git a/lib/Plack/Middleware/ETag.pm b/lib/Plack/Middleware/ETag.pm
index 90ae1e1..6adc512 100644
--- a/lib/Plack/Middleware/ETag.pm
+++ b/lib/Plack/Middleware/ETag.pm
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 use Digest::SHA;
 use Plack::Util;
-use Plack::Util::Accessor qw( file_etag );
+use Plack::Util::Accessor qw( file_etag cache_control);
 
 our $VERSION = '0.01';
 
@@ -19,9 +19,11 @@ sub call {
         sub {
             my $res = shift;
             my $headers = $res->[1];
-            return if ( !defined $res->[2] );#|| ref $res->[2] ne 'ARRAY' );
+            return if ( !defined $res->[2] );
             return if ( Plack::Util::header_exists( $headers, 'ETag' ) );
+
             my $etag;
+
             if ( Plack::Util::is_real_fh( $res->[2] ) ) {
 
                 my $file_attr = $self->file_etag || [qw/inode mtime size/];
@@ -49,11 +51,25 @@ sub call {
                 $etag = $sha->hexdigest;
             }
             Plack::Util::header_set( $headers, 'ETag', $etag );
+            $self->_set_cache_control($headers);
             return;
         }
     );
 }
 
+sub _set_cache_control {
+    my ( $self, $headers ) = @_;
+    return unless $self->cache_control;
+
+    if ( ref $self->cache_control && ref $self->cache_control eq 'ARRAY' ) {
+        Plack::Util::header_set( $headers, 'Cache-Control',
+            join( ', ', @{ $self->cache_control } ) );
+    }
+    else {
+        Plack::Util::header_set( $headers, 'Cache-Control', 'must-revalidate' );
+    }
+}
+
 1;
 __END__
 
@@ -90,6 +106,18 @@ If the content is a file handle, the ETag will be set using the inode, modified
 
     enable "Plack::Middleware::ETag", file_etag => [qw/size/];
 
+=item cache_control
+
+It's possible to add 'Cache-Control' header.
+
+    enable "Plack::Middleware::ETag", cache_control => 1;
+
+Will add "Cache-Control: must-revalidate" to the headers.
+
+    enable "Plack::Middleware::ETag", cache_control => [ 'must-revalidate', 'max-age=3600' ];
+
+Will add "Cache-Control: must-revalidate, max-age=3600" to the headers.
+
 =back
 
 =head1 AUTHOR
diff --git a/t/01_basic.t b/t/01_basic.t
index 60cdb66..368d5cc 100644
--- a/t/01_basic.t
+++ b/t/01_basic.t
@@ -38,6 +38,17 @@ my $file_handler = builder {
    sub {[200, ['Content-Type' => 'text/html', ], $fh]};
 };
 
+my $cache_control = builder {
+    enable "Plack::Middleware::ETag", cache_control => 1;
+    sub { [ '200', [ 'Content-Type' => 'text/html' ], $content ] };
+};
+
+my $cache_control_array = builder {
+    enable "Plack::Middleware::ETag",
+      cache_control => [ 'must-revalidate', 'max-age=3600', 'no-store' ];
+    sub { [ '200', [ 'Content-Type' => 'text/html' ], $content ] };
+};
+
 test_psgi
     app    => $handler,
     client => sub {
@@ -88,4 +99,31 @@ test_psgi
     }
 };
 
+test_psgi
+  app    => $cache_control,
+  client => sub {
+    my $cb = shift;
+    {
+        my $req = GET "http://localhost/";
+        my $res = $cb->($req);
+        ok $res->header('ETag');
+        is $res->header('ETag'),          $sha;
+        is $res->header('Cache-Control'), 'must-revalidate';
+    }
+  };
+
+test_psgi
+  app    => $cache_control_array,
+  client => sub {
+    my $cb = shift;
+    {
+        my $req = GET "http://localhost/";
+        my $res = $cb->($req);
+        ok $res->header('ETag');
+        is $res->header('ETag'), $sha;
+        is $res->header('Cache-Control'),
+          'must-revalidate, max-age=3600, no-store';
+    }
+  };
+
 done_testing;