annotate proxy_cache_valid.t @ 1163:bd1cf0a80b28

Tests: Cache-Control max-age/s-maxage set in response.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 12 Apr 2017 12:58:10 +0300
parents
children 66426ca24671
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1163
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Sergey Kandaurov
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6 # Tests for http proxy cache, the proxy_cache_valid directive
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7 # used with the caching parameters set in the response header.
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9 ###############################################################################
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use warnings;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12 use strict;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14 use Test::More;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16 BEGIN { use FindBin; chdir($FindBin::Bin); }
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18 use lib 'lib';
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19 use Test::Nginx;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21 ###############################################################################
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23 select STDERR; $| = 1;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24 select STDOUT; $| = 1;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26 my $t = Test::Nginx->new()->has(qw/http proxy cache/)->plan(8)
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
27 ->write_file_expand('nginx.conf', <<'EOF');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
29 %%TEST_GLOBALS%%
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31 daemon off;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33 events {
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34 }
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36 http {
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37 %%TEST_GLOBALS_HTTP%%
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39 proxy_cache_path %%TESTDIR%%/cache levels=1:2
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40 keys_zone=NAME:1m;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
42 server {
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43 listen 127.0.0.1:8080;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 server_name localhost;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 location / {
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47 proxy_pass http://127.0.0.1:8081;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 proxy_cache NAME;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 proxy_cache_valid 1m;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52 add_header X-Cache-Status $upstream_cache_status;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 }
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 }
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 server {
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56 listen 127.0.0.1:8081;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 server_name localhost;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59 location / {
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60 add_header Cache-Control $http_x_cc;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61 }
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62 }
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 }
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65 EOF
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67 $t->write_file('t.html', 'SEE-THIS');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
68 $t->run();
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70 ###############################################################################
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72 like(get('/t.html?1', 'X-CC: max-age=1'), qr/MISS/, 'max-age');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73 like(get('/t.html?2', 'X-CC: max-age=1, s-maxage=10'), qr/MISS/, 's-maxage');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 like(http_get('/t.html?3'), qr/MISS/, 'proxy_cache_valid');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76 $t->write_file('t.html', 'NOOP');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 like(http_get('/t.html?1'), qr/HIT/, 'max-age cached');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 like(http_get('/t.html?2'), qr/HIT/, 's-maxage cached');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80 like(http_get('/t.html?3'), qr/HIT/, 'proxy_cache_valid cached');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82 select undef, undef, undef, 2.1;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 # Cache-Control in the response header overrides proxy_cache_valid
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
85
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86 like(http_get('/t.html?1'), qr/EXPIRED/, 'max-age ceased');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
87 like(http_get('/t.html?2'), qr/HIT/, 's-maxage overrides max-age');
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89 ###############################################################################
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
91 sub get {
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
92 my ($url, $extra) = @_;
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93 return http(<<EOF);
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
94 GET $url HTTP/1.1
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
95 Host: localhost
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
96 Connection: close
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
97 $extra
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
98
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
99 EOF
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
100 }
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
101
bd1cf0a80b28 Tests: Cache-Control max-age/s-maxage set in response.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
102 ###############################################################################