root/trunk/tests/test_smtpserver.rb

Revision 668, 2.3 kB (checked in by blackhedd, 1 year ago)

migrated version_0 to trunk

  • Property svn:keywords set to Id
Line 
1 # $Id$
2 #
3 # Author:: Francis Cianfrocca (gmail: blackhedd)
4 # Homepage::  http://rubyeventmachine.com
5 # Date:: 8 April 2006
6 #
7 # See EventMachine and EventMachine::Connection for documentation and
8 # usage examples.
9 #
10 #----------------------------------------------------------------------------
11 #
12 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13 # Gmail: blackhedd
14 #
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of either: 1) the GNU General Public License
17 # as published by the Free Software Foundation; either version 2 of the
18 # License, or (at your option) any later version; or 2) Ruby's License.
19 #
20 # See the file COPYING for complete licensing information.
21 #
22 #---------------------------------------------------------------------------
23 #
24 #
25 #
26
27 $:.unshift "../lib"
28 require 'eventmachine'
29 require 'test/unit'
30
31 class TestSmtpServer < Test::Unit::TestCase
32
33         # Don't test on port 25. It requires superuser and there's probably
34         # a mail server already running there anyway.
35         Localhost = "127.0.0.1"
36         Localport = 25001
37
38         # This class is an example of what you need to write in order
39         # to implement a mail server. You override the methods you are
40         # interested in. Some, but not all, of these are illustrated here.
41         #
42         class Mailserver < EM::Protocols::SmtpServer
43
44                 attr_reader :my_msg_body, :my_sender, :my_recipients
45
46                 def initialize *args
47                         super
48                 end
49                 def receive_sender sender
50                         @my_sender = sender
51                         #p sender
52                         true
53                 end
54                 def receive_recipient rcpt
55                         @my_recipients ||= []
56                         @my_recipients << rcpt
57                         true
58                 end
59                 def receive_data_chunk c
60                         @my_msg_body = c.last
61                 end
62                 def connection_ended
63                         EM.stop
64                 end
65         end
66
67
68         def setup
69         end
70
71         def teardown
72         end
73
74         def test_mail
75                 c = nil
76                 EM.run {
77                         EM.start_server( Localhost, Localport, Mailserver ) {|conn| c = conn}
78                         EM::Timer.new(2) {EM.stop} # prevent hanging the test suite in case of error
79                         EM::Protocols::SmtpClient.send :host=>Localhost,
80                                 :port=>Localport,
81                                 :domain=>"bogus",
82                                 :from=>"me@example.com",
83                                 :to=>"you@example.com",
84                                 :header=> {"Subject"=>"Email subject line", "Reply-to"=>"me@example.com"},
85                                 :body=>"Not much of interest here."
86
87                 }
88                 assert_equal( "Not much of interest here.", c.my_msg_body )
89                 assert_equal( "<me@example.com>", c.my_sender )
90                 assert_equal( ["<you@example.com>"], c.my_recipients )
91         end
92 end
93
Note: See TracBrowser for help on using the browser.