2008-01-18 22:03:38 -05:00
|
|
|
# encoding: utf-8
|
2008-01-05 08:32:22 -05:00
|
|
|
require 'abstract_unit'
|
2005-09-01 10:26:13 -04:00
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
class QuotingTest < Test::Unit::TestCase
|
2007-10-09 09:34:51 -04:00
|
|
|
# Move some tests from TMAIL here
|
|
|
|
def test_unquote_quoted_printable
|
2008-04-01 01:32:11 -04:00
|
|
|
a ="=?ISO-8859-1?Q?[166417]_Bekr=E6ftelse_fra_Rejsefeber?="
|
2009-11-21 07:55:53 -05:00
|
|
|
b = Mail::Encodings.unquote_and_convert_to(a, 'utf-8')
|
2007-10-09 09:34:51 -04:00
|
|
|
assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unquote_base64
|
|
|
|
a ="=?ISO-8859-1?B?WzE2NjQxN10gQmVrcuZmdGVsc2UgZnJhIFJlanNlZmViZXI=?="
|
2009-11-21 07:55:53 -05:00
|
|
|
b = Mail::Encodings.unquote_and_convert_to(a, 'utf-8')
|
2007-10-09 09:34:51 -04:00
|
|
|
assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unquote_without_charset
|
2008-04-01 01:32:11 -04:00
|
|
|
a ="[166417]_Bekr=E6ftelse_fra_Rejsefeber"
|
2009-11-21 07:55:53 -05:00
|
|
|
b = Mail::Encodings.unquote_and_convert_to(a, 'utf-8')
|
2007-10-09 09:34:51 -04:00
|
|
|
assert_equal "[166417]_Bekr=E6ftelse_fra_Rejsefeber", b
|
2008-04-01 01:32:11 -04:00
|
|
|
end
|
|
|
|
|
2007-10-09 09:34:51 -04:00
|
|
|
def test_unqoute_multiple
|
2008-04-01 01:32:11 -04:00
|
|
|
a ="=?utf-8?q?Re=3A_=5B12=5D_=23137=3A_Inkonsistente_verwendung_von_=22Hin?==?utf-8?b?enVmw7xnZW4i?="
|
2009-11-21 07:55:53 -05:00
|
|
|
b = Mail::Encodings.unquote_and_convert_to(a, 'utf-8')
|
2007-10-09 09:34:51 -04:00
|
|
|
assert_equal "Re: [12] #137: Inkonsistente verwendung von \"Hinzuf\303\274gen\"", b
|
|
|
|
end
|
2008-04-01 01:32:11 -04:00
|
|
|
|
2007-10-09 09:34:51 -04:00
|
|
|
def test_unqoute_in_the_middle
|
2008-04-01 01:32:11 -04:00
|
|
|
a ="Re: Photos =?ISO-8859-1?Q?Brosch=FCre_Rand?="
|
2009-11-21 07:55:53 -05:00
|
|
|
b = Mail::Encodings.unquote_and_convert_to(a, 'utf-8')
|
2007-10-09 09:34:51 -04:00
|
|
|
assert_equal "Re: Photos Brosch\303\274re Rand", b
|
|
|
|
end
|
2008-04-01 01:32:11 -04:00
|
|
|
|
2007-10-09 09:34:51 -04:00
|
|
|
def test_unqoute_iso
|
2008-04-01 01:32:11 -04:00
|
|
|
a ="=?ISO-8859-1?Q?Brosch=FCre_Rand?="
|
2009-11-21 07:55:53 -05:00
|
|
|
b = Mail::Encodings.unquote_and_convert_to(a, 'iso-8859-1')
|
2008-01-18 22:03:38 -05:00
|
|
|
expected = "Brosch\374re Rand"
|
|
|
|
expected.force_encoding 'iso-8859-1' if expected.respond_to?(:force_encoding)
|
|
|
|
assert_equal expected, b
|
2007-10-09 09:34:51 -04:00
|
|
|
end
|
2008-04-01 01:32:11 -04:00
|
|
|
|
2005-09-01 10:26:13 -04:00
|
|
|
def test_quote_multibyte_chars
|
|
|
|
original = "\303\246 \303\270 and \303\245"
|
2008-04-01 01:32:11 -04:00
|
|
|
original.force_encoding('ASCII-8BIT') if original.respond_to?(:force_encoding)
|
2005-09-01 10:26:13 -04:00
|
|
|
|
|
|
|
result = execute_in_sandbox(<<-CODE)
|
|
|
|
$:.unshift(File.dirname(__FILE__) + "/../lib/")
|
2009-01-10 09:43:29 -05:00
|
|
|
if RUBY_VERSION < '1.9'
|
|
|
|
$KCODE = 'u'
|
|
|
|
end
|
2005-09-01 10:26:13 -04:00
|
|
|
require 'action_mailer/quoting'
|
|
|
|
include ActionMailer::Quoting
|
|
|
|
quoted_printable(#{original.inspect}, "UTF-8")
|
|
|
|
CODE
|
|
|
|
|
2009-11-21 07:55:53 -05:00
|
|
|
unquoted = Mail::Encodings.unquote_and_convert_to(result, nil)
|
2009-11-23 04:58:22 -05:00
|
|
|
|
|
|
|
unquoted.force_encoding(Encoding::ASCII_8BIT) if unquoted.respond_to?(:force_encoding)
|
|
|
|
original.force_encoding(Encoding::ASCII_8BIT) if original.respond_to?(:force_encoding)
|
|
|
|
|
2005-09-01 10:26:13 -04:00
|
|
|
assert_equal unquoted, original
|
|
|
|
end
|
2008-04-01 01:32:11 -04:00
|
|
|
|
|
|
|
|
2006-09-09 16:16:42 -04:00
|
|
|
# test an email that has been created using \r\n newlines, instead of
|
|
|
|
# \n newlines.
|
|
|
|
def test_email_quoted_with_0d0a
|
2009-11-19 22:10:57 -05:00
|
|
|
mail = Mail.new(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_quoted_with_0d0a"))
|
2009-11-12 00:08:50 -05:00
|
|
|
# CHANGED: subject returns an object now
|
|
|
|
# assert_match %r{Elapsed time}, mail.body
|
2009-12-27 04:56:16 -05:00
|
|
|
assert_match %r{Elapsed time}, mail.body.to_s
|
2006-09-09 16:16:42 -04:00
|
|
|
end
|
|
|
|
|
2006-09-09 17:14:43 -04:00
|
|
|
def test_email_with_partially_quoted_subject
|
2009-11-19 22:10:57 -05:00
|
|
|
mail = Mail.new(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_partially_quoted_subject"))
|
2009-11-12 00:08:50 -05:00
|
|
|
# CHANGED: subject returns an object now
|
|
|
|
# assert_equal "Re: Test: \"\346\274\242\345\255\227\" mid \"\346\274\242\345\255\227\" tail", mail.subject
|
2010-01-02 19:15:50 -05:00
|
|
|
assert_equal "Re: Test: \"\346\274\242\345\255\227\" mid \"\346\274\242\345\255\227\" tail", mail.subject
|
2006-09-09 17:14:43 -04:00
|
|
|
end
|
2008-04-01 01:32:11 -04:00
|
|
|
|
2005-09-01 10:26:13 -04:00
|
|
|
private
|
|
|
|
# This whole thing *could* be much simpler, but I don't think Tempfile,
|
|
|
|
# popen and others exist on all platforms (like Windows).
|
|
|
|
def execute_in_sandbox(code)
|
|
|
|
test_name = "#{File.dirname(__FILE__)}/am-quoting-test.#{$$}.rb"
|
|
|
|
res_name = "#{File.dirname(__FILE__)}/am-quoting-test.#{$$}.out"
|
|
|
|
|
|
|
|
File.open(test_name, "w+") do |file|
|
|
|
|
file.write(<<-CODE)
|
|
|
|
block = Proc.new do
|
|
|
|
#{code}
|
|
|
|
end
|
|
|
|
puts block.call
|
|
|
|
CODE
|
|
|
|
end
|
|
|
|
|
|
|
|
system("ruby #{test_name} > #{res_name}") or raise "could not run test in sandbox"
|
2006-09-09 17:14:43 -04:00
|
|
|
File.read(res_name).chomp
|
2005-09-01 10:26:13 -04:00
|
|
|
ensure
|
|
|
|
File.delete(test_name) rescue nil
|
|
|
|
File.delete(res_name) rescue nil
|
|
|
|
end
|
|
|
|
end
|