comparison proxy_unfinished.t @ 307:81c98592661f

Tests: more unfinished tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 26 Jun 2013 02:06:17 +0400
parents 44c42894fdfd
children e9de4da234c0
comparison
equal deleted inserted replaced
306:f175dc25f249 307:81c98592661f
31 select STDERR; $| = 1; 31 select STDERR; $| = 1;
32 select STDOUT; $| = 1; 32 select STDOUT; $| = 1;
33 33
34 plan(skip_all => 'win32') if $^O eq 'MSWin32'; 34 plan(skip_all => 'win32') if $^O eq 'MSWin32';
35 35
36 my $t = Test::Nginx->new()->has(qw/http proxy cache sub/)->plan(4) 36 my $t = Test::Nginx->new()->has(qw/http proxy cache sub/)->plan(15)
37 ->write_file_expand('nginx.conf', <<'EOF'); 37 ->write_file_expand('nginx.conf', <<'EOF');
38 38
39 %%TEST_GLOBALS%% 39 %%TEST_GLOBALS%%
40 40
41 daemon off; 41 daemon off;
57 sub_filter foo bar; 57 sub_filter foo bar;
58 sub_filter_types *; 58 sub_filter_types *;
59 proxy_pass http://127.0.0.1:8081; 59 proxy_pass http://127.0.0.1:8081;
60 } 60 }
61 61
62 location /un/ {
63 sub_filter foo bar;
64 sub_filter_types *;
65 proxy_pass http://127.0.0.1:8081/;
66 proxy_buffering off;
67 }
68
62 location /cache/ { 69 location /cache/ {
63 proxy_pass http://127.0.0.1:8081/; 70 proxy_pass http://127.0.0.1:8081/;
64 proxy_cache one; 71 proxy_cache one;
72 add_header X-Cache-Status $upstream_cache_status;
73 }
74
75 location /proxy/ {
76 sub_filter foo bar;
77 sub_filter_types *;
78 proxy_pass http://127.0.0.1:8080/local/;
79 proxy_buffer_size 1k;
80 proxy_buffers 4 1k;
81 }
82
83 location /local/ {
84 alias %%TESTDIR%%/;
65 } 85 }
66 } 86 }
67 } 87 }
68 88
69 EOF 89 EOF
90
91 $t->write_file('big.html', 'X' x (1024 * 1024) . 'finished');
70 92
71 $t->run_daemon(\&http_daemon); 93 $t->run_daemon(\&http_daemon);
72 $t->run()->waitforsocket('127.0.0.1:8081'); 94 $t->run()->waitforsocket('127.0.0.1:8081');
73 95
74 ############################################################################### 96 ###############################################################################
75 97
76 my ($r, $n); 98 http_get('/cache/length');
77 99 like(http_get('/cache/length'), qr/MISS/, 'unfinished not cached');
78 $r = http_get('/cache/length');
79 $r =~ m/unfinished (\d+)/; $n = $1 + 1;
80 like(http_get('/cache/length'), qr/unfinished $n/, 'unfinished not cached');
81 100
82 TODO: { 101 TODO: {
83 local $TODO = 'not yet'; 102 local $TODO = 'not yet';
84 103
85 # chunked encoding has enough information to don't cache a response, 104 # chunked encoding has enough information to don't cache a response,
86 # much like with Content-Length available 105 # much like with Content-Length available
87 106
88 $r = http_get('/cache/chunked'); 107 http_get('/cache/chunked');
89 $r =~ m/unfinished (\d+)/; $n = $1 + 1; 108 like(http_get('/cache/chunked'), qr/MISS/, 'unfinished chunked');
90 like(http_get('/cache/chunked'), qr/unfinished $n/, 'unfinished chunked'); 109
91 110 }
92 } 111
93 112 TODO: {
94 TODO: { 113 local $TODO = 'not yet';
95 local $TODO = 'not yet'; 114
96 115 # make sure there is no final chunk in unfinished responses
97 # make sure there is no final chunk in normal responses
98 116
99 like(http_get_11('/length'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s, 117 like(http_get_11('/length'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s,
100 'length no final chunk'); 118 'length no final chunk');
101 like(http_get_11('/chunked'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s, 119 like(http_get_11('/chunked'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s,
102 'chunked no final chunk'); 120 'chunked no final chunk');
103 121
104 } 122 }
105 123
124 # but there is final chunk in complete responses
125
126 like(http_get_11('/length/ok'), qr/finished.*\x0d\x0a?0\x0d\x0a?/s,
127 'length final chunk');
128 like(http_get_11('/chunked/ok'), qr/finished.*\x0d\x0a?0\x0d\x0a?/s,
129 'chunked final chunk');
130
131 TODO: {
132 local $TODO = 'not yet';
133
134 # the same with proxy_buffering set to off
135
136 like(http_get_11('/un/length'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s,
137 'unbuffered length no final chunk');
138 like(http_get_11('/un/chunked'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s,
139 'unbuffered chunked no final chunk');
140
141 }
142
143 like(http_get_11('/un/length/ok'), qr/finished.*\x0d\x0a?0\x0d\x0a?/s,
144 'unbuffered length final chunk');
145 like(http_get_11('/un/chunked/ok'), qr/finished.*\x0d\x0a?0\x0d\x0a?/s,
146 'unbuffered chunked final chunk');
147
148 # big responses
149
150 like(http_get('/big', sleep => 0.1), qr/unfinished/s, 'big unfinished');
151 like(http_get('/big/ok', sleep => 0.1), qr/finished/s, 'big finished');
152 like(http_get('/un/big', sleep => 0.1), qr/unfinished/s, 'big unfinished un');
153 like(http_get('/un/big/ok', sleep => 0.1), qr/finished/s, 'big finished un');
154
155 TODO: {
156 local $TODO = 'not yet';
157
158 # if disk buffering fails for some reason, there should be
159 # no final chunk
160
161 chmod(0000, $t->testdir() . '/proxy_temp');
162 like(http_get_11('/proxy/big.html', sleep => 0.5),
163 qr/X(?!.*\x0d\x0a?0\x0d\x0a?)/s, 'no proxy temp');
164
165 }
166
106 ############################################################################### 167 ###############################################################################
107 168
108 sub http_get_11 { 169 sub http_get_11 {
109 my ($uri) = @_; 170 my ($uri, %extra) = @_;
110 171
111 return http( 172 return http(
112 "GET $uri HTTP/1.1" . CRLF . 173 "GET $uri HTTP/1.1" . CRLF .
113 "Connection: close" . CRLF . 174 "Connection: close" . CRLF .
114 "Host: localhost" . CRLF . CRLF 175 "Host: localhost" . CRLF . CRLF,
176 %extra
115 ); 177 );
116 } 178 }
117 179
118 ############################################################################### 180 ###############################################################################
119 181
126 ) 188 )
127 or die "Can't create listening socket: $!\n"; 189 or die "Can't create listening socket: $!\n";
128 190
129 local $SIG{PIPE} = 'IGNORE'; 191 local $SIG{PIPE} = 'IGNORE';
130 192
131 my $num = 0;
132
133 while (my $client = $server->accept()) { 193 while (my $client = $server->accept()) {
134 $client->autoflush(1); 194 $client->autoflush(1);
135 195
136 my $headers = ''; 196 my $headers = '';
137 my $uri = ''; 197 my $uri = '';
140 $headers .= $_; 200 $headers .= $_;
141 last if (/^\x0d?\x0a?$/); 201 last if (/^\x0d?\x0a?$/);
142 } 202 }
143 203
144 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; 204 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
145 $num++;
146 205
147 if ($uri eq '/length') { 206 if ($uri eq '/length') {
148 print $client 207 print $client
149 "HTTP/1.1 200 OK" . CRLF . 208 "HTTP/1.1 200 OK" . CRLF .
150 "Content-Length: 100" . CRLF . 209 "Content-Length: 100" . CRLF .
151 "Cache-Control: max-age=300" . CRLF . 210 "Cache-Control: max-age=300" . CRLF .
152 "Connection: close" . CRLF . 211 "Connection: close" . CRLF .
153 CRLF . 212 CRLF .
154 "unfinished $num" . CRLF; 213 "unfinished" . CRLF;
214
215 } elsif ($uri eq '/length/ok') {
216 print $client
217 "HTTP/1.1 200 OK" . CRLF .
218 "Content-Length: 10" . CRLF .
219 "Cache-Control: max-age=300" . CRLF .
220 "Connection: close" . CRLF .
221 CRLF .
222 "finished" . CRLF;
223
224 } elsif ($uri eq '/big') {
225 print $client
226 "HTTP/1.1 200 OK" . CRLF .
227 "Content-Length: 1000100" . CRLF .
228 "Cache-Control: max-age=300" . CRLF .
229 "Connection: close" . CRLF .
230 CRLF;
231 for (1 .. 10000) {
232 print $client ("X" x 98) . CRLF;
233 }
234 print $client "unfinished" . CRLF;
235
236 } elsif ($uri eq '/big/ok') {
237 print $client
238 "HTTP/1.1 200 OK" . CRLF .
239 "Content-Length: 1000010" . CRLF .
240 "Cache-Control: max-age=300" . CRLF .
241 "Connection: close" . CRLF .
242 CRLF;
243 for (1 .. 10000) {
244 print $client ("X" x 98) . CRLF;
245 }
246 print $client "finished" . CRLF;
155 247
156 } elsif ($uri eq '/chunked') { 248 } elsif ($uri eq '/chunked') {
157 print $client 249 print $client
158 "HTTP/1.1 200 OK" . CRLF . 250 "HTTP/1.1 200 OK" . CRLF .
159 "Transfer-Encoding: chunked" . CRLF . 251 "Transfer-Encoding: chunked" . CRLF .
160 "Cache-Control: max-age=300" . CRLF . 252 "Cache-Control: max-age=300" . CRLF .
161 "Connection: close" . CRLF . 253 "Connection: close" . CRLF .
162 CRLF . 254 CRLF .
163 "ff" . CRLF . 255 "ff" . CRLF .
164 "unfinished $num" . CRLF; 256 "unfinished" . CRLF;
257
258 } elsif ($uri eq '/chunked/ok') {
259 print $client
260 "HTTP/1.1 200 OK" . CRLF .
261 "Transfer-Encoding: chunked" . CRLF .
262 "Cache-Control: max-age=300" . CRLF .
263 "Connection: close" . CRLF .
264 CRLF .
265 "a" . CRLF .
266 "finished" . CRLF .
267 CRLF . "0" . CRLF . CRLF;
165 } 268 }
166 } 269 }
167 } 270 }
168 271
169 ############################################################################### 272 ###############################################################################