forked from nginx/nginx-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_cache_control.t
274 lines (197 loc) · 7.26 KB
/
proxy_cache_control.t
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/perl
# (C) Sergey Kandaurov
# (C) Nginx, Inc.
# Tests for proxy headers Expires / Cache-Control / X-Accel-Expires.
###############################################################################
use warnings;
use strict;
use Test::More;
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
use Test::Nginx;
###############################################################################
select STDERR; $| = 1;
select STDOUT; $| = 1;
my $t = Test::Nginx->new()->has(qw/http proxy cache rewrite/)->plan(19);
$t->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
proxy_cache_path %%TESTDIR%%/cache keys_zone=NAME:1m;
server {
listen 127.0.0.1:8080;
server_name localhost;
add_header X-Cache-Status $upstream_cache_status;
location / {
proxy_pass http://127.0.0.1:8081;
proxy_cache NAME;
proxy_cache_background_update on;
}
location /ignore {
proxy_pass http://127.0.0.1:8081;
proxy_cache NAME;
proxy_ignore_headers Cache-Control Expires;
proxy_ignore_headers X-Accel-Expires;
}
}
server {
listen 127.0.0.1:8081;
server_name localhost;
location /expires {
add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
return 204;
}
location /cache-control {
add_header Cache-Control max-age=60;
return 204;
}
location /x-accel-expires {
add_header X-Accel-Expires 60;
return 204;
}
location /x-accel-expires-at {
add_header X-Accel-Expires @60;
return 204;
}
location /x-accel-expires-duplicate {
add_header X-Accel-Expires 60;
add_header X-Accel-Expires 0;
return 204;
}
location /ignore {
add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
add_header Cache-Control max-age=60;
add_header X-Accel-Expires 60;
return 204;
}
location /cache-control-before-expires {
add_header Cache-Control max-age=60;
add_header Expires "Thu, 01 Jan 1970 00:00:01 GMT";
return 204;
}
location /cache-control-after-expires {
add_header Expires "Thu, 01 Jan 1970 00:00:01 GMT";
add_header Cache-Control max-age=60;
return 204;
}
location /cache-control-no-cache-before-expires {
add_header Cache-Control no-cache;
add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
return 204;
}
location /cache-control-no-cache-after-expires {
add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
add_header Cache-Control no-cache;
return 204;
}
location /x-accel-expires-before {
add_header X-Accel-Expires 60;
add_header Expires "Thu, 01 Jan 1970 00:00:01 GMT";
add_header Cache-Control no-cache;
return 204;
}
location /x-accel-expires-after {
add_header Expires "Thu, 01 Jan 1970 00:00:01 GMT";
add_header Cache-Control no-cache;
add_header X-Accel-Expires 60;
return 204;
}
location /x-accel-expires-0-before {
add_header X-Accel-Expires 0;
add_header Cache-Control max-age=60;
add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
return 204;
}
location /x-accel-expires-0-after {
add_header Cache-Control max-age=60;
add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
add_header X-Accel-Expires 0;
return 204;
}
location /cache-control-no-cache-one {
add_header Cache-Control "no-cache, max-age=60";
return 204;
}
location /cache-control-no-cache-multi {
add_header Cache-Control no-cache;
add_header Cache-Control max-age=60;
return 204;
}
location /extension-before-x-accel-expires {
add_header Cache-Control stale-while-revalidate=2145902155;
add_header X-Accel-Expires @1;
return 204;
}
location /extension-after-x-accel-expires {
add_header X-Accel-Expires @1;
add_header Cache-Control stale-while-revalidate=2145902155;
return 204;
}
location /set-cookie {
add_header Set-Cookie foo;
add_header Expires "Thu, 01 Jan 1970 00:00:01 GMT";
add_header Cache-control max-age=60;
return 204;
}
}
}
EOF
$t->run();
###############################################################################
# cache headers work
like(get('/expires'), qr/HIT/, 'expires');
like(get('/cache-control'), qr/HIT/, 'cache-control');
like(get('/x-accel-expires'), qr/HIT/, 'x-accel-expires');
like(get('/x-accel-expires-at'), qr/EXPIRED/, 'x-accel-expires at');
TODO: {
local $TODO = 'not yet' unless $t->has_version('1.23.0');
# the second header to disable cache is duplicate and ignored
like(get('/x-accel-expires-duplicate'), qr/HIT/, 'x-accel-expires duplicate');
}
# with cache headers ignored, the response will be fresh
like(get('/ignore'), qr/MISS/, 'cache headers ignored');
# Cache-Control is preferred over Expires
like(get('/cache-control-before-expires'), qr/HIT/,
'cache-control before expires');
TODO: {
local $TODO = 'not yet' unless $t->has_version('1.23.0');
like(get('/cache-control-after-expires'), qr/HIT/,
'cache-control after expires');
}
like(get('/cache-control-no-cache-before-expires'), qr/MISS/,
'cache-control no-cache before expires');
like(get('/cache-control-no-cache-after-expires'), qr/MISS/,
'cache-control no-cache after expires');
# X-Accel-Expires is preferred over both Cache-Control and Expires
like(get('/x-accel-expires-before'), qr/HIT/, 'x-accel-expires before');
TODO: {
local $TODO = 'not yet' unless $t->has_version('1.23.0');
like(get('/x-accel-expires-after'), qr/HIT/, 'x-accel-expires after');
}
like(get('/x-accel-expires-0-before'), qr/MISS/, 'x-accel-expires 0 before');
like(get('/x-accel-expires-0-after'), qr/MISS/, 'x-accel-expires 0 after');
# "Cache-Control: no-cache" disables caching, no matter of "max-age"
like(get('/cache-control-no-cache-one'), qr/MISS/,
'cache-control no-cache');
like(get('/cache-control-no-cache-multi'), qr/MISS/,
'cache-control no-cache multi line');
# Cache-Control extensions are preserved with X-Accel-Expires
like(get('/extension-before-x-accel-expires'),
qr/STALE/, 'cache-control extensions before x-accel-expires');
TODO: {
local $TODO = 'not yet' unless $t->has_version('1.23.0');
like(get('/extension-after-x-accel-expires'),
qr/STALE/, 'cache-control extensions after x-accel-expires');
}
# Set-Cookie is considered when caching with Cache-Control
like(get('/set-cookie'), qr/MISS/, 'set-cookie not cached');
###############################################################################
sub get {
my ($uri) = @_;
http_get($uri);
http_get($uri);
}
###############################################################################