AnyEvent-WebSocket-Server

 view release on metacpan or  search on metacpan

t/error.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use FindBin;
use lib ($FindBin::RealBin);
use testlib::Util qw(start_server set_timeout);
use testlib::ConnConfig;
use AnyEvent::WebSocket::Server;
use Try::Tiny;
use Protocol::WebSocket::Handshake::Client;
use Protocol::WebSocket::Frame;
use AnyEvent::Handle;

set_timeout;

sub establish_error_case {
    my (%args) = @_;
    my ($label, $code) = @args{qw(label code)};
    subtest $label, sub {
        if(defined($args{skip})) {
            plan skip_all => $args{skip};
        }
        testlib::ConnConfig->for_all_ok_conn_configs(sub {
            my ($cconfig) = @_;
            my $cv_finish = AnyEvent->condvar;
    
            my $server = AnyEvent::WebSocket::Server->new($cconfig->server_args);
            my $cv_port = start_server sub {
                my ($fh) = @_;
                $server->establish($fh)->cb(sub {
                    my $cv_conn = shift;
                    try {
                        $cv_conn->recv;
                        fail("establish() should fail");
                        $cv_finish->croak("establish() should fail");
                    }catch {
                        my $e = shift;
                        $cv_finish->send($e, $fh);
                    };
                });
            };
            my $port = $cv_port->recv;
            $code->($cv_finish, $port, $cconfig);
        });
    };
}

subtest "give undef as fh", sub {
    my $server = AnyEvent::WebSocket::Server->new;
    my $cv_conn = $server->establish(undef);
    try {
        $cv_conn->recv;
        fail("establish should fail.");
    }catch {
        pass("establish should fail.");
    };
};

establish_error_case(
    label => "client closes the connection while sending the handshake request",
    code => sub {
        my ($cv_finish, $port, $cconfig) = @_;
        my $hs = Protocol::WebSocket::Handshake::Client->new(url => $cconfig->connect_url($port, "/"));
        my $hs_string = $hs->to_string;
        my $hs_partial = substr($hs_string, 0, int(length($hs_string) / 2));



( run in 1.237 second using v1.01-cache-2.11-cpan-39bf76dae61 )