3 # Author:: Francis Cianfrocca (gmail: blackhedd)
\r
4 # Homepage:: http://rubyeventmachine.com
\r
5 # Date:: 8 April 2006
\r
7 # See EventMachine and EventMachine::Connection for documentation and
\r
10 #----------------------------------------------------------------------------
\r
12 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
\r
15 # This program is free software; you can redistribute it and/or modify
\r
16 # it under the terms of either: 1) the GNU General Public License
\r
17 # as published by the Free Software Foundation; either version 2 of the
\r
18 # License, or (at your option) any later version; or 2) Ruby's License.
\r
20 # See the file COPYING for complete licensing information.
\r
22 #---------------------------------------------------------------------------
\r
28 require 'eventmachine'
\r
31 class TestSmtpServer < Test::Unit::TestCase
\r
33 # Don't test on port 25. It requires superuser and there's probably
\r
34 # a mail server already running there anyway.
\r
35 Localhost = "127.0.0.1"
\r
38 # This class is an example of what you need to write in order
\r
39 # to implement a mail server. You override the methods you are
\r
40 # interested in. Some, but not all, of these are illustrated here.
\r
42 class Mailserver < EM::Protocols::SmtpServer
\r
44 attr_reader :my_msg_body, :my_sender, :my_recipients
\r
46 def initialize *args
\r
49 def receive_sender sender
\r
54 def receive_recipient rcpt
\r
55 @my_recipients ||= []
\r
56 @my_recipients << rcpt
\r
59 def receive_data_chunk c
\r
60 @my_msg_body = c.last
\r
62 def connection_ended
\r
70 EM.start_server( Localhost, Localport, Mailserver ) {|conn| c = conn}
\r
71 EM::Timer.new(2) {EM.stop} # prevent hanging the test suite in case of error
\r
72 EM::Protocols::SmtpClient.send :host=>Localhost,
\r
75 :from=>"me@example.com",
\r
76 :to=>"you@example.com",
\r
77 :header=> {"Subject"=>"Email subject line", "Reply-to"=>"me@example.com"},
\r
78 :body=>"Not much of interest here."
\r
81 assert_equal( "Not much of interest here.", c.my_msg_body )
\r
82 assert_equal( "<me@example.com>", c.my_sender )
\r
83 assert_equal( ["<you@example.com>"], c.my_recipients )
\r