AnyEvent-WebSocket-Server

 view release on metacpan or  search on metacpan

t/handshake.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;
use AnyEvent::Handle;
use AnyEvent::WebSocket::Server;
use AnyEvent::WebSocket::Client;
use Protocol::WebSocket::Handshake::Client;

set_timeout;

sub start_passive_server {
    my ($websocket_server, $finish_cb) = @_;
    $finish_cb ||= sub {};
    my $port_cv = start_server sub {
        my ($fh) = @_;
        $websocket_server->establish($fh)->cb(sub {
            my $conn = shift->recv;
            $conn->on(finish => sub {
                undef $conn;
                $finish_cb->();
            });
        });
    };
    return $port_cv;
}

sub client_connection {
    my ($cconfig, $port, $path) = @_;
    return AnyEvent::WebSocket::Client->new($cconfig->client_args)
        ->connect($cconfig->connect_url($port, $path))->recv;
}

sub get_raw_response {
    my ($cconfig, $port, $path) = @_;
    my $raw_response_cv = AnyEvent->condvar;
    my $hs = Protocol::WebSocket::Handshake::Client->new(url => $cconfig->connect_url($port, $path));
    my $handle; $handle = AnyEvent::Handle->new(
        $cconfig->client_handle_args($port),
        on_error => sub { $raw_response_cv->croak("client handle error: $_[2]"); },
        on_connect => sub {
            my ($handle) = @_;
            $handle->push_write($hs->to_string);
        },
        on_read => sub {
            my ($handle) = @_;
            if($handle->{rbuf} =~ s/^(.+\r\n\r\n)//s) {
                $raw_response_cv->send($1);
                $handle->push_shutdown();
                return;
            }
        },
        on_eof => sub {
            undef $handle;
        }
    );
    return $raw_response_cv;
}

sub handshake_error_case {
    my (%args) = @_;



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