File: test_header_parsing.rb

package info (click to toggle)
sup-mail 0.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,168 kB
  • sloc: ruby: 12,583; sh: 115; makefile: 9
file content (168 lines) | stat: -rw-r--r-- 3,628 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/ruby

require 'test_helper'
require 'sup'
require 'stringio'

include Redwood

class TestMBoxParsing < Minitest::Test

  def setup
    @path = Dir.mktmpdir
    @mbox = File.join(@path, 'test_mbox')
  end

  def teardown
    FileUtils.rm_r @path
  end

  def test_normal_headers
    h = Source.parse_raw_email_header StringIO.new(<<EOS)
From: Bob <bob@bob.com>
To: Sally <sally@sally.com>
EOS

    assert_equal "Bob <bob@bob.com>", h["from"]
    assert_equal "Sally <sally@sally.com>", h["to"]
    assert_nil h["message-id"]
  end

  def test_multiline
    h = Source.parse_raw_email_header StringIO.new(<<EOS)
From: Bob <bob@bob.com>
Subject: one two three
  four five six
To: Sally <sally@sally.com>
References: <seven>
  <eight>
Seven: Eight
EOS

    assert_equal "one two three four five six", h["subject"]
    assert_equal "Sally <sally@sally.com>", h["to"]
    assert_equal "<seven> <eight>", h["references"]
  end

  def test_ignore_spacing
    variants = [
      "Subject:one two  three   end\n",
      "Subject:    one two  three   end\n",
      "Subject:   one two  three   end    \n",
    ]
    variants.each do |s|
      h = Source.parse_raw_email_header StringIO.new(s)
      assert_equal "one two  three   end", h["subject"]
    end
  end

  def test_message_id_ignore_spacing
    variants = [
      "Message-Id:     <one@bob.com>       \n",
      "Message-Id:<one@bob.com>       \n",
    ]
    variants.each do |s|
      h = Source.parse_raw_email_header StringIO.new(s)
      assert_equal "<one@bob.com>", h["message-id"]
    end
  end

  def test_blank_lines
    h = Source.parse_raw_email_header StringIO.new("")
    assert_equal nil, h["message-id"]
  end

  def test_empty_headers
    variants = [
      "Message-Id:       \n",
      "Message-Id:\n",
    ]
    variants.each do |s|
      h = Source.parse_raw_email_header StringIO.new(s)
      assert_equal "", h["message-id"]
    end
  end

  def test_detect_end_of_headers
    h = Source.parse_raw_email_header StringIO.new(<<EOS)
From: Bob <bob@bob.com>

To: a dear friend
EOS
  assert_equal "Bob <bob@bob.com>", h["from"]
  assert_nil h["to"]

  h = Source.parse_raw_email_header StringIO.new(<<EOS)
From: Bob <bob@bob.com>
\r
To: a dear friend
EOS
  assert_equal "Bob <bob@bob.com>", h["from"]
  assert_nil h["to"]

  h = Source.parse_raw_email_header StringIO.new(<<EOS)
From: Bob <bob@bob.com>
\r\n\r
To: a dear friend
EOS
  assert_equal "Bob <bob@bob.com>", h["from"]
  assert_nil h["to"]
  end

  def test_from_line_splitting
    l = MBox.new mbox_for_string(<<EOS)
From sup-talk-bounces@rubyforge.org Mon Apr 27 12:56:18 2009
From: Bob <bob@bob.com>
To: a dear friend

Hello there friend. How are you?

From sea to shining sea

From bob@bob.com I get only spam.

From bob@bob.com

From bob@bob.com

(that second one has spaces at the endj

This is the end of the email.
EOS
    offset = l.next_offset 0
    assert_equal 61, offset
    offset = l.next_offset 61
    assert_nil offset
  end

  def test_more_from_line_splitting
    l = MBox.new mbox_for_string(<<EOS)
From sup-talk-bounces@rubyforge.org Mon Apr 27 12:56:18 2009
From: Bob <bob@bob.com>
To: a dear friend

Hello there friend. How are you?

From bob@bob.com Mon Apr 27 12:56:19 2009
From: Bob <bob@bob.com>
To: a dear friend

Hello again! Would you like to buy my products?
EOS
    offset = l.next_offset 0
    refute_nil offset

    offset = l.next_offset offset
    refute_nil offset

    offset = l.next_offset offset
    assert_nil offset
  end

  def mbox_for_string content
    File.open(@mbox, 'w') do |f|
      f.write content
    end
    "mbox://#{@mbox}"
  end
end