from test
import test_support
from cStringIO
import StringIO
from StringIO
import StringIO
class MessageTestCase(unittest
.TestCase
):
def create_message(self
, msg
):
return rfc822
.Message(StringIO(msg
))
msg
= self
.create_message(
'To: "last, first" <userid@foo.net>\n\ntest\n')
self
.assert_(msg
.get("to") == '"last, first" <userid@foo.net>')
self
.assert_(msg
.get("TO") == '"last, first" <userid@foo.net>')
self
.assert_(msg
.get("No-Such-Header") is None)
self
.assert_(msg
.get("No-Such-Header", "No-Such-Value")
def test_setdefault(self
):
msg
= self
.create_message(
'To: "last, first" <userid@foo.net>\n\ntest\n')
self
.assert_(not msg
.has_key("New-Header"))
self
.assert_(msg
.setdefault("New-Header", "New-Value") == "New-Value")
self
.assert_(msg
.setdefault("New-Header", "Different-Value")
self
.assert_(msg
["new-header"] == "New-Value")
self
.assert_(msg
.setdefault("Another-Header") == "")
self
.assert_(msg
["another-header"] == "")
def check(self
, msg
, results
):
"""Check addresses and the date."""
m
= self
.create_message(msg
)
for n
, a
in m
.getaddrlist('to') + m
.getaddrlist('cc'):
mn
, ma
= results
[i
][0], results
[i
][1]
print 'extra parsed address:', repr(n
), repr(a
)
print 'not found:', repr(n
), repr(a
)
(1999, 1, 13, 23, 57, 35, 0, 1, 0),
"date conversion failed")
# Note: all test cases must have the same date (in various formats),
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
'\t : Rossum" <guido@python.org>\n'
[('Guido van\n\t : Rossum', 'guido@python.org')])
'From: Barry <bwarsaw@python.org\n'
'To: guido@python.org (Guido: the Barbarian)\n'
'Date: Wednesday, January 13 1999 23:57:35 -0500\n'
[('Guido: the Barbarian', 'guido@python.org')])
'From: Barry <bwarsaw@python.org\n'
'To: guido@python.org (Guido: the Barbarian)\n'
'Cc: "Guido: the Madman" <guido@python.org>\n'
'Date: 13-Jan-1999 23:57:35 EST\n'
[('Guido: the Barbarian', 'guido@python.org'),
('Guido: the Madman', 'guido@python.org')
'To: "The monster with\n'
' the very long name: Guido" <guido@python.org>\n'
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
[('The monster with\n the very long name: Guido',
'To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>\n'
'CC: Mike Fletcher <mfletch@vrtelecom.com>,\n'
' "\'string-sig@python.org\'" <string-sig@python.org>\n'
'Cc: fooz@bat.com, bart@toof.com\n'
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
[('Amit J. Patel', 'amitp@Theory.Stanford.EDU'),
('Mike Fletcher', 'mfletch@vrtelecom.com'),
("'string-sig@python.org'", 'string-sig@python.org'),
'To: Some One <someone@dom.ain>\n'
'From: Anudder Persin <subuddy.else@dom.ain>\n'
[('Some One', 'someone@dom.ain')])
'To: person@dom.ain (User J. Person)\n\n',
[('User J. Person', 'person@dom.ain')])
# This one is just twisted. I don't know what the proper
# result should be, but it shouldn't be to infloop, which is
'To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>\n'
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
('', 'dd47@mail.xxx.edu'),
('', '_at_hmhq@hdq-mdm1-imgout.companay.com'),
def test_commas_in_full_name(self
):
# This exercises the old commas-in-a-full-name bug, which
# should be doing the right thing in recent versions of the
'To: "last, first" <userid@foo.net>\n'
[('last, first', 'userid@foo.net')])
def test_quoted_name(self
):
'To: (Comment stuff) "Quoted name"@somewhere.com\n'
[('Comment stuff', '"Quoted name"@somewhere.com')])
def test_bogus_to_header(self
):
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
def test_addr_ipquad(self
):
'To: guido@[132.151.1.21]\n'
[('', 'guido@[132.151.1.21]')])
m
= rfc822
.Message(StringIO(
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
'\t : Rossum" <guido@python.org>\n'
self
.assertEqual(sorted(m
), ['date', 'from', 'subject', 'to'])
def test_rfc2822_phrases(self
):
# RFC 2822 (the update to RFC 822) specifies that dots in phrases are
# obsolete syntax, which conforming programs MUST recognize but NEVER
# generate (see $4.1 Miscellaneous obsolete tokens). This is a
# departure from RFC 822 which did not allow dots in non-quoted
self
.check('To: User J. Person <person@dom.ain>\n\n',
[('User J. Person', 'person@dom.ain')])
# This takes too long to add to the test suite
## def test_an_excrutiatingly_long_address_field(self):
## OBSCENELY_LONG_HEADER_MULTIPLIER = 10000
## oneaddr = ('Person' * 10) + '@' + ('.'.join(['dom']*10)) + '.com'
## addr = ', '.join([oneaddr] * OBSCENELY_LONG_HEADER_MULTIPLIER)
## lst = rfc822.AddrlistClass(addr).getaddrlist()
## self.assertEqual(len(lst), OBSCENELY_LONG_HEADER_MULTIPLIER)
def test_2getaddrlist(self
):
msg
= self
.create_message("""\
['bperson@dom.ain', 'cperson@dom.ain', 'dperson@dom.ain']]
addrs
= msg
.getaddrlist('cc')
# Try again, this one used to fail
addrs
= msg
.getaddrlist('cc')
def test_parseaddr(self
):
eq(rfc822
.parseaddr('<>'), ('', ''))
eq(rfc822
.parseaddr('aperson@dom.ain'), ('', 'aperson@dom.ain'))
eq(rfc822
.parseaddr('bperson@dom.ain (Bea A. Person)'),
('Bea A. Person', 'bperson@dom.ain'))
eq(rfc822
.parseaddr('Cynthia Person <cperson@dom.ain>'),
('Cynthia Person', 'cperson@dom.ain'))
def test_quote_unquote(self
):
eq(rfc822
.quote('foo\\wacky"name'), 'foo\\\\wacky\\"name')
eq(rfc822
.unquote('"foo\\\\wacky\\"name"'), 'foo\\wacky"name')
test_support
.run_unittest(MessageTestCase
)
if __name__
== "__main__":