# HG changeset patch # User Sergey Kandaurov # Date 1404463034 -14400 # Node ID daea9cf92c1414f9d68283bfac4214b95b0122df # Parent 7cb6af00afdd22c11c0d0a622cbe6ce260095645 Tests: added proxy protocol tests over SPDY. diff --git a/spdy.t b/spdy.t --- a/spdy.t +++ b/spdy.t @@ -13,6 +13,7 @@ use strict; use Test::More; use IO::Select; +use Socket qw/ CRLF /; BEGIN { use FindBin; chdir($FindBin::Bin); } @@ -34,9 +35,10 @@ eval { plan(skip_all => 'Compress::Raw::Zlib not installed') if $@; plan(skip_all => 'win32') if $^O eq 'MSWin32'; -my $t = Test::Nginx->new()->has(qw/http proxy cache limit_conn rewrite spdy/); +my $t = Test::Nginx->new() + ->has(qw/http proxy cache limit_conn rewrite spdy realip/); -$t->plan(72)->write_file_expand('nginx.conf', <<'EOF'); +$t->plan(74)->write_file_expand('nginx.conf', <<'EOF'); %%TEST_GLOBALS%% @@ -54,12 +56,19 @@ http { server { listen 127.0.0.1:8080 spdy; listen 127.0.0.1:8081; + listen 127.0.0.1:8082 proxy_protocol spdy; server_name localhost; location /s { add_header X-Header X-Foo; return 200 'body'; } + location /pp { + set_real_ip_from 127.0.0.1/32; + real_ip_header proxy_protocol; + alias %%TESTDIR%%/t2.html; + add_header X-PP $remote_addr; + } location /spdy { return 200 $spdy; } @@ -166,6 +175,17 @@ is($frame->{headers}->{'x-header'}, 'X-F ($frame) = grep { $_->{type} eq "DATA" } @$frames; is($frame, undef, 'HEAD no body'); +# GET with PROXY protocol + +my $proxy = 'PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF; +$sess = new_session(8082, proxy => $proxy); +$sid1 = spdy_stream($sess, { path => '/pp' }); +$frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); + +($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; +ok($frame, 'PROXY SYN_REPLAY frame'); +is($frame->{headers}->{'x-pp'}, '192.0.2.1', 'PROXY remote addr'); + # request header $sess = new_session(); @@ -872,7 +892,8 @@ sub raw_write { } sub new_session { - my ($d, $i, $status); + my ($port, %extra) = @_; + my ($d, $i, $status, $s); ($d, $status) = Compress::Raw::Zlib::Deflate->new( -WindowBits => 12, @@ -887,20 +908,29 @@ sub new_session { ); fail "Zlib failure: $status" unless $i; + $s = new_socket($port); + + if ($extra{proxy}) { + raw_write($s, $extra{proxy}); + } + return { zlib => { i => $i, d => $d }, - socket => new_socket(), last_stream => -1 }; + socket => $s, last_stream => -1 }; } sub new_socket { + my ($port) = @_; my $s; + $port = 8080 unless defined $port; + eval { local $SIG{ALRM} = sub { die "timeout\n" }; local $SIG{PIPE} = sub { die "sigpipe\n" }; alarm(2); $s = IO::Socket::INET->new( Proto => 'tcp', - PeerAddr => '127.0.0.1:8080', + PeerAddr => "127.0.0.1:$port", ); alarm(0); };