]> git.cworth.org Git - sup/blob - test/test_header_parsing.rb
Merge branch 'better-buffer-list' into next
[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 end