home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / TEST_RFC822.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  3.7 KB  |  127 lines

  1. from test_support import verbose
  2. import rfc822, sys
  3. try:
  4.     from cStringIO import StringIO
  5. except ImportError:
  6.     from StringIO import StringIO
  7.  
  8. def test(msg, results):
  9.     fp = StringIO()
  10.     fp.write(msg)
  11.     fp.seek(0)
  12.     m = rfc822.Message(fp)
  13.     i = 0
  14.     
  15.     for n, a in m.getaddrlist('to') + m.getaddrlist('cc'):
  16.         if verbose:
  17.             print 'name:', repr(n), 'addr:', repr(a)
  18.         try:
  19.             mn, ma = results[i][0], results[i][1]
  20.         except IndexError:
  21.             print 'extra parsed address:', repr(n), repr(a)
  22.             continue
  23.         i = i + 1
  24.         if mn == n and ma == a:
  25.             if verbose:
  26.                 print '    [matched]'
  27.         else:
  28.             if verbose:
  29.                 print '    [no match]'
  30.             print 'not found:', repr(n), repr(a)
  31.  
  32.     out = m.getdate('date')
  33.     if out:
  34.         if verbose:
  35.             print 'Date:', m.getheader('date')
  36.         if out == (1999, 1, 13, 23, 57, 35, 0, 0, 0):
  37.             if verbose:
  38.                 print '    [matched]'
  39.         else:
  40.             if verbose:
  41.                 print '    [no match]'
  42.             print 'Date conversion failed:', out
  43.  
  44. # Note: all test cases must have the same date (in various formats),
  45. # or no date!
  46.  
  47. test('''Date:    Wed, 13 Jan 1999 23:57:35 -0500
  48. From:    Guido van Rossum <guido@CNRI.Reston.VA.US>
  49. To:      "Guido van
  50.      : Rossum" <guido@python.org>
  51. Subject: test2
  52.  
  53. test2
  54. ''', [('Guido van\n     : Rossum', 'guido@python.org')])
  55.  
  56. test('''From: Barry <bwarsaw@python.org
  57. To: guido@python.org (Guido: the Barbarian)
  58. Subject: nonsense
  59. Date: Wednesday, January 13 1999 23:57:35 -0500
  60.  
  61. test''', [('Guido: the Barbarian', 'guido@python.org'),
  62.           ])
  63.  
  64. test('''From: Barry <bwarsaw@python.org
  65. To: guido@python.org (Guido: the Barbarian)
  66. Cc: "Guido: the Madman" <guido@python.org>
  67. Date:  13-Jan-1999 23:57:35 EST
  68.  
  69. test''', [('Guido: the Barbarian', 'guido@python.org'),
  70.           ('Guido: the Madman', 'guido@python.org')
  71.           ])
  72.  
  73. test('''To: "The monster with
  74.      the very long name: Guido" <guido@python.org>
  75. Date:    Wed, 13 Jan 1999 23:57:35 -0500
  76.  
  77. test''', [('The monster with\n     the very long name: Guido',
  78.            'guido@python.org')])
  79.  
  80. test('''To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>
  81. CC: Mike Fletcher <mfletch@vrtelecom.com>,
  82.         "'string-sig@python.org'" <string-sig@python.org>
  83. Cc: fooz@bat.com, bart@toof.com
  84. Cc: goit@lip.com
  85. Date:    Wed, 13 Jan 1999 23:57:35 -0500
  86.  
  87. test''', [('Amit J. Patel', 'amitp@Theory.Stanford.EDU'),
  88.           ('Mike Fletcher', 'mfletch@vrtelecom.com'),
  89.           ("'string-sig@python.org'", 'string-sig@python.org'),
  90.           ('', 'fooz@bat.com'),
  91.           ('', 'bart@toof.com'),
  92.           ('', 'goit@lip.com'),
  93.           ])
  94.  
  95. # This one is just twisted.  I don't know what the proper result should be,
  96. # but it shouldn't be to infloop, which is what used to happen!
  97. test('''To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>
  98. Date:    Wed, 13 Jan 1999 23:57:35 -0500
  99.  
  100. test''', [('', ''),
  101.           ('', 'dd47@mail.xxx.edu'),
  102.           ('', '_at_hmhq@hdq-mdm1-imgout.companay.com')
  103.           ])
  104.  
  105. # This exercises the old commas-in-a-full-name bug, which should be doing the
  106. # right thing in recent versions of the module.
  107. test('''To: "last, first" <userid@foo.net>
  108.  
  109. test''', [('last, first', 'userid@foo.net'),
  110.           ])
  111.  
  112. test('''To: (Comment stuff) "Quoted name"@somewhere.com
  113.  
  114. test''', [('Comment stuff', '"Quoted name"@somewhere.com'),
  115.           ])
  116.  
  117. test('''To: :
  118. Cc: goit@lip.com
  119. Date:    Wed, 13 Jan 1999 23:57:35 -0500
  120.  
  121. test''', [('', 'goit@lip.com')])
  122.  
  123.  
  124. test('''To: guido@[132.151.1.21]
  125.  
  126. foo''', [('', 'guido@[132.151.1.21]')])
  127.