blob: 4040cdb859385815c66a23a7478f27dea4fcd891 [file] [log] [blame]
Fred Drake876dc702001-05-22 19:38:31 +00001import rfc822
2import sys
3import test_support
4import unittest
5
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>')
21 self.assert_(msg.get("No-Such-Header") == "")
22 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')
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")
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:
45 print 'extra parsed address:', repr(n), repr(a)
46 continue
47 i = i + 1
48 if mn == n and ma == a:
49 pass
50 else:
51 print 'not found:', repr(n), repr(a)
Barry Warsawe75888e1999-01-14 20:00:58 +000052
Fred Drake876dc702001-05-22 19:38:31 +000053 out = m.getdate('date')
54 if out:
55 self.assertEqual(out,
56 (1999, 1, 13, 23, 57, 35, 0, 0, 0),
57 "date conversion failed")
Barry Warsaw7c5b9d11999-07-12 18:47:00 +000058
Barry Warsaw38bfc4d2000-09-25 15:09:28 +000059
Fred Drake876dc702001-05-22 19:38:31 +000060 # Note: all test cases must have the same date (in various formats),
61 # or no date!
Barry Warsaw38bfc4d2000-09-25 15:09:28 +000062
Fred Drake876dc702001-05-22 19:38:31 +000063 def test_basic(self):
64 self.check(
65 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
66 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
67 'To: "Guido van\n'
68 '\t : Rossum" <guido@python.org>\n'
69 'Subject: test2\n'
70 '\n'
71 'test2\n',
72 [('Guido van\n\t : Rossum', 'guido@python.org')])
73
74 self.check(
75 'From: Barry <bwarsaw@python.org\n'
76 'To: guido@python.org (Guido: the Barbarian)\n'
77 'Subject: nonsense\n'
78 'Date: Wednesday, January 13 1999 23:57:35 -0500\n'
79 '\n'
80 'test',
81 [('Guido: the Barbarian', 'guido@python.org')])
82
83 self.check(
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'
88 '\n'
89 'test',
90 [('Guido: the Barbarian', 'guido@python.org'),
91 ('Guido: the Madman', 'guido@python.org')
92 ])
93
94 self.check(
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'
98 '\n'
99 'test',
100 [('The monster with\n the very long name: Guido',
101 'guido@python.org')])
102
103 self.check(
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'
108 'Cc: goit@lip.com\n'
109 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
110 '\n'
111 'test',
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'),
118 ])
119
Barry Warsaw06069332001-07-16 20:44:16 +0000120 self.check(
121 'To: person@dom.ain (User J. Person)\n\n',
122 [('User J. Person', 'person@dom.ain')])
123
Fred Drake876dc702001-05-22 19:38:31 +0000124 def test_twisted(self):
125 # This one is just twisted. I don't know what the proper
126 # result should be, but it shouldn't be to infloop, which is
127 # what used to happen!
128 self.check(
129 'To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>\n'
130 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
131 '\n'
132 'test',
133 [('', ''),
134 ('', 'dd47@mail.xxx.edu'),
135 ('', '_at_hmhq@hdq-mdm1-imgout.companay.com'),
136 ])
137
138 def test_commas_in_full_name(self):
139 # This exercises the old commas-in-a-full-name bug, which
140 # should be doing the right thing in recent versions of the
141 # module.
142 self.check(
143 'To: "last, first" <userid@foo.net>\n'
144 '\n'
145 'test',
146 [('last, first', 'userid@foo.net')])
147
148 def test_quoted_name(self):
149 self.check(
150 'To: (Comment stuff) "Quoted name"@somewhere.com\n'
151 '\n'
152 'test',
153 [('Comment stuff', '"Quoted name"@somewhere.com')])
154
155 def test_bogus_to_header(self):
156 self.check(
157 'To: :\n'
158 'Cc: goit@lip.com\n'
159 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
160 '\n'
161 'test',
162 [('', 'goit@lip.com')])
163
164 def test_addr_ipquad(self):
165 self.check(
166 'To: guido@[132.151.1.21]\n'
167 '\n'
168 'foo',
169 [('', 'guido@[132.151.1.21]')])
Fred Drakecf71fef2001-05-22 15:02:19 +0000170
Barry Warsaw06069332001-07-16 20:44:16 +0000171 def test_rfc2822_phrases(self):
172 # RFC 2822 (the update to RFC 822) specifies that dots in phrases are
173 # obsolete syntax, which conforming programs MUST recognize but NEVER
174 # generate (see $4.1 Miscellaneous obsolete tokens). This is a
175 # departure from RFC 822 which did not allow dots in non-quoted
176 # phrases.
177 self.check('To: User J. Person <person@dom.ain>\n\n',
178 [('User J. Person', 'person@dom.ain')])
Fred Drakecf71fef2001-05-22 15:02:19 +0000179
Fred Drake876dc702001-05-22 19:38:31 +0000180test_support.run_unittest(MessageTestCase)