Mercurial > hg > nginx-tests
annotate proxy_cache_vary.t @ 1701:408fe0dd3fed
Tests: fixed mail_imap_ssl.t too long shutdown.
Prior to literals support in IMAP test backend (e7f0b4ca0a1a), early backend
response was treated as invalid, with subsequent proxy connection close.
Now that the connection continues successfully, this requires connection
close before nginx shutdown. Otherwise, it would wait for proxy_timeout.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Thu, 17 Jun 2021 19:52:36 +0300 |
parents | 5ac6efbe5552 |
children | ce4419d32383 |
rev | line source |
---|---|
471 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for http proxy cache, the Vary header. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 | |
14 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
15 | |
16 use lib 'lib'; | |
17 use Test::Nginx; | |
18 | |
19 ############################################################################### | |
20 | |
21 select STDERR; $| = 1; | |
22 select STDOUT; $| = 1; | |
23 | |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
24 my $t = Test::Nginx->new()->has(qw/http proxy cache gzip rewrite/) |
1590
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
25 ->plan(49)->write_file_expand('nginx.conf', <<'EOF'); |
471 | 26 |
27 %%TEST_GLOBALS%% | |
28 | |
29 daemon off; | |
30 | |
31 events { | |
32 } | |
33 | |
34 http { | |
35 %%TEST_GLOBALS_HTTP%% | |
36 | |
37 proxy_cache_path %%TESTDIR%%/cache keys_zone=one:1m inactive=5s; | |
38 proxy_cache_key $uri; | |
39 | |
40 server { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
41 listen 127.0.0.1:8080; |
471 | 42 server_name localhost; |
43 | |
44 add_header X-Cache-Status $upstream_cache_status; | |
45 | |
46 location / { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
47 proxy_pass http://127.0.0.1:8081/; |
471 | 48 proxy_cache one; |
49 } | |
50 | |
51 location /replace/ { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
52 proxy_pass http://127.0.0.1:8081/; |
471 | 53 proxy_cache one; |
54 } | |
55 | |
56 location /revalidate/ { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
57 proxy_pass http://127.0.0.1:8081/; |
471 | 58 proxy_cache one; |
59 proxy_cache_revalidate on; | |
60 } | |
61 | |
62 location /ignore/ { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
63 proxy_pass http://127.0.0.1:8081/; |
471 | 64 proxy_cache one; |
65 proxy_ignore_headers Vary; | |
66 } | |
67 } | |
68 | |
69 server { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
70 listen 127.0.0.1:8081; |
471 | 71 server_name localhost; |
72 | |
73 gzip on; | |
74 gzip_min_length 0; | |
75 gzip_http_version 1.0; | |
76 gzip_vary on; | |
77 | |
78 expires 2s; | |
79 | |
80 location / { | |
81 if ($args = "novary") { | |
82 return 200 "the only variant\n"; | |
83 } | |
84 } | |
85 | |
86 location /asterisk { | |
87 gzip off; | |
88 add_header Vary "*"; | |
89 } | |
90 | |
91 location /complex { | |
92 gzip off; | |
93 add_header Vary ",, Accept-encoding , ,"; | |
94 } | |
1672
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
95 |
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
96 location /cold { |
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
97 expires max; |
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
98 add_header Vary $arg_vary; |
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
99 add_header Xtra $arg_xtra; |
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
100 } |
471 | 101 } |
102 } | |
103 | |
104 EOF | |
105 | |
106 $t->write_file('index.html', 'SEE-THIS'); | |
107 $t->write_file('asterisk', 'SEE-THIS'); | |
108 $t->write_file('complex', 'SEE-THIS'); | |
1672
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
109 $t->write_file('cold', 'SEE-THIS'); |
471 | 110 |
568
907e89fba9c3
Tests: removed TODO and try_run() checks for legacy versions.
Sergey Kandaurov <pluknet@nginx.com>
parents:
507
diff
changeset
|
111 $t->run(); |
471 | 112 |
113 ############################################################################### | |
114 | |
115 like(get('/', 'gzip'), qr/MISS/ms, 'first request'); | |
116 like(get('/', 'gzip'), qr/HIT/ms, 'vary match cached'); | |
117 like(get('/', 'deflate'), qr/MISS/ms, 'vary mismatch'); | |
118 like(get('/', 'deflate'), qr/HIT/ms, 'vary mismatch cached'); | |
119 like(get('/', 'foo'), qr/MISS/ms, 'vary mismatch 2'); | |
120 like(get('/', 'foo'), qr/HIT/ms, 'vary mismatch 2 cached'); | |
121 like(get('/', 'gzip'), qr/HIT/ms, 'multiple representations cached'); | |
122 | |
123 SKIP: { | |
124 skip 'long tests', 6 unless $ENV{TEST_NGINX_UNSAFE}; | |
125 | |
126 # make sure all variants are properly expire | |
127 # and removed after inactive timeout | |
128 | |
129 sleep(3); | |
130 | |
131 like(get('/', 'gzip'), qr/EXPIRED/ms, 'first expired'); | |
132 like(get('/', 'deflate'), qr/EXPIRED/ms, 'second variant expired'); | |
133 | |
134 like(get('/', 'gzip'), qr/HIT/ms, 'first cached after expire'); | |
135 like(get('/', 'deflate'), qr/HIT/ms, 'second cached after expire'); | |
136 | |
137 sleep(12); | |
138 | |
139 like(get('/', 'gzip'), qr/MISS/ms, 'first inactive removed'); | |
140 like(get('/', 'deflate'), qr/MISS/ms, 'second variant removed'); | |
141 | |
142 } | |
143 | |
144 SKIP: { | |
145 skip 'long tests', 6 unless $ENV{TEST_NGINX_UNSAFE}; | |
146 | |
147 # check if the variant which was loaded first will be properly | |
148 # removed if it's not requested (but another variant is requested | |
149 # at the same time) | |
150 | |
151 sleep(3); | |
152 like(get('/', 'deflate'), qr/EXPIRED/ms, 'bump1'); | |
153 sleep(3); | |
154 like(get('/', 'deflate'), qr/EXPIRED/ms, 'bump2'); | |
155 sleep(3); | |
156 like(get('/', 'deflate'), qr/EXPIRED/ms, 'bump3'); | |
157 sleep(3); | |
158 like(get('/', 'deflate'), qr/EXPIRED/ms, 'bump4'); | |
159 | |
160 TODO: { | |
161 local $TODO = 'not yet'; | |
162 | |
476
4e335141aa4b
Tests: fix head() remnants in Vary tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
471
diff
changeset
|
163 like(get('/', 'gzip'), qr/MISS/ms, 'first not bumped by second requests'); |
471 | 164 |
165 } | |
166 | |
476
4e335141aa4b
Tests: fix head() remnants in Vary tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
471
diff
changeset
|
167 like(get('/', 'deflate'), qr/HIT/ms, 'second variant cached'); |
471 | 168 |
169 } | |
170 | |
171 # if a response without Vary is returned to replace previously returned | |
172 # responses with Vary, make sure it is then used in all cases | |
173 | |
174 like(get('/replace/', 'gzip'), qr/MISS/, 'replace first'); | |
175 like(get('/replace/', 'deflate'), qr/MISS/, 'replace second'); | |
176 | |
177 sleep(3); | |
178 | |
179 like(get('/replace/?novary', 'deflate'), qr/EXPIRED/, 'replace novary'); | |
180 like(get('/replace/?zztest', 'gzip'), qr/HIT/, 'all replaced'); | |
181 | |
182 # make sure revalidation of variants works fine | |
183 | |
184 like(get('/revalidate/', 'gzip'), qr/MISS/, 'revalidate first'); | |
185 like(get('/revalidate/', 'deflate'), qr/MISS/, 'revalidate second'); | |
186 | |
187 sleep(3); | |
188 | |
189 like(get('/revalidate/', 'gzip'), qr/REVALIDATED/, 'revalidated first'); | |
190 like(get('/revalidate/', 'deflate'), qr/REVALIDATED/, 'revalidated second'); | |
191 like(get('/revalidate/', 'gzip'), qr/HIT/, 'revalidate first after'); | |
192 like(get('/revalidate/', 'deflate'), qr/HIT/, 'revalidate second after'); | |
193 | |
194 # if the Vary header is ignored, cached version can be returned | |
195 # regardless of request headers | |
196 | |
197 like(get('/ignore/', 'gzip'), qr/MISS/ms, 'another request'); | |
198 like(get('/ignore/', 'deflate'), qr/HIT/ms, 'vary ignored'); | |
199 | |
200 # check parsing of Vary with multiple headers listed | |
201 | |
202 like(get('/complex', 'gzip'), qr/MISS/ms, 'vary complex first'); | |
203 like(get('/complex', 'deflate'), qr/MISS/ms, 'vary complex second'); | |
204 like(get('/complex', 'gzip'), qr/HIT/ms, 'vary complex first cached'); | |
205 like(get('/complex', 'deflate'), qr/HIT/ms, 'vary complex second cached'); | |
206 | |
207 # From RFC 7231, "7.1.4. Vary", | |
208 # http://tools.ietf.org/html/rfc7231#section-7.1.4: | |
209 # | |
210 # A Vary field value of "*" signals that anything about the request | |
211 # might play a role in selecting the response representation, possibly | |
212 # including elements outside the message syntax (e.g., the client's | |
213 # network address). A recipient will not be able to determine whether | |
214 # this response is appropriate for a later request without forwarding | |
215 # the request to the origin server. | |
216 # | |
217 # In theory, If-None-Match can be used to check if the representation | |
218 # present in the cache is appropriate. This seems to be only possible | |
219 # with strong entity tags though, as representation with different | |
220 # content condings may share the same weak entity tag. | |
221 | |
222 like(get('/asterisk', 'gzip'), qr/MISS/ms, 'vary asterisk first'); | |
223 like(get('/asterisk', 'gzip'), qr/MISS/ms, 'vary asterisk second'); | |
224 | |
225 # From RFC 7234, "4.1. Calculating Secondary Keys with Vary", | |
226 # http://tools.ietf.org/html/rfc7234#section-4.1: | |
227 # | |
228 # The selecting header fields from two requests are defined to match if | |
229 # and only if those in the first request can be transformed to those in | |
230 # the second request by applying any of the following: | |
231 # | |
232 # o adding or removing whitespace, where allowed in the header field's | |
233 # syntax | |
507 | 234 # |
471 | 235 # o combining multiple header fields with the same field name (see |
236 # Section 3.2 of [RFC7230]) | |
507 | 237 # |
471 | 238 # o normalizing both header field values in a way that is known to |
239 # have identical semantics, according to the header field's | |
240 # specification (e.g., reordering field values when order is not | |
241 # significant; case-normalization, where values are defined to be | |
242 # case-insensitive) | |
489
3036e3af0e08
Tests: more Vary normalization tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
481
diff
changeset
|
243 # |
3036e3af0e08
Tests: more Vary normalization tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
481
diff
changeset
|
244 # Only whitespace normalization is currently implemented. |
471 | 245 |
246 like(get('/', 'foo, bar'), qr/MISS/ms, 'normalize first'); | |
481
7e823c8f7d31
Tests: adjusted TODOs for cache Vary support committed in 1.7.7.
Sergey Kandaurov <pluknet@nginx.com>
parents:
478
diff
changeset
|
247 like(get('/', 'foo,bar'), qr/HIT/ms, 'normalize whitespace'); |
7e823c8f7d31
Tests: adjusted TODOs for cache Vary support committed in 1.7.7.
Sergey Kandaurov <pluknet@nginx.com>
parents:
478
diff
changeset
|
248 like(get('/', 'foo,, ,bar , '), qr/HIT/ms, 'normalize empty'); |
489
3036e3af0e08
Tests: more Vary normalization tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
481
diff
changeset
|
249 like(get('/', 'foobar'), qr/MISS/ms, 'normalize no whitespace mismatch'); |
471 | 250 |
251 TODO: { | |
252 local $TODO = 'not yet'; | |
253 | |
254 like(get('/', 'bar,foo'), qr/HIT/ms, 'normalize order'); | |
255 | |
256 } | |
257 | |
1590
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
258 # keep c->body_start when Vary changes (ticket #2029) |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
259 |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
260 # before 1.19.3, this prevented updating c->body_start of a main key |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
261 # triggering "cache file .. has too long header" critical errors |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
262 |
1672
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
263 get1('/cold?vary=z', 'z:1'); |
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
264 like(get1('/cold?vary=x,y', 'x:1'), qr/MISS/, 'change first'); |
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
265 like(get1('/cold?vary=x,y', 'x:1'), qr/HIT/, 'change first cached'); |
1590
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
266 |
1672
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
267 like(get1('/cold?vary=x,y&xtra=1', 'x:2'), qr/MISS/, 'change second'); |
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
268 like(get1('/cold?vary=x,y&xtra=1', 'x:2'), qr/HIT/, 'change second cached'); |
1590
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
269 |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
270 $t->stop(); |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
271 $t->run(); |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
272 |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
273 # reset c->body_start when loading a secondary key variant |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
274 |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
275 # before 1.19.3, it was loaded using a variant stored with a main key |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
276 # triggering "cache file .. has too long header" critical errors |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
277 |
1672
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
278 like(get1('/cold?vary=x,y', 'x:1'), qr/HIT/, 'cold first'); |
feb754918372
Tests: adjusted caching time for Vary tests with cold cache.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1590
diff
changeset
|
279 like(get1('/cold?vary=x,y&xtra=1', 'x:2'), qr/HIT/, 'cold second'); |
1590
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
280 |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
281 $t->stop(); |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
282 |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
283 like(`grep -F '[crit]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no crit'); |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
284 |
471 | 285 ############################################################################### |
286 | |
287 sub get { | |
288 my ($url, $extra) = @_; | |
289 return http(<<EOF); | |
290 GET $url HTTP/1.1 | |
291 Host: localhost | |
292 Connection: close | |
293 Accept-Encoding: $extra | |
294 | |
295 EOF | |
296 } | |
297 | |
1590
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
298 sub get1 { |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
299 my ($url, $extra) = @_; |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
300 return http(<<EOF); |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
301 GET $url HTTP/1.1 |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
302 Host: localhost |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
303 Connection: close |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
304 $extra |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
305 |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
306 EOF |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
307 } |
e682d5ad3861
Tests: added two cache Vary cases fixed in 1.19.3.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
308 |
471 | 309 ############################################################################### |