test_rfc822.py 3.57 KB
Newer Older
1
from test_support import verbose
2 3 4 5 6 7 8 9 10 11 12 13
import rfc822, sys
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

def test(msg, results):
    fp = StringIO()
    fp.write(msg)
    fp.seek(0)
    m = rfc822.Message(fp)
    i = 0
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    for n, a in m.getaddrlist('to') + m.getaddrlist('cc'):
        if verbose:
            print 'name:', repr(n), 'addr:', repr(a)
        try:
            mn, ma = results[i][0], results[i][1]
        except IndexError:
            print 'extra parsed address:', repr(n), repr(a)
            continue
        i = i + 1
        if mn == n and ma == a:
            if verbose:
                print '    [matched]'
        else:
            if verbose:
                print '    [no match]'
            print 'not found:', repr(n), repr(a)

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    out = m.getdate('date')
    if out:
        if verbose:
            print 'Date:', m.getheader('date')
        if out == (1999, 1, 13, 23, 57, 35, 0, 0, 0):
            if verbose:
                print '    [matched]'
        else:
            if verbose:
                print '    [no match]'
            print 'Date conversion failed:', out

# Note: all test cases must have the same date (in various formats),
# or no date!

47 48 49
test('''Date:    Wed, 13 Jan 1999 23:57:35 -0500
From:    Guido van Rossum <guido@CNRI.Reston.VA.US>
To:      "Guido van
50
\t : Rossum" <guido@python.org>
51 52 53
Subject: test2

test2
54
''', [('Guido van\n\t : Rossum', 'guido@python.org')])
55 56 57 58

test('''From: Barry <bwarsaw@python.org
To: guido@python.org (Guido: the Barbarian)
Subject: nonsense
59
Date: Wednesday, January 13 1999 23:57:35 -0500
60 61 62 63 64 65 66

test''', [('Guido: the Barbarian', 'guido@python.org'),
          ])

test('''From: Barry <bwarsaw@python.org
To: guido@python.org (Guido: the Barbarian)
Cc: "Guido: the Madman" <guido@python.org>
67
Date:  13-Jan-1999 23:57:35 EST
68 69 70 71 72 73 74

test''', [('Guido: the Barbarian', 'guido@python.org'),
          ('Guido: the Madman', 'guido@python.org')
          ])

test('''To: "The monster with
     the very long name: Guido" <guido@python.org>
75
Date:    Wed, 13 Jan 1999 23:57:35 -0500
76 77 78 79 80 81 82 83 84

test''', [('The monster with\n     the very long name: Guido',
           'guido@python.org')])

test('''To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>
CC: Mike Fletcher <mfletch@vrtelecom.com>,
        "'string-sig@python.org'" <string-sig@python.org>
Cc: fooz@bat.com, bart@toof.com
Cc: goit@lip.com
85
Date:    Wed, 13 Jan 1999 23:57:35 -0500
86 87 88 89 90 91 92 93 94 95 96 97

test''', [('Amit J. Patel', 'amitp@Theory.Stanford.EDU'),
          ('Mike Fletcher', 'mfletch@vrtelecom.com'),
          ("'string-sig@python.org'", 'string-sig@python.org'),
          ('', 'fooz@bat.com'),
          ('', 'bart@toof.com'),
          ('', 'goit@lip.com'),
          ])

# This one is just twisted.  I don't know what the proper result should be,
# but it shouldn't be to infloop, which is what used to happen!
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 101 102 103

test''', [('', ''),
          ('', 'dd47@mail.xxx.edu'),
          ('', '_at_hmhq@hdq-mdm1-imgout.companay.com')
          ])
104 105 106 107 108 109 110

# This exercises the old commas-in-a-full-name bug, which should be doing the
# right thing in recent versions of the module.
test('''To: "last, first" <userid@foo.net>

test''', [('last, first', 'userid@foo.net'),
          ])
111 112 113 114 115

test('''To: (Comment stuff) "Quoted name"@somewhere.com

test''', [('Comment stuff', '"Quoted name"@somewhere.com'),
          ])
116 117 118 119 120 121 122

test('''To: :
Cc: goit@lip.com
Date:    Wed, 13 Jan 1999 23:57:35 -0500

test''', [('', 'goit@lip.com')])

123 124 125 126

test('''To: guido@[132.151.1.21]

foo''', [('', 'guido@[132.151.1.21]')])