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
127
128
129
|
use strict;
use warnings;
use Test::More;
use Digest::SHA;
use Plack::Test;
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";
sub { [ '200', [ 'Content-Type' => 'text/html' ], $content ] };
};
my $second_handler = builder {
enable "Plack::Middleware::ETag";
sub {
[
'200', [ 'Content-Type' => 'text/html', 'ETag' => '123' ],
$content
];
};
};
my $unmodified_handler = builder {
enable "Plack::Middleware::ConditionalGET";
enable "Plack::Middleware::ETag";
sub { [ '200', [ 'Content-Type' => 'text/html' ], $content ] };
};
my $file_handler = builder {
enable "Plack::Middleware::ETag";
open my $fh, 'README.md';
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 {
my $cb = shift;
{
my $req = GET "http://localhost/";
my $res = $cb->($req);
ok $res->header('ETag');
is $res->header('ETag'), $sha;
}
};
test_psgi
app => $second_handler,
client => sub {
my $cb = shift;
{
my $req = GET "http://localhost/";
my $res = $cb->($req);
ok $res->header('ETag');
is $res->header('ETag'), '123';
}
};
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;
}
};
test_psgi
app => $file_handler,
client => sub {
my $cb = shift;
{
my $req = GET "http://localhost/";
my $res = $cb->($req);
ok $res->header('ETag');
is $res->code, 200;
ok $res->content;
}
};
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;
|