2
from test import test_support
4
rfc822 = test_support.import_module("rfc822", deprecated=True)
7
from cStringIO import StringIO
9
from StringIO import StringIO
12
class MessageTestCase(unittest.TestCase):
13
def create_message(self, msg):
14
return rfc822.Message(StringIO(msg))
17
msg = self.create_message(
18
'To: "last, first" <userid@foo.net>\n\ntest\n')
19
self.assert_(msg.get("to") == '"last, first" <userid@foo.net>')
20
self.assert_(msg.get("TO") == '"last, first" <userid@foo.net>')
21
self.assert_(msg.get("No-Such-Header") is None)
22
self.assert_(msg.get("No-Such-Header", "No-Such-Value")
25
def test_setdefault(self):
26
msg = self.create_message(
27
'To: "last, first" <userid@foo.net>\n\ntest\n')
28
self.assert_(not msg.has_key("New-Header"))
29
self.assert_(msg.setdefault("New-Header", "New-Value") == "New-Value")
30
self.assert_(msg.setdefault("New-Header", "Different-Value")
32
self.assert_(msg["new-header"] == "New-Value")
34
self.assert_(msg.setdefault("Another-Header") == "")
35
self.assert_(msg["another-header"] == "")
37
def check(self, msg, results):
38
"""Check addresses and the date."""
39
m = self.create_message(msg)
41
for n, a in m.getaddrlist('to') + m.getaddrlist('cc'):
43
mn, ma = results[i][0], results[i][1]
45
print 'extra parsed address:', repr(n), repr(a)
48
self.assertEqual(mn, n,
49
"Un-expected name: %s != %s" % (`mn`, `n`))
50
self.assertEqual(ma, a,
51
"Un-expected address: %s != %s" % (`ma`, `a`))
52
if mn == n and ma == a:
55
print 'not found:', repr(n), repr(a)
57
out = m.getdate('date')
60
(1999, 1, 13, 23, 57, 35, 0, 1, 0),
61
"date conversion failed")
64
# Note: all test cases must have the same date (in various formats),
69
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
70
'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
72
'\t : Rossum" <guido@python.org>\n'
76
[('Guido van\n\t : Rossum', 'guido@python.org')])
79
'From: Barry <bwarsaw@python.org\n'
80
'To: guido@python.org (Guido: the Barbarian)\n'
82
'Date: Wednesday, January 13 1999 23:57:35 -0500\n'
85
[('Guido: the Barbarian', 'guido@python.org')])
88
'From: Barry <bwarsaw@python.org\n'
89
'To: guido@python.org (Guido: the Barbarian)\n'
90
'Cc: "Guido: the Madman" <guido@python.org>\n'
91
'Date: 13-Jan-1999 23:57:35 EST\n'
94
[('Guido: the Barbarian', 'guido@python.org'),
95
('Guido: the Madman', 'guido@python.org')
99
'To: "The monster with\n'
100
' the very long name: Guido" <guido@python.org>\n'
101
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
104
[('The monster with\n the very long name: Guido',
105
'guido@python.org')])
108
'To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>\n'
109
'CC: Mike Fletcher <mfletch@vrtelecom.com>,\n'
110
' "\'string-sig@python.org\'" <string-sig@python.org>\n'
111
'Cc: fooz@bat.com, bart@toof.com\n'
113
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
116
[('Amit J. Patel', 'amitp@Theory.Stanford.EDU'),
117
('Mike Fletcher', 'mfletch@vrtelecom.com'),
118
("'string-sig@python.org'", 'string-sig@python.org'),
119
('', 'fooz@bat.com'),
120
('', 'bart@toof.com'),
121
('', 'goit@lip.com'),
125
'To: Some One <someone@dom.ain>\n'
126
'From: Anudder Persin <subuddy.else@dom.ain>\n'
130
[('Some One', 'someone@dom.ain')])
133
'To: person@dom.ain (User J. Person)\n\n',
134
[('User J. Person', 'person@dom.ain')])
136
def test_doublecomment(self):
137
# The RFC allows comments within comments in an email addr
139
'To: person@dom.ain ((User J. Person)), John Doe <foo@bar.com>\n\n',
140
[('User J. Person', 'person@dom.ain'), ('John Doe', 'foo@bar.com')])
142
def test_twisted(self):
143
# This one is just twisted. I don't know what the proper
144
# result should be, but it shouldn't be to infloop, which is
145
# what used to happen!
147
'To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>\n'
148
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
152
('', 'dd47@mail.xxx.edu'),
153
('', '_at_hmhq@hdq-mdm1-imgout.companay.com'),
156
def test_commas_in_full_name(self):
157
# This exercises the old commas-in-a-full-name bug, which
158
# should be doing the right thing in recent versions of the
161
'To: "last, first" <userid@foo.net>\n'
164
[('last, first', 'userid@foo.net')])
166
def test_quoted_name(self):
168
'To: (Comment stuff) "Quoted name"@somewhere.com\n'
171
[('Comment stuff', '"Quoted name"@somewhere.com')])
173
def test_bogus_to_header(self):
177
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
180
[('', 'goit@lip.com')])
182
def test_addr_ipquad(self):
184
'To: guido@[132.151.1.21]\n'
187
[('', 'guido@[132.151.1.21]')])
190
m = rfc822.Message(StringIO(
191
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
192
'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
194
'\t : Rossum" <guido@python.org>\n'
198
self.assertEqual(sorted(m), ['date', 'from', 'subject', 'to'])
200
def test_rfc2822_phrases(self):
201
# RFC 2822 (the update to RFC 822) specifies that dots in phrases are
202
# obsolete syntax, which conforming programs MUST recognize but NEVER
203
# generate (see $4.1 Miscellaneous obsolete tokens). This is a
204
# departure from RFC 822 which did not allow dots in non-quoted
206
self.check('To: User J. Person <person@dom.ain>\n\n',
207
[('User J. Person', 'person@dom.ain')])
209
# This takes too long to add to the test suite
210
## def test_an_excrutiatingly_long_address_field(self):
211
## OBSCENELY_LONG_HEADER_MULTIPLIER = 10000
212
## oneaddr = ('Person' * 10) + '@' + ('.'.join(['dom']*10)) + '.com'
213
## addr = ', '.join([oneaddr] * OBSCENELY_LONG_HEADER_MULTIPLIER)
214
## lst = rfc822.AddrlistClass(addr).getaddrlist()
215
## self.assertEqual(len(lst), OBSCENELY_LONG_HEADER_MULTIPLIER)
217
def test_2getaddrlist(self):
218
eq = self.assertEqual
219
msg = self.create_message("""\
227
ccs = [('', a) for a in
228
['bperson@dom.ain', 'cperson@dom.ain', 'dperson@dom.ain']]
229
addrs = msg.getaddrlist('cc')
232
# Try again, this one used to fail
233
addrs = msg.getaddrlist('cc')
237
def test_parseaddr(self):
238
eq = self.assertEqual
239
eq(rfc822.parseaddr('<>'), ('', ''))
240
eq(rfc822.parseaddr('aperson@dom.ain'), ('', 'aperson@dom.ain'))
241
eq(rfc822.parseaddr('bperson@dom.ain (Bea A. Person)'),
242
('Bea A. Person', 'bperson@dom.ain'))
243
eq(rfc822.parseaddr('Cynthia Person <cperson@dom.ain>'),
244
('Cynthia Person', 'cperson@dom.ain'))
246
def test_quote_unquote(self):
247
eq = self.assertEqual
248
eq(rfc822.quote('foo\\wacky"name'), 'foo\\\\wacky\\"name')
249
eq(rfc822.unquote('"foo\\\\wacky\\"name"'), 'foo\\wacky"name')
253
test_support.run_unittest(MessageTestCase)
256
if __name__ == "__main__":