Mercurial > hg > nginx-tests
changeset 1079:cbc8641a204e
Tests: fixed "proxy connect timeout" http tests.
Previously, proxy connection hadn't been established before delay so
proxy_connect_timeout had no chances to break connection. Now used backend
with delay greater than proxy_connect_timeout value.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Mon, 21 Nov 2016 02:07:21 +0300 |
parents | 36437be7b3f4 |
children | cd4395a68fc6 |
files | proxy.t proxy_ssl.t |
diffstat | 2 files changed, 76 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/proxy.t Thu Nov 03 13:20:20 2016 +0300 +++ b/proxy.t Mon Nov 21 02:07:21 2016 +0300 @@ -56,6 +56,11 @@ proxy_read_timeout 1s; proxy_connect_timeout 2s; } + + location /timeout { + proxy_pass http://127.0.0.1:8081; + proxy_connect_timeout 2s; + } } } @@ -89,11 +94,7 @@ } -my $s = http('', start => 1); - -sleep 3; - -like(http_get('/', socket => $s), qr/200 OK/, 'proxy connect timeout'); +like(http_get('/timeout'), qr/200 OK/, 'proxy connect timeout'); ############################################################################### @@ -142,6 +143,15 @@ select undef, undef, undef, 0.1; print $client 'AND-THIS'; + } elsif ($uri eq '/timeout') { + sleep 3; + + print $client <<"EOF"; +HTTP/1.1 200 OK +Connection: close + +EOF + } else { print $client <<"EOF";
--- a/proxy_ssl.t Thu Nov 03 13:20:20 2016 +0300 +++ b/proxy_ssl.t Mon Nov 21 02:07:21 2016 +0300 @@ -21,6 +21,9 @@ select STDERR; $| = 1; select STDOUT; $| = 1; +eval { require IO::Socket::SSL; }; +plan(skip_all => 'IO::Socket::SSL not installed') if $@; + my $t = Test::Nginx->new()->has(qw/http proxy http_ssl/)->has_daemon('openssl') ->plan(5)->write_file_expand('nginx.conf', <<'EOF'); @@ -61,7 +64,7 @@ } location /timeout { - proxy_pass https://127.0.0.1:8081/; + proxy_pass https://127.0.0.1:8082; proxy_connect_timeout 2s; } } @@ -89,7 +92,9 @@ or die "Can't create certificate for $name: $!\n"; } +$t->run_daemon(\&http_daemon, port(8082)); $t->run(); +$t->waitforsocket('127.0.0.1:' . port(8082)); ############################################################################### @@ -97,11 +102,60 @@ like(http_get('/ssl'), qr/200 OK.*X-Session: \./s, 'ssl 2'); like(http_get('/ssl_reuse'), qr/200 OK.*X-Session: \./s, 'ssl reuse session'); like(http_get('/ssl_reuse'), qr/200 OK.*X-Session: r/s, 'ssl reuse session 2'); - -my $s = http('', start => 1); - -sleep 3; - -like(http_get('/timeout', socket => $s), qr/200 OK/, 'proxy connect timeout'); +like(http_get('/timeout'), qr/200 OK/, 'proxy connect timeout'); ############################################################################### + +sub http_daemon { + my ($port) = @_; + my $server = IO::Socket::INET->new( + Proto => 'tcp', + LocalHost => '127.0.0.1:' . $port, + Listen => 5, + Reuse => 1 + ) + or die "Can't create listening socket: $!\n"; + + local $SIG{PIPE} = 'IGNORE'; + + while (my $client = $server->accept()) { + $client->autoflush(1); + + my $headers = ''; + my $uri = ''; + + # would fail on waitforsocket + + eval { + IO::Socket::SSL->start_SSL($client, + SSL_server => 1, + SSL_cert_file => "$d/localhost.crt", + SSL_key_file => "$d/localhost.key", + SSL_error_trap => sub { die $_[1] } + ); + }; + next if $@; + + while (<$client>) { + $headers .= $_; + last if (/^\x0d?\x0a?$/); + } + + $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; + next if $uri eq ''; + + if ($uri eq '/timeout') { + sleep 3; + + print $client <<EOF; +HTTP/1.1 200 OK +Connection: close + +EOF + } + + close $client; + } +} + +###############################################################################