PageRenderTime 74ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/org.modelsphere.sms/lib/jython-2.2.1/Lib/test/test_rfc822.py

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