[PATCH v2] Tests: added Expires and Cache-Control headers test

Vadim Fedorenko vadim.fedorenko at cdnnow.ru
Tue Apr 19 22:20:42 UTC 2022


# 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');



More information about the nginx-devel mailing list