Mercurial > hg > nginx-tests
diff stream_proxy_protocol.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 |
line wrap: on
line diff
--- a/stream_proxy_protocol.t +++ b/stream_proxy_protocol.t @@ -13,12 +13,13 @@ use strict; use Test::More; use IO::Select; -use Socket qw/ CRLF /; +use Socket qw/ $CRLF /; BEGIN { use FindBin; chdir($FindBin::Bin); } use lib 'lib'; use Test::Nginx; +use Test::Nginx::Stream qw/ stream /; ############################################################################### @@ -58,71 +59,12 @@ EOF ############################################################################### -my %r = stream_get('close'); -is($r{'data'}, "PROXY TCP4 127.0.0.1 127.0.0.1 $r{'sp'} 8080" . CRLF . "close", - 'protocol on'); - -%r = stream_get('close', '127.0.0.1:8082'); -is($r{'data'}, 'close', 'protocol off'); - -############################################################################### - -sub stream_get { - my ($data, $peer) = @_; - - my $s = stream_connect($peer); - my $sockport = $s->sockport(); - stream_write($s, $data); - - $data = ''; - while (my $buf = stream_read($s)) { - $data .= $buf; - } - - return ('data' => $data, 'sp' => $sockport); -} - -sub stream_connect { - my $peer = shift; - my $s = IO::Socket::INET->new( - Proto => 'tcp', - PeerAddr => $peer || '127.0.0.1:8080' - ) - or die "Can't connect to nginx: $!\n"; +my $s = stream(); +my $data = $s->io('close'); +my $sp = $s->sockport(); +is($data, "PROXY TCP4 127.0.0.1 127.0.0.1 $sp 8080${CRLF}close", 'protocol on'); - return $s; -} - -sub stream_write { - my ($s, $message) = @_; - - local $SIG{PIPE} = 'IGNORE'; - - $s->blocking(0); - while (IO::Select->new($s)->can_write(1.5)) { - my $n = $s->syswrite($message); - last unless $n; - $message = substr($message, $n); - last unless length $message; - } - - if (length $message) { - $s->close(); - } -} - -sub stream_read { - my ($s) = @_; - my ($buf); - - $s->blocking(0); - if (IO::Select->new($s)->can_read(5)) { - $s->sysread($buf, 1024); - }; - - log_in($buf); - return $buf; -} +is(stream('127.0.0.1:8082')->io('close'), 'close', 'protocol off'); ###############################################################################