]> git.cworth.org Git - sup/blob - test/test_mbox_parsing.rb
Merge commit 'origin/ncurses-widechar'
[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 TestMessage < 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_ignore_empty_lines
78     variants = [
79       "",
80       "Message-Id:       \n",
81       "Message-Id:\n",
82     ]
83     variants.each do |s|
84       h = MBox.read_header StringIO.new(s)
85       assert_nil h["Message-Id"]
86     end
87   end
88
89   def test_detect_end_of_headers
90     h = MBox.read_header StringIO.new(<<EOS)
91 From: Bob <bob@bob.com>
92
93 To: a dear friend
94 EOS
95   assert_equal "Bob <bob@bob.com>", h["From"]
96   assert_nil h["To"]
97
98   h = MBox.read_header StringIO.new(<<EOS)
99 From: Bob <bob@bob.com>
100 \r
101 To: a dear friend
102 EOS
103   assert_equal "Bob <bob@bob.com>", h["From"]
104   assert_nil h["To"]
105
106   h = MBox.read_header StringIO.new(<<EOS)
107 From: Bob <bob@bob.com>
108 \r\n\r
109 To: a dear friend
110 EOS
111   assert_equal "Bob <bob@bob.com>", h["From"]
112   assert_nil h["To"]
113   end
114 end