The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

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

SYNOPSIS

use Test::Mojo::IRC -basic;

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

# simulate server/client communication
$t->run(
  [
    # Send "welcome.irc" from the DATA section when client sends "NICK"
    qr{\bNICK\b} => [qw(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

server

$str = $self->server;

Returns the server address, "host:port", that "start_server" set up.

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, sub { my $self = shift });

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...

  • Scalar

    Plain text.

  • Scalar ref

    Path to file on disk.

  • Array ref

    The module name and file passed on to "data_section" in Mojo::Loader. The default package is "main", meaning the two examples below is the same:

    $self->run([qr{JOIN}, ["join-reply.irc"]], sub { my $self = shift });
    $self->run([qr{JOIN}, ["main", "join-reply.irc"]], sub { my $self = shift });

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;
$self   = Test::Mojo::IRC->start_server;

Will start a test server and return "server". It can also be called as a class method which will return a new object.

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