Test::Mock::Net::Server::Mail - mock SMTP server for use in tests
version 1.02
In a test:
use Test::More; use Test::Mock::Net::Server::Mail; use_ok(Net::YourClient); my $s = Test::Mock::Net::Server::Mail->new; $s->start_ok; my $c = Net::YourClient->new( host => $s->bind_address, port => $s->port, ); # check... $s->stop_ok;
Test::Mock::Net::Server::Mail is a mock SMTP server based on Net::Server::Mail. If could be used in unit tests to check SMTP clients.
It will accept all MAIL FROM and RCPT TO commands except they start with 'bad' in the user or domain part. And it will accept all mail except mail containing the string 'bad mail content'.
If a different behaviour is need a subclass could be used to overwrite process_<verb> methods.
If the logging option is enabled (by default) the mock server will log received commands in a temporary log file. The content of this log file can be inspected with the methods next_log() or tested with next_log_ok().
# setup server($s) and client($c)... $c->ehlo('localhost'); $s->next_log; # {"verb" => "EHLO","params" => "localhost"} $c->mail_from('user@domain.tld'); $s->next_log_ok('MAIL', 'user@domain.tld, 'server received MAIL cmd'); $c->rcpt_to('targetuser@targetdomain.tld'); $s->next_log_ok('RCPT', qr/target/, 'server received RCPT cmd'); # shutdown...
The address to bind to.
First port number to try when searching for a free port.
Load 8BITMIME extension?
Load PIPELINING extension?
Load STARTTLS extension?
Log commands received by the server.
Which verbs the server should add mockup to.
By default:
qw( EHLO HELO MAIL RCPT DATA QUIT )
Retrieve the port of the running mock server.
Retrieve the process id of the running mock server.
Reads one log from the servers log and returns a hashref.
Example:
{"verb"=>"EHLO","params"=>"localhost"}
Will read a log using next_log() and test it.
The logs 'verb' must exactly match $verb.
The logs 'params' are checked against $expected. It must be a string,regexp or undef.
Examples:
$s->next_log_ok('EHLO', 'localhost', 'server received EHLO command'); $s->next_log_ok('MAIL', 'gooduser@gooddomain', 'server received MAIL command'); $s->next_log_ok('RCPT', 'gooduser@gooddomain', 'server received RCPT command'); $s->next_log_ok('DATA', qr/bad mail content/, 'server received DATA command'); $s->next_log_ok('QUIT', undef, 'server received QUIT command');
Overwrite this method in a subclass if you need to register additional command callbacks via Net::Server::Mail.
Net::Server::Mail object is passed via $smtp.
Will refuse EHLO names containing the string 'bad' otherwise will accept any EHLO.
Will accept all senders except senders where user or domain starts with 'bad'.
Will accept all reciepients except recipients where user or domain starts with 'bad'.
Overwrite on of this methods in a subclass if you need to implement your own handler.
Start main loop.
Will accept connections forever and will never return.
Start mock server in background (fork).
After the server is started $obj->port and $obj->pid will be set.
Start the mock server and return a test result.
Stop mock smtp server.
Stop the mock server and return a test result.
Markus Benning <ich@markusbenning.de>
This software is copyright (c) 2015 by Markus Benning <ich@markusbenning.de>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Test::Mock::Net::Server::Mail, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Mock::Net::Server::Mail
CPAN shell
perl -MCPAN -e shell install Test::Mock::Net::Server::Mail
For more information on module installation, please visit the detailed CPAN module installation guide.