Mercurial > hg > nginx-tests
comparison proxy_cache.t @ 148:b714d6df958c
Tests: rename some tests for better sorting.
Use underscore instead of dash. Addtionally, rename some tests to better
match "module" + "details" scheme used: use "http_" prefix for http core
module tests, use "mail_" prefix for mail module tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 04 Mar 2011 16:07:15 +0300 |
parents | proxy-cache.t@2ea7cd95ff05 |
children | c0ae29632905 |
comparison
equal
deleted
inserted
replaced
147:fd865ada95c8 | 148:b714d6df958c |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for http proxy 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 qw/ :DEFAULT :gzip /; | |
18 | |
19 ############################################################################### | |
20 | |
21 select STDERR; $| = 1; | |
22 select STDOUT; $| = 1; | |
23 | |
24 my $t = Test::Nginx->new()->has(qw/http proxy cache gzip/)->plan(12) | |
25 ->write_file_expand('nginx.conf', <<'EOF'); | |
26 | |
27 %%TEST_GLOBALS%% | |
28 | |
29 master_process off; | |
30 daemon off; | |
31 | |
32 events { | |
33 } | |
34 | |
35 http { | |
36 %%TEST_GLOBALS_HTTP%% | |
37 | |
38 proxy_cache_path %%TESTDIR%%/cache levels=1:2 | |
39 keys_zone=NAME:10m; | |
40 | |
41 server { | |
42 listen 127.0.0.1:8080; | |
43 server_name localhost; | |
44 | |
45 gzip on; | |
46 gzip_min_length 0; | |
47 | |
48 location / { | |
49 proxy_pass http://127.0.0.1:8081; | |
50 | |
51 proxy_cache NAME; | |
52 | |
53 proxy_cache_valid 200 302 1s; | |
54 proxy_cache_valid 301 1d; | |
55 proxy_cache_valid any 1m; | |
56 | |
57 proxy_cache_min_uses 1; | |
58 | |
59 proxy_cache_use_stale error timeout invalid_header http_500 | |
60 http_404; | |
61 } | |
62 | |
63 location /fake/ { | |
64 proxy_pass http://127.0.0.1:8082; | |
65 proxy_cache NAME; | |
66 } | |
67 } | |
68 server { | |
69 listen 127.0.0.1:8081; | |
70 server_name localhost; | |
71 | |
72 location / { | |
73 } | |
74 } | |
75 } | |
76 | |
77 EOF | |
78 | |
79 $t->write_file('t.html', 'SEE-THIS'); | |
80 $t->write_file('t2.html', 'SEE-THIS'); | |
81 $t->write_file('empty.html', ''); | |
82 $t->run_daemon(\&http_fake_daemon); | |
83 $t->run(); | |
84 | |
85 ############################################################################### | |
86 | |
87 like(http_get('/t.html'), qr/SEE-THIS/, 'proxy request'); | |
88 | |
89 $t->write_file('t.html', 'NOOP'); | |
90 like(http_get('/t.html'), qr/SEE-THIS/, 'proxy request cached'); | |
91 | |
92 unlike(http_head('/t2.html'), qr/SEE-THIS/, 'head request'); | |
93 like(http_get('/t2.html'), qr/SEE-THIS/, 'get after head'); | |
94 unlike(http_head('/t2.html'), qr/SEE-THIS/, 'head after get'); | |
95 | |
96 like(http_get_range('/t.html', 'Range: bytes=4-'), qr/^THIS/m, 'cached range'); | |
97 like(http_get_range('/t.html', 'Range: bytes=0-2,4-'), qr/^SEE.*^THIS/ms, | |
98 'cached multipart range'); | |
99 | |
100 like(http_get('/empty.html'), qr/HTTP/, 'empty get first'); | |
101 like(http_get('/empty.html'), qr/HTTP/, 'empty get second'); | |
102 | |
103 { | |
104 local $TODO = 'not fixed yet'; | |
105 | |
106 sleep(2); | |
107 unlink $t->testdir() . '/t.html'; | |
108 like(http_gzip_request('/t.html'), | |
109 qr/HTTP.*1c\x0d\x0a.{28}\x0d\x0a0\x0d\x0a\x0d\x0a\z/s, | |
110 'non-empty get stale'); | |
111 } | |
112 | |
113 { | |
114 local $TODO = 'broken in 0.8.31'; | |
115 | |
116 unlink $t->testdir() . '/empty.html'; | |
117 like(http_gzip_request('/empty.html'), | |
118 qr/HTTP.*14\x0d\x0a.{20}\x0d\x0a0\x0d\x0a\x0d\x0a\z/s, | |
119 'empty get stale'); | |
120 } | |
121 | |
122 { | |
123 local $TODO = 'patch pending'; | |
124 | |
125 http_get('/fake/unfinished'); | |
126 like(http_get('/fake/unfinished'), qr/unfinished 2/, 'unfinished not cached'); | |
127 } | |
128 | |
129 ############################################################################### | |
130 | |
131 sub http_get_range { | |
132 my ($url, $extra) = @_; | |
133 return http(<<EOF); | |
134 GET $url HTTP/1.1 | |
135 Host: localhost | |
136 Connection: close | |
137 $extra | |
138 | |
139 EOF | |
140 } | |
141 | |
142 ############################################################################### | |
143 | |
144 sub http_fake_daemon { | |
145 my $server = IO::Socket::INET->new( | |
146 Proto => 'tcp', | |
147 LocalAddr => '127.0.0.1:8082', | |
148 Listen => 5, | |
149 Reuse => 1 | |
150 ) | |
151 or die "Can't create listening socket: $!\n"; | |
152 | |
153 my $num = 0; | |
154 | |
155 while (my $client = $server->accept()) { | |
156 $client->autoflush(1); | |
157 | |
158 while (<$client>) { | |
159 last if (/^\x0d?\x0a?$/); | |
160 } | |
161 | |
162 $num++; | |
163 print $client <<"EOF"; | |
164 HTTP/1.1 200 OK | |
165 Content-Length: 100 | |
166 Cache-Control: max-age=300 | |
167 Connection: close | |
168 | |
169 unfinished $num | |
170 EOF | |
171 } | |
172 } | |
173 | |
174 ############################################################################### |