File: test_quote.rb

package info (click to toggle)
ruby-tmail 1.2.7.1-3%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,712 kB
  • sloc: ruby: 15,207; ansic: 482; yacc: 349; makefile: 30
file content (107 lines) | stat: -rw-r--r-- 4,087 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
$:.unshift File.dirname(__FILE__)
require 'test_helper'

class TestQuote < Test::Unit::TestCase
  def test_unquote_quoted_printable
    a ="=?ISO-8859-1?Q?[166417]_Bekr=E6ftelse_fra_Rejsefeber?="
    b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
    expected = "[166417] Bekr\303\246ftelse fra Rejsefeber"
    expected.force_encoding 'utf-8' if expected.respond_to? :force_encoding
    assert_equal expected, b
  end

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

  def test_unquote_without_charset
    a ="[166417]_Bekr=E6ftelse_fra_Rejsefeber"
    b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
    expected = "[166417]_Bekr=E6ftelse_fra_Rejsefeber"
    expected.force_encoding 'utf-8' if expected.respond_to? :force_encoding
    assert_equal expected, b
  end
  
  def test_unqoute_multiple
    a ="=?utf-8?q?Re=3A_=5B12=5D_=23137=3A_Inkonsistente_verwendung_von_=22Hin?==?utf-8?b?enVmw7xnZW4i?=" 
    b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
    expected = "Re: [12] #137: Inkonsistente verwendung von \"Hinzuf\303\274gen\""
    expected.force_encoding 'utf-8' if expected.respond_to? :force_encoding
    assert_equal expected, b
  end
  
  # See section 8 of http://www.faqs.org/rfcs/rfc2047.html
  def test_unquote_multiple_separated_by_whitespace
    a ="=?utf-8?Q?hello?= =?utf-8?Q?there?=" 
    b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
    expected = "hellothere"
    expected.force_encoding 'utf-8' if expected.respond_to? :force_encoding
    assert_equal expected, b    
  end  

  def test_unqoute_in_the_middle
    a ="Re: Photos =?ISO-8859-1?Q?Brosch=FCre_Rand?=" 
    b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
    expected = "Re: Photos Brosch\303\274re Rand"
    expected.force_encoding 'utf-8' if expected.respond_to? :force_encoding
    assert_equal expected, b
  end

  def test_unqoute_iso
    a ="=?ISO-8859-1?Q?Brosch=FCre_Rand?=" 
    b = TMail::Unquoter.unquote_and_convert_to(a, 'iso-8859-1')
    expected = "Brosch\374re Rand" 
    expected.force_encoding 'iso-8859-1' if expected.respond_to? :force_encoding 
    assert_equal expected, b
  end
  
  def test_quote_multibyte_chars
    original = "\303\246 \303\270 and \303\245"
    unquoted = TMail::Unquoter.unquote_and_convert_to(original, nil)
    original.force_encoding 'utf-8' if original.respond_to? :force_encoding
    unquoted.force_encoding 'utf-8' if unquoted.respond_to? :force_encoding
    assert_equal unquoted, original
  end

  # 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

  def test_email_with_partially_quoted_subject
    mail = TMail::Mail.parse(IO.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_partially_quoted_subject"))
    expected = "Re: Test: \"\346\274\242\345\255\227\" mid \"\346\274\242\345\255\227\" tail"
    expected.force_encoding 'utf-8' if expected.respond_to? :force_encoding
    assert_equal expected, mail.subject
  end

  def test_decode
    encoded, decoded = expected_base64_strings
    assert_equal decoded, TMail::Base64.decode(encoded)
  end

  def test_encode
    encoded, decoded = expected_base64_strings
    assert_equal encoded, TMail::Base64.encode(decoded)
  end

  private

  def expected_base64_strings
      if RUBY_VERSION < '1.9'
        options = "r" 
      else
        options = "r:ASCII-8BIT"
      end
      encoded = File.open("#{File.dirname(__FILE__)}/fixtures/raw_base64_encoded_string", options) {|f| f.read }
      decoded = File.open("#{File.dirname(__FILE__)}/fixtures/raw_base64_decoded_string", options) {|f| f.read }
      [encoded, decoded]
  end

end