quoting_test.rb 3.4 KB
Newer Older
1
# encoding: utf-8
2
require 'abstract_unit'
3 4 5
require 'tempfile'

class QuotingTest < Test::Unit::TestCase
6 7
  # Move some tests from TMAIL here
  def test_unquote_quoted_printable
8
    a ="=?ISO-8859-1?Q?[166417]_Bekr=E6ftelse_fra_Rejsefeber?="
9 10 11 12 13 14 15 16 17 18 19
    b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
    assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
  end

  def test_unquote_base64
    a ="=?ISO-8859-1?B?WzE2NjQxN10gQmVrcuZmdGVsc2UgZnJhIFJlanNlZmViZXI=?="
    b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
    assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
  end

  def test_unquote_without_charset
20
    a ="[166417]_Bekr=E6ftelse_fra_Rejsefeber"
21 22
    b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
    assert_equal "[166417]_Bekr=E6ftelse_fra_Rejsefeber", b
23 24
  end

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 28 29
    b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
    assert_equal "Re: [12] #137: Inkonsistente verwendung von \"Hinzuf\303\274gen\"", b
  end
30

31
  def test_unqoute_in_the_middle
32
    a ="Re: Photos =?ISO-8859-1?Q?Brosch=FCre_Rand?="
33 34 35
    b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
    assert_equal "Re: Photos Brosch\303\274re Rand", b
  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 41 42
    expected = "Brosch\374re Rand"
    expected.force_encoding 'iso-8859-1' if expected.respond_to?(:force_encoding)
    assert_equal expected, b
43
  end
44

45 46
  def test_quote_multibyte_chars
    original = "\303\246 \303\270 and \303\245"
47
    original.force_encoding('ASCII-8BIT') if original.respond_to?(:force_encoding)
48 49 50

    result = execute_in_sandbox(<<-CODE)
      $:.unshift(File.dirname(__FILE__) + "/../lib/")
51 52 53 54
      if RUBY_VERSION < '1.9'
        $KCODE = 'u'
        require 'jcode'
      end
55 56 57 58 59 60 61 62
      require 'action_mailer/quoting'
      include ActionMailer::Quoting
      quoted_printable(#{original.inspect}, "UTF-8")
    CODE

    unquoted = TMail::Unquoter.unquote_and_convert_to(result, nil)
    assert_equal unquoted, original
  end
63 64


65 66 67 68 69 70 71
  # test an email that has been created using \r\n newlines, instead of
  # \n newlines.
  def test_email_quoted_with_0d0a
    mail = TMail::Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_quoted_with_0d0a"))
    assert_match %r{Elapsed time}, mail.body
  end

72 73 74 75
  def test_email_with_partially_quoted_subject
    mail = TMail::Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_partially_quoted_subject"))
    assert_equal "Re: Test: \"\346\274\242\345\255\227\" mid \"\346\274\242\345\255\227\" tail", mail.subject
  end
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  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"
94
      File.read(res_name).chomp
95 96 97 98 99
    ensure
      File.delete(test_name) rescue nil
      File.delete(res_name) rescue nil
    end
end