RT 4.0.22
[freeside.git] / rt / t / web / helpers-http-cache-headers.t
1 use strict;
2 use warnings;
3
4 # trs: I'd write a quick t/web/caching-headers.t file which loops the available
5 #      endpoints checking for the right headers.
6
7 use File::Find;
8
9 BEGIN {
10     # Ensure that the test and server processes use the same fixed time.
11     use constant TIME => 1365175699;
12     use Test::MockTime 'set_fixed_time';
13     set_fixed_time(TIME);
14
15     use RT::Test
16         tests   => undef,
17         config  => "use Test::MockTime 'set_fixed_time'; set_fixed_time(".TIME.");";
18 }
19
20 my ($base, $m) = RT::Test->started_ok;
21 ok $m->login, 'logged in';
22
23 my $docroot = join '/', qw(share html);
24
25 # find endpoints to loop over
26 my @endpoints = ('/NoAuth/css/print.css');
27 find({
28   wanted => sub {
29     if ( -f $_ && $_ !~ m|autohandler$| ) {
30       ( my $endpoint = $_ ) =~ s|^$docroot||;
31       push @endpoints, $endpoint;
32     }
33   },
34   no_chdir => 1,
35 } => join '/', $docroot => 'Helpers');
36
37 my $ticket_id;
38 diag "create a ticket via the API";
39 {
40     my $ticket = RT::Ticket->new( RT->SystemUser );
41     my ($id, $txn, $msg) = $ticket->Create(
42         Queue => 'General',
43         Subject => 'test ticket',
44     );
45     ok $id, 'created a ticket #'. $id or diag "error: $msg";
46     is $ticket->Subject, 'test ticket', 'correct subject';
47     $ticket_id = $id;
48 }
49
50
51 my $expected;
52 diag "set up expected date headers";
53 {
54
55   # expected headers
56   $expected = {
57     Autocomplete => {
58       'Cache-Control' => 'max-age=120, private',
59       'Expires'       => 'Fri, 5 Apr 2013 15:30:19 GMT',
60     },
61     NoAuth      => {
62       'Cache-Control' => 'max-age=2592000, public',
63       'Expires'       => 'Sun, 5 May 2013 15:28:19 GMT',
64     },
65     default      => {
66       'Cache-Control' => 'no-cache',
67       'Expires'       => 'Fri, 5 Apr 2013 15:28:19 GMT',
68     },
69   };
70
71 }
72
73 foreach my $endpoint ( @endpoints ) {
74   $m->get_ok( $endpoint . "?id=${ticket_id}&Status=open&Requestor=root" );
75
76   my $header_key = 'default';
77   if ( $endpoint =~ m|Autocomplete| ) {
78     $header_key =  'Autocomplete';
79   } elsif ( $endpoint =~ m|NoAuth| ) {
80     $header_key =  'NoAuth';
81   }
82   my $headers = $expected->{$header_key};
83
84   is(
85       $m->res->header('Cache-Control') => $headers->{'Cache-Control'},
86       'got expected Cache-Control header'
87   );
88
89   is(
90     $m->res->header('Expires') => $headers->{'Expires'},
91     'got expected Expires header'
92   );
93 }
94
95 undef $m;
96 done_testing;