comparison proxy_cache_revalidate.t @ 353:19f58e91617a

Tests: proxy cache revalidate tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 14 Nov 2013 17:24:40 +0400
parents
children fb366c51eac6
comparison
equal deleted inserted replaced
352:145c37f27c5a 353:19f58e91617a
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4
5 # Tests for http proxy cache revalidation with conditional requests.
6
7 ###############################################################################
8
9 use warnings;
10 use strict;
11
12 use Test::More;
13 use Socket qw/ CRLF /;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx qw/ :DEFAULT :gzip /;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 plan(skip_all => 'win32') if $^O eq 'MSWin32';
26
27 my $t = Test::Nginx->new()->has(qw/http proxy cache/)
28 ->write_file_expand('nginx.conf', <<'EOF');
29
30 %%TEST_GLOBALS%%
31
32 daemon off;
33
34 events {
35 }
36
37 http {
38 %%TEST_GLOBALS_HTTP%%
39
40 proxy_cache_path %%TESTDIR%%/cache levels=1:2
41 keys_zone=one:1m;
42
43 proxy_cache_revalidate on;
44
45 server {
46 listen 127.0.0.1:8080;
47 server_name localhost;
48
49 location / {
50 proxy_pass http://127.0.0.1:8081;
51 proxy_cache one;
52
53 proxy_cache_valid 200 1s;
54
55 add_header X-Cache-Status $upstream_cache_status;
56 }
57 }
58
59 server {
60 listen 127.0.0.1:8081;
61 server_name localhost;
62 }
63 }
64
65 EOF
66
67 $t->write_file('t', 'SEE-THIS');
68 $t->write_file('t2', 'SEE-THIS');
69
70 eval {
71 open OLDERR, ">&", \*STDERR; close STDERR;
72 $t->run();
73 open STDERR, ">&", \*OLDERR;
74 };
75 plan(skip_all => 'no proxy_cache_revalidate') if $@;
76 $t->plan(9);
77
78 ###############################################################################
79
80 # request documents and make sure they are cached
81
82 like(http_get('/t'), qr/X-Cache-Status: MISS.*SEE/ms, 'request');
83 like(http_get('/t'), qr/X-Cache-Status: HIT.*SEE/ms, 'request cached');
84
85 like(http_get('/t2'), qr/X-Cache-Status: MISS.*SEE/ms, '2nd request');
86 like(http_get('/t2'), qr/X-Cache-Status: HIT.*SEE/ms, '2nd request cached');
87
88 # wait for a while for cached responses to expire
89
90 select undef, undef, undef, 2.5;
91
92 # 1st document isn't modified, and should be revalidated on first request
93 # (a 304 status code will appear in backend's logs), then cached again
94
95 like(http_get('/t'), qr/X-Cache-Status: REVALIDATED.*SEE/ms, 'revalidated');
96 like(read_file($t->testdir() . '/access.log'), qr/ 304 /, 'not modified');
97 like(http_get('/t'), qr/X-Cache-Status: HIT.*SEE/ms, 'request cached');
98
99 # 2nd document is recreated with a new content
100
101 $t->write_file('t2', 'NEW');
102 like(http_get('/t2'), qr/X-Cache-Status: EXPIRED.*NEW/ms, 'revalidate failed');
103 like(http_get('/t2'), qr/X-Cache-Status: HIT.*NEW/ms, 'new response cached');
104
105 ###############################################################################
106
107 sub read_file {
108 my ($file) = @_;
109 my $log;
110
111 local $/;
112
113 open LOG, $file or die "Can't open $file: $!\n";
114 $log = <LOG>;
115 close LOG;
116
117 return $log;
118 }
119
120 ###############################################################################