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