test_rfc822.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import unittest
  2. from test import test_support
  3. rfc822 = test_support.import_module("rfc822", deprecated=True)
  4. try:
  5. from cStringIO import StringIO
  6. except ImportError:
  7. from StringIO import StringIO
  8. class MessageTestCase(unittest.TestCase):
  9. def create_message(self, msg):
  10. return rfc822.Message(StringIO(msg))
  11. def test_get(self):
  12. msg = self.create_message(
  13. 'To: "last, first" <userid@foo.net>\n\ntest\n')
  14. self.assertTrue(msg.get("to") == '"last, first" <userid@foo.net>')
  15. self.assertTrue(msg.get("TO") == '"last, first" <userid@foo.net>')
  16. self.assertTrue(msg.get("No-Such-Header") is None)
  17. self.assertTrue(msg.get("No-Such-Header", "No-Such-Value")
  18. == "No-Such-Value")
  19. def test_setdefault(self):
  20. msg = self.create_message(
  21. 'To: "last, first" <userid@foo.net>\n\ntest\n')
  22. self.assertTrue(not msg.has_key("New-Header"))
  23. self.assertTrue(msg.setdefault("New-Header", "New-Value") == "New-Value")
  24. self.assertTrue(msg.setdefault("New-Header", "Different-Value")
  25. == "New-Value")
  26. self.assertTrue(msg["new-header"] == "New-Value")
  27. self.assertTrue(msg.setdefault("Another-Header") == "")
  28. self.assertTrue(msg["another-header"] == "")
  29. def check(self, msg, results):
  30. """Check addresses and the date."""
  31. m = self.create_message(msg)
  32. i = 0
  33. for n, a in m.getaddrlist('to') + m.getaddrlist('cc'):
  34. try:
  35. mn, ma = results[i][0], results[i][1]
  36. except IndexError:
  37. print 'extra parsed address:', repr(n), repr(a)
  38. continue
  39. i = i + 1
  40. self.assertEqual(mn, n,
  41. "Un-expected name: %r != %r" % (mn, n))
  42. self.assertEqual(ma, a,
  43. "Un-expected address: %r != %r" % (ma, a))
  44. if mn == n and ma == a:
  45. pass
  46. else:
  47. print 'not found:', repr(n), repr(a)
  48. out = m.getdate('date')
  49. if out:
  50. self.assertEqual(out,
  51. (1999, 1, 13, 23, 57, 35, 0, 1, 0),
  52. "date conversion failed")
  53. # Note: all test cases must have the same date (in various formats),
  54. # or no date!
  55. def test_basic(self):
  56. self.check(
  57. 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
  58. 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
  59. 'To: "Guido van\n'
  60. '\t : Rossum" <guido@python.org>\n'
  61. 'Subject: test2\n'
  62. '\n'
  63. 'test2\n',
  64. [('Guido van\n\t : Rossum', 'guido@python.org')])
  65. self.check(
  66. 'From: Barry <bwarsaw@python.org\n'
  67. 'To: guido@python.org (Guido: the Barbarian)\n'
  68. 'Subject: nonsense\n'
  69. 'Date: Wednesday, January 13 1999 23:57:35 -0500\n'
  70. '\n'
  71. 'test',
  72. [('Guido: the Barbarian', 'guido@python.org')])
  73. self.check(
  74. 'From: Barry <bwarsaw@python.org\n'
  75. 'To: guido@python.org (Guido: the Barbarian)\n'
  76. 'Cc: "Guido: the Madman" <guido@python.org>\n'
  77. 'Date: 13-Jan-1999 23:57:35 EST\n'
  78. '\n'
  79. 'test',
  80. [('Guido: the Barbarian', 'guido@python.org'),
  81. ('Guido: the Madman', 'guido@python.org')
  82. ])
  83. self.check(
  84. 'To: "The monster with\n'
  85. ' the very long name: Guido" <guido@python.org>\n'
  86. 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
  87. '\n'
  88. 'test',
  89. [('The monster with\n the very long name: Guido',
  90. 'guido@python.org')])
  91. self.check(
  92. 'To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>\n'
  93. 'CC: Mike Fletcher <mfletch@vrtelecom.com>,\n'
  94. ' "\'string-sig@python.org\'" <string-sig@python.org>\n'
  95. 'Cc: fooz@bat.com, bart@toof.com\n'
  96. 'Cc: goit@lip.com\n'
  97. 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
  98. '\n'
  99. 'test',
  100. [('Amit J. Patel', 'amitp@Theory.Stanford.EDU'),
  101. ('Mike Fletcher', 'mfletch@vrtelecom.com'),
  102. ("'string-sig@python.org'", 'string-sig@python.org'),
  103. ('', 'fooz@bat.com'),
  104. ('', 'bart@toof.com'),
  105. ('', 'goit@lip.com'),
  106. ])
  107. self.check(
  108. 'To: Some One <someone@dom.ain>\n'
  109. 'From: Anudder Persin <subuddy.else@dom.ain>\n'
  110. 'Date:\n'
  111. '\n'
  112. 'test',
  113. [('Some One', 'someone@dom.ain')])
  114. self.check(
  115. 'To: person@dom.ain (User J. Person)\n\n',
  116. [('User J. Person', 'person@dom.ain')])
  117. def test_doublecomment(self):
  118. # The RFC allows comments within comments in an email addr
  119. self.check(
  120. 'To: person@dom.ain ((User J. Person)), John Doe <foo@bar.com>\n\n',
  121. [('User J. Person', 'person@dom.ain'), ('John Doe', 'foo@bar.com')])
  122. def test_twisted(self):
  123. # This one is just twisted. I don't know what the proper
  124. # result should be, but it shouldn't be to infloop, which is
  125. # what used to happen!
  126. self.check(
  127. 'To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>\n'
  128. 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
  129. '\n'
  130. 'test',
  131. [('', ''),
  132. ('', 'dd47@mail.xxx.edu'),
  133. ('', '_at_hmhq@hdq-mdm1-imgout.companay.com'),
  134. ])
  135. def test_commas_in_full_name(self):
  136. # This exercises the old commas-in-a-full-name bug, which
  137. # should be doing the right thing in recent versions of the
  138. # module.
  139. self.check(
  140. 'To: "last, first" <userid@foo.net>\n'
  141. '\n'
  142. 'test',
  143. [('last, first', 'userid@foo.net')])
  144. def test_quoted_name(self):
  145. self.check(
  146. 'To: (Comment stuff) "Quoted name"@somewhere.com\n'
  147. '\n'
  148. 'test',
  149. [('Comment stuff', '"Quoted name"@somewhere.com')])
  150. def test_bogus_to_header(self):
  151. self.check(
  152. 'To: :\n'
  153. 'Cc: goit@lip.com\n'
  154. 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
  155. '\n'
  156. 'test',
  157. [('', 'goit@lip.com')])
  158. def test_addr_ipquad(self):
  159. self.check(
  160. 'To: guido@[132.151.1.21]\n'
  161. '\n'
  162. 'foo',
  163. [('', 'guido@[132.151.1.21]')])
  164. def test_iter(self):
  165. m = rfc822.Message(StringIO(
  166. 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
  167. 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
  168. 'To: "Guido van\n'
  169. '\t : Rossum" <guido@python.org>\n'
  170. 'Subject: test2\n'
  171. '\n'
  172. 'test2\n' ))
  173. self.assertEqual(sorted(m), ['date', 'from', 'subject', 'to'])
  174. def test_rfc2822_phrases(self):
  175. # RFC 2822 (the update to RFC 822) specifies that dots in phrases are
  176. # obsolete syntax, which conforming programs MUST recognize but NEVER
  177. # generate (see $4.1 Miscellaneous obsolete tokens). This is a
  178. # departure from RFC 822 which did not allow dots in non-quoted
  179. # phrases.
  180. self.check('To: User J. Person <person@dom.ain>\n\n',
  181. [('User J. Person', 'person@dom.ain')])
  182. # This takes too long to add to the test suite
  183. ## def test_an_excrutiatingly_long_address_field(self):
  184. ## OBSCENELY_LONG_HEADER_MULTIPLIER = 10000
  185. ## oneaddr = ('Person' * 10) + '@' + ('.'.join(['dom']*10)) + '.com'
  186. ## addr = ', '.join([oneaddr] * OBSCENELY_LONG_HEADER_MULTIPLIER)
  187. ## lst = rfc822.AddrlistClass(addr).getaddrlist()
  188. ## self.assertEqual(len(lst), OBSCENELY_LONG_HEADER_MULTIPLIER)
  189. def test_2getaddrlist(self):
  190. eq = self.assertEqual
  191. msg = self.create_message("""\
  192. To: aperson@dom.ain
  193. Cc: bperson@dom.ain
  194. Cc: cperson@dom.ain
  195. Cc: dperson@dom.ain
  196. A test message.
  197. """)
  198. ccs = [('', a) for a in
  199. ['bperson@dom.ain', 'cperson@dom.ain', 'dperson@dom.ain']]
  200. addrs = msg.getaddrlist('cc')
  201. addrs.sort()
  202. eq(addrs, ccs)
  203. # Try again, this one used to fail
  204. addrs = msg.getaddrlist('cc')
  205. addrs.sort()
  206. eq(addrs, ccs)
  207. def test_parseaddr(self):
  208. eq = self.assertEqual
  209. eq(rfc822.parseaddr('<>'), ('', ''))
  210. eq(rfc822.parseaddr('aperson@dom.ain'), ('', 'aperson@dom.ain'))
  211. eq(rfc822.parseaddr('bperson@dom.ain (Bea A. Person)'),
  212. ('Bea A. Person', 'bperson@dom.ain'))
  213. eq(rfc822.parseaddr('Cynthia Person <cperson@dom.ain>'),
  214. ('Cynthia Person', 'cperson@dom.ain'))
  215. def test_quote_unquote(self):
  216. eq = self.assertEqual
  217. eq(rfc822.quote('foo\\wacky"name'), 'foo\\\\wacky\\"name')
  218. eq(rfc822.unquote('"foo\\\\wacky\\"name"'), 'foo\\wacky"name')
  219. def test_invalid_headers(self):
  220. eq = self.assertEqual
  221. msg = self.create_message("First: val\n: otherval\nSecond: val2\n")
  222. eq(msg.getheader('First'), 'val')
  223. eq(msg.getheader('Second'), 'val2')
  224. def test_main():
  225. test_support.run_unittest(MessageTestCase)
  226. if __name__ == "__main__":
  227. test_main()