]> git.cworth.org Git - sup/blob - test/test_header_parsing.rb
add an after-add-message hook
[sup] / test / test_header_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 = Source.parse_raw_email_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   def test_multiline
28     h = Source.parse_raw_email_header StringIO.new(<<EOS)
29 From: Bob <bob@bob.com>
30 Subject: one two three
31   four five six
32 To: Sally <sally@sally.com>
33 References: <seven>
34   <eight>
35 Seven: Eight
36 EOS
37
38     assert_equal "one two three four five six", h["subject"]
39     assert_equal "Sally <sally@sally.com>", h["to"]
40     assert_equal "<seven> <eight>", h["references"]
41   end
42
43   def test_ignore_spacing
44     variants = [
45       "Subject:one two  three   end\n",
46       "Subject:    one two  three   end\n",
47       "Subject:   one two  three   end    \n",
48     ]
49     variants.each do |s|
50       h = Source.parse_raw_email_header StringIO.new(s)
51       assert_equal "one two  three   end", h["subject"]
52     end
53   end
54
55   def test_message_id_ignore_spacing
56     variants = [
57       "Message-Id:     <one@bob.com>       \n",
58       "Message-Id:<one@bob.com>       \n",
59     ]
60     variants.each do |s|
61       h = Source.parse_raw_email_header StringIO.new(s)
62       assert_equal "<one@bob.com>", h["message-id"]
63     end
64   end
65
66   def test_blank_lines
67     h = Source.parse_raw_email_header StringIO.new("")
68     assert_equal nil, h["message-id"]
69   end
70
71   def test_empty_headers
72     variants = [
73       "Message-Id:       \n",
74       "Message-Id:\n",
75     ]
76     variants.each do |s|
77       h = Source.parse_raw_email_header StringIO.new(s)
78       assert_equal "", h["message-id"]
79     end
80   end
81
82   def test_detect_end_of_headers
83     h = Source.parse_raw_email_header StringIO.new(<<EOS)
84 From: Bob <bob@bob.com>
85
86 To: a dear friend
87 EOS
88   assert_equal "Bob <bob@bob.com>", h["from"]
89   assert_nil h["to"]
90
91   h = Source.parse_raw_email_header StringIO.new(<<EOS)
92 From: Bob <bob@bob.com>
93 \r
94 To: a dear friend
95 EOS
96   assert_equal "Bob <bob@bob.com>", h["from"]
97   assert_nil h["to"]
98
99   h = Source.parse_raw_email_header StringIO.new(<<EOS)
100 From: Bob <bob@bob.com>
101 \r\n\r
102 To: a dear friend
103 EOS
104   assert_equal "Bob <bob@bob.com>", h["from"]
105   assert_nil h["to"]
106   end
107
108   def test_from_line_splitting
109     l = MBox::Loader.new StringIO.new(<<EOS)
110 From sup-talk-bounces@rubyforge.org Mon Apr 27 12:56:18 2009
111 From: Bob <bob@bob.com>
112 To: a dear friend
113
114 Hello there friend. How are you?
115
116 From sea to shining sea
117
118 From bob@bob.com I get only spam.
119
120 From bob@bob.com   
121
122 From bob@bob.com
123
124 (that second one has spaces at the endj
125
126 This is the end of the email.
127 EOS
128     offset, labels = l.next
129     assert_equal 0, offset
130     offset, labels = l.next
131     assert_nil offset
132   end
133
134   def test_more_from_line_splitting
135     l = MBox::Loader.new StringIO.new(<<EOS)
136 From sup-talk-bounces@rubyforge.org Mon Apr 27 12:56:18 2009
137 From: Bob <bob@bob.com>
138 To: a dear friend
139
140 Hello there friend. How are you?
141
142 From bob@bob.com Mon Apr 27 12:56:19 2009
143 From: Bob <bob@bob.com>
144 To: a dear friend
145
146 Hello again! Would you like to buy my products?
147 EOS
148     offset, labels = l.next
149     assert_not_nil offset
150
151     offset, labels = l.next
152     assert_not_nil offset
153
154     offset, labels = l.next
155     assert_nil offset
156   end
157 end