OSDN Git Service

Replaced mongrel with thin
[redminele/redminele.git] / ruby / lib / ruby / gems / 1.8 / gems / eventmachine-0.12.10-x86-mswin32-60 / tests / test_smtpserver.rb
1 # $Id$\r
2 #\r
3 # Author:: Francis Cianfrocca (gmail: blackhedd)\r
4 # Homepage::  http://rubyeventmachine.com\r
5 # Date:: 8 April 2006\r
6\r
7 # See EventMachine and EventMachine::Connection for documentation and\r
8 # usage examples.\r
9 #\r
10 #----------------------------------------------------------------------------\r
11 #\r
12 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.\r
13 # Gmail: blackhedd\r
14\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
19\r
20 # See the file COPYING for complete licensing information.\r
21 #\r
22 #---------------------------------------------------------------------------\r
23 #\r
24 #\r
25 #\r
26 \r
27 $:.unshift "../lib"\r
28 require 'eventmachine'\r
29 require 'test/unit'\r
30 \r
31 class TestSmtpServer < Test::Unit::TestCase\r
32 \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
36   Localport = 25001\r
37 \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
41   #\r
42   class Mailserver < EM::Protocols::SmtpServer\r
43 \r
44     attr_reader :my_msg_body, :my_sender, :my_recipients\r
45 \r
46     def initialize *args\r
47       super\r
48     end\r
49     def receive_sender sender\r
50       @my_sender = sender\r
51       #p sender\r
52       true\r
53     end\r
54     def receive_recipient rcpt\r
55       @my_recipients ||= []\r
56       @my_recipients << rcpt\r
57       true\r
58     end\r
59     def receive_data_chunk c\r
60       @my_msg_body = c.last\r
61     end\r
62     def connection_ended\r
63       EM.stop\r
64     end\r
65   end\r
66 \r
67   def test_mail\r
68     c = nil\r
69     EM.run {\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
73         :port=>Localport,\r
74         :domain=>"bogus",\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
79 \r
80     }\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
84   end\r
85 end\r