blob: 197887d4aa939f063778c0f61bc305f26219749e [file] [log] [blame]
Fred Drake876dc702001-05-22 19:38:31 +00001import rfc822
Fred Drake876dc702001-05-22 19:38:31 +00002import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00003from test import test_support
Fred Drake876dc702001-05-22 19:38:31 +00004
Barry Warsawe75888e1999-01-14 20:00:58 +00005try:
6 from cStringIO import StringIO
7except ImportError:
8 from StringIO import StringIO
9
Fred Drake098b55a2000-10-23 17:30:23 +000010
Fred Drake876dc702001-05-22 19:38:31 +000011class MessageTestCase(unittest.TestCase):
12 def create_message(self, msg):
13 return rfc822.Message(StringIO(msg))
Barry Warsawe75888e1999-01-14 20:00:58 +000014
Fred Drake876dc702001-05-22 19:38:31 +000015 def test_get(self):
16 msg = self.create_message(
17 'To: "last, first" <userid@foo.net>\n\ntest\n')
18 self.assert_(msg.get("to") == '"last, first" <userid@foo.net>')
19 self.assert_(msg.get("TO") == '"last, first" <userid@foo.net>')
Guido van Rossumd842e072002-06-05 19:07:39 +000020 self.assert_(msg.get("No-Such-Header") is None)
Fred Drake876dc702001-05-22 19:38:31 +000021 self.assert_(msg.get("No-Such-Header", "No-Such-Value")
22 == "No-Such-Value")
Guido van Rossum3ed1be91999-05-03 19:57:01 +000023
Fred Drake876dc702001-05-22 19:38:31 +000024 def test_setdefault(self):
25 msg = self.create_message(
26 'To: "last, first" <userid@foo.net>\n\ntest\n')
27 self.assert_(not msg.has_key("New-Header"))
28 self.assert_(msg.setdefault("New-Header", "New-Value") == "New-Value")
29 self.assert_(msg.setdefault("New-Header", "Different-Value")
30 == "New-Value")
31 self.assert_(msg["new-header"] == "New-Value")
Guido van Rossum3ed1be91999-05-03 19:57:01 +000032
Fred Drake876dc702001-05-22 19:38:31 +000033 self.assert_(msg.setdefault("Another-Header") == "")
34 self.assert_(msg["another-header"] == "")
Barry Warsawe75888e1999-01-14 20:00:58 +000035
Fred Drake876dc702001-05-22 19:38:31 +000036 def check(self, msg, results):
37 """Check addresses and the date."""
38 m = self.create_message(msg)
39 i = 0
40 for n, a in m.getaddrlist('to') + m.getaddrlist('cc'):
41 try:
42 mn, ma = results[i][0], results[i][1]
43 except IndexError:
44 print 'extra parsed address:', repr(n), repr(a)
45 continue
46 i = i + 1
Barry Warsawdbcc8d92006-05-01 03:03:02 +000047 self.assertEqual(mn, n,
48 "Un-expected name: %s != %s" % (`mn`, `n`))
49 self.assertEqual(ma, a,
50 "Un-expected address: %s != %s" % (`ma`, `a`))
Fred Drake876dc702001-05-22 19:38:31 +000051 if mn == n and ma == a:
52 pass
53 else:
54 print 'not found:', repr(n), repr(a)
Barry Warsawe75888e1999-01-14 20:00:58 +000055
Fred Drake876dc702001-05-22 19:38:31 +000056 out = m.getdate('date')
57 if out:
58 self.assertEqual(out,
Barry Warsawe8bedeb2004-08-07 16:38:40 +000059 (1999, 1, 13, 23, 57, 35, 0, 1, 0),
Fred Drake876dc702001-05-22 19:38:31 +000060 "date conversion failed")
Barry Warsaw7c5b9d11999-07-12 18:47:00 +000061
Barry Warsaw38bfc4d2000-09-25 15:09:28 +000062
Fred Drake876dc702001-05-22 19:38:31 +000063 # Note: all test cases must have the same date (in various formats),
64 # or no date!
Barry Warsaw38bfc4d2000-09-25 15:09:28 +000065
Fred Drake876dc702001-05-22 19:38:31 +000066 def test_basic(self):
67 self.check(
68 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
69 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
70 'To: "Guido van\n'
71 '\t : Rossum" <guido@python.org>\n'
72 'Subject: test2\n'
73 '\n'
74 'test2\n',
75 [('Guido van\n\t : Rossum', 'guido@python.org')])
76
77 self.check(
78 'From: Barry <bwarsaw@python.org\n'
79 'To: guido@python.org (Guido: the Barbarian)\n'
80 'Subject: nonsense\n'
81 'Date: Wednesday, January 13 1999 23:57:35 -0500\n'
82 '\n'
83 'test',
84 [('Guido: the Barbarian', 'guido@python.org')])
85
86 self.check(
87 'From: Barry <bwarsaw@python.org\n'
88 'To: guido@python.org (Guido: the Barbarian)\n'
89 'Cc: "Guido: the Madman" <guido@python.org>\n'
90 'Date: 13-Jan-1999 23:57:35 EST\n'
91 '\n'
92 'test',
93 [('Guido: the Barbarian', 'guido@python.org'),
94 ('Guido: the Madman', 'guido@python.org')
95 ])
96
97 self.check(
98 'To: "The monster with\n'
99 ' the very long name: Guido" <guido@python.org>\n'
100 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
101 '\n'
102 'test',
103 [('The monster with\n the very long name: Guido',
104 'guido@python.org')])
105
106 self.check(
107 'To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>\n'
108 'CC: Mike Fletcher <mfletch@vrtelecom.com>,\n'
109 ' "\'string-sig@python.org\'" <string-sig@python.org>\n'
110 'Cc: fooz@bat.com, bart@toof.com\n'
111 'Cc: goit@lip.com\n'
112 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
113 '\n'
114 'test',
115 [('Amit J. Patel', 'amitp@Theory.Stanford.EDU'),
116 ('Mike Fletcher', 'mfletch@vrtelecom.com'),
117 ("'string-sig@python.org'", 'string-sig@python.org'),
118 ('', 'fooz@bat.com'),
119 ('', 'bart@toof.com'),
120 ('', 'goit@lip.com'),
121 ])
122
Barry Warsaw06069332001-07-16 20:44:16 +0000123 self.check(
Barry Warsaw19c10ca2001-11-13 18:01:37 +0000124 'To: Some One <someone@dom.ain>\n'
125 'From: Anudder Persin <subuddy.else@dom.ain>\n'
126 'Date:\n'
127 '\n'
128 'test',
129 [('Some One', 'someone@dom.ain')])
130
131 self.check(
Barry Warsaw06069332001-07-16 20:44:16 +0000132 'To: person@dom.ain (User J. Person)\n\n',
133 [('User J. Person', 'person@dom.ain')])
134
Barry Warsawdbcc8d92006-05-01 03:03:02 +0000135 def test_doublecomment(self):
136 # The RFC allows comments within comments in an email addr
137 self.check(
138 'To: person@dom.ain ((User J. Person)), John Doe <foo@bar.com>\n\n',
139 [('User J. Person', 'person@dom.ain'), ('John Doe', 'foo@bar.com')])
140
Fred Drake876dc702001-05-22 19:38:31 +0000141 def test_twisted(self):
142 # This one is just twisted. I don't know what the proper
143 # result should be, but it shouldn't be to infloop, which is
144 # what used to happen!
145 self.check(
146 'To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>\n'
147 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
148 '\n'
149 'test',
150 [('', ''),
151 ('', 'dd47@mail.xxx.edu'),
152 ('', '_at_hmhq@hdq-mdm1-imgout.companay.com'),
153 ])
154
155 def test_commas_in_full_name(self):
156 # This exercises the old commas-in-a-full-name bug, which
157 # should be doing the right thing in recent versions of the
158 # module.
159 self.check(
160 'To: "last, first" <userid@foo.net>\n'
161 '\n'
162 'test',
163 [('last, first', 'userid@foo.net')])
164
165 def test_quoted_name(self):
166 self.check(
167 'To: (Comment stuff) "Quoted name"@somewhere.com\n'
168 '\n'
169 'test',
170 [('Comment stuff', '"Quoted name"@somewhere.com')])
171
172 def test_bogus_to_header(self):
173 self.check(
174 'To: :\n'
175 'Cc: goit@lip.com\n'
176 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
177 '\n'
178 'test',
179 [('', 'goit@lip.com')])
180
181 def test_addr_ipquad(self):
182 self.check(
183 'To: guido@[132.151.1.21]\n'
184 '\n'
185 'foo',
186 [('', 'guido@[132.151.1.21]')])
Fred Drakecf71fef2001-05-22 15:02:19 +0000187
Raymond Hettingerce96d8b2004-09-22 17:17:32 +0000188 def test_iter(self):
189 m = rfc822.Message(StringIO(
190 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
191 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
192 'To: "Guido van\n'
193 '\t : Rossum" <guido@python.org>\n'
194 'Subject: test2\n'
195 '\n'
196 'test2\n' ))
197 self.assertEqual(sorted(m), ['date', 'from', 'subject', 'to'])
198
Barry Warsaw06069332001-07-16 20:44:16 +0000199 def test_rfc2822_phrases(self):
200 # RFC 2822 (the update to RFC 822) specifies that dots in phrases are
201 # obsolete syntax, which conforming programs MUST recognize but NEVER
202 # generate (see $4.1 Miscellaneous obsolete tokens). This is a
203 # departure from RFC 822 which did not allow dots in non-quoted
204 # phrases.
205 self.check('To: User J. Person <person@dom.ain>\n\n',
206 [('User J. Person', 'person@dom.ain')])
Fred Drakecf71fef2001-05-22 15:02:19 +0000207
Barry Warsaw0a8d4d52002-05-21 19:46:13 +0000208 # This takes too long to add to the test suite
Barry Warsaw135cce82001-11-13 21:33:52 +0000209## def test_an_excrutiatingly_long_address_field(self):
210## OBSCENELY_LONG_HEADER_MULTIPLIER = 10000
211## oneaddr = ('Person' * 10) + '@' + ('.'.join(['dom']*10)) + '.com'
212## addr = ', '.join([oneaddr] * OBSCENELY_LONG_HEADER_MULTIPLIER)
213## lst = rfc822.AddrlistClass(addr).getaddrlist()
214## self.assertEqual(len(lst), OBSCENELY_LONG_HEADER_MULTIPLIER)
215
Barry Warsaw0a8d4d52002-05-21 19:46:13 +0000216 def test_2getaddrlist(self):
217 eq = self.assertEqual
218 msg = self.create_message("""\
219To: aperson@dom.ain
220Cc: bperson@dom.ain
221Cc: cperson@dom.ain
222Cc: dperson@dom.ain
223
224A test message.
225""")
226 ccs = [('', a) for a in
227 ['bperson@dom.ain', 'cperson@dom.ain', 'dperson@dom.ain']]
228 addrs = msg.getaddrlist('cc')
229 addrs.sort()
230 eq(addrs, ccs)
231 # Try again, this one used to fail
232 addrs = msg.getaddrlist('cc')
233 addrs.sort()
234 eq(addrs, ccs)
Fred Drake2e2be372001-09-20 21:33:42 +0000235
Barry Warsawf6553282002-05-23 03:21:01 +0000236 def test_parseaddr(self):
237 eq = self.assertEqual
238 eq(rfc822.parseaddr('<>'), ('', ''))
239 eq(rfc822.parseaddr('aperson@dom.ain'), ('', 'aperson@dom.ain'))
240 eq(rfc822.parseaddr('bperson@dom.ain (Bea A. Person)'),
241 ('Bea A. Person', 'bperson@dom.ain'))
242 eq(rfc822.parseaddr('Cynthia Person <cperson@dom.ain>'),
243 ('Cynthia Person', 'cperson@dom.ain'))
244
Barry Warsaw1a5b9562002-09-11 02:32:57 +0000245 def test_quote_unquote(self):
246 eq = self.assertEqual
247 eq(rfc822.quote('foo\\wacky"name'), 'foo\\\\wacky\\"name')
248 eq(rfc822.unquote('"foo\\\\wacky\\"name"'), 'foo\\wacky"name')
249
250
Fred Drake2e2be372001-09-20 21:33:42 +0000251def test_main():
252 test_support.run_unittest(MessageTestCase)
253
254
255if __name__ == "__main__":
256 test_main()