1989
|
1 #!/usr/bin/perl
|
|
2
|
|
3 # (C) Maxim Dounin
|
|
4
|
|
5 # Tests for http proxy cache, proxy_no_cache.
|
|
6
|
|
7 ###############################################################################
|
|
8
|
|
9 use warnings;
|
|
10 use strict;
|
|
11
|
|
12 use Test::More;
|
|
13
|
|
14 BEGIN { use FindBin; chdir($FindBin::Bin); }
|
|
15
|
|
16 use lib 'lib';
|
|
17 use Test::Nginx;
|
|
18
|
|
19 ###############################################################################
|
|
20
|
|
21 select STDERR; $| = 1;
|
|
22 select STDOUT; $| = 1;
|
|
23
|
|
24 my $t = Test::Nginx->new()->has(qw/http proxy cache rewrite/)->plan(16)
|
|
25 ->write_file_expand('nginx.conf', <<'EOF');
|
|
26
|
|
27 %%TEST_GLOBALS%%
|
|
28
|
|
29 daemon off;
|
|
30
|
|
31 events {
|
|
32 }
|
|
33
|
|
34 http {
|
|
35 %%TEST_GLOBALS_HTTP%%
|
|
36
|
|
37 proxy_cache_path %%TESTDIR%%/cache keys_zone=one:1m;
|
|
38
|
|
39 server {
|
|
40 listen 127.0.0.1:8080;
|
|
41 server_name localhost;
|
|
42
|
|
43 location / {
|
|
44 proxy_pass http://127.0.0.1:8081;
|
|
45
|
|
46 proxy_cache one;
|
|
47 proxy_cache_key $uri;
|
|
48 proxy_cache_valid any 1y;
|
|
49 proxy_no_cache $arg_nocache;
|
|
50
|
|
51 proxy_intercept_errors on;
|
|
52 error_page 404 = @fallback;
|
|
53 }
|
|
54
|
|
55 location /t3 {
|
|
56 proxy_pass http://127.0.0.1:8081;
|
|
57
|
|
58 proxy_cache one;
|
|
59 proxy_cache_key $uri;
|
|
60 proxy_cache_valid any 1y;
|
|
61 proxy_no_cache $arg_nocache;
|
|
62 }
|
|
63
|
|
64 location /t4 {
|
|
65 proxy_pass http://127.0.0.1:8081;
|
|
66
|
|
67 proxy_cache one;
|
|
68 proxy_cache_key $uri;
|
|
69 proxy_cache_valid any 1s;
|
|
70 proxy_no_cache $upstream_http_x_no_cache;
|
|
71
|
|
72 proxy_cache_revalidate on;
|
|
73 }
|
|
74
|
|
75 location @fallback {
|
|
76 return 403;
|
|
77 }
|
|
78
|
|
79 add_header X-Cache-Status $upstream_cache_status always;
|
|
80 }
|
|
81
|
|
82 server {
|
|
83 listen 127.0.0.1:8081;
|
|
84 server_name localhost;
|
|
85
|
|
86 location / {
|
|
87 }
|
|
88
|
|
89 location /t3 {
|
|
90 set $nocache "";
|
|
91 if ($arg_expires) {
|
|
92 set $nocache "no-cache";
|
|
93 }
|
|
94 add_header Cache-Control $nocache;
|
|
95 add_header Transfer-Encoding invalid;
|
|
96 }
|
|
97
|
|
98 location /t4 {
|
|
99 set $nocache "";
|
|
100 if ($arg_expires) {
|
|
101 set $nocache "no-cache";
|
|
102 }
|
|
103 add_header Cache-Control $nocache;
|
|
104 add_header X-No-Cache $arg_nocache;
|
|
105 }
|
|
106 }
|
|
107 }
|
|
108
|
|
109 EOF
|
|
110
|
|
111 $t->write_file('t', 'SEE-THIS');
|
|
112 $t->write_file('t3', 'SEE-THIS');
|
|
113 $t->write_file('t4', 'SEE-THIS');
|
|
114
|
|
115 $t->run();
|
|
116
|
|
117 ###############################################################################
|
|
118
|
|
119 like(http_get('/t?nocache=1'), qr/MISS.*SEE-THIS/s, 'request');
|
|
120 like(http_get('/t'), qr/MISS.*SEE-THIS/s, 'request not cached');
|
|
121 like(http_get('/t'), qr/HIT.*SEE-THIS/s, 'request cached');
|
|
122
|
|
123 # proxy_no_cache with intercepted errors,
|
|
124 # ngx_http_upstream_intercept_errors()
|
|
125
|
|
126 like(http_get('/t2?nocache=1'), qr/403 Forbidden/, 'intercepted error');
|
|
127
|
|
128 TODO: {
|
|
129 local $TODO = 'not yet' unless $t->has_version('1.27.2');
|
|
130
|
|
131 like(http_get('/t2'), qr/403 Forbidden.*MISS/s, 'intercepted error not cached');
|
|
132
|
|
133 }
|
|
134
|
|
135 like(http_get('/t2'), qr/403 Forbidden.*HIT/s, 'intercepted error cached');
|
|
136
|
|
137 # proxy_no_cache with internal 502/504 errors,
|
|
138 # ngx_http_upstream_finalize_request()
|
|
139
|
|
140 like(http_get('/t3?nocache=1'), qr/502 Bad/, 'internal 502 error');
|
|
141
|
|
142 TODO: {
|
|
143 local $TODO = 'not yet' unless $t->has_version('1.27.2');
|
|
144
|
|
145 like(http_get('/t3?expires=1'), qr/502 Bad.*MISS/s,
|
|
146 'internal 502 error expires');
|
|
147 like(http_get('/t3'), qr/502 Bad.*MISS/s, 'internal 502 error not cached');
|
|
148
|
|
149 }
|
|
150
|
|
151 like(http_get('/t3'), qr/502 Bad.*HIT/s, 'internal 502 error cached');
|
|
152
|
|
153 # proxy_no_cache with revalidate and 304,
|
|
154 # ngx_http_upstream_test_next()
|
|
155
|
|
156 like(http_get('/t4'), qr/MISS.*SEE-THIS/s, 'revalidate');
|
|
157 like(http_get('/t4'), qr/HIT.*SEE-THIS/s, 'revalidate cached');
|
|
158 select undef, undef, undef, 2.5;
|
|
159 like(http_get('/t4?nocache=1'), qr/REVALIDATED.*SEE-THIS/s,
|
|
160 'revalidate nocache');
|
|
161
|
|
162 TODO: {
|
|
163 local $TODO = 'not yet' unless $t->has_version('1.27.2');
|
|
164
|
|
165 like(http_get('/t4?expires=1'), qr/REVALIDATED.*SEE-THIS/s,
|
|
166 'revalidate expires');
|
|
167 like(http_get('/t4'), qr/REVALIDATED.*SEE-THIS/s,
|
|
168 'revalidate again');
|
|
169
|
|
170 }
|
|
171
|
|
172 like(http_get('/t4'), qr/HIT.*SEE-THIS/s, 'revalidate again cached');
|
|
173
|
|
174 ###############################################################################
|