Froze rails gems
[depot.git] / vendor / rails / actionmailer / test / quoting_test.rb
1 # encoding: utf-8
2 require 'abstract_unit'
3 require 'tmail'
4 require 'tempfile'
5
6 class QuotingTest < Test::Unit::TestCase
7 # Move some tests from TMAIL here
8 def test_unquote_quoted_printable
9 a ="=?ISO-8859-1?Q?[166417]_Bekr=E6ftelse_fra_Rejsefeber?="
10 b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
11 assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
12 end
13
14 def test_unquote_base64
15 a ="=?ISO-8859-1?B?WzE2NjQxN10gQmVrcuZmdGVsc2UgZnJhIFJlanNlZmViZXI=?="
16 b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
17 assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
18 end
19
20 def test_unquote_without_charset
21 a ="[166417]_Bekr=E6ftelse_fra_Rejsefeber"
22 b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
23 assert_equal "[166417]_Bekr=E6ftelse_fra_Rejsefeber", b
24 end
25
26 def test_unqoute_multiple
27 a ="=?utf-8?q?Re=3A_=5B12=5D_=23137=3A_Inkonsistente_verwendung_von_=22Hin?==?utf-8?b?enVmw7xnZW4i?="
28 b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
29 assert_equal "Re: [12] #137: Inkonsistente verwendung von \"Hinzuf\303\274gen\"", b
30 end
31
32 def test_unqoute_in_the_middle
33 a ="Re: Photos =?ISO-8859-1?Q?Brosch=FCre_Rand?="
34 b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
35 assert_equal "Re: Photos Brosch\303\274re Rand", b
36 end
37
38 def test_unqoute_iso
39 a ="=?ISO-8859-1?Q?Brosch=FCre_Rand?="
40 b = TMail::Unquoter.unquote_and_convert_to(a, 'iso-8859-1')
41 expected = "Brosch\374re Rand"
42 expected.force_encoding 'iso-8859-1' if expected.respond_to?(:force_encoding)
43 assert_equal expected, b
44 end
45
46 def test_quote_multibyte_chars
47 original = "\303\246 \303\270 and \303\245"
48 original.force_encoding('ASCII-8BIT') if original.respond_to?(:force_encoding)
49
50 result = execute_in_sandbox(<<-CODE)
51 $:.unshift(File.dirname(__FILE__) + "/../lib/")
52 $KCODE = 'u'
53 require 'jcode'
54 require 'action_mailer/quoting'
55 include ActionMailer::Quoting
56 quoted_printable(#{original.inspect}, "UTF-8")
57 CODE
58
59 unquoted = TMail::Unquoter.unquote_and_convert_to(result, nil)
60 assert_equal unquoted, original
61 end
62
63
64 # test an email that has been created using \r\n newlines, instead of
65 # \n newlines.
66 def test_email_quoted_with_0d0a
67 mail = TMail::Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_quoted_with_0d0a"))
68 assert_match %r{Elapsed time}, mail.body
69 end
70
71 def test_email_with_partially_quoted_subject
72 mail = TMail::Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_partially_quoted_subject"))
73 assert_equal "Re: Test: \"\346\274\242\345\255\227\" mid \"\346\274\242\345\255\227\" tail", mail.subject
74 end
75
76 private
77 # This whole thing *could* be much simpler, but I don't think Tempfile,
78 # popen and others exist on all platforms (like Windows).
79 def execute_in_sandbox(code)
80 test_name = "#{File.dirname(__FILE__)}/am-quoting-test.#{$$}.rb"
81 res_name = "#{File.dirname(__FILE__)}/am-quoting-test.#{$$}.out"
82
83 File.open(test_name, "w+") do |file|
84 file.write(<<-CODE)
85 block = Proc.new do
86 #{code}
87 end
88 puts block.call
89 CODE
90 end
91
92 system("ruby #{test_name} > #{res_name}") or raise "could not run test in sandbox"
93 File.read(res_name).chomp
94 ensure
95 File.delete(test_name) rescue nil
96 File.delete(res_name) rescue nil
97 end
98 end