comparison stream_proxy_protocol_ipv6.t @ 816:77359b849cd5

Tests: stream package.
author Andrey Zelenkov <zelenkov@nginx.com>
date Mon, 20 Jul 2015 15:06:09 +0300
parents 824754da4afc
children e9064d691790
comparison
equal deleted inserted replaced
815:9f5f604a840e 816:77359b849cd5
17 17
18 BEGIN { use FindBin; chdir($FindBin::Bin); } 18 BEGIN { use FindBin; chdir($FindBin::Bin); }
19 19
20 use lib 'lib'; 20 use lib 'lib';
21 use Test::Nginx; 21 use Test::Nginx;
22 use Test::Nginx::Stream qw/ stream /;
22 23
23 ############################################################################### 24 ###############################################################################
24 25
25 select STDERR; $| = 1; 26 select STDERR; $| = 1;
26 select STDOUT; $| = 1; 27 select STDOUT; $| = 1;
64 $t->try_run('no inet6 support or stream proxy_protocol')->plan(2); 65 $t->try_run('no inet6 support or stream proxy_protocol')->plan(2);
65 $t->waitforsocket('127.0.0.1:8082'); 66 $t->waitforsocket('127.0.0.1:8082');
66 67
67 ############################################################################### 68 ###############################################################################
68 69
69 like(stream_get('close'), qr/PROXY TCP6 ::1 ::1 \d+ 8080$CRLF/, 'protocol on'); 70 like(stream()->io('close'), qr/PROXY TCP6 ::1 ::1 \d+ 8080$CRLF/,
70 unlike(stream_get('close', '127.0.0.1:8081'), qr/PROXY/, 'protocol off'); 71 'protocol on');
71 72 unlike(stream('127.0.0.1:8081')->io('close'), qr/PROXY/, 'protocol off');
72 ###############################################################################
73
74 sub stream_get {
75 my ($data, $peer) = @_;
76
77 my $s = stream_connect($peer);
78 stream_write($s, $data);
79
80 $data = '';
81 while (my $buf = stream_read($s)) {
82 $data .= $buf;
83 }
84
85 return $data;
86 }
87
88 sub stream_connect {
89 my $peer = shift;
90 my $s = IO::Socket::INET->new(
91 Proto => 'tcp',
92 PeerAddr => $peer || '127.0.0.1:8080'
93 )
94 or die "Can't connect to nginx: $!\n";
95
96 return $s;
97 }
98
99 sub stream_write {
100 my ($s, $message) = @_;
101
102 local $SIG{PIPE} = 'IGNORE';
103
104 $s->blocking(0);
105 while (IO::Select->new($s)->can_write(1.5)) {
106 my $n = $s->syswrite($message);
107 last unless $n;
108 $message = substr($message, $n);
109 last unless length $message;
110 }
111
112 if (length $message) {
113 $s->close();
114 }
115 }
116
117 sub stream_read {
118 my ($s) = @_;
119 my ($buf);
120
121 $s->blocking(0);
122 if (IO::Select->new($s)->can_read(5)) {
123 $s->sysread($buf, 1024);
124 };
125
126 log_in($buf);
127 return $buf;
128 }
129 73
130 ############################################################################### 74 ###############################################################################
131 75
132 sub stream_daemon { 76 sub stream_daemon {
133 my $server = IO::Socket::INET->new( 77 my $server = IO::Socket::INET->new(