The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::Mojo::IRC - Module for testing Mojo::IRC

SYNOPSIS

  use Test::Mojo::IRC -basic;

  my $t      = Test::Mojo::IRC->new;
  my $server = $t->start_server;
  my $irc    = Mojo::IRC->new(server => $server);

  # simulate server/client communication
  $t->run(
    [
      # Send "welcome.irc" from the DATA section when client sends "NICK"
      qr{\bNICK\b} => [ "main", "motd.irc" ],
    ],
    sub {
      my $err;
      my $motd = 0;
      $t->on($irc, irc_rpl_motd => sub { $motd++ });
      $t->on($irc, irc_rpl_endofmotd => sub { Mojo::IOLoop->stop; }); # need to manually stop the IOLoop
      $irc->connect(sub { $err = $_[1]; });
      Mojo::IOLoop->start; # need to manually start the IOLoop
      is $err, "", "connected";
      is $motd, 3, "message of the day";
    },
  );

  done_testing;

  __DATA__
  @@ motd.irc
  :spectral.shadowcat.co.uk 375 test123 :- spectral.shadowcat.co.uk Message of the Day -
  :spectral.shadowcat.co.uk 372 test123 :- We scan all connecting clients for open proxies and other
  :spectral.shadowcat.co.uk 372 test123 :- exploitable nasties. If you don't wish to be scanned,
  :spectral.shadowcat.co.uk 372 test123 :- don't connect again, and sorry for scanning you this time.
  :spectral.shadowcat.co.uk 376 test123 :End of /MOTD command.

DESCRIPTION

Test::Mojo::IRC is a module for making it easier to test Mojo::IRC applications.

ENVIRONMENT VARIABLES

TEST_MOJO_IRC_SERVER

TEST_MOJO_IRC_SERVER can be set to point to a live server. If the variable is set, "start_server" will simply return TEST_MOJO_IRC_SERVER instead of setting up a server.

ATTRIBUTES

welcome_message

  $str = $self->welcome_message;
  $self = $self->welcome_message($str);

Holds a message which will be sent to the client on connect.

METHODS

on

  $self->on($irc, $event, $cb);

Will attach events to the $irc object which is removed after "run" has completed. See "SYNOPSIS" for example code.

run

  $self->run($reply_on, $cb);

Used to simulate communication between IRC server and client. The way this works is that the $cb will initiate connect or write to the server and the server will then respond with the data from either "welcome_message" or $reply_on on these events.

$reply_on is an array-ref of regex/buffer pairs. Each time a message from the client match the first regex in the $reply_on array the buffer will be sent back to the client and the regex/buffer will be removed. This means that the order of the pairs are important. The buffer can be...

Note that starting and stopping the IOLoop is up to you, but there is also a master timeout which will stop the IOLoop if running for too long.

See "SYNOPSIS" for example.

start_server

  $server = $self->start_server;

Will start a test server and return the "host:port" which it listens to.

import

  use Test::Mojo::IRC -basic;

Loading this module with "-basic" will import strict, warnings, utf8, Test::More and 5.10 features into the caller namespace.

COPYRIGHT AND LICENSE

Copyright (C) 2014, Jan Henning Thorsen

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

AUTHOR

Jan Henning Thorsen - jhthorsen@cpan.org