Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 1 | import rfc822 |
| 2 | import sys |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 3 | import unittest |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 4 | from test import test_support |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 5 | |
Barry Warsaw | e75888e | 1999-01-14 20:00:58 +0000 | [diff] [blame] | 6 | try: |
| 7 | from cStringIO import StringIO |
| 8 | except ImportError: |
| 9 | from StringIO import StringIO |
| 10 | |
Fred Drake | 098b55a | 2000-10-23 17:30:23 +0000 | [diff] [blame] | 11 | |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 12 | class MessageTestCase(unittest.TestCase): |
| 13 | def create_message(self, msg): |
| 14 | return rfc822.Message(StringIO(msg)) |
Barry Warsaw | e75888e | 1999-01-14 20:00:58 +0000 | [diff] [blame] | 15 | |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 16 | 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 Rossum | d842e07 | 2002-06-05 19:07:39 +0000 | [diff] [blame] | 21 | self.assert_(msg.get("No-Such-Header") is None) |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 22 | self.assert_(msg.get("No-Such-Header", "No-Such-Value") |
| 23 | == "No-Such-Value") |
Guido van Rossum | 3ed1be9 | 1999-05-03 19:57:01 +0000 | [diff] [blame] | 24 | |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 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") |
| 31 | == "New-Value") |
| 32 | self.assert_(msg["new-header"] == "New-Value") |
Guido van Rossum | 3ed1be9 | 1999-05-03 19:57:01 +0000 | [diff] [blame] | 33 | |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 34 | self.assert_(msg.setdefault("Another-Header") == "") |
| 35 | self.assert_(msg["another-header"] == "") |
Barry Warsaw | e75888e | 1999-01-14 20:00:58 +0000 | [diff] [blame] | 36 | |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 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 | if mn == n and ma == a: |
| 49 | pass |
| 50 | else: |
| 51 | print 'not found:', repr(n), repr(a) |
Barry Warsaw | e75888e | 1999-01-14 20:00:58 +0000 | [diff] [blame] | 52 | |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 53 | 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 Warsaw | 7c5b9d1 | 1999-07-12 18:47:00 +0000 | [diff] [blame] | 58 | |
Barry Warsaw | 38bfc4d | 2000-09-25 15:09:28 +0000 | [diff] [blame] | 59 | |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 60 | # Note: all test cases must have the same date (in various formats), |
| 61 | # or no date! |
Barry Warsaw | 38bfc4d | 2000-09-25 15:09:28 +0000 | [diff] [blame] | 62 | |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 63 | 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 Warsaw | 0606933 | 2001-07-16 20:44:16 +0000 | [diff] [blame] | 120 | self.check( |
Barry Warsaw | 19c10ca | 2001-11-13 18:01:37 +0000 | [diff] [blame] | 121 | 'To: Some One <someone@dom.ain>\n' |
| 122 | 'From: Anudder Persin <subuddy.else@dom.ain>\n' |
| 123 | 'Date:\n' |
| 124 | '\n' |
| 125 | 'test', |
| 126 | [('Some One', 'someone@dom.ain')]) |
| 127 | |
| 128 | self.check( |
Barry Warsaw | 0606933 | 2001-07-16 20:44:16 +0000 | [diff] [blame] | 129 | 'To: person@dom.ain (User J. Person)\n\n', |
| 130 | [('User J. Person', 'person@dom.ain')]) |
| 131 | |
Fred Drake | 876dc70 | 2001-05-22 19:38:31 +0000 | [diff] [blame] | 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! |
| 136 | self.check( |
| 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' |
| 139 | '\n' |
| 140 | 'test', |
| 141 | [('', ''), |
| 142 | ('', 'dd47@mail.xxx.edu'), |
| 143 | ('', '_at_hmhq@hdq-mdm1-imgout.companay.com'), |
| 144 | ]) |
| 145 | |
| 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 |
| 149 | # module. |
| 150 | self.check( |
| 151 | 'To: "last, first" <userid@foo.net>\n' |
| 152 | '\n' |
| 153 | 'test', |
| 154 | [('last, first', 'userid@foo.net')]) |
| 155 | |
| 156 | def test_quoted_name(self): |
| 157 | self.check( |
| 158 | 'To: (Comment stuff) "Quoted name"@somewhere.com\n' |
| 159 | '\n' |
| 160 | 'test', |
| 161 | [('Comment stuff', '"Quoted name"@somewhere.com')]) |
| 162 | |
| 163 | def test_bogus_to_header(self): |
| 164 | self.check( |
| 165 | 'To: :\n' |
| 166 | 'Cc: goit@lip.com\n' |
| 167 | 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' |
| 168 | '\n' |
| 169 | 'test', |
| 170 | [('', 'goit@lip.com')]) |
| 171 | |
| 172 | def test_addr_ipquad(self): |
| 173 | self.check( |
| 174 | 'To: guido@[132.151.1.21]\n' |
| 175 | '\n' |
| 176 | 'foo', |
| 177 | [('', 'guido@[132.151.1.21]')]) |
Fred Drake | cf71fef | 2001-05-22 15:02:19 +0000 | [diff] [blame] | 178 | |
Barry Warsaw | 0606933 | 2001-07-16 20:44:16 +0000 | [diff] [blame] | 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 |
| 184 | # phrases. |
| 185 | self.check('To: User J. Person <person@dom.ain>\n\n', |
| 186 | [('User J. Person', 'person@dom.ain')]) |
Fred Drake | cf71fef | 2001-05-22 15:02:19 +0000 | [diff] [blame] | 187 | |
Barry Warsaw | 0a8d4d5 | 2002-05-21 19:46:13 +0000 | [diff] [blame] | 188 | # This takes too long to add to the test suite |
Barry Warsaw | 135cce8 | 2001-11-13 21:33:52 +0000 | [diff] [blame] | 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) |
| 195 | |
Barry Warsaw | 0a8d4d5 | 2002-05-21 19:46:13 +0000 | [diff] [blame] | 196 | def test_2getaddrlist(self): |
| 197 | eq = self.assertEqual |
| 198 | msg = self.create_message("""\ |
| 199 | To: aperson@dom.ain |
| 200 | Cc: bperson@dom.ain |
| 201 | Cc: cperson@dom.ain |
| 202 | Cc: dperson@dom.ain |
| 203 | |
| 204 | A test message. |
| 205 | """) |
| 206 | ccs = [('', a) for a in |
| 207 | ['bperson@dom.ain', 'cperson@dom.ain', 'dperson@dom.ain']] |
| 208 | addrs = msg.getaddrlist('cc') |
| 209 | addrs.sort() |
| 210 | eq(addrs, ccs) |
| 211 | # Try again, this one used to fail |
| 212 | addrs = msg.getaddrlist('cc') |
| 213 | addrs.sort() |
| 214 | eq(addrs, ccs) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 215 | |
Barry Warsaw | f655328 | 2002-05-23 03:21:01 +0000 | [diff] [blame] | 216 | def test_parseaddr(self): |
| 217 | eq = self.assertEqual |
| 218 | eq(rfc822.parseaddr('<>'), ('', '')) |
| 219 | eq(rfc822.parseaddr('aperson@dom.ain'), ('', 'aperson@dom.ain')) |
| 220 | eq(rfc822.parseaddr('bperson@dom.ain (Bea A. Person)'), |
| 221 | ('Bea A. Person', 'bperson@dom.ain')) |
| 222 | eq(rfc822.parseaddr('Cynthia Person <cperson@dom.ain>'), |
| 223 | ('Cynthia Person', 'cperson@dom.ain')) |
| 224 | |
Barry Warsaw | 1a5b956 | 2002-09-11 02:32:57 +0000 | [diff] [blame] | 225 | def test_quote_unquote(self): |
| 226 | eq = self.assertEqual |
| 227 | eq(rfc822.quote('foo\\wacky"name'), 'foo\\\\wacky\\"name') |
| 228 | eq(rfc822.unquote('"foo\\\\wacky\\"name"'), 'foo\\wacky"name') |
| 229 | |
| 230 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 231 | def test_main(): |
| 232 | test_support.run_unittest(MessageTestCase) |
| 233 | |
| 234 | |
| 235 | if __name__ == "__main__": |
| 236 | test_main() |