AnyEvent-WebSocket-Client

 view release on metacpan or  search on metacpan

t/anyevent_websocket_connection.t  view on Meta::CPAN

    };

  }

};

subtest 'Connection should respond to a ping frame with a pong frame' => sub {

  my ($x_conn, $y_handle) = create_connection_and_handle;

  my $parser = Protocol::WebSocket::Frame->new;
  my $cv_finish = AnyEvent->condvar;
  $y_handle->on_read(sub {
    my ($handle) = @_;
    $parser->append($handle->{rbuf});
    my $payload = $parser->next_bytes;
    return if !defined($payload);
    is $parser->opcode, 10, "pong frame received";
    is $payload, "foobar", "... payload is identical to what b_handle has sent.";
    $cv_finish->send;
  });
  $y_handle->push_write(Protocol::WebSocket::Frame->new(type => "ping", buffer => "foobar")->to_bytes);

  $cv_finish->recv;
};

subtest 'connection close data' => sub {

  subtest 'ascii' => sub {

    my($x, $y) = create_connection_pair;

    my $cv = AnyEvent->condvar;
    my $reason;
    my $code;

    $y->on(finish => sub {
      my($con) = @_;
      $code   = $con->close_code;
      $reason = $con->close_reason;
      $cv->send;
    });

    $x->close(1009 => 'anything');

    $cv->recv;

    is $code,   1009,       'code is available in finish callback';
    is $reason, 'anything', 'reason is available in finish callback';

    is(
      $y,
      object {
        call close_code   => 1009;
        call close_reason => 'anything';
      },
      'connection has finish code and reason',
    );
  };

  subtest 'unicode' => sub {

    my($x, $y) = create_connection_pair;

    my $cv = AnyEvent->condvar;
    my $reason;
    my $code;

    $y->on(finish => sub {
      my($con) = @_;
      $code   = $con->close_code;
      $reason = $con->close_reason;
      $cv->send;
    });

    $x->close(1009 => 'UTF8 WIDE CHARACTERS');

    $cv->recv;

    is $code,   1009,                                     'code is available in finish callback';
    is $reason, 'UTF8 WIDE CHARACTERS', 'reason is available in finish callback';

    is(
      $y,
      object {
        call close_code   => 1009;
        call close_reason => 'UTF8 WIDE CHARACTERS';
      },
      'connection has finish code and reason',
    );

  };
};

subtest 'Connection should not send after sending close frame, should not receive after receiving close frame' => sub {

  subtest "it should not send after sending close frame", sub {
    my ($x_conn, $y_handle) = create_connection_and_handle;

    my $y_received;
    my $cv_finish = AnyEvent->condvar;
    $cv_finish->begin;
    $cv_finish->begin;
    $y_handle->on_read(sub { });
    $y_handle->on_error(sub {
      $y_received = $_[0]->{rbuf};
      $_[0]->{rbuf} = "";
      $cv_finish->end;
    });
    $x_conn->on(finish => sub {
      $cv_finish->end;
    });
    $x_conn->close();
    $x_conn->send("hoge");
    $cv_finish->recv;

    my $parser = Protocol::WebSocket::Frame->new();
    $parser->append($y_received);
    ok defined($parser->next_bytes), "received a complete frame";
    ok $parser->is_close, "... and it's a close frame";
    ok !defined($parser->next_bytes), "no more frame";



( run in 0.759 second using v1.01-cache-2.11-cpan-d7f47b0818f )