| 1 |
import unittest |
| 2 |
from test import test_support |
| 3 |
|
| 4 |
rfc822 = test_support.import_module("rfc822", deprecated=True) |
| 5 |
|
| 6 |
try: |
| 7 |
from cStringIO import StringIO |
| 8 |
except ImportError: |
| 9 |
from StringIO import StringIO |
| 10 |
|
| 11 |
|
| 12 |
class MessageTestCase(unittest.TestCase): |
| 13 |
def create_message(self, msg): |
| 14 |
return rfc822.Message(StringIO(msg)) |
| 15 |
|
| 16 |
def test_get(self): |
| 17 |
msg = self.create_message( |
| 18 |
'To: "last, first" <[email protected]>\n\ntest\n') |
| 19 |
self.assertTrue(msg.get("to") == '"last, first" <[email protected]>') |
| 20 |
self.assertTrue(msg.get("TO") == '"last, first" <[email protected]>') |
| 21 |
self.assertTrue(msg.get("No-Such-Header") is None) |
| 22 |
self.assertTrue(msg.get("No-Such-Header", "No-Such-Value") |
| 23 |
== "No-Such-Value") |
| 24 |
|
| 25 |
def test_setdefault(self): |
| 26 |
msg = self.create_message( |
| 27 |
'To: "last, first" <[email protected]>\n\ntest\n') |
| 28 |
self.assertTrue(not msg.has_key("New-Header")) |
| 29 |
self.assertTrue(msg.setdefault("New-Header", "New-Value") == "New-Value") |
| 30 |
self.assertTrue(msg.setdefault("New-Header", "Different-Value") |
| 31 |
== "New-Value") |
| 32 |
self.assertTrue(msg["new-header"] == "New-Value") |
| 33 |
|
| 34 |
self.assertTrue(msg.setdefault("Another-Header") == "") |
| 35 |
self.assertTrue(msg["another-header"] == "") |
| 36 |
|
| 37 |
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 |
self.assertEqual(mn, n, |
| 49 |
"Un-expected name: %r != %r" % (mn, n)) |
| 50 |
self.assertEqual(ma, a, |
| 51 |
"Un-expected address: %r != %r" % (ma, a)) |
| 52 |
if mn == n and ma == a: |
| 53 |
pass |
| 54 |
else: |
| 55 |
print 'not found:', repr(n), repr(a) |
| 56 |
|
| 57 |
out = m.getdate('date') |
| 58 |
if out: |
| 59 |
self.assertEqual(out, |
| 60 |
(1999, 1, 13, 23, 57, 35, 0, 1, 0), |
| 61 |
"date conversion failed") |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
def test_basic(self): |
| 68 |
self.check( |
| 69 |
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' |
| 70 |
'From: Guido van Rossum <[email protected]>\n' |
| 71 |
'To: "Guido van\n' |
| 72 |
'\t : Rossum" <[email protected]>\n' |
| 73 |
'Subject: test2\n' |
| 74 |
'\n' |
| 75 |
'test2\n', |
| 76 |
[('Guido van\n\t : Rossum', '[email protected]')]) |
| 77 |
|
| 78 |
self.check( |
| 79 |
'From: Barry <[email protected]\n' |
| 80 |
'To: [email protected] (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', '[email protected]')]) |
| 86 |
|
| 87 |
self.check( |
| 88 |
'From: Barry <[email protected]\n' |
| 89 |
'To: [email protected] (Guido: the Barbarian)\n' |
| 90 |
'Cc: "Guido: the Madman" <[email protected]>\n' |
| 91 |
'Date: 13-Jan-1999 23:57:35 EST\n' |
| 92 |
'\n' |
| 93 |
'test', |
| 94 |
[('Guido: the Barbarian', '[email protected]'), |
| 95 |
('Guido: the Madman', '[email protected]') |
| 96 |
]) |
| 97 |
|
| 98 |
self.check( |
| 99 |
'To: "The monster with\n' |
| 100 |
' the very long name: Guido" <[email protected]>\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 |
'[email protected]')]) |
| 106 |
|
| 107 |
self.check( |
| 108 |
'To: "Amit J. Patel" <[email protected]>\n' |
| 109 |
'CC: Mike Fletcher <[email protected]>,\n' |
| 110 |
' "\'[email protected]\'" <[email protected]>\n' |
| 111 |
'Cc: [email protected], [email protected]\n' |
| 112 |
'Cc: [email protected]\n' |
| 113 |
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' |
| 114 |
'\n' |
| 115 |
'test', |
| 116 |
[('Amit J. Patel', '[email protected]'), |
| 117 |
('Mike Fletcher', '[email protected]'), |
| 118 |
("'[email protected]'", '[email protected]'), |
| 119 |
('', '[email protected]'), |
| 120 |
('', '[email protected]'), |
| 121 |
('', '[email protected]'), |
| 122 |
]) |
| 123 |
|
| 124 |
self.check( |
| 125 |
'To: Some One <[email protected]>\n' |
| 126 |
'From: Anudder Persin <[email protected]>\n' |
| 127 |
'Date:\n' |
| 128 |
'\n' |
| 129 |
'test', |
| 130 |
[('Some One', '[email protected]')]) |
| 131 |
|
| 132 |
self.check( |
| 133 |
'To: [email protected] (User J. Person)\n\n', |
| 134 |
[('User J. Person', '[email protected]')]) |
| 135 |
|
| 136 |
def test_doublecomment(self): |
| 137 |
|
| 138 |
self.check( |
| 139 |
'To: [email protected] ((User J. Person)), John Doe <[email protected]>\n\n', |
| 140 |
[('User J. Person', '[email protected]'), ('John Doe', '[email protected]')]) |
| 141 |
|
| 142 |
def test_twisted(self): |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
self.check( |
| 147 |
'To: <[smtp:[email protected]][email protected]>\n' |
| 148 |
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' |
| 149 |
'\n' |
| 150 |
'test', |
| 151 |
[('', ''), |
| 152 |
('', '[email protected]'), |
| 153 |
('', '[email protected]'), |
| 154 |
]) |
| 155 |
|
| 156 |
def test_commas_in_full_name(self): |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
self.check( |
| 161 |
'To: "last, first" <[email protected]>\n' |
| 162 |
'\n' |
| 163 |
'test', |
| 164 |
[('last, first', '[email protected]')]) |
| 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: [email protected]\n' |
| 177 |
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' |
| 178 |
'\n' |
| 179 |
'test', |
| 180 |
[('', '[email protected]')]) |
| 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]')]) |
| 188 |
|
| 189 |
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 <[email protected]>\n' |
| 193 |
'To: "Guido van\n' |
| 194 |
'\t : Rossum" <[email protected]>\n' |
| 195 |
'Subject: test2\n' |
| 196 |
'\n' |
| 197 |
'test2\n' )) |
| 198 |
self.assertEqual(sorted(m), ['date', 'from', 'subject', 'to']) |
| 199 |
|
| 200 |
def test_rfc2822_phrases(self): |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
self.check('To: User J. Person <[email protected]>\n\n', |
| 207 |
[('User J. Person', '[email protected]')]) |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
def test_2getaddrlist(self): |
| 218 |
eq = self.assertEqual |
| 219 |
msg = self.create_message("""\ |
| 220 |
To: [email protected] |
| 221 |
Cc: [email protected] |
| 222 |
Cc: [email protected] |
| 223 |
Cc: [email protected] |
| 224 |
|
| 225 |
A test message. |
| 226 |
""") |
| 227 |
ccs = [('', a) for a in |
| 228 |
['[email protected]', '[email protected]', '[email protected]']] |
| 229 |
addrs = msg.getaddrlist('cc') |
| 230 |
addrs.sort() |
| 231 |
eq(addrs, ccs) |
| 232 |
|
| 233 |
addrs = msg.getaddrlist('cc') |
| 234 |
addrs.sort() |
| 235 |
eq(addrs, ccs) |
| 236 |
|
| 237 |
def test_parseaddr(self): |
| 238 |
eq = self.assertEqual |
| 239 |
eq(rfc822.parseaddr('<>'), ('', '')) |
| 240 |
eq(rfc822.parseaddr('[email protected]'), ('', '[email protected]')) |
| 241 |
eq(rfc822.parseaddr('[email protected] (Bea A. Person)'), |
| 242 |
('Bea A. Person', '[email protected]')) |
| 243 |
eq(rfc822.parseaddr('Cynthia Person <[email protected]>'), |
| 244 |
('Cynthia Person', '[email protected]')) |
| 245 |
|
| 246 |
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 |
def test_invalid_headers(self): |
| 252 |
eq = self.assertEqual |
| 253 |
msg = self.create_message("First: val\n: otherval\nSecond: val2\n") |
| 254 |
eq(msg.getheader('First'), 'val') |
| 255 |
eq(msg.getheader('Second'), 'val2') |
| 256 |
|
| 257 |
|
| 258 |
def test_main(): |
| 259 |
test_support.run_unittest(MessageTestCase) |
| 260 |
|
| 261 |
|
| 262 |
if __name__ == "__main__": |
| 263 |
test_main() |