# HG changeset patch # User Maxim Dounin # Date 1372197977 -14400 # Node ID 81c98592661f894852fa048c8a313b3b46196c39 # Parent f175dc25f2490dc0d6dcf2e9c77bacf630f5363a Tests: more unfinished tests. diff --git a/proxy_unfinished.t b/proxy_unfinished.t --- a/proxy_unfinished.t +++ b/proxy_unfinished.t @@ -33,7 +33,7 @@ select STDOUT; $| = 1; plan(skip_all => 'win32') if $^O eq 'MSWin32'; -my $t = Test::Nginx->new()->has(qw/http proxy cache sub/)->plan(4) +my $t = Test::Nginx->new()->has(qw/http proxy cache sub/)->plan(15) ->write_file_expand('nginx.conf', <<'EOF'); %%TEST_GLOBALS%% @@ -59,25 +59,44 @@ http { proxy_pass http://127.0.0.1:8081; } + location /un/ { + sub_filter foo bar; + sub_filter_types *; + proxy_pass http://127.0.0.1:8081/; + proxy_buffering off; + } + location /cache/ { proxy_pass http://127.0.0.1:8081/; proxy_cache one; + add_header X-Cache-Status $upstream_cache_status; + } + + location /proxy/ { + sub_filter foo bar; + sub_filter_types *; + proxy_pass http://127.0.0.1:8080/local/; + proxy_buffer_size 1k; + proxy_buffers 4 1k; + } + + location /local/ { + alias %%TESTDIR%%/; } } } EOF +$t->write_file('big.html', 'X' x (1024 * 1024) . 'finished'); + $t->run_daemon(\&http_daemon); $t->run()->waitforsocket('127.0.0.1:8081'); ############################################################################### -my ($r, $n); - -$r = http_get('/cache/length'); -$r =~ m/unfinished (\d+)/; $n = $1 + 1; -like(http_get('/cache/length'), qr/unfinished $n/, 'unfinished not cached'); +http_get('/cache/length'); +like(http_get('/cache/length'), qr/MISS/, 'unfinished not cached'); TODO: { local $TODO = 'not yet'; @@ -85,16 +104,15 @@ local $TODO = 'not yet'; # chunked encoding has enough information to don't cache a response, # much like with Content-Length available -$r = http_get('/cache/chunked'); -$r =~ m/unfinished (\d+)/; $n = $1 + 1; -like(http_get('/cache/chunked'), qr/unfinished $n/, 'unfinished chunked'); +http_get('/cache/chunked'); +like(http_get('/cache/chunked'), qr/MISS/, 'unfinished chunked'); } TODO: { local $TODO = 'not yet'; -# make sure there is no final chunk in normal responses +# make sure there is no final chunk in unfinished responses like(http_get_11('/length'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s, 'length no final chunk'); @@ -103,15 +121,59 @@ like(http_get_11('/chunked'), qr/unfinis } +# but there is final chunk in complete responses + +like(http_get_11('/length/ok'), qr/finished.*\x0d\x0a?0\x0d\x0a?/s, + 'length final chunk'); +like(http_get_11('/chunked/ok'), qr/finished.*\x0d\x0a?0\x0d\x0a?/s, + 'chunked final chunk'); + +TODO: { +local $TODO = 'not yet'; + +# the same with proxy_buffering set to off + +like(http_get_11('/un/length'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s, + 'unbuffered length no final chunk'); +like(http_get_11('/un/chunked'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s, + 'unbuffered chunked no final chunk'); + +} + +like(http_get_11('/un/length/ok'), qr/finished.*\x0d\x0a?0\x0d\x0a?/s, + 'unbuffered length final chunk'); +like(http_get_11('/un/chunked/ok'), qr/finished.*\x0d\x0a?0\x0d\x0a?/s, + 'unbuffered chunked final chunk'); + +# big responses + +like(http_get('/big', sleep => 0.1), qr/unfinished/s, 'big unfinished'); +like(http_get('/big/ok', sleep => 0.1), qr/finished/s, 'big finished'); +like(http_get('/un/big', sleep => 0.1), qr/unfinished/s, 'big unfinished un'); +like(http_get('/un/big/ok', sleep => 0.1), qr/finished/s, 'big finished un'); + +TODO: { +local $TODO = 'not yet'; + +# if disk buffering fails for some reason, there should be +# no final chunk + +chmod(0000, $t->testdir() . '/proxy_temp'); +like(http_get_11('/proxy/big.html', sleep => 0.5), + qr/X(?!.*\x0d\x0a?0\x0d\x0a?)/s, 'no proxy temp'); + +} + ############################################################################### sub http_get_11 { - my ($uri) = @_; + my ($uri, %extra) = @_; return http( "GET $uri HTTP/1.1" . CRLF . "Connection: close" . CRLF . - "Host: localhost" . CRLF . CRLF + "Host: localhost" . CRLF . CRLF, + %extra ); } @@ -128,8 +190,6 @@ sub http_daemon { local $SIG{PIPE} = 'IGNORE'; - my $num = 0; - while (my $client = $server->accept()) { $client->autoflush(1); @@ -142,7 +202,6 @@ sub http_daemon { } $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; - $num++; if ($uri eq '/length') { print $client @@ -151,7 +210,40 @@ sub http_daemon { "Cache-Control: max-age=300" . CRLF . "Connection: close" . CRLF . CRLF . - "unfinished $num" . CRLF; + "unfinished" . CRLF; + + } elsif ($uri eq '/length/ok') { + print $client + "HTTP/1.1 200 OK" . CRLF . + "Content-Length: 10" . CRLF . + "Cache-Control: max-age=300" . CRLF . + "Connection: close" . CRLF . + CRLF . + "finished" . CRLF; + + } elsif ($uri eq '/big') { + print $client + "HTTP/1.1 200 OK" . CRLF . + "Content-Length: 1000100" . CRLF . + "Cache-Control: max-age=300" . CRLF . + "Connection: close" . CRLF . + CRLF; + for (1 .. 10000) { + print $client ("X" x 98) . CRLF; + } + print $client "unfinished" . CRLF; + + } elsif ($uri eq '/big/ok') { + print $client + "HTTP/1.1 200 OK" . CRLF . + "Content-Length: 1000010" . CRLF . + "Cache-Control: max-age=300" . CRLF . + "Connection: close" . CRLF . + CRLF; + for (1 .. 10000) { + print $client ("X" x 98) . CRLF; + } + print $client "finished" . CRLF; } elsif ($uri eq '/chunked') { print $client @@ -161,7 +253,18 @@ sub http_daemon { "Connection: close" . CRLF . CRLF . "ff" . CRLF . - "unfinished $num" . CRLF; + "unfinished" . CRLF; + + } elsif ($uri eq '/chunked/ok') { + print $client + "HTTP/1.1 200 OK" . CRLF . + "Transfer-Encoding: chunked" . CRLF . + "Cache-Control: max-age=300" . CRLF . + "Connection: close" . CRLF . + CRLF . + "a" . CRLF . + "finished" . CRLF . + CRLF . "0" . CRLF . CRLF; } } }