# HG changeset patch # User Maxim Dounin # Date 1719341210 -10800 # Node ID bf027a972ccf8d6ff141f85e9a41615009e8005b # Parent b5c1c3ef234570408fdbb79343b40b063bc3b83b Tests: proxy_no_cache tests. diff --git a/proxy_cache_bypass.t b/proxy_cache_bypass.t --- a/proxy_cache_bypass.t +++ b/proxy_cache_bypass.t @@ -21,7 +21,7 @@ use Test::Nginx; select STDERR; $| = 1; select STDOUT; $| = 1; -my $t = Test::Nginx->new()->has(qw/http proxy cache rewrite/)->plan(8) +my $t = Test::Nginx->new()->has(qw/http proxy cache rewrite/)->plan(12) ->write_file_expand('nginx.conf', <<'EOF'); %%TEST_GLOBALS%% @@ -65,12 +65,17 @@ http { location / { } + + location /t3 { + add_header Transfer-Encoding $arg_bypass; + } } } EOF $t->write_file('t', 'SEE-THIS'); +$t->write_file('t3', 'SEE-THIS'); $t->run(); @@ -82,6 +87,9 @@ like(http_get('/t'), qr/SEE-THIS/, 'requ like(http_get('/t'), qr/SEE-THIS/, 'request cached'); like(http_get('/t?bypass=1'), qr/NOOP/, 'cache bypassed'); + +unlink $t->testdir() . '/t'; + like(http_get('/t'), qr/NOOP/, 'cached after bypass'); # ticket #827, cache item "error" field was not cleared @@ -93,6 +101,33 @@ like(http_get('/t2'), qr/403 Forbidden/, like(http_get('/t2'), qr/403 Forbidden/, 'error cached'); like(http_get('/t2?bypass=1'), qr/NOOP/, 'error cache bypassed'); -like(http_get('/t2'), qr/NOOP/, 'error cached after bypass'); + +unlink $t->testdir() . '/t2'; + +like(http_get('/t2'), qr/NOOP/, 'file cached after bypass'); + +# make sure the error is cached after bypass + +like(http_get('/t2?bypass=1'), qr/403 Forbidden/, 'file cache bypassed'); + +$t->write_file('t2', 'NOOP'); + +TODO: { +local $TODO = 'not yet' unless $t->has_version('1.27.2'); + +like(http_get('/t2'), qr/403 Forbidden/, 'error cached again'); + +} + +# similarly, internal 502/504 is cached after bypass + +like(http_get('/t3?bypass=1'), qr/502 Bad/, 'internal 502'); + +TODO: { +local $TODO = 'not yet' unless $t->has_version('1.27.2'); + +like(http_get('/t3'), qr/502 Bad/, 'internal 502 cached'); + +} ############################################################################### diff --git a/proxy_no_cache.t b/proxy_no_cache.t new file mode 100644 --- /dev/null +++ b/proxy_no_cache.t @@ -0,0 +1,174 @@ +#!/usr/bin/perl + +# (C) Maxim Dounin + +# Tests for http proxy cache, proxy_no_cache. + +############################################################################### + +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(16) + ->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + proxy_cache_path %%TESTDIR%%/cache keys_zone=one:1m; + + server { + listen 127.0.0.1:8080; + server_name localhost; + + location / { + proxy_pass http://127.0.0.1:8081; + + proxy_cache one; + proxy_cache_key $uri; + proxy_cache_valid any 1y; + proxy_no_cache $arg_nocache; + + proxy_intercept_errors on; + error_page 404 = @fallback; + } + + location /t3 { + proxy_pass http://127.0.0.1:8081; + + proxy_cache one; + proxy_cache_key $uri; + proxy_cache_valid any 1y; + proxy_no_cache $arg_nocache; + } + + location /t4 { + proxy_pass http://127.0.0.1:8081; + + proxy_cache one; + proxy_cache_key $uri; + proxy_cache_valid any 1s; + proxy_no_cache $upstream_http_x_no_cache; + + proxy_cache_revalidate on; + } + + location @fallback { + return 403; + } + + add_header X-Cache-Status $upstream_cache_status always; + } + + server { + listen 127.0.0.1:8081; + server_name localhost; + + location / { + } + + location /t3 { + set $nocache ""; + if ($arg_expires) { + set $nocache "no-cache"; + } + add_header Cache-Control $nocache; + add_header Transfer-Encoding invalid; + } + + location /t4 { + set $nocache ""; + if ($arg_expires) { + set $nocache "no-cache"; + } + add_header Cache-Control $nocache; + add_header X-No-Cache $arg_nocache; + } + } +} + +EOF + +$t->write_file('t', 'SEE-THIS'); +$t->write_file('t3', 'SEE-THIS'); +$t->write_file('t4', 'SEE-THIS'); + +$t->run(); + +############################################################################### + +like(http_get('/t?nocache=1'), qr/MISS.*SEE-THIS/s, 'request'); +like(http_get('/t'), qr/MISS.*SEE-THIS/s, 'request not cached'); +like(http_get('/t'), qr/HIT.*SEE-THIS/s, 'request cached'); + +# proxy_no_cache with intercepted errors, +# ngx_http_upstream_intercept_errors() + +like(http_get('/t2?nocache=1'), qr/403 Forbidden/, 'intercepted error'); + +TODO: { +local $TODO = 'not yet' unless $t->has_version('1.27.2'); + +like(http_get('/t2'), qr/403 Forbidden.*MISS/s, 'intercepted error not cached'); + +} + +like(http_get('/t2'), qr/403 Forbidden.*HIT/s, 'intercepted error cached'); + +# proxy_no_cache with internal 502/504 errors, +# ngx_http_upstream_finalize_request() + +like(http_get('/t3?nocache=1'), qr/502 Bad/, 'internal 502 error'); + +TODO: { +local $TODO = 'not yet' unless $t->has_version('1.27.2'); + +like(http_get('/t3?expires=1'), qr/502 Bad.*MISS/s, + 'internal 502 error expires'); +like(http_get('/t3'), qr/502 Bad.*MISS/s, 'internal 502 error not cached'); + +} + +like(http_get('/t3'), qr/502 Bad.*HIT/s, 'internal 502 error cached'); + +# proxy_no_cache with revalidate and 304, +# ngx_http_upstream_test_next() + +like(http_get('/t4'), qr/MISS.*SEE-THIS/s, 'revalidate'); +like(http_get('/t4'), qr/HIT.*SEE-THIS/s, 'revalidate cached'); +select undef, undef, undef, 2.5; +like(http_get('/t4?nocache=1'), qr/REVALIDATED.*SEE-THIS/s, + 'revalidate nocache'); + +TODO: { +local $TODO = 'not yet' unless $t->has_version('1.27.2'); + +like(http_get('/t4?expires=1'), qr/REVALIDATED.*SEE-THIS/s, + 'revalidate expires'); +like(http_get('/t4'), qr/REVALIDATED.*SEE-THIS/s, + 'revalidate again'); + +} + +like(http_get('/t4'), qr/HIT.*SEE-THIS/s, 'revalidate again cached'); + +###############################################################################