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 if mn
== n
and ma
== a
:
51 print 'not found:', repr(n
), repr(a
)
53 out
= m
.getdate('date')
56 (1999, 1, 13, 23, 57, 35, 0, 0, 0),
57 "date conversion failed")
60 # Note: all test cases must have the same date (in various formats),
65 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
66 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
68 '\t : Rossum" <guido@python.org>\n'
72 [('Guido van\n\t : Rossum', 'guido@python.org')])
75 'From: Barry <bwarsaw@python.org\n'
76 'To: guido@python.org (Guido: the Barbarian)\n'
78 'Date: Wednesday, January 13 1999 23:57:35 -0500\n'
81 [('Guido: the Barbarian', 'guido@python.org')])
84 'From: Barry <bwarsaw@python.org\n'
85 'To: guido@python.org (Guido: the Barbarian)\n'
86 'Cc: "Guido: the Madman" <guido@python.org>\n'
87 'Date: 13-Jan-1999 23:57:35 EST\n'
90 [('Guido: the Barbarian', 'guido@python.org'),
91 ('Guido: the Madman', 'guido@python.org')
95 'To: "The monster with\n'
96 ' the very long name: Guido" <guido@python.org>\n'
97 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
100 [('The monster with\n the very long name: Guido',
101 'guido@python.org')])
104 'To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>\n'
105 'CC: Mike Fletcher <mfletch@vrtelecom.com>,\n'
106 ' "\'string-sig@python.org\'" <string-sig@python.org>\n'
107 'Cc: fooz@bat.com, bart@toof.com\n'
109 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
112 [('Amit J. Patel', 'amitp@Theory.Stanford.EDU'),
113 ('Mike Fletcher', 'mfletch@vrtelecom.com'),
114 ("'string-sig@python.org'", 'string-sig@python.org'),
115 ('', 'fooz@bat.com'),
116 ('', 'bart@toof.com'),
117 ('', 'goit@lip.com'),
121 'To: Some One <someone@dom.ain>\n'
122 'From: Anudder Persin <subuddy.else@dom.ain>\n'
126 [('Some One', 'someone@dom.ain')])
129 'To: person@dom.ain (User J. Person)\n\n',
130 [('User J. Person', 'person@dom.ain')])
132 def test_twisted(self
):
133 # This one is just twisted. I don't know what the proper
134 # result should be, but it shouldn't be to infloop, which is
135 # what used to happen!
137 'To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>\n'
138 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
142 ('', 'dd47@mail.xxx.edu'),
143 ('', '_at_hmhq@hdq-mdm1-imgout.companay.com'),
146 def test_commas_in_full_name(self
):
147 # This exercises the old commas-in-a-full-name bug, which
148 # should be doing the right thing in recent versions of the
151 'To: "last, first" <userid@foo.net>\n'
154 [('last, first', 'userid@foo.net')])
156 def test_quoted_name(self
):
158 'To: (Comment stuff) "Quoted name"@somewhere.com\n'
161 [('Comment stuff', '"Quoted name"@somewhere.com')])
163 def test_bogus_to_header(self
):
167 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
170 [('', 'goit@lip.com')])
172 def test_addr_ipquad(self
):
174 'To: guido@[132.151.1.21]\n'
177 [('', 'guido@[132.151.1.21]')])
179 def test_rfc2822_phrases(self
):
180 # RFC 2822 (the update to RFC 822) specifies that dots in phrases are
181 # obsolete syntax, which conforming programs MUST recognize but NEVER
182 # generate (see $4.1 Miscellaneous obsolete tokens). This is a
183 # departure from RFC 822 which did not allow dots in non-quoted
185 self
.check('To: User J. Person <person@dom.ain>\n\n',
186 [('User J. Person', 'person@dom.ain')])
188 # This takes to long to add to the test suite
189 ## def test_an_excrutiatingly_long_address_field(self):
190 ## OBSCENELY_LONG_HEADER_MULTIPLIER = 10000
191 ## oneaddr = ('Person' * 10) + '@' + ('.'.join(['dom']*10)) + '.com'
192 ## addr = ', '.join([oneaddr] * OBSCENELY_LONG_HEADER_MULTIPLIER)
193 ## lst = rfc822.AddrlistClass(addr).getaddrlist()
194 ## self.assertEqual(len(lst), OBSCENELY_LONG_HEADER_MULTIPLIER)
197 def test_parseaddr(self
):
198 eq
= self
.assertEqual
199 eq(rfc822
.parseaddr('<>'), ('', ''))
200 eq(rfc822
.parseaddr('aperson@dom.ain'), ('', 'aperson@dom.ain'))
201 eq(rfc822
.parseaddr('bperson@dom.ain (Bea A. Person)'),
202 ('Bea A. Person', 'bperson@dom.ain'))
203 eq(rfc822
.parseaddr('Cynthia Person <cperson@dom.ain>'),
204 ('Cynthia Person', 'cperson@dom.ain'))
207 test_support
.run_unittest(MessageTestCase
)
210 if __name__
== "__main__":