AnyEvent-WebSocket-Client

 view release on metacpan or  search on metacpan

t/anyevent_websocket_client__proxy.t  view on Meta::CPAN

use lib 't/lib';
use Test2::Plugin::AnyEvent::Timeout;
use Test2::V0 -no_srand => 1;
use AnyEvent;
use AnyEvent::WebSocket::Client;

sub get_env {
    my ($env_name, $desc) = @_;
    my $val = $ENV{$env_name};
    if(!defined($val) || $val eq "") {
        skip_all "Set $env_name environment variable to $desc to enable this test.";
    }
    return $val;
}

sub test_client_at {
    my ($client, $echo_url, $exp_conn_success) = @_;
    my $conn = eval { $client->connect($echo_url)->recv };
    my $err = $@;
    if(!$exp_conn_success) {
        is $conn, undef;
        like $err, qr/unable to connect/i;
        return;
    }
    isnt $conn, undef;
    is $err, '';
    my $res_cv = AnyEvent->condvar;
    $conn->on(next_message => sub {
        $res_cv->send($_[1]->decoded_body);
    });
    $conn->send("foo bar");
    my $got = $res_cv->recv;
    is $got, "foo bar", $echo_url;
}

sub test_client {
    my ($client, $exp_conn_success) = @_;
    test_client_at($client, "ws://echo.websocket.org/", $exp_conn_success);
    test_client_at($client, "wss://echo.websocket.org/", $exp_conn_success);
}


my $PROXY_URL = get_env("PERL_AE_WS_C_TEST_PROXY_URL", "the proxy URL");
my $PROXY_ON =  get_env("PERL_AE_WS_C_TEST_PROXY_ON", "0 (if the proxy is down) or 1 (if the proxy is up)");

note(<<'NOTE');
squid HTTP proxy denies connection to ports other than 443 (HTTPS) by default.
In this case, this test fails. To pass the test, you have to configure squid.conf
to allow connection to 80 (HTTP) and 443. For example,

  ## http_access deny !Safe_ports
  http_access allow CONNECT Safe_ports
NOTE

foreach my $n (qw(ws http wss https)) {
    my $e = "${n}_proxy";
    delete $ENV{lc($e)};
    delete $ENV{uc($e)};
}

subtest "no proxy", sub {
    test_client(AnyEvent::WebSocket::Client->new(), 1);

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.588 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )