1 require "#{File.dirname(__FILE__)}/abstract_unit"
3 class FunkyPathMailer < ActionMailer::Base
4 self.template_root = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
6 def multipart_with_template_path_with_dots(recipient)
8 subject "Have a lovely picture"
9 from "Chad Fowler <chad@chadfowler.com>"
10 attachment :content_type => "image/jpeg",
11 :body => "not really a jpeg, we're only testing, after all"
15 class TestMailer < ActionMailer::Base
16 def signed_up(recipient)
17 @recipients = recipient
18 @subject = "[Signed up] Welcome #{recipient}"
19 @from = "system@loudthinking.com"
20 @sent_on = Time.local(2004, 12, 12)
21 @body["recipient"] = recipient
24 def cancelled_account(recipient)
25 self.recipients = recipient
26 self.subject = "[Cancelled] Goodbye #{recipient}"
27 self.from = "system@loudthinking.com"
28 self.sent_on = Time.local(2004, 12, 12)
29 self.body = "Goodbye, Mr. #{recipient}"
34 subject "testing bcc/cc"
35 from "system@loudthinking.com"
36 sent_on Time.local(2004, 12, 12)
37 cc "nobody@loudthinking.com"
38 bcc "root@loudthinking.com"
39 body "Nothing to see here."
42 def iso_charset(recipient)
43 @recipients = recipient
44 @subject = "testing isø charsets"
45 @from = "system@loudthinking.com"
46 @sent_on = Time.local 2004, 12, 12
47 @cc = "nobody@loudthinking.com"
48 @bcc = "root@loudthinking.com"
49 @body = "Nothing to see here."
50 @charset = "iso-8859-1"
53 def unencoded_subject(recipient)
54 @recipients = recipient
55 @subject = "testing unencoded subject"
56 @from = "system@loudthinking.com"
57 @sent_on = Time.local 2004, 12, 12
58 @cc = "nobody@loudthinking.com"
59 @bcc = "root@loudthinking.com"
60 @body = "Nothing to see here."
63 def extended_headers(recipient)
64 @recipients = recipient
65 @subject = "testing extended headers"
66 @from = "Grytøyr <stian1@example.net>"
67 @sent_on = Time.local 2004, 12, 12
68 @cc = "Grytøyr <stian2@example.net>"
69 @bcc = "Grytøyr <stian3@example.net>"
70 @body = "Nothing to see here."
71 @charset = "iso-8859-1"
74 def utf8_body(recipient)
75 @recipients = recipient
76 @subject = "testing utf-8 body"
77 @from = "Foo áëô îü <extended@example.net>"
78 @sent_on = Time.local 2004, 12, 12
79 @cc = "Foo áëô îü <extended@example.net>"
80 @bcc = "Foo áëô îü <extended@example.net>"
85 def multipart_with_mime_version(recipient)
87 subject "multipart with mime_version"
88 from "test@example.com"
89 sent_on Time.local(2004, 12, 12)
91 content_type "multipart/alternative"
93 part "text/plain" do |p|
97 part "text/html" do |p|
98 p.body = "<b>blah</b>"
102 def multipart_with_utf8_subject(recipient)
105 from "test@example.com"
108 part "text/plain" do |p|
112 part "text/html" do |p|
113 p.body = "<b>blah</b>"
117 def explicitly_multipart_example(recipient, ct=nil)
119 subject "multipart example"
120 from "test@example.com"
121 sent_on Time.local(2004, 12, 12)
122 body "plain text default"
123 content_type ct if ct
125 part "text/html" do |p|
126 p.charset = "iso-8859-1"
130 attachment :content_type => "image/jpeg", :filename => "foo.jpg",
134 def implicitly_multipart_example(recipient, cs = nil, order = nil)
135 @recipients = recipient
136 @subject = "multipart example"
137 @from = "test@example.com"
138 @sent_on = Time.local 2004, 12, 12
139 @body = { "recipient" => recipient }
141 @implicit_parts_order = order if order
144 def implicitly_multipart_with_utf8
145 recipients "no.one@nowhere.test"
147 from "some.one@somewhere.test"
148 template "implicitly_multipart_example"
149 body ({ "recipient" => "no.one@nowhere.test" })
152 def html_mail(recipient)
155 from "test@example.com"
156 body "<em>Emphasize</em> <strong>this</strong>"
157 content_type "text/html"
160 def html_mail_with_underscores(recipient)
161 subject "html mail with underscores"
162 body %{<a href="http://google.com" target="_blank">_Google</a>}
165 def custom_template(recipient)
167 subject "[Signed up] Welcome #{recipient}"
168 from "system@loudthinking.com"
169 sent_on Time.local(2004, 12, 12)
172 body["recipient"] = recipient
175 def custom_templating_extension(recipient)
177 subject "[Signed up] Welcome #{recipient}"
178 from "system@loudthinking.com"
179 sent_on Time.local(2004, 12, 12)
181 body["recipient"] = recipient
184 def various_newlines(recipient)
186 subject "various newlines"
187 from "test@example.com"
188 body "line #1\nline #2\rline #3\r\nline #4\r\r" +
189 "line #5\n\nline#6\r\n\r\nline #7"
192 def various_newlines_multipart(recipient)
194 subject "various newlines multipart"
195 from "test@example.com"
196 content_type "multipart/alternative"
197 part :content_type => "text/plain", :body => "line #1\nline #2\rline #3\r\nline #4\r\r"
198 part :content_type => "text/html", :body => "<p>line #1</p>\n<p>line #2</p>\r<p>line #3</p>\r\n<p>line #4</p>\r\r"
201 def nested_multipart(recipient)
203 subject "nested multipart"
204 from "test@example.com"
205 content_type "multipart/mixed"
206 part :content_type => "multipart/alternative", :content_disposition => "inline", :headers => { "foo" => "bar" } do |p|
207 p.part :content_type => "text/plain", :body => "test text\nline #2"
208 p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>\nline #2"
210 attachment :content_type => "application/octet-stream",:filename => "test.txt", :body => "test abcdefghijklmnopqstuvwxyz"
213 def attachment_with_custom_header(recipient)
215 subject "custom header in attachment"
216 from "test@example.com"
217 content_type "multipart/related"
218 part :content_type => "text/html", :body => 'yo'
219 attachment :content_type => "image/jpeg",:filename => "test.jpeg", :body => "i am not a real picture", :headers => { 'Content-ID' => '<test@test.com>' }
222 def unnamed_attachment(recipient)
224 subject "nested multipart"
225 from "test@example.com"
226 content_type "multipart/mixed"
227 part :content_type => "text/plain", :body => "hullo"
228 attachment :content_type => "application/octet-stream", :body => "test abcdefghijklmnopqstuvwxyz"
231 def headers_with_nonalpha_chars(recipient)
233 subject "nonalpha chars"
234 from "One: Two <test@example.com>"
235 cc "Three: Four <test@example.com>"
236 bcc "Five: Six <test@example.com>"
240 def custom_content_type_attributes
241 recipients "no.one@nowhere.test"
242 subject "custom content types"
243 from "some.one@somewhere.test"
244 content_type "text/plain; format=flowed"
249 recipients "no.one@nowhere.test"
250 subject "return path test"
251 from "some.one@somewhere.test"
253 headers "return-path" => "another@somewhere.test"
257 attr_accessor :received_body
261 self.class.received_body = mail.body
265 uses_mocha 'ActionMailerTest' do
267 class ActionMailerTest < Test::Unit::TestCase
268 include ActionMailer::Quoting
270 def encode( text, charset="utf-8" )
271 quoted_printable( text, charset )
274 def new_mail( charset="utf-8" )
275 mail = TMail::Mail.new
276 mail.mime_version = "1.0"
278 mail.set_content_type "text", "plain", { "charset" => charset }
283 # Replacing logger work around for mocha bug. Should be fixed in mocha 0.3.3
285 set_delivery_method :test
286 ActionMailer::Base.perform_deliveries = true
287 ActionMailer::Base.raise_delivery_errors = true
288 ActionMailer::Base.deliveries = []
290 @original_logger = TestMailer.logger
291 @recipient = 'test@localhost'
295 TestMailer.logger = @original_logger
296 restore_delivery_method
299 def test_nested_parts
301 assert_nothing_raised { created = TestMailer.create_nested_multipart(@recipient)}
302 assert_equal 2,created.parts.size
303 assert_equal 2,created.parts.first.parts.size
305 assert_equal "multipart/mixed", created.content_type
306 assert_equal "multipart/alternative", created.parts.first.content_type
307 assert_equal "bar", created.parts.first.header['foo'].to_s
308 assert_equal "text/plain", created.parts.first.parts.first.content_type
309 assert_equal "text/html", created.parts.first.parts[1].content_type
310 assert_equal "application/octet-stream", created.parts[1].content_type
313 def test_attachment_with_custom_header
315 assert_nothing_raised { created = TestMailer.create_attachment_with_custom_header(@recipient)}
316 assert_equal "<test@test.com>", created.parts[1].header['content-id'].to_s
321 expected.to = @recipient
322 expected.subject = "[Signed up] Welcome #{@recipient}"
323 expected.body = "Hello there, \n\nMr. #{@recipient}"
324 expected.from = "system@loudthinking.com"
325 expected.date = Time.local(2004, 12, 12)
328 assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) }
329 assert_not_nil created
330 assert_equal expected.encoded, created.encoded
332 assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
333 assert_not_nil ActionMailer::Base.deliveries.first
334 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
337 def test_custom_template
339 expected.to = @recipient
340 expected.subject = "[Signed up] Welcome #{@recipient}"
341 expected.body = "Hello there, \n\nMr. #{@recipient}"
342 expected.from = "system@loudthinking.com"
343 expected.date = Time.local(2004, 12, 12)
346 assert_nothing_raised { created = TestMailer.create_custom_template(@recipient) }
347 assert_not_nil created
348 assert_equal expected.encoded, created.encoded
351 def test_custom_templating_extension
353 # N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
355 expected.to = @recipient
356 expected.subject = "[Signed up] Welcome #{@recipient}"
357 expected.body = "Hello there, \n\nMr. #{@recipient}"
358 expected.from = "system@loudthinking.com"
359 expected.date = Time.local(2004, 12, 12)
361 # Stub the render method so no alternative renderers need be present.
362 ActionView::Base.any_instance.stubs(:render).returns("Hello there, \n\nMr. #{@recipient}")
364 # If the template is not registered, there should be no parts.
366 assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
367 assert_not_nil created
368 assert_equal 0, created.parts.length
370 ActionMailer::Base.register_template_extension('haml')
372 # Now that the template is registered, there should be one part. The text/plain part.
374 assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) }
375 assert_not_nil created
376 assert_equal 2, created.parts.length
377 assert_equal 'text/plain', created.parts[0].content_type
378 assert_equal 'text/html', created.parts[1].content_type
381 def test_cancelled_account
383 expected.to = @recipient
384 expected.subject = "[Cancelled] Goodbye #{@recipient}"
385 expected.body = "Goodbye, Mr. #{@recipient}"
386 expected.from = "system@loudthinking.com"
387 expected.date = Time.local(2004, 12, 12)
390 assert_nothing_raised { created = TestMailer.create_cancelled_account(@recipient) }
391 assert_not_nil created
392 assert_equal expected.encoded, created.encoded
394 assert_nothing_raised { TestMailer.deliver_cancelled_account(@recipient) }
395 assert_not_nil ActionMailer::Base.deliveries.first
396 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
401 expected.to = @recipient
402 expected.subject = "testing bcc/cc"
403 expected.body = "Nothing to see here."
404 expected.from = "system@loudthinking.com"
405 expected.cc = "nobody@loudthinking.com"
406 expected.bcc = "root@loudthinking.com"
407 expected.date = Time.local 2004, 12, 12
410 assert_nothing_raised do
411 created = TestMailer.create_cc_bcc @recipient
413 assert_not_nil created
414 assert_equal expected.encoded, created.encoded
416 assert_nothing_raised do
417 TestMailer.deliver_cc_bcc @recipient
420 assert_not_nil ActionMailer::Base.deliveries.first
421 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
425 expected = new_mail( "iso-8859-1" )
426 expected.to = @recipient
427 expected.subject = encode "testing isø charsets", "iso-8859-1"
428 expected.body = "Nothing to see here."
429 expected.from = "system@loudthinking.com"
430 expected.cc = "nobody@loudthinking.com"
431 expected.bcc = "root@loudthinking.com"
432 expected.date = Time.local 2004, 12, 12
435 assert_nothing_raised do
436 created = TestMailer.create_iso_charset @recipient
438 assert_not_nil created
439 assert_equal expected.encoded, created.encoded
441 assert_nothing_raised do
442 TestMailer.deliver_iso_charset @recipient
445 assert_not_nil ActionMailer::Base.deliveries.first
446 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
449 def test_unencoded_subject
451 expected.to = @recipient
452 expected.subject = "testing unencoded subject"
453 expected.body = "Nothing to see here."
454 expected.from = "system@loudthinking.com"
455 expected.cc = "nobody@loudthinking.com"
456 expected.bcc = "root@loudthinking.com"
457 expected.date = Time.local 2004, 12, 12
460 assert_nothing_raised do
461 created = TestMailer.create_unencoded_subject @recipient
463 assert_not_nil created
464 assert_equal expected.encoded, created.encoded
466 assert_nothing_raised do
467 TestMailer.deliver_unencoded_subject @recipient
470 assert_not_nil ActionMailer::Base.deliveries.first
471 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
474 def test_instances_are_nil
475 assert_nil ActionMailer::Base.new
476 assert_nil TestMailer.new
479 def test_deliveries_array
480 assert_not_nil ActionMailer::Base.deliveries
481 assert_equal 0, ActionMailer::Base.deliveries.size
482 TestMailer.deliver_signed_up(@recipient)
483 assert_equal 1, ActionMailer::Base.deliveries.size
484 assert_not_nil ActionMailer::Base.deliveries.first
487 def test_perform_deliveries_flag
488 ActionMailer::Base.perform_deliveries = false
489 TestMailer.deliver_signed_up(@recipient)
490 assert_equal 0, ActionMailer::Base.deliveries.size
491 ActionMailer::Base.perform_deliveries = true
492 TestMailer.deliver_signed_up(@recipient)
493 assert_equal 1, ActionMailer::Base.deliveries.size
496 def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
497 ActionMailer::Base.raise_delivery_errors = false
498 TestMailer.any_instance.expects(:perform_delivery_test).raises(Exception)
499 assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
502 def test_performs_delivery_via_sendmail
504 sm.expects(:print).with(anything)
506 IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm)
507 ActionMailer::Base.delivery_method = :sendmail
508 TestMailer.deliver_signed_up(@recipient)
511 def test_delivery_logs_sent_mail
512 mail = TestMailer.create_signed_up(@recipient)
514 logger.expects(:info).with("Sent mail:\n #{mail.encoded}")
515 TestMailer.logger = logger
516 TestMailer.deliver_signed_up(@recipient)
519 def test_unquote_quoted_printable_subject
522 Subject: =?utf-8?Q?testing_testing_=D6=A4?=
523 Content-Type: text/plain; charset=iso-8859-1
527 mail = TMail::Mail.parse(msg)
528 assert_equal "testing testing \326\244", mail.subject
529 assert_equal "=?utf-8?Q?testing_testing_=D6=A4?=", mail.quoted_subject
532 def test_unquote_7bit_subject
535 Subject: this == working?
536 Content-Type: text/plain; charset=iso-8859-1
540 mail = TMail::Mail.parse(msg)
541 assert_equal "this == working?", mail.subject
542 assert_equal "this == working?", mail.quoted_subject
545 def test_unquote_7bit_body
549 Content-Type: text/plain; charset=iso-8859-1
550 Content-Transfer-Encoding: 7bit
554 mail = TMail::Mail.parse(msg)
555 assert_equal "The=3Dbody", mail.body.strip
556 assert_equal "The=3Dbody", mail.quoted_body.strip
559 def test_unquote_quoted_printable_body
563 Content-Type: text/plain; charset=iso-8859-1
564 Content-Transfer-Encoding: quoted-printable
568 mail = TMail::Mail.parse(msg)
569 assert_equal "The=body", mail.body.strip
570 assert_equal "The=3Dbody", mail.quoted_body.strip
573 def test_unquote_base64_body
577 Content-Type: text/plain; charset=iso-8859-1
578 Content-Transfer-Encoding: base64
582 mail = TMail::Mail.parse(msg)
583 assert_equal "The body", mail.body.strip
584 assert_equal "VGhlIGJvZHk=", mail.quoted_body.strip
587 def test_extended_headers
588 @recipient = "Grytøyr <test@localhost>"
590 expected = new_mail "iso-8859-1"
591 expected.to = quote_address_if_necessary @recipient, "iso-8859-1"
592 expected.subject = "testing extended headers"
593 expected.body = "Nothing to see here."
594 expected.from = quote_address_if_necessary "Grytøyr <stian1@example.net>", "iso-8859-1"
595 expected.cc = quote_address_if_necessary "Grytøyr <stian2@example.net>", "iso-8859-1"
596 expected.bcc = quote_address_if_necessary "Grytøyr <stian3@example.net>", "iso-8859-1"
597 expected.date = Time.local 2004, 12, 12
600 assert_nothing_raised do
601 created = TestMailer.create_extended_headers @recipient
604 assert_not_nil created
605 assert_equal expected.encoded, created.encoded
607 assert_nothing_raised do
608 TestMailer.deliver_extended_headers @recipient
611 assert_not_nil ActionMailer::Base.deliveries.first
612 assert_equal expected.encoded, ActionMailer::Base.deliveries.first.encoded
615 def test_utf8_body_is_not_quoted
616 @recipient = "Foo áëô îü <extended@example.net>"
617 expected = new_mail "utf-8"
618 expected.to = quote_address_if_necessary @recipient, "utf-8"
619 expected.subject = "testing utf-8 body"
620 expected.body = "åœö blah"
621 expected.from = quote_address_if_necessary @recipient, "utf-8"
622 expected.cc = quote_address_if_necessary @recipient, "utf-8"
623 expected.bcc = quote_address_if_necessary @recipient, "utf-8"
624 expected.date = Time.local 2004, 12, 12
626 created = TestMailer.create_utf8_body @recipient
627 assert_match(/åœö blah/, created.encoded)
630 def test_multiple_utf8_recipients
631 @recipient = ["\"Foo áëô îü\" <extended@example.net>", "\"Example Recipient\" <me@example.com>"]
632 expected = new_mail "utf-8"
633 expected.to = quote_address_if_necessary @recipient, "utf-8"
634 expected.subject = "testing utf-8 body"
635 expected.body = "åœö blah"
636 expected.from = quote_address_if_necessary @recipient.first, "utf-8"
637 expected.cc = quote_address_if_necessary @recipient, "utf-8"
638 expected.bcc = quote_address_if_necessary @recipient, "utf-8"
639 expected.date = Time.local 2004, 12, 12
641 created = TestMailer.create_utf8_body @recipient
642 assert_match(/\nFrom: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>\r/, created.encoded)
643 assert_match(/\nTo: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>, Example Recipient <me/, created.encoded)
646 def test_receive_decodes_base64_encoded_mail
647 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email")
648 TestMailer.receive(fixture)
649 assert_match(/Jamis/, TestMailer.received_body)
652 def test_receive_attachments
653 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email2")
654 mail = TMail::Mail.parse(fixture)
655 attachment = mail.attachments.last
656 assert_equal "smime.p7s", attachment.original_filename
657 assert_equal "application/pkcs7-signature", attachment.content_type
660 def test_decode_attachment_without_charset
661 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email3")
662 mail = TMail::Mail.parse(fixture)
663 attachment = mail.attachments.last
664 assert_equal 1026, attachment.read.length
667 def test_attachment_using_content_location
668 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email12")
669 mail = TMail::Mail.parse(fixture)
670 assert_equal 1, mail.attachments.length
671 assert_equal "Photo25.jpg", mail.attachments.first.original_filename
674 def test_attachment_with_text_type
675 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email13")
676 mail = TMail::Mail.parse(fixture)
677 assert mail.has_attachments?
678 assert_equal 1, mail.attachments.length
679 assert_equal "hello.rb", mail.attachments.first.original_filename
682 def test_decode_part_without_content_type
683 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email4")
684 mail = TMail::Mail.parse(fixture)
685 assert_nothing_raised { mail.body }
688 def test_decode_message_without_content_type
689 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email5")
690 mail = TMail::Mail.parse(fixture)
691 assert_nothing_raised { mail.body }
694 def test_decode_message_with_incorrect_charset
695 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email6")
696 mail = TMail::Mail.parse(fixture)
697 assert_nothing_raised { mail.body }
700 def test_multipart_with_mime_version
701 mail = TestMailer.create_multipart_with_mime_version(@recipient)
702 assert_equal "1.1", mail.mime_version
705 def test_multipart_with_utf8_subject
706 mail = TestMailer.create_multipart_with_utf8_subject(@recipient)
707 assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded)
710 def test_implicitly_multipart_with_utf8
711 mail = TestMailer.create_implicitly_multipart_with_utf8
712 assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded)
715 def test_explicitly_multipart_messages
716 mail = TestMailer.create_explicitly_multipart_example(@recipient)
717 assert_equal 3, mail.parts.length
718 assert_nil mail.content_type
719 assert_equal "text/plain", mail.parts[0].content_type
721 assert_equal "text/html", mail.parts[1].content_type
722 assert_equal "iso-8859-1", mail.parts[1].sub_header("content-type", "charset")
723 assert_equal "inline", mail.parts[1].content_disposition
725 assert_equal "image/jpeg", mail.parts[2].content_type
726 assert_equal "attachment", mail.parts[2].content_disposition
727 assert_equal "foo.jpg", mail.parts[2].sub_header("content-disposition", "filename")
728 assert_equal "foo.jpg", mail.parts[2].sub_header("content-type", "name")
729 assert_nil mail.parts[2].sub_header("content-type", "charset")
732 def test_explicitly_multipart_with_content_type
733 mail = TestMailer.create_explicitly_multipart_example(@recipient, "multipart/alternative")
734 assert_equal 3, mail.parts.length
735 assert_equal "multipart/alternative", mail.content_type
738 def test_explicitly_multipart_with_invalid_content_type
739 mail = TestMailer.create_explicitly_multipart_example(@recipient, "text/xml")
740 assert_equal 3, mail.parts.length
741 assert_nil mail.content_type
744 def test_implicitly_multipart_messages
745 mail = TestMailer.create_implicitly_multipart_example(@recipient)
746 assert_equal 6, mail.parts.length
747 assert_equal "1.0", mail.mime_version
748 assert_equal "multipart/alternative", mail.content_type
749 assert_equal "text/yaml", mail.parts[0].content_type
750 assert_equal "utf-8", mail.parts[0].sub_header("content-type", "charset")
751 assert_equal "text/plain", mail.parts[2].content_type
752 assert_equal "utf-8", mail.parts[2].sub_header("content-type", "charset")
753 assert_equal "text/html", mail.parts[4].content_type
754 assert_equal "utf-8", mail.parts[4].sub_header("content-type", "charset")
757 def test_implicitly_multipart_messages_with_custom_order
758 mail = TestMailer.create_implicitly_multipart_example(@recipient, nil, ["text/yaml", "text/plain"])
759 assert_equal 6, mail.parts.length
760 assert_equal "text/html", mail.parts[0].content_type
761 assert_equal "text/plain", mail.parts[2].content_type
762 assert_equal "text/yaml", mail.parts[4].content_type
765 def test_implicitly_multipart_messages_with_charset
766 mail = TestMailer.create_implicitly_multipart_example(@recipient, 'iso-8859-1')
768 assert_equal "multipart/alternative", mail.header['content-type'].body
770 assert_equal 'iso-8859-1', mail.parts[0].sub_header("content-type", "charset")
771 assert_equal 'iso-8859-1', mail.parts[1].sub_header("content-type", "charset")
772 assert_equal 'iso-8859-1', mail.parts[2].sub_header("content-type", "charset")
776 mail = TestMailer.create_html_mail(@recipient)
777 assert_equal "text/html", mail.content_type
780 def test_html_mail_with_underscores
781 mail = TestMailer.create_html_mail_with_underscores(@recipient)
782 assert_equal %{<a href="http://google.com" target="_blank">_Google</a>}, mail.body
785 def test_various_newlines
786 mail = TestMailer.create_various_newlines(@recipient)
787 assert_equal("line #1\nline #2\nline #3\nline #4\n\n" +
788 "line #5\n\nline#6\n\nline #7", mail.body)
791 def test_various_newlines_multipart
792 mail = TestMailer.create_various_newlines_multipart(@recipient)
793 assert_equal "line #1\nline #2\nline #3\nline #4\n\n", mail.parts[0].body
794 assert_equal "<p>line #1</p>\n<p>line #2</p>\n<p>line #3</p>\n<p>line #4</p>\n\n", mail.parts[1].body
797 def test_headers_removed_on_smtp_delivery
798 ActionMailer::Base.delivery_method = :smtp
799 TestMailer.deliver_cc_bcc(@recipient)
800 assert MockSMTP.deliveries[0][2].include?("root@loudthinking.com")
801 assert MockSMTP.deliveries[0][2].include?("nobody@loudthinking.com")
802 assert MockSMTP.deliveries[0][2].include?(@recipient)
803 assert_match %r{^Cc: nobody@loudthinking.com}, MockSMTP.deliveries[0][0]
804 assert_match %r{^To: #{@recipient}}, MockSMTP.deliveries[0][0]
805 assert_no_match %r{^Bcc: root@loudthinking.com}, MockSMTP.deliveries[0][0]
808 def test_recursive_multipart_processing
809 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7")
810 mail = TMail::Mail.parse(fixture)
811 assert_equal "This is the first part.\n\nAttachment: test.rb\nAttachment: test.pdf\n\n\nAttachment: smime.p7s\n", mail.body
814 def test_decode_encoded_attachment_filename
815 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email8")
816 mail = TMail::Mail.parse(fixture)
817 attachment = mail.attachments.last
818 assert_equal "01QuienTeDijat.Pitbull.mp3", attachment.original_filename
821 def test_wrong_mail_header
822 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email9")
823 assert_raise(TMail::SyntaxError) { TMail::Mail.parse(fixture) }
826 def test_decode_message_with_unknown_charset
827 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email10")
828 mail = TMail::Mail.parse(fixture)
829 assert_nothing_raised { mail.body }
832 def test_decode_message_with_unquoted_atchar_in_header
833 fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email11")
834 mail = TMail::Mail.parse(fixture)
835 assert_not_nil mail.from
838 def test_empty_header_values_omitted
839 result = TestMailer.create_unnamed_attachment(@recipient).encoded
840 assert_match %r{Content-Type: application/octet-stream[^;]}, result
841 assert_match %r{Content-Disposition: attachment[^;]}, result
844 def test_headers_with_nonalpha_chars
845 mail = TestMailer.create_headers_with_nonalpha_chars(@recipient)
846 assert !mail.from_addrs.empty?
847 assert !mail.cc_addrs.empty?
848 assert !mail.bcc_addrs.empty?
849 assert_match(/:/, mail.from_addrs.to_s)
850 assert_match(/:/, mail.cc_addrs.to_s)
851 assert_match(/:/, mail.bcc_addrs.to_s)
854 def test_deliver_with_mail_object
855 mail = TestMailer.create_headers_with_nonalpha_chars(@recipient)
856 assert_nothing_raised { TestMailer.deliver(mail) }
857 assert_equal 1, TestMailer.deliveries.length
860 def test_multipart_with_template_path_with_dots
861 mail = FunkyPathMailer.create_multipart_with_template_path_with_dots(@recipient)
862 assert_equal 2, mail.parts.length
865 def test_custom_content_type_attributes
866 mail = TestMailer.create_custom_content_type_attributes
867 assert_match %r{format=flowed}, mail['content-type'].to_s
868 assert_match %r{charset=utf-8}, mail['content-type'].to_s
871 def test_return_path_with_create
872 mail = TestMailer.create_return_path
873 assert_equal "<another@somewhere.test>", mail['return-path'].to_s
876 def test_return_path_with_deliver
877 ActionMailer::Base.delivery_method = :smtp
878 TestMailer.deliver_return_path
879 assert_match %r{^Return-Path: <another@somewhere.test>}, MockSMTP.deliveries[0][0]
885 class InheritableTemplateRootTest < Test::Unit::TestCase
887 expected = "#{File.dirname(__FILE__)}/fixtures/path.with.dots"
888 assert_equal expected, FunkyPathMailer.template_root
890 sub = Class.new(FunkyPathMailer)
891 sub.template_root = 'test/path'
893 assert_equal 'test/path', sub.template_root
894 assert_equal expected, FunkyPathMailer.template_root
898 class MethodNamingTest < Test::Unit::TestCase
899 class TestMailer < ActionMailer::Base
906 set_delivery_method :test
907 ActionMailer::Base.perform_deliveries = true
908 ActionMailer::Base.deliveries = []
912 restore_delivery_method
916 assert_nothing_raised do
918 TestMailer.deliver_send