[PATCH v2] Tests: added Expires and Cache-Control headers test
Yugo Horie
yugo-horie at jocdn.co.jp
Tue Apr 26 00:18:41 UTC 2022
Hi, Vadim
We assume that your test has been passed whether applying Maxim's
patch or not the conventional one.
(my verification may be inadequate...)
: server {
: listen 127.0.0.1:8080;
: location / {
: proxy_pass http://127.0.0.2:8080;
: proxy_cache cache_zone;
: }
: }
: server {
: listen 127.0.0.2:8080;
When I changed the upstream server port to another one(e.g 8081), The
conventional Nginx had failed on the `/fail` case and Maxim's patched
ones had come to pass. We consider that this seems to be the right
direction.
And I prefer to add the cache status headers instead of using your
cache file counter.
The config phase:
: server {
: listen 127.0.0.1:8080;
: location / {
: proxy_pass http://127.0.0.2:8081;
: proxy_cache cache_zone;
: add_header X-Cache-Status \$upstream_cache_status;
: }
: }
: server {
: listen 127.0.0.2:8081;
: location /success {
: add_header Cache-Control "max-age=3600";
: add_header Expires "Tue, 15 Nov 1994 12:45:26 GMT";
: return 200 "Hello world!";
: }
: location /fail {
: add_header Expires "Tue, 15 Nov 1994 12:45:26 GMT";
: add_header Cache-Control "max-age=3600";
: return 200 "Hello world!";
: }
On the test phase:
: $r = http_get('/success');
: like($r, qr/Cache-Control/, 'cache-control-first-cache-control-is-present');
: like($r, qr/Expires/, 'cache-control-first-expires-is-present');
: $r = http_get('/success');
: like($r, qr/X-Cache-Status: HIT/, 'cache-status-is-hit');
:
: $r = http_get('/fail');
: like($r, qr/Cache-Control/, 'expires-first-cache-control-is-present');
: like($r, qr/Expires/, 'expires-first-expires-is-present');
: $r = http_get('/fail');
: like($r, qr/X-Cache-Status: HIT/, 'cache-status-is-hit');
Thanks a lot,
Yugo Horie
2022年4月20日(水) 7:22 Vadim Fedorenko via nginx-devel <nginx-devel at nginx.org>:
>
> # HG changeset patch
> # User Vadim Fedorenko <vadim.fedorenko at cdnnow.ru>
> # Date 1649976970 -10800
> # Fri Apr 15 01:56:10 2022 +0300
> # Node ID 3d5684530a8ef228cd7f20ff3e51f9ea5e77f2ec
> # Parent 0c50a00e67334659d58d3cf7cb81fcf5872a8285
> Tests: added Expires and Cache-Control headers test
>
> diff -r 0c50a00e6733 -r 3d5684530a8e proxy_cache_expires_cache_control.t
> --- /dev/null Thu Jan 01 00:00:00 1970 +0000
> +++ b/proxy_cache_expires_cache_control.t Fri Apr 15 01:56:10 2022 +0300
> @@ -0,0 +1,99 @@
> +#!/usr/bin/perl
> +
> +# (C) Georgii Dzebisashvili
> +# (C) Vadim Fedorenko
> +
> +# Tests for cache management regarding https://trac.nginx.org/nginx/ticket/964
> +
> +###############################################################################
> +
> +use warnings;
> +use strict;
> +
> +use Test::More;
> +
> +BEGIN { use FindBin; chdir($FindBin::Bin); }
> +
> +use lib 'lib';
> +use Test::Nginx;
> +
> +use File::Find;
> +
> +###############################################################################
> +
> +select STDERR; $| = 1;
> +select STDOUT; $| = 1;
> +
> +my $scriptDir = $FindBin::Bin;
> +my $cacheDir = $scriptDir."/testcache";
> +my $tempDir = $scriptDir."/testtemp";
> +
> +my $t = Test::Nginx->new()->plan(8)->write_file_expand('nginx.conf', <<"EOF");
> +
> +daemon off;
> +events {
> + worker_connections 64;
> +}
> +http {
> + access_log off;
> + proxy_cache_path $cacheDir levels=2 keys_zone=cache_zone:1m inactive=24h max_size=10m;
> + proxy_temp_path $tempDir;
> + server {
> + listen 127.0.0.1:8080;
> + location / {
> + proxy_pass http://127.0.0.2:8080;
> + proxy_cache cache_zone;
> + }
> + }
> + server {
> + listen 127.0.0.2:8080;
> + location /success {
> + add_header Cache-Control "max-age=3600";
> + add_header Expires "Tue, 15 Nov 1994 12:45:26 GMT";
> + return 200 "Hello world!";
> + }
> + location /fail {
> + add_header Expires "Tue, 15 Nov 1994 12:45:26 GMT";
> + add_header Cache-Control "max-age=3600";
> + return 200 "Hello world!";
> + }
> + location /store_xaccelexpire {
> + add_header Expires "Tue, 15 Nov 1994 12:45:26 GMT";
> + add_header Cache-Control "no-cache";
> + add_header X-Accel-Expires "60";
> + return 200 "Hello world!";
> + }
> + }
> +}
> +
> +
> +EOF
> +
> +$t->run();
> +
> +###############################################################################
> +
> +my $counter = 0;
> +
> +sub fileMatchCallback {
> + -f && $counter++; # Only count files
> +}
> +
> +my $r;
> +
> +$r = http_get('/success');
> +like($r, qr/Cache-Control/, 'cache-control-first-cache-control-is-present');
> +like($r, qr/Expires/, 'cache-control-first-expires-is-present');
> +
> +$r = http_get('/fail');
> +like($r, qr/Cache-Control/, 'expires-first-cache-control-is-present');
> +like($r, qr/Expires/, 'expires-first-expires-is-present');
> +
> +$r = http_get('/store_xaccelexpire');
> +like($r, qr/Cache-Control/, 'cache-control-with-xaccel-is-present');
> +like($r, qr/Expires/, 'expires-with-xaccel-is-present');
> +unlike($r, qr/X-Accel-Expires/, 'xaccel-is-not-present');
> +
> +find(\&fileMatchCallback, $cacheDir);
> +
> +is($counter, 3, 'overall number of cached requests');
> _______________________________________________
> nginx-devel mailing list -- nginx-devel at nginx.org
> To unsubscribe send an email to nginx-devel-leave at nginx.org
More information about the nginx-devel
mailing list