[PATCH] Tests for stale responses from upstream that may be cached.
Thorvaldur Thorvaldsson
thorvaldur.thorvaldsson at gmail.com
Thu Dec 17 14:22:30 UTC 2015
# HG changeset patch
# User Thorvaldur Thorvaldsson <thorvaldur.thorvaldsson at gmail.com>
# Date 1450275691 -3600
# Wed Dec 16 15:21:31 2015 +0100
# Node ID 692075b59d386ffcbf1aff8e560488fd110d04f7
# Parent dba758c045edbe010de380176235bea6b700367e
Tests for stale responses from upstream that may be cached.
The tests correspond to a patch that's been submitted to nginx-devel.
The test cases verify the caching and revalidation behaviour when:
1) "proxy_cache_revalidate" directive is "on";
2) the upstream response includes a "Cache-Control: max-age=0" header
along with various combinations of ETag/Last-Modified headers.
There's also test for the case where there's no "proxy_cache_valid"
directive and also no "Cache-Control: max-age=0" header, or
X-Accel-Expires, or Expired header but still where the
"proxy_cache_revalidate" is on and the response includes an ETag header.
diff -r dba758c045ed -r 692075b59d38 proxy_cache_revalidate.t
--- a/proxy_cache_revalidate.t Thu Dec 17 17:18:07 2015 +0300
+++ b/proxy_cache_revalidate.t Wed Dec 16 15:21:31 2015 +0100
@@ -21,7 +21,7 @@
select STDERR; $| = 1;
select STDOUT; $| = 1;
-my $t = Test::Nginx->new()->has(qw/http proxy cache rewrite shmem/)->plan(23)
+my $t = Test::Nginx->new()->has(qw/http proxy cache rewrite shmem/)->plan(33)
->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
@@ -51,6 +51,11 @@
add_header X-Cache-Status $upstream_cache_status;
}
+ location /no-valid-directive/ {
+ proxy_pass http://127.0.0.1:8081/etag-no-max-age/;
+ proxy_cache one;
+ add_header X-Cache-Status $upstream_cache_status;
+ }
}
server {
@@ -68,6 +73,33 @@
add_header X-If-Modified-Since $http_if_modified_since;
return 201;
}
+ location /stale-etag/ {
+ proxy_pass http://127.0.0.1:8081/;
+ proxy_hide_header Last-Modified;
+ add_header Cache-Control "max-age=0";
+ }
+ location /etag-no-max-age/ {
+ proxy_pass http://127.0.0.1:8081/;
+ proxy_hide_header Last-Modified;
+ }
+ location /stale-last-modified/ {
+ proxy_pass http://127.0.0.1:8081/;
+ proxy_hide_header ETag;
+ add_header Cache-Control "max-age=0";
+ }
+ location /stale-cannot-revalidate/ {
+ proxy_pass http://127.0.0.1:8081/;
+ proxy_hide_header ETag;
+ proxy_hide_header Last-Modified;
+ add_header Cache-Control "max-age=0";
+ }
+ location /stale-invalid-last-modified/ {
+ proxy_pass http://127.0.0.1:8081/;
+ proxy_hide_header ETag;
+ proxy_hide_header Last-Modified;
+ add_header Cache-Control "max-age=0";
+ add_header Last-Modified "invalid";
+ }
}
}
@@ -94,6 +126,31 @@
like(http_get('/etag/t'), qr/X-Cache-Status: MISS.*SEE/ms, 'etag');
like(http_get('/etag/t'), qr/X-Cache-Status: HIT.*SEE/ms, 'etag cached');
+my $CACHE_MISS = qr/X-Cache-Status: MISS.*?SEE/ms;
+my $CACHE_REVALIDATED = qr/X-Cache-Status: REVALIDATED.*?SEE/ms;
+
+like(http_get('/stale-etag/t'), $CACHE_MISS, 'stale etag');
+like(http_get('/stale-etag/t'), $CACHE_REVALIDATED, 'stale etag revalidated');
+
+like(http_get('/stale-last-modified/t'), $CACHE_MISS, 'stale last-modified');
+like(http_get('/stale-last-modified/t'), $CACHE_REVALIDATED,
+ 'stale last-modified revalidated');
+
+like(http_get('/stale-cannot-revalidate/t'), $CACHE_MISS,
+ 'stale cannot revalidate');
+like(http_get('/stale-cannot-revalidate/t'), $CACHE_MISS,
+ 'stale cannot revalidate not cached');
+
+like(http_get('/stale-invalid-last-modified/t'), $CACHE_MISS,
+ 'stale invalid last-modified');
+like(http_get('/stale-invalid-last-modified/t'), $CACHE_MISS,
+ 'stale invalid last-modified not cached');
+
+like(http_get('/no-valid-directive/etag-no-max-age/t'), $CACHE_MISS,
+ 'no cache-valid no max-age');
+like(http_get('/no-valid-directive/etag-no-max-age/t'), $CACHE_MISS,
+ 'no cache-valid no max-age not cached');
+
like(http_get('/etag/t2'), qr/X-Cache-Status: MISS.*SEE/ms, 'etag2');
like(http_get('/etag/t2'), qr/X-Cache-Status: HIT.*SEE/ms, 'etag2 cached');
More information about the nginx-devel
mailing list