Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionmailer / test / quoting_test.rb
blob4af9014e43dcf2bcaef62d6da01562d1357d7300
1 require "#{File.dirname(__FILE__)}/abstract_unit"
2 require 'tmail'
3 require 'tempfile'
5 class QuotingTest < Test::Unit::TestCase
6   
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
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
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     assert_equal "Brosch\374re Rand", b
42   end
43     
44   def test_quote_multibyte_chars
45     original = "\303\246 \303\270 and \303\245"
47     result = execute_in_sandbox(<<-CODE)
48       $:.unshift(File.dirname(__FILE__) + "/../lib/")
49       $KCODE = 'u'
50       require 'jcode'
51       require 'action_mailer/quoting'
52       include ActionMailer::Quoting
53       quoted_printable(#{original.inspect}, "UTF-8")
54     CODE
56     unquoted = TMail::Unquoter.unquote_and_convert_to(result, nil)
57     assert_equal unquoted, original
58   end
59   
60   
61   # test an email that has been created using \r\n newlines, instead of
62   # \n newlines.
63   def test_email_quoted_with_0d0a
64     mail = TMail::Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_quoted_with_0d0a"))
65     assert_match %r{Elapsed time}, mail.body
66   end
68   def test_email_with_partially_quoted_subject
69     mail = TMail::Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_partially_quoted_subject"))
70     assert_equal "Re: Test: \"\346\274\242\345\255\227\" mid \"\346\274\242\345\255\227\" tail", mail.subject
71   end
72   
73   def test_rb_decode
74     encoded, decoded = expected_base64_strings
75     assert_equal decoded, TMail::Base64.rb_decode(encoded)
76   end
77   
78   def test_rb_encode
79     encoded, decoded = expected_base64_strings
80     assert_equal encoded.length, TMail::Base64.rb_encode(decoded).length
81   end
82   
83   def test_rb_decode_should_match_c_decode_if_available
84     encoded, decoded = expected_base64_strings
85     
86     begin
87       require 'tmail/base64.so'
88       assert_equal TMail::Base64.rb_decode(encoded), TMail::Base64.c_decode(encoded)
89     rescue LoadError
90       # No .so
91     end
92   end
93   
94   def test_rb_encode_should_match_c_encode_if_available
95     encoded, decoded = expected_base64_strings
96     
97     begin
98       require 'tmail/base64.so'
99       assert_equal TMail::Base64.rb_encode(decoded), TMail::Base64.c_encode(decoded)
100     rescue LoadError
101       # No .so
102     end
103   end
104   
105   private
106     
107     # This whole thing *could* be much simpler, but I don't think Tempfile,
108     # popen and others exist on all platforms (like Windows).
109     def execute_in_sandbox(code)
110       test_name = "#{File.dirname(__FILE__)}/am-quoting-test.#{$$}.rb"
111       res_name = "#{File.dirname(__FILE__)}/am-quoting-test.#{$$}.out"
113       File.open(test_name, "w+") do |file|
114         file.write(<<-CODE)
115           block = Proc.new do
116             #{code}
117           end
118           puts block.call
119         CODE
120       end
122       system("ruby #{test_name} > #{res_name}") or raise "could not run test in sandbox"
123       File.read(res_name).chomp
124     ensure
125       File.delete(test_name) rescue nil
126       File.delete(res_name) rescue nil
127     end
128     
129     def expected_base64_strings
130       [ File.read("#{File.dirname(__FILE__)}/fixtures/raw_base64_encoded_string"), File.read("#{File.dirname(__FILE__)}/fixtures/raw_base64_decoded_string") ]
131     end