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