[Rt-commit] rt branch, 4.0/helpers-http-cache-headers, created. rt-4.0.8-309-gc30ce3f

Todd Wade todd at bestpractical.com
Tue Jan 8 10:54:07 EST 2013


The branch, 4.0/helpers-http-cache-headers has been created
        at  c30ce3feff621365f7570bc55e52c756ae78b111 (commit)

- Log -----------------------------------------------------------------
commit a0a1763823640ad0b17ea783ea09b48d40d7420d
Author: Todd Wade <todd at bestpractical.com>
Date:   Fri Dec 28 16:53:07 2012 -0500

    set cache related headers on helper endpoints
    
    * 1-2 min cache for share/html/Helpers/Autocomplete/*
    * no cache for share/html/Helpers/*
    
    The test functions but needs updated before it is production worthy. Currently
    it will only pass if the entire test runs in the same second.

diff --git a/share/html/Helpers/Autocomplete/autohandler b/share/html/Helpers/Autocomplete/autohandler
new file mode 100644
index 0000000..492bfd9
--- /dev/null
+++ b/share/html/Helpers/Autocomplete/autohandler
@@ -0,0 +1,59 @@
+%# BEGIN BPS TAGGED BLOCK {{{
+%#
+%# COPYRIGHT:
+%#
+%# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
+%#                                          <sales at bestpractical.com>
+%#
+%# (Except where explicitly superseded by other copyright notices)
+%#
+%#
+%# LICENSE:
+%#
+%# This work is made available to you under the terms of Version 2 of
+%# the GNU General Public License. A copy of that license should have
+%# been provided with this software, but in any event can be snarfed
+%# from www.gnu.org.
+%#
+%# This work is distributed in the hope that it will be useful, but
+%# WITHOUT ANY WARRANTY; without even the implied warranty of
+%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+%# General Public License for more details.
+%#
+%# You should have received a copy of the GNU General Public License
+%# along with this program; if not, write to the Free Software
+%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+%# 02110-1301 or visit their web page on the internet at
+%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+%#
+%#
+%# CONTRIBUTION SUBMISSION POLICY:
+%#
+%# (The following paragraph is not intended to limit the rights granted
+%# to you to modify and distribute this software under the terms of
+%# the GNU General Public License and is only of importance to you if
+%# you choose to contribute your changes and enhancements to the
+%# community by submitting them to Best Practical Solutions, LLC.)
+%#
+%# By intentionally submitting any modifications, corrections or
+%# derivatives to this work, or any other work intended for use with
+%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+%# you are the copyright holder for those contributions and you grant
+%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+%# royalty-free, perpetual, license to use, copy, create derivative
+%# works based on those contributions, and sublicense and distribute
+%# those contributions and any derivatives thereof.
+%#
+%# END BPS TAGGED BLOCK }}}
+<%init>
+  $r->headers_out->{'Cache-Control'} = 'max-age=120, private';
+
+  my $expires = RT::Date->new(RT->SystemUser);
+  $expires->SetToNow;
+  $expires->AddSeconds(2*60); 
+
+  $r->headers_out->{'Expires'} = $expires->RFC2616;
+
+  $m->call_next;
+</%init>
+
diff --git a/share/html/Helpers/autohandler b/share/html/Helpers/autohandler
new file mode 100644
index 0000000..4b5c89d
--- /dev/null
+++ b/share/html/Helpers/autohandler
@@ -0,0 +1,57 @@
+%# BEGIN BPS TAGGED BLOCK {{{
+%#
+%# COPYRIGHT:
+%#
+%# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
+%#                                          <sales at bestpractical.com>
+%#
+%# (Except where explicitly superseded by other copyright notices)
+%#
+%#
+%# LICENSE:
+%#
+%# This work is made available to you under the terms of Version 2 of
+%# the GNU General Public License. A copy of that license should have
+%# been provided with this software, but in any event can be snarfed
+%# from www.gnu.org.
+%#
+%# This work is distributed in the hope that it will be useful, but
+%# WITHOUT ANY WARRANTY; without even the implied warranty of
+%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+%# General Public License for more details.
+%#
+%# You should have received a copy of the GNU General Public License
+%# along with this program; if not, write to the Free Software
+%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+%# 02110-1301 or visit their web page on the internet at
+%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+%#
+%#
+%# CONTRIBUTION SUBMISSION POLICY:
+%#
+%# (The following paragraph is not intended to limit the rights granted
+%# to you to modify and distribute this software under the terms of
+%# the GNU General Public License and is only of importance to you if
+%# you choose to contribute your changes and enhancements to the
+%# community by submitting them to Best Practical Solutions, LLC.)
+%#
+%# By intentionally submitting any modifications, corrections or
+%# derivatives to this work, or any other work intended for use with
+%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+%# you are the copyright holder for those contributions and you grant
+%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+%# royalty-free, perpetual, license to use, copy, create derivative
+%# works based on those contributions, and sublicense and distribute
+%# those contributions and any derivatives thereof.
+%#
+%# END BPS TAGGED BLOCK }}}
+<%init>
+  $r->headers_out->{'Cache-Control'} = 'no-cache';
+
+  my $expires = RT::Date->new(RT->SystemUser);
+  $expires->SetToNow;
+
+  $r->headers_out->{'Expires'} = $expires->RFC2616;
+
+  $m->call_next;
+</%init>
diff --git a/t/web/helpers-http-cache-headers.t b/t/web/helpers-http-cache-headers.t
new file mode 100644
index 0000000..a3c43a6
--- /dev/null
+++ b/t/web/helpers-http-cache-headers.t
@@ -0,0 +1,81 @@
+use strict;
+use warnings;
+use utf8;
+
+# trs: I'd write a quick t/web/caching-headers.t file which loops the available
+#      endpoints checking for the right headers.
+
+use File::Spec;
+use File::Find;
+
+use Data::Dumper;
+
+use RT::Test tests => undef;
+
+my ($base, $m) = RT::Test->started_ok;
+ok $m->login, 'logged in';
+
+my $docroot = File::Spec->catfile( $RT::BasePath => qw(share html) );
+
+# find endpoints to loop over
+my @endpoints;
+find({
+  wanted => sub {
+    if ( -f $_ && $_ !~ m|autohandler$| ) {
+      ( my $endpoint = $_ ) =~ s|^$docroot||;
+      push @endpoints, $endpoint;
+    }
+  },
+  no_chdir => 1,
+} => File::Spec->catfile( $docroot => 'Helpers' ) );
+
+my $ticket_id;
+diag "create a ticket via the API";
+{
+    my $ticket = RT::Ticket->new( RT->SystemUser );
+    my ($id, $txn, $msg) = $ticket->Create(
+        Queue => 'General',
+        Subject => 'test ticket',
+    );
+    ok $id, 'created a ticket #'. $id or diag "error: $msg";
+    is $ticket->Subject, 'test ticket', 'correct subject';
+    $ticket_id = $id;
+}
+
+
+my $expected;
+diag "set up expected date headers";
+{
+  my $autocomplete = RT::Date->new(RT->SystemUser);
+  $autocomplete->SetToNow;
+  $autocomplete->AddSeconds(2*60); 
+
+  my $default = RT::Date->new(RT->SystemUser);
+  $default->SetToNow;
+
+  # expected headers
+  $expected = {
+    Autocomplete => {
+      'Cache-Control' => 'max-age=120, private',
+      'Expires'       => $autocomplete->RFC2616,
+    },
+    default      => {
+      'Cache-Control' => 'no-cache',
+      'Expires'       => $default->RFC2616,
+    },
+  };
+}
+
+foreach my $endpoint ( @endpoints ) {
+  $m->get_ok( $endpoint . "?id=${ticket_id}&Status=open&Requestor=root" );
+
+  my $header_key = $endpoint =~ m|Autocomplete| ? 'Autocomplete' : 'default';
+  my $headers = $expected->{$header_key};
+
+  is( $m->res->header('Cache-Control') => $headers->{'Cache-Control'} );
+  is( $m->res->header('Expires') => $headers->{'Expires'} );
+
+}
+
+undef $m;
+done_testing;

commit 121172fa9364d2b6918b67ab290c63d897dda269
Author: Todd Wade <todd at bestpractical.com>
Date:   Thu Jan 3 10:07:01 2013 -0500

    move actual header modification in to RT::Interface::Web

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 3295c0d..d26b2a4 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -871,6 +871,28 @@ sub Redirect {
     $HTML::Mason::Commands::m->abort;
 }
 
+=head2 CacheControlExpiresHeaders
+
+set both Cache-Control and Expires http headers
+
+=cut
+
+sub CacheControlExpiresHeaders {
+    my %args = @_;
+
+    my $CacheControl = $args{Seconds}
+        ? sprintf "max-age=%d, private", $args{Seconds}
+        : 'no-cache'
+    ;
+    $HTML::Mason::Commands::r->headers_out->{'Cache-Control'} = $CacheControl;
+
+    my $expires = RT::Date->new(RT->SystemUser);
+    $expires->SetToNow;
+    $expires->AddSeconds( $args{Seconds} ) if $args{Seconds};
+
+    $HTML::Mason::Commands::r->headers_out->{'Expires'} = $expires->RFC2616;
+}
+
 =head2 StaticFileHeaders 
 
 Send the browser a few headers to try to get it to (somewhat agressively)
diff --git a/share/html/Helpers/Autocomplete/autohandler b/share/html/Helpers/Autocomplete/autohandler
index 492bfd9..f1ea709 100644
--- a/share/html/Helpers/Autocomplete/autohandler
+++ b/share/html/Helpers/Autocomplete/autohandler
@@ -46,13 +46,7 @@
 %#
 %# END BPS TAGGED BLOCK }}}
 <%init>
-  $r->headers_out->{'Cache-Control'} = 'max-age=120, private';
-
-  my $expires = RT::Date->new(RT->SystemUser);
-  $expires->SetToNow;
-  $expires->AddSeconds(2*60); 
-
-  $r->headers_out->{'Expires'} = $expires->RFC2616;
+  RT::Interface::Web::CacheControlExpiresHeaders( Seconds => 2 * 60 );
 
   $m->call_next;
 </%init>
diff --git a/share/html/Helpers/autohandler b/share/html/Helpers/autohandler
index 4b5c89d..5bd9f55 100644
--- a/share/html/Helpers/autohandler
+++ b/share/html/Helpers/autohandler
@@ -46,12 +46,7 @@
 %#
 %# END BPS TAGGED BLOCK }}}
 <%init>
-  $r->headers_out->{'Cache-Control'} = 'no-cache';
-
-  my $expires = RT::Date->new(RT->SystemUser);
-  $expires->SetToNow;
-
-  $r->headers_out->{'Expires'} = $expires->RFC2616;
+  RT::Interface::Web::CacheControlExpiresHeaders();
 
   $m->call_next;
 </%init>

commit b81b6d7531908c01ec47dd35ef1254701735571d
Author: Todd Wade <todd at bestpractical.com>
Date:   Thu Jan 3 13:58:52 2013 -0500

    - ditch File::Spec and $RT::BasePath
      * chmrr: The test shouldn't prepend $RT::BasePath, as that's the install
               location, and you can run tests without installing.
      * chmrr: File::Spec->catfile is also of limited utility; RT only runs on
               places whose directory separator is "/"
    
    - give a 5 second window for the tests to pass so they don't fail if not ran
      within the same second

diff --git a/t/web/helpers-http-cache-headers.t b/t/web/helpers-http-cache-headers.t
index a3c43a6..fb4395f 100644
--- a/t/web/helpers-http-cache-headers.t
+++ b/t/web/helpers-http-cache-headers.t
@@ -5,17 +5,14 @@ use utf8;
 # trs: I'd write a quick t/web/caching-headers.t file which loops the available
 #      endpoints checking for the right headers.
 
-use File::Spec;
 use File::Find;
 
-use Data::Dumper;
-
 use RT::Test tests => undef;
 
 my ($base, $m) = RT::Test->started_ok;
 ok $m->login, 'logged in';
 
-my $docroot = File::Spec->catfile( $RT::BasePath => qw(share html) );
+my $docroot = join '/', qw(share html);
 
 # find endpoints to loop over
 my @endpoints;
@@ -27,7 +24,7 @@ find({
     }
   },
   no_chdir => 1,
-} => File::Spec->catfile( $docroot => 'Helpers' ) );
+} => join '/', $docroot => 'Helpers');
 
 my $ticket_id;
 diag "create a ticket via the API";
@@ -57,11 +54,11 @@ diag "set up expected date headers";
   $expected = {
     Autocomplete => {
       'Cache-Control' => 'max-age=120, private',
-      'Expires'       => $autocomplete->RFC2616,
+      'Expires'       => $autocomplete,
     },
     default      => {
       'Cache-Control' => 'no-cache',
-      'Expires'       => $default->RFC2616,
+      'Expires'       => $default,
     },
   };
 }
@@ -72,8 +69,27 @@ foreach my $endpoint ( @endpoints ) {
   my $header_key = $endpoint =~ m|Autocomplete| ? 'Autocomplete' : 'default';
   my $headers = $expected->{$header_key};
 
-  is( $m->res->header('Cache-Control') => $headers->{'Cache-Control'} );
-  is( $m->res->header('Expires') => $headers->{'Expires'} );
+  is(
+      $m->res->header('Cache-Control') => $headers->{'Cache-Control'},
+      'got expected Cache-Control header'
+  );
+
+#  tests only pass for this if test script runs in the same second
+#  is( $m->res->header('Expires') => $headers->{'Expires'} );
+
+  # parse header in to RT::Date object
+  my $date = RT::Date->new(RT->SystemUser);
+  $date->Set(
+      Format   => 'unknown',
+      Timezone => 'GMT',
+      Value    => $m->res->header('Expires'),
+  );
+
+  # date/time in header cannot be less than expected date
+  cmp_ok( $date->Unix, '>=', $headers->{'Expires'}->Unix, '>= expected time' );
+
+  # date/time in header cannot be more than 5 seconds past expected date
+  cmp_ok( $date->Unix, '<', $headers->{'Expires'}->Unix + 2, '< expected time+5' );
 
 }
 

commit c30ce3feff621365f7570bc55e52c756ae78b111
Author: Todd Wade <todd at bestpractical.com>
Date:   Thu Jan 3 14:07:01 2013 -0500

    use the value that matches the comments/tests

diff --git a/t/web/helpers-http-cache-headers.t b/t/web/helpers-http-cache-headers.t
index fb4395f..7988a77 100644
--- a/t/web/helpers-http-cache-headers.t
+++ b/t/web/helpers-http-cache-headers.t
@@ -89,7 +89,7 @@ foreach my $endpoint ( @endpoints ) {
   cmp_ok( $date->Unix, '>=', $headers->{'Expires'}->Unix, '>= expected time' );
 
   # date/time in header cannot be more than 5 seconds past expected date
-  cmp_ok( $date->Unix, '<', $headers->{'Expires'}->Unix + 2, '< expected time+5' );
+  cmp_ok( $date->Unix, '<', $headers->{'Expires'}->Unix + 5, '< expected time+5' );
 
 }
 

-----------------------------------------------------------------------


More information about the Rt-commit mailing list