blob: 91e694a279f99217c893a47d49a40cc0ff219510 [file] [log] [blame]
Fred Drake876dc702001-05-22 19:38:31 +00001import rfc822
2import sys
Fred Drake876dc702001-05-22 19:38:31 +00003import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00004from test import test_support
Fred Drake876dc702001-05-22 19:38:31 +00005
Barry Warsawe75888e1999-01-14 20:00:58 +00006try:
7 from cStringIO import StringIO
8except ImportError:
9 from StringIO import StringIO
10
Fred Drake098b55a2000-10-23 17:30:23 +000011
Fred Drake876dc702001-05-22 19:38:31 +000012class MessageTestCase(unittest.TestCase):
13 def create_message(self, msg):
14 return rfc822.Message(StringIO(msg))
Barry Warsawe75888e1999-01-14 20:00:58 +000015
Fred Drake876dc702001-05-22 19:38:31 +000016 def test_get(self):
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>')
Guido van Rossumd842e072002-06-05 19:07:39 +000021 self.assert_(msg.get("No-Such-Header") is None)
Fred Drake876dc702001-05-22 19:38:31 +000022 self.assert_(msg.get("No-Such-Header", "No-Such-Value")
23 == "No-Such-Value")
Guido van Rossum3ed1be91999-05-03 19:57:01 +000024
Fred Drake876dc702001-05-22 19:38:31 +000025 def test_setdefault(self):
26 msg = self.create_message(
27 'To: "last, first" <userid@foo.net>\n\ntest\n')
Guido van Rossume2b70bc2006-08-18 22:13:04 +000028 self.assert_("New-Header" not in msg)
Fred Drake876dc702001-05-22 19:38:31 +000029 self.assert_(msg.setdefault("New-Header", "New-Value") == "New-Value")
30 self.assert_(msg.setdefault("New-Header", "Different-Value")
31 == "New-Value")
32 self.assert_(msg["new-header"] == "New-Value")
Guido van Rossum3ed1be91999-05-03 19:57:01 +000033
Fred Drake876dc702001-05-22 19:38:31 +000034 self.assert_(msg.setdefault("Another-Header") == "")
35 self.assert_(msg["another-header"] == "")
Barry Warsawe75888e1999-01-14 20:00:58 +000036
Fred Drake876dc702001-05-22 19:38:31 +000037 def check(self, msg, results):
38 """Check addresses and the date."""
39 m = self.create_message(msg)
40 i = 0
41 for n, a in m.getaddrlist('to') + m.getaddrlist('cc'):
42 try:
43 mn, ma = results[i][0], results[i][1]
44 except IndexError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000045 print('extra parsed address:', repr(n), repr(a))
Fred Drake876dc702001-05-22 19:38:31 +000046 continue
47 i = i + 1
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 self.assertEqual(mn, n,
Brett Cannon7ed3fcf2006-08-25 03:01:11 +000049 "Un-expected name: %r != %r" % (mn, n))
Thomas Wouters477c8d52006-05-27 19:21:47 +000050 self.assertEqual(ma, a,
Brett Cannon7ed3fcf2006-08-25 03:01:11 +000051 "Un-expected address: %r != %r" % (ma, a))
Fred Drake876dc702001-05-22 19:38:31 +000052 if mn == n and ma == a:
53 pass
54 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000055 print('not found:', repr(n), repr(a))
Barry Warsawe75888e1999-01-14 20:00:58 +000056
Fred Drake876dc702001-05-22 19:38:31 +000057 out = m.getdate('date')
58 if out:
59 self.assertEqual(out,
Barry Warsawe8bedeb2004-08-07 16:38:40 +000060 (1999, 1, 13, 23, 57, 35, 0, 1, 0),
Fred Drake876dc702001-05-22 19:38:31 +000061 "date conversion failed")
Barry Warsaw7c5b9d11999-07-12 18:47:00 +000062
Barry Warsaw38bfc4d2000-09-25 15:09:28 +000063
Fred Drake876dc702001-05-22 19:38:31 +000064 # Note: all test cases must have the same date (in various formats),
65 # or no date!
Barry Warsaw38bfc4d2000-09-25 15:09:28 +000066
Fred Drake876dc702001-05-22 19:38:31 +000067 def test_basic(self):
68 self.check(
69 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
70 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
71 'To: "Guido van\n'
72 '\t : Rossum" <guido@python.org>\n'
73 'Subject: test2\n'
74 '\n'
75 'test2\n',
76 [('Guido van\n\t : Rossum', 'guido@python.org')])
77
78 self.check(
79 'From: Barry <bwarsaw@python.org\n'
80 'To: guido@python.org (Guido: the Barbarian)\n'
81 'Subject: nonsense\n'
82 'Date: Wednesday, January 13 1999 23:57:35 -0500\n'
83 '\n'
84 'test',
85 [('Guido: the Barbarian', 'guido@python.org')])
86
87 self.check(
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'
92 '\n'
93 'test',
94 [('Guido: the Barbarian', 'guido@python.org'),
95 ('Guido: the Madman', 'guido@python.org')
96 ])
97
98 self.check(
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'
102 '\n'
103 'test',
104 [('The monster with\n the very long name: Guido',
105 'guido@python.org')])
106
107 self.check(
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'
112 'Cc: goit@lip.com\n'
113 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
114 '\n'
115 'test',
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'),
122 ])
123
Barry Warsaw06069332001-07-16 20:44:16 +0000124 self.check(
Barry Warsaw19c10ca2001-11-13 18:01:37 +0000125 'To: Some One <someone@dom.ain>\n'
126 'From: Anudder Persin <subuddy.else@dom.ain>\n'
127 'Date:\n'
128 '\n'
129 'test',
130 [('Some One', 'someone@dom.ain')])
131
132 self.check(
Barry Warsaw06069332001-07-16 20:44:16 +0000133 'To: person@dom.ain (User J. Person)\n\n',
134 [('User J. Person', 'person@dom.ain')])
135
Thomas Wouters477c8d52006-05-27 19:21:47 +0000136 def test_doublecomment(self):
137 # The RFC allows comments within comments in an email addr
138 self.check(
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')])
141
Fred Drake876dc702001-05-22 19:38:31 +0000142 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!
146 self.check(
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'
149 '\n'
150 'test',
151 [('', ''),
152 ('', 'dd47@mail.xxx.edu'),
153 ('', '_at_hmhq@hdq-mdm1-imgout.companay.com'),
154 ])
155
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
159 # module.
160 self.check(
161 'To: "last, first" <userid@foo.net>\n'
162 '\n'
163 'test',
164 [('last, first', 'userid@foo.net')])
165
166 def test_quoted_name(self):
167 self.check(
168 'To: (Comment stuff) "Quoted name"@somewhere.com\n'
169 '\n'
170 'test',
171 [('Comment stuff', '"Quoted name"@somewhere.com')])
172
173 def test_bogus_to_header(self):
174 self.check(
175 'To: :\n'
176 'Cc: goit@lip.com\n'
177 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
178 '\n'
179 'test',
180 [('', 'goit@lip.com')])
181
182 def test_addr_ipquad(self):
183 self.check(
184 'To: guido@[132.151.1.21]\n'
185 '\n'
186 'foo',
187 [('', 'guido@[132.151.1.21]')])
Fred Drakecf71fef2001-05-22 15:02:19 +0000188
Raymond Hettingerce96d8b2004-09-22 17:17:32 +0000189 def test_iter(self):
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'
193 'To: "Guido van\n'
194 '\t : Rossum" <guido@python.org>\n'
195 'Subject: test2\n'
196 '\n'
197 'test2\n' ))
198 self.assertEqual(sorted(m), ['date', 'from', 'subject', 'to'])
199
Barry Warsaw06069332001-07-16 20:44:16 +0000200 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
205 # phrases.
206 self.check('To: User J. Person <person@dom.ain>\n\n',
207 [('User J. Person', 'person@dom.ain')])
Fred Drakecf71fef2001-05-22 15:02:19 +0000208
Barry Warsaw0a8d4d52002-05-21 19:46:13 +0000209 # This takes too long to add to the test suite
Barry Warsaw135cce82001-11-13 21:33:52 +0000210## 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)
216
Barry Warsaw0a8d4d52002-05-21 19:46:13 +0000217 def test_2getaddrlist(self):
218 eq = self.assertEqual
219 msg = self.create_message("""\
220To: aperson@dom.ain
221Cc: bperson@dom.ain
222Cc: cperson@dom.ain
223Cc: dperson@dom.ain
224
225A test message.
226""")
227 ccs = [('', a) for a in
228 ['bperson@dom.ain', 'cperson@dom.ain', 'dperson@dom.ain']]
229 addrs = msg.getaddrlist('cc')
230 addrs.sort()
231 eq(addrs, ccs)
232 # Try again, this one used to fail
233 addrs = msg.getaddrlist('cc')
234 addrs.sort()
235 eq(addrs, ccs)
Fred Drake2e2be372001-09-20 21:33:42 +0000236
Barry Warsawf6553282002-05-23 03:21:01 +0000237 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'))
245
Barry Warsaw1a5b9562002-09-11 02:32:57 +0000246 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')
250
251
Fred Drake2e2be372001-09-20 21:33:42 +0000252def test_main():
253 test_support.run_unittest(MessageTestCase)
254
255
256if __name__ == "__main__":
257 test_main()