comparison proxy_cache_control.t @ 1774:5a625ce0de34

Tests: added proxy cache tests with upstream cache headers.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 13 Jun 2022 15:13:30 +0400
parents
children a095b971fbcc
comparison
equal deleted inserted replaced
1773:3f9b25f36e19 1774:5a625ce0de34
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for proxy headers Expires / Cache-Control / X-Accel-Expires.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http proxy cache rewrite/)->plan(19);
26
27 $t->write_file_expand('nginx.conf', <<'EOF');
28
29 %%TEST_GLOBALS%%
30
31 daemon off;
32
33 events {
34 }
35
36 http {
37 %%TEST_GLOBALS_HTTP%%
38
39 proxy_cache_path %%TESTDIR%%/cache keys_zone=NAME:1m;
40
41 server {
42 listen 127.0.0.1:8080;
43 server_name localhost;
44
45 add_header X-Cache-Status $upstream_cache_status;
46
47 location / {
48 proxy_pass http://127.0.0.1:8081;
49 proxy_cache NAME;
50
51 proxy_cache_background_update on;
52 }
53
54 location /ignore {
55 proxy_pass http://127.0.0.1:8081;
56 proxy_cache NAME;
57
58 proxy_ignore_headers Cache-Control Expires;
59 proxy_ignore_headers X-Accel-Expires;
60 }
61 }
62
63 server {
64 listen 127.0.0.1:8081;
65 server_name localhost;
66
67 location /expires {
68 add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
69 return 204;
70 }
71
72 location /cache-control {
73 add_header Cache-Control max-age=60;
74 return 204;
75 }
76
77 location /x-accel-expires {
78 add_header X-Accel-Expires 60;
79 return 204;
80 }
81
82 location /x-accel-expires-at {
83 add_header X-Accel-Expires @60;
84 return 204;
85 }
86
87 location /x-accel-expires-duplicate {
88 add_header X-Accel-Expires 60;
89 add_header X-Accel-Expires 0;
90 return 204;
91 }
92
93 location /ignore {
94 add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
95 add_header Cache-Control max-age=60;
96 add_header X-Accel-Expires 60;
97 return 204;
98 }
99
100 location /cache-control-before-expires {
101 add_header Cache-Control max-age=60;
102 add_header Expires "Thu, 01 Jan 1970 00:00:01 GMT";
103 return 204;
104 }
105
106 location /cache-control-after-expires {
107 add_header Expires "Thu, 01 Jan 1970 00:00:01 GMT";
108 add_header Cache-Control max-age=60;
109 return 204;
110 }
111
112 location /cache-control-no-cache-before-expires {
113 add_header Cache-Control no-cache;
114 add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
115 return 204;
116 }
117
118 location /cache-control-no-cache-after-expires {
119 add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
120 add_header Cache-Control no-cache;
121 return 204;
122 }
123
124 location /x-accel-expires-before {
125 add_header X-Accel-Expires 60;
126 add_header Expires "Thu, 01 Jan 1970 00:00:01 GMT";
127 add_header Cache-Control no-cache;
128 return 204;
129 }
130
131 location /x-accel-expires-after {
132 add_header Expires "Thu, 01 Jan 1970 00:00:01 GMT";
133 add_header Cache-Control no-cache;
134 add_header X-Accel-Expires 60;
135 return 204;
136 }
137
138 location /x-accel-expires-0-before {
139 add_header X-Accel-Expires 0;
140 add_header Cache-Control max-age=60;
141 add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
142 return 204;
143 }
144
145 location /x-accel-expires-0-after {
146 add_header Cache-Control max-age=60;
147 add_header Expires "Thu, 31 Dec 2037 23:55:55 GMT";
148 add_header X-Accel-Expires 0;
149 return 204;
150 }
151
152 location /cache-control-no-cache-one {
153 add_header Cache-Control "no-cache, max-age=60";
154 return 204;
155 }
156
157 location /cache-control-no-cache-multi {
158 add_header Cache-Control no-cache;
159 add_header Cache-Control max-age=60;
160 return 204;
161 }
162
163 location /extension-before-x-accel-expires {
164 add_header Cache-Control stale-while-revalidate=2145902155;
165 add_header X-Accel-Expires @1;
166 return 204;
167 }
168
169 location /extension-after-x-accel-expires {
170 add_header X-Accel-Expires @1;
171 add_header Cache-Control stale-while-revalidate=2145902155;
172 return 204;
173 }
174
175 location /set-cookie {
176 add_header Set-Cookie foo;
177 add_header Expires "Thu, 01 Jan 1970 00:00:01 GMT";
178 add_header Cache-control max-age=60;
179 return 204;
180 }
181 }
182 }
183
184 EOF
185
186 $t->run();
187
188 ###############################################################################
189
190 # cache headers work
191
192 like(get('/expires'), qr/HIT/, 'expires');
193 like(get('/cache-control'), qr/HIT/, 'cache-control');
194 like(get('/x-accel-expires'), qr/HIT/, 'x-accel-expires');
195 like(get('/x-accel-expires-at'), qr/EXPIRED/, 'x-accel-expires at');
196
197 TODO: {
198 local $TODO = 'not yet' unless $t->has_version('1.23.0');
199
200 # the second header to disable cache is duplicate and ignored
201
202 like(get('/x-accel-expires-duplicate'), qr/HIT/, 'x-accel-expires duplicate');
203
204 }
205
206 # with cache headers ignored, the response will be fresh
207
208 like(get('/ignore'), qr/MISS/, 'cache headers ignored');
209
210 # Cache-Control is preferred over Expires
211
212 like(get('/cache-control-before-expires'), qr/HIT/,
213 'cache-control before expires');
214
215 TODO: {
216 local $TODO = 'not yet' unless $t->has_version('1.23.0');
217
218 like(get('/cache-control-after-expires'), qr/HIT/,
219 'cache-control after expires');
220
221 }
222
223 like(get('/cache-control-no-cache-before-expires'), qr/MISS/,
224 'cache-control no-cache before expires');
225 like(get('/cache-control-no-cache-after-expires'), qr/MISS/,
226 'cache-control no-cache after expires');
227
228 # X-Accel-Expires is preferred over both Cache-Control and Expires
229
230 like(get('/x-accel-expires-before'), qr/HIT/, 'x-accel-expires before');
231
232 TODO: {
233 local $TODO = 'not yet' unless $t->has_version('1.23.0');
234
235 like(get('/x-accel-expires-after'), qr/HIT/, 'x-accel-expires after');
236
237 }
238
239 like(get('/x-accel-expires-0-before'), qr/MISS/, 'x-accel-expires 0 before');
240 like(get('/x-accel-expires-0-after'), qr/MISS/, 'x-accel-expires 0 after');
241
242 # "Cache-Control: no-cache" disables caching, no matter of "max-age"
243
244 like(get('/cache-control-no-cache-one'), qr/MISS/,
245 'cache-control no-cache');
246 like(get('/cache-control-no-cache-multi'), qr/MISS/,
247 'cache-control no-cache multi line');
248
249 # Cache-Control extensions are preserved with X-Accel-Expires
250
251 like(get('/extension-before-x-accel-expires'),
252 qr/STALE/, 'cache-control extensions before x-accel-expires');
253
254 TODO: {
255 local $TODO = 'not yet' unless $t->has_version('1.23.0');
256
257 like(get('/extension-after-x-accel-expires'),
258 qr/STALE/, 'cache-control extensions after x-accel-expires');
259
260 }
261
262 # Set-Cookie is considered when caching with Cache-Control
263
264 like(get('/set-cookie'), qr/MISS/, 'set-cookie not cached');
265
266 ###############################################################################
267
268 sub get {
269 my ($uri) = @_;
270 http_get($uri);
271 http_get($uri);
272 }
273
274 ###############################################################################