0001 import rfc822 0002 import sys 0003 import unittest 0004 from test import test_support 0005 0006 try: 0007 from cStringIO import StringIO 0008 except ImportError: 0009 from StringIO import StringIO 0010 0011 0012 class MessageTestCase(unittest.TestCase): 0013 def create_message(self, msg): 0014 return rfc822.Message(StringIO(msg)) 0015 0016 def test_get(self): 0017 msg = self.create_message( 0018 'To: "last, first" <userid@foo.net>\n\ntest\n') 0019 self.assert_(msg.get("to") == '"last, first" <userid@foo.net>') 0020 self.assert_(msg.get("TO") == '"last, first" <userid@foo.net>') 0021 self.assert_(msg.get("No-Such-Header") is None) 0022 self.assert_(msg.get("No-Such-Header", "No-Such-Value") 0023 == "No-Such-Value") 0024 0025 def test_setdefault(self): 0026 msg = self.create_message( 0027 'To: "last, first" <userid@foo.net>\n\ntest\n') 0028 self.assert_(not msg.has_key("New-Header")) 0029 self.assert_(msg.setdefault("New-Header", "New-Value") == "New-Value") 0030 self.assert_(msg.setdefault("New-Header", "Different-Value") 0031 == "New-Value") 0032 self.assert_(msg["new-header"] == "New-Value") 0033 0034 self.assert_(msg.setdefault("Another-Header") == "") 0035 self.assert_(msg["another-header"] == "") 0036 0037 def check(self, msg, results): 0038 """Check addresses and the date.""" 0039 m = self.create_message(msg) 0040 i = 0 0041 for n, a in m.getaddrlist('to') + m.getaddrlist('cc'): 0042 try: 0043 mn, ma = results[i][0], results[i][1] 0044 except IndexError: 0045 print 'extra parsed address:', repr(n), repr(a) 0046 continue 0047 i = i + 1 0048 if mn == n and ma == a: 0049 pass 0050 else: 0051 print 'not found:', repr(n), repr(a) 0052 0053 out = m.getdate('date') 0054 if out: 0055 self.assertEqual(out, 0056 (1999, 1, 13, 23, 57, 35, 0, 1, 0), 0057 "date conversion failed") 0058 0059 0060 # Note: all test cases must have the same date (in various formats), 0061 # or no date! 0062 0063 def test_basic(self): 0064 self.check( 0065 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 0066 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n' 0067 'To: "Guido van\n' 0068 '\t : Rossum" <guido@python.org>\n' 0069 'Subject: test2\n' 0070 '\n' 0071 'test2\n', 0072 [('Guido van\n\t : Rossum', 'guido@python.org')]) 0073 0074 self.check( 0075 'From: Barry <bwarsaw@python.org\n' 0076 'To: guido@python.org (Guido: the Barbarian)\n' 0077 'Subject: nonsense\n' 0078 'Date: Wednesday, January 13 1999 23:57:35 -0500\n' 0079 '\n' 0080 'test', 0081 [('Guido: the Barbarian', 'guido@python.org')]) 0082 0083 self.check( 0084 'From: Barry <bwarsaw@python.org\n' 0085 'To: guido@python.org (Guido: the Barbarian)\n' 0086 'Cc: "Guido: the Madman" <guido@python.org>\n' 0087 'Date: 13-Jan-1999 23:57:35 EST\n' 0088 '\n' 0089 'test', 0090 [('Guido: the Barbarian', 'guido@python.org'), 0091 ('Guido: the Madman', 'guido@python.org') 0092 ]) 0093 0094 self.check( 0095 'To: "The monster with\n' 0096 ' the very long name: Guido" <guido@python.org>\n' 0097 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 0098 '\n' 0099 'test', 0100 [('The monster with\n the very long name: Guido', 0101 'guido@python.org')]) 0102 0103 self.check( 0104 'To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>\n' 0105 'CC: Mike Fletcher <mfletch@vrtelecom.com>,\n' 0106 ' "\'string-sig@python.org\'" <string-sig@python.org>\n' 0107 'Cc: fooz@bat.com, bart@toof.com\n' 0108 'Cc: goit@lip.com\n' 0109 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 0110 '\n' 0111 'test', 0112 [('Amit J. Patel', 'amitp@Theory.Stanford.EDU'), 0113 ('Mike Fletcher', 'mfletch@vrtelecom.com'), 0114 ("'string-sig@python.org'", 'string-sig@python.org'), 0115 ('', 'fooz@bat.com'), 0116 ('', 'bart@toof.com'), 0117 ('', 'goit@lip.com'), 0118 ]) 0119 0120 self.check( 0121 'To: Some One <someone@dom.ain>\n' 0122 'From: Anudder Persin <subuddy.else@dom.ain>\n' 0123 'Date:\n' 0124 '\n' 0125 'test', 0126 [('Some One', 'someone@dom.ain')]) 0127 0128 self.check( 0129 'To: person@dom.ain (User J. Person)\n\n', 0130 [('User J. Person', 'person@dom.ain')]) 0131 0132 def test_twisted(self): 0133 # This one is just twisted. I don't know what the proper 0134 # result should be, but it shouldn't be to infloop, which is 0135 # what used to happen! 0136 self.check( 0137 'To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>\n' 0138 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 0139 '\n' 0140 'test', 0141 [('', ''), 0142 ('', 'dd47@mail.xxx.edu'), 0143 ('', '_at_hmhq@hdq-mdm1-imgout.companay.com'), 0144 ]) 0145 0146 def test_commas_in_full_name(self): 0147 # This exercises the old commas-in-a-full-name bug, which 0148 # should be doing the right thing in recent versions of the 0149 # module. 0150 self.check( 0151 'To: "last, first" <userid@foo.net>\n' 0152 '\n' 0153 'test', 0154 [('last, first', 'userid@foo.net')]) 0155 0156 def test_quoted_name(self): 0157 self.check( 0158 'To: (Comment stuff) "Quoted name"@somewhere.com\n' 0159 '\n' 0160 'test', 0161 [('Comment stuff', '"Quoted name"@somewhere.com')]) 0162 0163 def test_bogus_to_header(self): 0164 self.check( 0165 'To: :\n' 0166 'Cc: goit@lip.com\n' 0167 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 0168 '\n' 0169 'test', 0170 [('', 'goit@lip.com')]) 0171 0172 def test_addr_ipquad(self): 0173 self.check( 0174 'To: guido@[132.151.1.21]\n' 0175 '\n' 0176 'foo', 0177 [('', 'guido@[132.151.1.21]')]) 0178 0179 def test_iter(self): 0180 m = rfc822.Message(StringIO( 0181 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 0182 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n' 0183 'To: "Guido van\n' 0184 '\t : Rossum" <guido@python.org>\n' 0185 'Subject: test2\n' 0186 '\n' 0187 'test2\n' )) 0188 self.assertEqual(sorted(m), ['date', 'from', 'subject', 'to']) 0189 0190 def test_rfc2822_phrases(self): 0191 # RFC 2822 (the update to RFC 822) specifies that dots in phrases are 0192 # obsolete syntax, which conforming programs MUST recognize but NEVER 0193 # generate (see $4.1 Miscellaneous obsolete tokens). This is a 0194 # departure from RFC 822 which did not allow dots in non-quoted 0195 # phrases. 0196 self.check('To: User J. Person <person@dom.ain>\n\n', 0197 [('User J. Person', 'person@dom.ain')]) 0198 0199 # This takes too long to add to the test suite 0200 ## def test_an_excrutiatingly_long_address_field(self): 0201 ## OBSCENELY_LONG_HEADER_MULTIPLIER = 10000 0202 ## oneaddr = ('Person' * 10) + '@' + ('.'.join(['dom']*10)) + '.com' 0203 ## addr = ', '.join([oneaddr] * OBSCENELY_LONG_HEADER_MULTIPLIER) 0204 ## lst = rfc822.AddrlistClass(addr).getaddrlist() 0205 ## self.assertEqual(len(lst), OBSCENELY_LONG_HEADER_MULTIPLIER) 0206 0207 def test_2getaddrlist(self): 0208 eq = self.assertEqual 0209 msg = self.create_message("""\ 0210 To: aperson@dom.ain 0211 Cc: bperson@dom.ain 0212 Cc: cperson@dom.ain 0213 Cc: dperson@dom.ain 0214 0215 A test message. 0216 """) 0217 ccs = [('', a) for a in 0218 ['bperson@dom.ain', 'cperson@dom.ain', 'dperson@dom.ain']] 0219 addrs = msg.getaddrlist('cc') 0220 addrs.sort() 0221 eq(addrs, ccs) 0222 # Try again, this one used to fail 0223 addrs = msg.getaddrlist('cc') 0224 addrs.sort() 0225 eq(addrs, ccs) 0226 0227 def test_parseaddr(self): 0228 eq = self.assertEqual 0229 eq(rfc822.parseaddr('<>'), ('', '')) 0230 eq(rfc822.parseaddr('aperson@dom.ain'), ('', 'aperson@dom.ain')) 0231 eq(rfc822.parseaddr('bperson@dom.ain (Bea A. Person)'), 0232 ('Bea A. Person', 'bperson@dom.ain')) 0233 eq(rfc822.parseaddr('Cynthia Person <cperson@dom.ain>'), 0234 ('Cynthia Person', 'cperson@dom.ain')) 0235 0236 def test_quote_unquote(self): 0237 eq = self.assertEqual 0238 eq(rfc822.quote('foo\\wacky"name'), 'foo\\\\wacky\\"name') 0239 eq(rfc822.unquote('"foo\\\\wacky\\"name"'), 'foo\\wacky"name') 0240 0241 0242 def test_main(): 0243 test_support.run_unittest(MessageTestCase) 0244 0245 0246 if __name__ == "__main__": 0247 test_main() 0248
Generated by PyXR 0.9.4