File: helpers-http-cache-headers.t

package info (click to toggle)
request-tracker5 5.0.7%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 80,216 kB
  • sloc: javascript: 191,898; perl: 87,146; sh: 1,412; makefile: 487; python: 37; php: 15
file content (126 lines) | stat: -rw-r--r-- 3,312 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use strict;
use warnings;

# trs: I'd write a quick t/web/caching-headers.t file which loops the available
#      endpoints checking for the right headers.

use File::Find;

BEGIN {
    # Ensure that the test and server processes use the same fixed time.
    use constant TIME => 1365175699;
    use Test::MockTime 'set_fixed_time';
    set_fixed_time(TIME);

    use RT::Test
        tests   => undef,
        config  => "use Test::MockTime 'set_fixed_time'; set_fixed_time(".TIME.");";
}

my ($base, $m) = RT::Test->started_ok;
ok $m->login, 'logged in';

my $docroot = join '/', qw(share html);

# files to exclude from testing headers
my @exclude = (
    'SpawnLinkedTicket', # results in redirect, expires header not expected
);

# find endpoints to loop over
my @endpoints = (
    "/NoAuth/css/elevator-light/squished-".("0"x32).".css",
    '/static/images/bpslogo.png',
);
find({
  wanted => sub {
    if ( -f $_ && $_ !~ m|autohandler$| ) {
      return if m{/\.[^/]+\.sw[op]$}; # vim swap files
      ( my $endpoint = $_ ) =~ s|^$docroot||;
      return if grep $endpoint =~ m{/$_$}, @exclude;
      push @endpoints, $endpoint;
    }
  },
  no_chdir => 1,
} => join '/', $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 $asset_id;
# Create an asset so requests to /Helpers/AssetUpdate can succeed.
diag "create an asset via the API";
{
    my $asset = RT::Asset->new( RT->SystemUser );
    my ($id, $txn, $msg) = $asset->Create(
        Catalog => 'General assets',
        Name    => 'test asset',
    );
    ok $id, 'created a asset #'. $id or diag "error: $msg";
    is $asset->Name, 'test asset', 'correct name';
    $asset_id = $id;
}

my $expected;
diag "set up expected date headers";
{

  # expected headers
  $expected = {
    Autocomplete => {
      'Cache-Control' => 'max-age=120, private',
      'Expires'       => 'Fri, 05 Apr 2013 15:30:19 GMT',
    },
    NoAuth      => {
      'Cache-Control' => 'max-age=2592000, public',
      'Expires'       => 'Sun, 05 May 2013 15:28:19 GMT',
    },
    default      => {
      'Cache-Control' => 'no-cache, max-age=0',
      'Expires'       => 'Fri, 05 Apr 2013 15:27:49 GMT',
    },
  };

}

foreach my $endpoint ( @endpoints ) {
  if ( $endpoint =~ m{/Helpers/AssetUpdate} ) {
    $m->get_ok( $endpoint . "?id=${asset_id}&Status=allocated" );
  }
  elsif ( $endpoint =~ m{/Helpers/Permalink} ) {
    $m->get_ok( $endpoint . "?URL=/" );
  }
  else {
    $m->get_ok( $endpoint . "?id=${ticket_id}&Status=open&Requestor=root" );
  }

  my $header_key = 'default';
  if ( $endpoint =~ m|Autocomplete| ) {
    $header_key =  'Autocomplete';
  } elsif ( $endpoint =~ m/NoAuth|static/ ) {
    $header_key =  'NoAuth';
  }
  my $headers = $expected->{$header_key};

  is(
      $m->res->header('Cache-Control') => $headers->{'Cache-Control'},
      'got expected Cache-Control header'
  );

  is(
    $m->res->header('Expires') => $headers->{'Expires'},
    'got expected Expires header'
  );
}

done_testing;