comparison spdy.t @ 427:daea9cf92c14

Tests: added proxy protocol tests over SPDY.
author Sergey Kandaurov <pluknet@nginx.com>
date Fri, 04 Jul 2014 12:37:14 +0400
parents 7cb6af00afdd
children e95c3340d94a
comparison
equal deleted inserted replaced
426:7cb6af00afdd 427:daea9cf92c14
11 use strict; 11 use strict;
12 12
13 use Test::More; 13 use Test::More;
14 14
15 use IO::Select; 15 use IO::Select;
16 use Socket qw/ CRLF /;
16 17
17 BEGIN { use FindBin; chdir($FindBin::Bin); } 18 BEGIN { use FindBin; chdir($FindBin::Bin); }
18 19
19 use lib 'lib'; 20 use lib 'lib';
20 use Test::Nginx; 21 use Test::Nginx;
32 Compress::Raw::Zlib->WANT_GZIP_OR_ZLIB; 33 Compress::Raw::Zlib->WANT_GZIP_OR_ZLIB;
33 }; 34 };
34 plan(skip_all => 'Compress::Raw::Zlib not installed') if $@; 35 plan(skip_all => 'Compress::Raw::Zlib not installed') if $@;
35 plan(skip_all => 'win32') if $^O eq 'MSWin32'; 36 plan(skip_all => 'win32') if $^O eq 'MSWin32';
36 37
37 my $t = Test::Nginx->new()->has(qw/http proxy cache limit_conn rewrite spdy/); 38 my $t = Test::Nginx->new()
38 39 ->has(qw/http proxy cache limit_conn rewrite spdy realip/);
39 $t->plan(72)->write_file_expand('nginx.conf', <<'EOF'); 40
41 $t->plan(74)->write_file_expand('nginx.conf', <<'EOF');
40 42
41 %%TEST_GLOBALS%% 43 %%TEST_GLOBALS%%
42 44
43 daemon off; 45 daemon off;
44 46
52 limit_conn_zone $binary_remote_addr zone=conn:1m; 54 limit_conn_zone $binary_remote_addr zone=conn:1m;
53 55
54 server { 56 server {
55 listen 127.0.0.1:8080 spdy; 57 listen 127.0.0.1:8080 spdy;
56 listen 127.0.0.1:8081; 58 listen 127.0.0.1:8081;
59 listen 127.0.0.1:8082 proxy_protocol spdy;
57 server_name localhost; 60 server_name localhost;
58 61
59 location /s { 62 location /s {
60 add_header X-Header X-Foo; 63 add_header X-Header X-Foo;
61 return 200 'body'; 64 return 200 'body';
65 }
66 location /pp {
67 set_real_ip_from 127.0.0.1/32;
68 real_ip_header proxy_protocol;
69 alias %%TESTDIR%%/t2.html;
70 add_header X-PP $remote_addr;
62 } 71 }
63 location /spdy { 72 location /spdy {
64 return 200 $spdy; 73 return 200 $spdy;
65 } 74 }
66 location /prio { 75 location /prio {
164 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header HEAD'); 173 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header HEAD');
165 174
166 ($frame) = grep { $_->{type} eq "DATA" } @$frames; 175 ($frame) = grep { $_->{type} eq "DATA" } @$frames;
167 is($frame, undef, 'HEAD no body'); 176 is($frame, undef, 'HEAD no body');
168 177
178 # GET with PROXY protocol
179
180 my $proxy = 'PROXY TCP4 192.0.2.1 192.0.2.2 1234 5678' . CRLF;
181 $sess = new_session(8082, proxy => $proxy);
182 $sid1 = spdy_stream($sess, { path => '/pp' });
183 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]);
184
185 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames;
186 ok($frame, 'PROXY SYN_REPLAY frame');
187 is($frame->{headers}->{'x-pp'}, '192.0.2.1', 'PROXY remote addr');
188
169 # request header 189 # request header
170 190
171 $sess = new_session(); 191 $sess = new_session();
172 $sid1 = spdy_stream($sess, { path => '/t1.html', 192 $sid1 = spdy_stream($sess, { path => '/t1.html',
173 headers => { "range" => "bytes=10-19" } 193 headers => { "range" => "bytes=10-19" }
870 last unless length $message; 890 last unless length $message;
871 } 891 }
872 } 892 }
873 893
874 sub new_session { 894 sub new_session {
875 my ($d, $i, $status); 895 my ($port, %extra) = @_;
896 my ($d, $i, $status, $s);
876 897
877 ($d, $status) = Compress::Raw::Zlib::Deflate->new( 898 ($d, $status) = Compress::Raw::Zlib::Deflate->new(
878 -WindowBits => 12, 899 -WindowBits => 12,
879 -Dictionary => dictionary(), 900 -Dictionary => dictionary(),
880 -Level => Compress::Raw::Zlib->Z_NO_COMPRESSION 901 -Level => Compress::Raw::Zlib->Z_NO_COMPRESSION
885 -WindowBits => Compress::Raw::Zlib->WANT_GZIP_OR_ZLIB, 906 -WindowBits => Compress::Raw::Zlib->WANT_GZIP_OR_ZLIB,
886 -Dictionary => dictionary() 907 -Dictionary => dictionary()
887 ); 908 );
888 fail "Zlib failure: $status" unless $i; 909 fail "Zlib failure: $status" unless $i;
889 910
911 $s = new_socket($port);
912
913 if ($extra{proxy}) {
914 raw_write($s, $extra{proxy});
915 }
916
890 return { zlib => { i => $i, d => $d }, 917 return { zlib => { i => $i, d => $d },
891 socket => new_socket(), last_stream => -1 }; 918 socket => $s, last_stream => -1 };
892 } 919 }
893 920
894 sub new_socket { 921 sub new_socket {
922 my ($port) = @_;
895 my $s; 923 my $s;
924
925 $port = 8080 unless defined $port;
896 926
897 eval { 927 eval {
898 local $SIG{ALRM} = sub { die "timeout\n" }; 928 local $SIG{ALRM} = sub { die "timeout\n" };
899 local $SIG{PIPE} = sub { die "sigpipe\n" }; 929 local $SIG{PIPE} = sub { die "sigpipe\n" };
900 alarm(2); 930 alarm(2);
901 $s = IO::Socket::INET->new( 931 $s = IO::Socket::INET->new(
902 Proto => 'tcp', 932 Proto => 'tcp',
903 PeerAddr => '127.0.0.1:8080', 933 PeerAddr => "127.0.0.1:$port",
904 ); 934 );
905 alarm(0); 935 alarm(0);
906 }; 936 };
907 alarm(0); 937 alarm(0);
908 938