# HG changeset patch # User Maxim Dounin # Date 1370956477 -14400 # Node ID 44c42894fdfd26d4874f01bd4b86aa5f42404272 # Parent e491290fe83ab23953a38519eb834a7abe6105c1 Tests: move unfinished tests to a separate file. Add more tests to catch unfinished chunked responses, as well as proxy-only aspect of the problem (we shouldn't send final chunk if we know the response isn't complete). diff --git a/proxy_cache.t b/proxy_cache.t --- a/proxy_cache.t +++ b/proxy_cache.t @@ -23,7 +23,7 @@ select STDOUT; $| = 1; plan(skip_all => 'win32') if $^O eq 'MSWin32'; -my $t = Test::Nginx->new()->has(qw/http proxy cache gzip/)->plan(12) +my $t = Test::Nginx->new()->has(qw/http proxy cache gzip/)->plan(11) ->write_file_expand('nginx.conf', <<'EOF'); %%TEST_GLOBALS%% @@ -60,11 +60,6 @@ http { proxy_cache_use_stale error timeout invalid_header http_500 http_404; } - - location /fake/ { - proxy_pass http://127.0.0.1:8082; - proxy_cache NAME; - } } server { listen 127.0.0.1:8081; @@ -81,8 +76,7 @@ EOF $t->write_file('t2.html', 'SEE-THIS'); $t->write_file('empty.html', ''); -$t->run_daemon(\&http_fake_daemon); -$t->run()->waitforsocket('127.0.0.1:8081'); +$t->run(); ############################################################################### @@ -113,9 +107,6 @@ like(http_gzip_request('/empty.html'), qr/HTTP.*14\x0d\x0a.{20}\x0d\x0a0\x0d\x0a\x0d\x0a\z/s, 'empty get stale'); -http_get('/fake/unfinished'); -like(http_get('/fake/unfinished'), qr/unfinished 2/, 'unfinished not cached'); - ############################################################################### sub http_get_range { @@ -130,37 +121,3 @@ EOF } ############################################################################### - -sub http_fake_daemon { - my $server = IO::Socket::INET->new( - Proto => 'tcp', - LocalAddr => '127.0.0.1:8082', - Listen => 5, - Reuse => 1 - ) - or die "Can't create listening socket: $!\n"; - - local $SIG{PIPE} = 'IGNORE'; - - my $num = 0; - - while (my $client = $server->accept()) { - $client->autoflush(1); - - while (<$client>) { - last if (/^\x0d?\x0a?$/); - } - - $num++; - print $client <<"EOF"; -HTTP/1.1 200 OK -Content-Length: 100 -Cache-Control: max-age=300 -Connection: close - -unfinished $num -EOF - } -} - -############################################################################### diff --git a/proxy_unfinished.t b/proxy_unfinished.t new file mode 100644 --- /dev/null +++ b/proxy_unfinished.t @@ -0,0 +1,169 @@ +#!/usr/bin/perl + +# (C) Maxim Dounin + +# Tests for http proxy and prematurely closed connections. Incomplete +# responses shouldn't loose information about their incompleteness. + +# In particular, incomplete responses: +# +# - shouldn't be cached +# +# - if a response is sent using chunked transfer encoding, +# final chunk shouldn't be sent + +############################################################################### + +use warnings; +use strict; + +use Test::More; + +use Socket qw/ CRLF /; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +plan(skip_all => 'win32') if $^O eq 'MSWin32'; + +my $t = Test::Nginx->new()->has(qw/http proxy cache sub/)->plan(4) + ->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + proxy_cache_path %%TESTDIR%%/cache levels=1:2 + keys_zone=one:1m; + + server { + listen 127.0.0.1:8080; + server_name localhost; + + location / { + sub_filter foo bar; + sub_filter_types *; + proxy_pass http://127.0.0.1:8081; + } + + location /cache/ { + proxy_pass http://127.0.0.1:8081/; + proxy_cache one; + } + } +} + +EOF + +$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'); + +TODO: { +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'); + +} + +TODO: { +local $TODO = 'not yet'; + +# make sure there is no final chunk in normal responses + +like(http_get_11('/length'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s, + 'length no final chunk'); +like(http_get_11('/chunked'), qr/unfinished(?!.*\x0d\x0a?0\x0d\x0a?)/s, + 'chunked no final chunk'); + +} + +############################################################################### + +sub http_get_11 { + my ($uri) = @_; + + return http( + "GET $uri HTTP/1.1" . CRLF . + "Connection: close" . CRLF . + "Host: localhost" . CRLF . CRLF + ); +} + +############################################################################### + +sub http_daemon { + my $server = IO::Socket::INET->new( + Proto => 'tcp', + LocalAddr => '127.0.0.1:8081', + Listen => 5, + Reuse => 1 + ) + or die "Can't create listening socket: $!\n"; + + local $SIG{PIPE} = 'IGNORE'; + + my $num = 0; + + while (my $client = $server->accept()) { + $client->autoflush(1); + + my $headers = ''; + my $uri = ''; + + while (<$client>) { + $headers .= $_; + last if (/^\x0d?\x0a?$/); + } + + $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; + $num++; + + if ($uri eq '/length') { + print $client + "HTTP/1.1 200 OK" . CRLF . + "Content-Length: 100" . CRLF . + "Cache-Control: max-age=300" . CRLF . + "Connection: close" . CRLF . + CRLF . + "unfinished $num" . CRLF; + + } elsif ($uri eq '/chunked') { + print $client + "HTTP/1.1 200 OK" . CRLF . + "Transfer-Encoding: chunked" . CRLF . + "Cache-Control: max-age=300" . CRLF . + "Connection: close" . CRLF . + CRLF . + "ff" . CRLF . + "unfinished $num" . CRLF; + } + } +} + +###############################################################################