]> git.cworth.org Git - sup/blob - test/test_mbox_parsing.rb
Merge branch 'background-save' into next
[sup] / test / test_mbox_parsing.rb
1 #!/usr/bin/ruby
2
3 require 'test/unit'
4 require 'sup'
5 require 'stringio'
6
7 include Redwood
8
9 class TestMBoxParsing < Test::Unit::TestCase
10   def setup
11   end
12
13   def teardown
14   end
15
16   def test_normal_headers
17     h = MBox.read_header StringIO.new(<<EOS)
18 From: Bob <bob@bob.com>
19 To: Sally <sally@sally.com>
20 EOS
21
22     assert_equal "Bob <bob@bob.com>", h["From"]
23     assert_equal "Sally <sally@sally.com>", h["To"]
24     assert_nil h["Message-Id"]
25   end
26
27   ## this is shitty behavior in retrospect, but it's built in now.
28   def test_message_id_stripping
29     h = MBox.read_header StringIO.new("Message-Id: <one@bob.com>\n")
30     assert_equal "one@bob.com", h["Message-Id"]
31
32     h = MBox.read_header StringIO.new("Message-Id: one@bob.com\n")
33     assert_equal "one@bob.com", h["Message-Id"]
34   end
35
36   def test_multiline
37     h = MBox.read_header StringIO.new(<<EOS)
38 From: Bob <bob@bob.com>
39 Subject: one two three
40   four five six
41 To: Sally <sally@sally.com>
42 References: seven
43   eight
44 Seven: Eight
45 EOS
46
47     assert_equal "one two three four five six", h["Subject"]
48     assert_equal "Sally <sally@sally.com>", h["To"]
49     assert_equal "seven eight", h["References"]
50   end
51
52   def test_ignore_spacing
53     variants = [
54       "Subject:one two  three   end\n",
55       "Subject:    one two  three   end\n",
56       "Subject:   one two  three   end    \n",
57     ]
58     variants.each do |s|
59       h = MBox.read_header StringIO.new(s)
60       assert_equal "one two  three   end", h["Subject"]
61     end
62   end
63
64   def test_message_id_ignore_spacing
65     variants = [
66       "Message-Id:     <one@bob.com>       \n",
67       "Message-Id:      one@bob.com        \n",
68       "Message-Id:<one@bob.com>       \n",
69       "Message-Id:one@bob.com       \n",
70     ]
71     variants.each do |s|
72       h = MBox.read_header StringIO.new(s)
73       assert_equal "one@bob.com", h["Message-Id"]
74     end
75   end
76
77   def test_blank_lines
78     h = MBox.read_header StringIO.new("")
79     assert_equal nil, h["Message-Id"]
80   end
81
82   def test_empty_headers
83     variants = [
84       "Message-Id:       \n",
85       "Message-Id:\n",
86     ]
87     variants.each do |s|
88       h = MBox.read_header StringIO.new(s)
89       assert_equal "", h["Message-Id"]
90     end
91   end
92
93   def test_detect_end_of_headers
94     h = MBox.read_header StringIO.new(<<EOS)
95 From: Bob <bob@bob.com>
96
97 To: a dear friend
98 EOS
99   assert_equal "Bob <bob@bob.com>", h["From"]
100   assert_nil h["To"]
101
102   h = MBox.read_header StringIO.new(<<EOS)
103 From: Bob <bob@bob.com>
104 \r
105 To: a dear friend
106 EOS
107   assert_equal "Bob <bob@bob.com>", h["From"]
108   assert_nil h["To"]
109
110   h = MBox.read_header StringIO.new(<<EOS)
111 From: Bob <bob@bob.com>
112 \r\n\r
113 To: a dear friend
114 EOS
115   assert_equal "Bob <bob@bob.com>", h["From"]
116   assert_nil h["To"]
117   end
118 end