about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--lib/Plack/Middleware/ETag.pm4
-rw-r--r--t/01_basic.t23
2 files changed, 23 insertions, 4 deletions
diff --git a/lib/Plack/Middleware/ETag.pm b/lib/Plack/Middleware/ETag.pm
index fbee411..0671ce9 100644
--- a/lib/Plack/Middleware/ETag.pm
+++ b/lib/Plack/Middleware/ETag.pm
@@ -21,7 +21,7 @@ sub call {
             return unless defined $res->[2];
             return
                 if ( Plack::Util::header_exists( $headers, 'ETag' )
-                || $env->{REQUEST_METHOD} ne 'GET' );
+                || $env->{REQUEST_METHOD} !~ /^(GET|HEAD)$/ );
             my $sha     = Digest::SHA->new;
             my $content = $res->[2];
             $sha->add(@$content);
@@ -49,7 +49,7 @@ Plack::Middleware::ETag - Adds automatically an ETag header.
 
 =head1 DESCRIPTION
 
-Plack::Middleware::ETag adds automatically an ETag header.
+Plack::Middleware::ETag adds automatically an ETag header. You may want to use it with C<Plack::Middleware::ConditionalGET>.
 
 =head1 AUTHOR
 
diff --git a/t/01_basic.t b/t/01_basic.t
index 0bc49c6..8564ece 100644
--- a/t/01_basic.t
+++ b/t/01_basic.t
@@ -9,6 +9,7 @@ use Plack::Builder;
 use HTTP::Request::Common;
 
 my $content = [qw/hello world/];
+my $sha = Digest::SHA->new->add(@$content)->hexdigest;
 
 my $handler = builder {
     enable "Plack::Middleware::ETag";
@@ -25,6 +26,12 @@ my $second_handler = builder {
     };
 };
 
+my $unmodified_handler = builder {
+    enable "Plack::Middleware::ConditionalGET";
+    enable "Plack::Middleware::ETag";
+    sub { [ '200', [ 'Content-Type' => 'text/html' ], $content ] };
+};
+
 test_psgi
     app    => $handler,
     client => sub {
@@ -33,8 +40,7 @@ test_psgi
         my $req = GET "http://localhost/";
         my $res = $cb->($req);
         ok $res->header('ETag');
-        my $sha = Digest::SHA->new->add(@$content);
-        is $res->header('ETag'), $sha->hexdigest;
+        is $res->header('ETag'), $sha;
     }
 };
 
@@ -50,4 +56,17 @@ test_psgi
     }
 };
 
+test_psgi
+    app    => $unmodified_handler,
+    client => sub {
+    my $cb = shift;
+    {
+        my $req = GET "http://localhost/", 'If-None-Match' => $sha;
+        my $res = $cb->($req);
+        ok $res->header('ETag');
+	is $res->code, 304;
+	ok !$res->content;
+    }
+};
+
 done_testing;