blob: 967b91b70deea4e951877854643df6a6f36e9649 [file] [log] [blame]
Fredrik Lundhf7850422001-01-17 21:51:36 +00001from test_support import verbose
Barry Warsawe75888e1999-01-14 20:00:58 +00002import rfc822, sys
3try:
4 from cStringIO import StringIO
5except ImportError:
6 from StringIO import StringIO
7
8def test(msg, results):
9 fp = StringIO()
10 fp.write(msg)
11 fp.seek(0)
12 m = rfc822.Message(fp)
13 i = 0
Fred Drake098b55a2000-10-23 17:30:23 +000014
Barry Warsawe75888e1999-01-14 20:00:58 +000015 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
Guido van Rossum3ed1be91999-05-03 19:57:01 +000032 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
Barry Warsawe75888e1999-01-14 20:00:58 +000047test('''Date: Wed, 13 Jan 1999 23:57:35 -0500
48From: Guido van Rossum <guido@CNRI.Reston.VA.US>
49To: "Guido van
Fred Drake098b55a2000-10-23 17:30:23 +000050\t : Rossum" <guido@python.org>
Barry Warsawe75888e1999-01-14 20:00:58 +000051Subject: test2
52
53test2
Fred Drake098b55a2000-10-23 17:30:23 +000054''', [('Guido van\n\t : Rossum', 'guido@python.org')])
Barry Warsawe75888e1999-01-14 20:00:58 +000055
56test('''From: Barry <bwarsaw@python.org
57To: guido@python.org (Guido: the Barbarian)
58Subject: nonsense
Guido van Rossum3ed1be91999-05-03 19:57:01 +000059Date: Wednesday, January 13 1999 23:57:35 -0500
Barry Warsawe75888e1999-01-14 20:00:58 +000060
61test''', [('Guido: the Barbarian', 'guido@python.org'),
62 ])
63
64test('''From: Barry <bwarsaw@python.org
65To: guido@python.org (Guido: the Barbarian)
66Cc: "Guido: the Madman" <guido@python.org>
Guido van Rossum3ed1be91999-05-03 19:57:01 +000067Date: 13-Jan-1999 23:57:35 EST
Barry Warsawe75888e1999-01-14 20:00:58 +000068
69test''', [('Guido: the Barbarian', 'guido@python.org'),
70 ('Guido: the Madman', 'guido@python.org')
71 ])
72
73test('''To: "The monster with
74 the very long name: Guido" <guido@python.org>
Guido van Rossum3ed1be91999-05-03 19:57:01 +000075Date: Wed, 13 Jan 1999 23:57:35 -0500
Barry Warsawe75888e1999-01-14 20:00:58 +000076
77test''', [('The monster with\n the very long name: Guido',
78 'guido@python.org')])
79
80test('''To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>
81CC: Mike Fletcher <mfletch@vrtelecom.com>,
82 "'string-sig@python.org'" <string-sig@python.org>
83Cc: fooz@bat.com, bart@toof.com
84Cc: goit@lip.com
Guido van Rossum3ed1be91999-05-03 19:57:01 +000085Date: Wed, 13 Jan 1999 23:57:35 -0500
Barry Warsawe75888e1999-01-14 20:00:58 +000086
87test''', [('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!
97test('''To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>
Guido van Rossum3ed1be91999-05-03 19:57:01 +000098Date: Wed, 13 Jan 1999 23:57:35 -0500
Barry Warsawe75888e1999-01-14 20:00:58 +000099
100test''', [('', ''),
101 ('', 'dd47@mail.xxx.edu'),
102 ('', '_at_hmhq@hdq-mdm1-imgout.companay.com')
103 ])
Fred Drake5712fa91999-04-28 17:38:31 +0000104
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.
107test('''To: "last, first" <userid@foo.net>
108
109test''', [('last, first', 'userid@foo.net'),
110 ])
Guido van Rossum47ac4e61999-06-15 18:56:46 +0000111
112test('''To: (Comment stuff) "Quoted name"@somewhere.com
113
114test''', [('Comment stuff', '"Quoted name"@somewhere.com'),
115 ])
Barry Warsaw7c5b9d11999-07-12 18:47:00 +0000116
117test('''To: :
118Cc: goit@lip.com
119Date: Wed, 13 Jan 1999 23:57:35 -0500
120
121test''', [('', 'goit@lip.com')])
122
Barry Warsaw38bfc4d2000-09-25 15:09:28 +0000123
124test('''To: guido@[132.151.1.21]
125
126foo''', [('', 'guido@[132.151.1.21]')])