test/test_header_parsing.rb (3944B) - raw
1 #!/usr/bin/ruby
2
3 require 'test_helper'
4 require 'sup'
5 require 'stringio'
6
7 class TestMBoxParsing < Minitest::Test
8
9 include Redwood
10
11 def setup
12 @path = Dir.mktmpdir
13 @mbox = File.join(@path, 'test_mbox')
14 @log = StringIO.new
15 Redwood::Logger.add_sink @log
16 Redwood::Logger.remove_sink $stderr
17 end
18
19 def teardown
20 Redwood::Logger.clear!
21 Redwood::Logger.remove_sink @log
22 Redwood::Logger.add_sink $stderr
23 FileUtils.rm_r @path
24 end
25
26 def test_normal_headers
27 h = Source.parse_raw_email_header StringIO.new(<<EOS)
28 From: Bob <bob@bob.com>
29 To: Sally <sally@sally.com>
30 EOS
31
32 assert_equal "Bob <bob@bob.com>", h["from"]
33 assert_equal "Sally <sally@sally.com>", h["to"]
34 assert_nil h["message-id"]
35 end
36
37 def test_multiline
38 h = Source.parse_raw_email_header StringIO.new(<<EOS)
39 From: Bob <bob@bob.com>
40 Subject: one two three
41 four five six
42 To: Sally <sally@sally.com>
43 References: <seven>
44 <eight>
45 Seven: Eight
46 EOS
47
48 assert_equal "one two three four five six", h["subject"]
49 assert_equal "Sally <sally@sally.com>", h["to"]
50 assert_equal "<seven> <eight>", h["references"]
51 end
52
53 def test_ignore_spacing
54 variants = [
55 "Subject:one two three end\n",
56 "Subject: one two three end\n",
57 "Subject: one two three end \n",
58 ]
59 variants.each do |s|
60 h = Source.parse_raw_email_header StringIO.new(s)
61 assert_equal "one two three end", h["subject"]
62 end
63 end
64
65 def test_message_id_ignore_spacing
66 variants = [
67 "Message-Id: <one@bob.com> \n",
68 "Message-Id:<one@bob.com> \n",
69 ]
70 variants.each do |s|
71 h = Source.parse_raw_email_header StringIO.new(s)
72 assert_equal "<one@bob.com>", h["message-id"]
73 end
74 end
75
76 def test_blank_lines
77 h = Source.parse_raw_email_header StringIO.new("")
78 assert_nil h["message-id"]
79 end
80
81 def test_empty_headers
82 variants = [
83 "Message-Id: \n",
84 "Message-Id:\n",
85 ]
86 variants.each do |s|
87 h = Source.parse_raw_email_header StringIO.new(s)
88 assert_equal "", h["message-id"]
89 end
90 end
91
92 def test_detect_end_of_headers
93 h = Source.parse_raw_email_header StringIO.new(<<EOS)
94 From: Bob <bob@bob.com>
95
96 To: a dear friend
97 EOS
98 assert_equal "Bob <bob@bob.com>", h["from"]
99 assert_nil h["to"]
100
101 h = Source.parse_raw_email_header StringIO.new(<<EOS)
102 From: Bob <bob@bob.com>
103 \r
104 To: a dear friend
105 EOS
106 assert_equal "Bob <bob@bob.com>", h["from"]
107 assert_nil h["to"]
108
109 h = Source.parse_raw_email_header StringIO.new(<<EOS)
110 From: Bob <bob@bob.com>
111 \r\n\r
112 To: a dear friend
113 EOS
114 assert_equal "Bob <bob@bob.com>", h["from"]
115 assert_nil h["to"]
116 end
117
118 def test_from_line_splitting
119 l = MBox.new mbox_for_string(<<EOS)
120 From sup-talk-bounces@rubyforge.org Mon Apr 27 12:56:18 2009
121 From: Bob <bob@bob.com>
122 To: a dear friend
123
124 Hello there friend. How are you?
125
126 From sea to shining sea
127
128 From bob@bob.com I get only spam.
129
130 From bob@bob.com
131
132 From bob@bob.com
133
134 (that second one has spaces at the endj
135
136 This is the end of the email.
137 EOS
138 offset = l.next_offset 0
139 assert_equal 61, offset
140 offset = l.next_offset 61
141 assert_nil offset
142 assert_match(/WARNING: found invalid date in potential mbox split line, not splitting/,
143 @log.string)
144 end
145
146 def test_more_from_line_splitting
147 l = MBox.new mbox_for_string(<<EOS)
148 From sup-talk-bounces@rubyforge.org Mon Apr 27 12:56:18 2009
149 From: Bob <bob@bob.com>
150 To: a dear friend
151
152 Hello there friend. How are you?
153
154 From bob@bob.com Mon Apr 27 12:56:19 2009
155 From: Bob <bob@bob.com>
156 To: a dear friend
157
158 Hello again! Would you like to buy my products?
159 EOS
160 offset = l.next_offset 0
161 refute_nil offset
162
163 offset = l.next_offset offset
164 refute_nil offset
165
166 offset = l.next_offset offset
167 assert_nil offset
168 end
169
170 def mbox_for_string content
171 File.open(@mbox, 'w') do |f|
172 f.write content
173 end
174 "mbox://#{@mbox}"
175 end
176 end