comparison proxy_cache_use_stale.t @ 1134:d77c331affff

Tests: proxy_cache_use_stale tests with Cache-Control in response.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 20 Feb 2017 18:49:12 +0300
parents
children 129b9e4ca3b3
comparison
equal deleted inserted replaced
1133:9edf2f99c58f 1134:d77c331affff
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for http proxy cache, proxy_cache_use_stale.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx qw/ :DEFAULT http_end /;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http proxy cache rewrite limit_req/)
26 ->write_file_expand('nginx.conf', <<'EOF');
27
28 %%TEST_GLOBALS%%
29
30 daemon off;
31
32 events {
33 }
34
35 http {
36 %%TEST_GLOBALS_HTTP%%
37
38 proxy_cache_path %%TESTDIR%%/cache levels=1:2 keys_zone=NAME:1m;
39
40 limit_req_zone $binary_remote_addr zone=one:1m rate=15r/m;
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
49 proxy_cache NAME;
50
51 proxy_cache_key $uri;
52
53 proxy_cache_revalidate on;
54
55 proxy_cache_background_update on;
56
57 add_header X-Cache-Status $upstream_cache_status;
58
59 location /t4.html {
60 proxy_pass http://127.0.0.1:8081/t.html;
61
62 proxy_cache_revalidate off;
63 }
64
65 location /t5.html {
66 proxy_pass http://127.0.0.1:8081/t.html;
67
68 proxy_cache_background_update off;
69 }
70
71 location /updating/ {
72 proxy_pass http://127.0.0.1:8081/;
73
74 proxy_cache_use_stale updating;
75 }
76
77 location /t8.html {
78 proxy_pass http://127.0.0.1:8081/t.html;
79
80 proxy_cache_valid 1s;
81 }
82 }
83 }
84 server {
85 listen 127.0.0.1:8081;
86 server_name localhost;
87
88 add_header Cache-Control $http_x_cache_control;
89
90 if ($arg_e) {
91 return 500;
92 }
93
94 location / { }
95
96 location /t6.html {
97 limit_req zone=one burst=2;
98 }
99 }
100 }
101
102 EOF
103
104 $t->write_file('t.html', 'SEE-THIS');
105 $t->write_file('tt.html', 'SEE-THIS');
106 $t->write_file('t2.html', 'SEE-THIS');
107 $t->write_file('t3.html', 'SEE-THIS');
108 $t->write_file('t6.html', 'SEE-THIS');
109
110 $t->try_run('no proxy_cache_background_update')->plan(25);
111
112 ###############################################################################
113
114 like(get('/t.html', 'max-age=1, stale-if-error=5'), qr/MISS/, 'stale-if-error');
115 like(http_get('/t.html?e=1'), qr/HIT/, 's-i-e - cached');
116
117 like(get('/t2.html', 'max-age=1, stale-while-revalidate=10'), qr/MISS/,
118 'stale-while-revalidate');
119 like(http_get('/t2.html'), qr/HIT/, 's-w-r - cached');
120
121 get('/tt.html', 'max-age=1, stale-if-error=2');
122 get('/t3.html', 'max-age=1, stale-while-revalidate=2');
123 get('/t4.html', 'max-age=1, stale-while-revalidate=2');
124 get('/t5.html', 'max-age=1, stale-while-revalidate=2');
125 get('/t6.html', 'max-age=1, stale-while-revalidate=2');
126 get('/updating/t.html', 'max-age=1');
127 get('/updating/t2.html', 'max-age=1, stale-while-revalidate=2');
128 get('/t8.html', 'stale-while-revalidate=10');
129
130 sleep 2;
131
132 like(http_get('/t.html?e=1'), qr/STALE/, 's-i-e - stale');
133 like(http_get('/tt.html?e=1'), qr/STALE/, 's-i-e - stale 2');
134 like(http_get('/t.html'), qr/REVALIDATED/, 's-i-e - revalidated');
135
136 like(http_get('/t2.html?e=1'), qr/STALE/, 's-w-r - revalidate error');
137 like(http_get('/t2.html'), qr/STALE/, 's-w-r - stale while revalidate');
138 like(http_get('/t2.html'), qr/HIT/, 's-w-r - revalidated');
139
140 like(get('/t4.html', 'max-age=1, stale-while-revalidate=2'), qr/STALE/,
141 's-w-r - unconditional revalidate');
142 like(http_get('/t4.html'), qr/HIT/, 's-w-r - unconditional revalidated');
143
144 like(http_get('/t5.html?e=1'), qr/STALE/,
145 's-w-r - foreground revalidate error');
146 like(http_get('/t5.html'), qr/REVALIDATED/, 's-w-r - foreground revalidated');
147
148 # UPDATING while s–w-r
149
150 $t->write_file('t6.html', 'SEE-THAT');
151
152 my $s = get('/t6.html', 'max-age=1, stale-while-revalidate=2', start => 1);
153 like(http_get('/t6.html'), qr/UPDATING.*SEE-THIS/s, 's-w-r - updating');
154 like(http_end($s), qr/STALE.*SEE-THIS/s, 's-w-r - updating stale');
155 like(http_get('/t6.html'), qr/HIT.*SEE-THAT/s, 's-w-r - updating revalidated');
156
157 # stale-while-revalidate with proxy_cache_use_stale updating
158
159 like(http_get('/updating/t.html'), qr/STALE/,
160 's-w-r - use_stale updating stale');
161 like(http_get('/updating/t.html'), qr/HIT/,
162 's-w-r - use_stale updating revalidated');
163
164 # stale-while-revalidate with proxy_cache_valid
165
166 like(http_get('/t8.html'), qr/STALE/, 's-w-r - proxy_cache_valid revalidate');
167 like(http_get('/t8.html'), qr/HIT/, 's-w-r - proxy_cache_valid revalidated');
168
169 sleep 2;
170
171 like(http_get('/t2.html?e=1'), qr/STALE/, 's-w-r - stale after revalidate');
172 like(http_get('/t3.html?e=1'), qr/ 500 /, 's-w-r - ceased');
173 like(http_get('/tt.html?e=1'), qr/ 500 /, 's-i-e - ceased');
174 like(http_get('/updating/t2.html'), qr/STALE/,
175 's-w-r - overriden with use_stale updating');
176
177 ###############################################################################
178
179 sub get {
180 my ($url, $extra, %extra) = @_;
181 return http(<<EOF, %extra);
182 GET $url HTTP/1.1
183 Host: localhost
184 Connection: close
185 X-Cache-Control: $extra
186
187 EOF
188 }
189
190 ###############################################################################