[PATCH] Tests for stale responses from upstream that may be cached.

Thorvaldur Thorvaldsson thorvaldur.thorvaldsson at gmail.com
Wed Dec 16 14:39:12 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 2843074e6e5f320ecae750cb3995b0ab4540dcad
# Parent  5540ee8a12ce6e86f15f7cce616b231fb0fcaf4c
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.

diff -r 5540ee8a12ce -r 2843074e6e5f proxy_cache_revalidate.t
--- a/proxy_cache_revalidate.t Wed Dec 16 15:27:49 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(31)
  ->write_file_expand('nginx.conf', <<'EOF');

 %%TEST_GLOBALS%%
@@ -68,6 +68,29 @@
             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 /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 +117,26 @@
 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('/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