A bunch of new tests, and updated tests for the email 3.0 FeedParser.

This commit is contained in:
Barry Warsaw 2004-05-09 03:16:03 +00:00
parent e7169eb9ed
commit b8b57e75b2
1 changed files with 191 additions and 65 deletions

View File

@ -1,4 +1,4 @@
# Copyright (C) 2001,2002,2003 Python Software Foundation # Copyright (C) 2001-2004 Python Software Foundation
# email package unit tests # email package unit tests
import os import os
@ -9,7 +9,6 @@ import difflib
import unittest import unittest
import warnings import warnings
from cStringIO import StringIO from cStringIO import StringIO
from types import StringType, ListType
import email import email
@ -42,12 +41,6 @@ SPACE = ' '
# We don't care about DeprecationWarnings # We don't care about DeprecationWarnings
warnings.filterwarnings('ignore', '', DeprecationWarning, __name__) warnings.filterwarnings('ignore', '', DeprecationWarning, __name__)
try:
True, False
except NameError:
True = 1
False = 0
def openfile(filename, mode='r'): def openfile(filename, mode='r'):
@ -1100,16 +1093,30 @@ This is the dingus fish.
unless(not m0.is_multipart()) unless(not m0.is_multipart())
unless(not m1.is_multipart()) unless(not m1.is_multipart())
def test_no_parts_in_a_multipart(self): def test_empty_multipart_idempotent(self):
text = """\
Content-Type: multipart/mixed; boundary="BOUNDARY"
MIME-Version: 1.0
Subject: A subject
To: aperson@dom.ain
From: bperson@dom.ain
--BOUNDARY
--BOUNDARY--
"""
msg = Parser().parsestr(text)
self.ndiffAssertEqual(text, msg.as_string())
def test_no_parts_in_a_multipart_with_none_epilogue(self):
outer = MIMEBase('multipart', 'mixed') outer = MIMEBase('multipart', 'mixed')
outer['Subject'] = 'A subject' outer['Subject'] = 'A subject'
outer['To'] = 'aperson@dom.ain' outer['To'] = 'aperson@dom.ain'
outer['From'] = 'bperson@dom.ain' outer['From'] = 'bperson@dom.ain'
outer.preamble = ''
outer.epilogue = ''
outer.set_boundary('BOUNDARY') outer.set_boundary('BOUNDARY')
msg = MIMEText('hello world') self.ndiffAssertEqual(outer.as_string(), '''\
self.assertEqual(outer.as_string(), '''\
Content-Type: multipart/mixed; boundary="BOUNDARY" Content-Type: multipart/mixed; boundary="BOUNDARY"
MIME-Version: 1.0 MIME-Version: 1.0
Subject: A subject Subject: A subject
@ -1118,6 +1125,25 @@ From: bperson@dom.ain
--BOUNDARY --BOUNDARY
--BOUNDARY--''')
def test_no_parts_in_a_multipart_with_empty_epilogue(self):
outer = MIMEBase('multipart', 'mixed')
outer['Subject'] = 'A subject'
outer['To'] = 'aperson@dom.ain'
outer['From'] = 'bperson@dom.ain'
outer.preamble = ''
outer.epilogue = ''
outer.set_boundary('BOUNDARY')
self.ndiffAssertEqual(outer.as_string(), '''\
Content-Type: multipart/mixed; boundary="BOUNDARY"
MIME-Version: 1.0
Subject: A subject
To: aperson@dom.ain
From: bperson@dom.ain
--BOUNDARY
--BOUNDARY-- --BOUNDARY--
''') ''')
@ -1128,8 +1154,6 @@ From: bperson@dom.ain
outer['Subject'] = 'A subject' outer['Subject'] = 'A subject'
outer['To'] = 'aperson@dom.ain' outer['To'] = 'aperson@dom.ain'
outer['From'] = 'bperson@dom.ain' outer['From'] = 'bperson@dom.ain'
outer.preamble = ''
outer.epilogue = ''
outer.set_boundary('BOUNDARY') outer.set_boundary('BOUNDARY')
msg = MIMEText('hello world') msg = MIMEText('hello world')
outer.attach(msg) outer.attach(msg)
@ -1145,18 +1169,122 @@ Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0 MIME-Version: 1.0
Content-Transfer-Encoding: 7bit Content-Transfer-Encoding: 7bit
hello world
--BOUNDARY--''')
def test_seq_parts_in_a_multipart_with_empty_preamble(self):
eq = self.ndiffAssertEqual
outer = MIMEBase('multipart', 'mixed')
outer['Subject'] = 'A subject'
outer['To'] = 'aperson@dom.ain'
outer['From'] = 'bperson@dom.ain'
outer.preamble = ''
msg = MIMEText('hello world')
outer.attach(msg)
outer.set_boundary('BOUNDARY')
eq(outer.as_string(), '''\
Content-Type: multipart/mixed; boundary="BOUNDARY"
MIME-Version: 1.0
Subject: A subject
To: aperson@dom.ain
From: bperson@dom.ain
--BOUNDARY
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
hello world
--BOUNDARY--''')
def test_seq_parts_in_a_multipart_with_none_preamble(self):
eq = self.ndiffAssertEqual
outer = MIMEBase('multipart', 'mixed')
outer['Subject'] = 'A subject'
outer['To'] = 'aperson@dom.ain'
outer['From'] = 'bperson@dom.ain'
outer.preamble = None
msg = MIMEText('hello world')
outer.attach(msg)
outer.set_boundary('BOUNDARY')
eq(outer.as_string(), '''\
Content-Type: multipart/mixed; boundary="BOUNDARY"
MIME-Version: 1.0
Subject: A subject
To: aperson@dom.ain
From: bperson@dom.ain
--BOUNDARY
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
hello world
--BOUNDARY--''')
def test_seq_parts_in_a_multipart_with_none_epilogue(self):
eq = self.ndiffAssertEqual
outer = MIMEBase('multipart', 'mixed')
outer['Subject'] = 'A subject'
outer['To'] = 'aperson@dom.ain'
outer['From'] = 'bperson@dom.ain'
outer.epilogue = None
msg = MIMEText('hello world')
outer.attach(msg)
outer.set_boundary('BOUNDARY')
eq(outer.as_string(), '''\
Content-Type: multipart/mixed; boundary="BOUNDARY"
MIME-Version: 1.0
Subject: A subject
To: aperson@dom.ain
From: bperson@dom.ain
--BOUNDARY
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
hello world
--BOUNDARY--''')
def test_seq_parts_in_a_multipart_with_empty_epilogue(self):
eq = self.ndiffAssertEqual
outer = MIMEBase('multipart', 'mixed')
outer['Subject'] = 'A subject'
outer['To'] = 'aperson@dom.ain'
outer['From'] = 'bperson@dom.ain'
outer.epilogue = ''
msg = MIMEText('hello world')
outer.attach(msg)
outer.set_boundary('BOUNDARY')
eq(outer.as_string(), '''\
Content-Type: multipart/mixed; boundary="BOUNDARY"
MIME-Version: 1.0
Subject: A subject
To: aperson@dom.ain
From: bperson@dom.ain
--BOUNDARY
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
hello world hello world
--BOUNDARY-- --BOUNDARY--
''') ''')
def test_seq_parts_in_a_multipart(self):
def test_seq_parts_in_a_multipart_with_nl_epilogue(self):
eq = self.ndiffAssertEqual eq = self.ndiffAssertEqual
outer = MIMEBase('multipart', 'mixed') outer = MIMEBase('multipart', 'mixed')
outer['Subject'] = 'A subject' outer['Subject'] = 'A subject'
outer['To'] = 'aperson@dom.ain' outer['To'] = 'aperson@dom.ain'
outer['From'] = 'bperson@dom.ain' outer['From'] = 'bperson@dom.ain'
outer.preamble = '' outer.epilogue = '\n'
outer.epilogue = ''
msg = MIMEText('hello world') msg = MIMEText('hello world')
outer.attach(msg) outer.attach(msg)
outer.set_boundary('BOUNDARY') outer.set_boundary('BOUNDARY')
@ -1174,6 +1302,7 @@ Content-Transfer-Encoding: 7bit
hello world hello world
--BOUNDARY-- --BOUNDARY--
''') ''')
@ -1187,25 +1316,21 @@ class TestNonConformant(TestEmailBase):
eq(msg.get_main_type(), None) eq(msg.get_main_type(), None)
eq(msg.get_subtype(), None) eq(msg.get_subtype(), None)
## XXX: No longer fails with the new parser. Should it ? def test_same_boundary_inner_outer(self):
## def test_bogus_boundary(self): unless = self.failUnless
## fp = openfile(findfile('msg_15.txt')) msg = self._msgobj('msg_15.txt')
## try: # XXX We can probably eventually do better
## data = fp.read() inner = msg.get_payload(0)
## finally: unless(hasattr(inner, 'defects'))
## fp.close() self.assertEqual(len(inner.defects), 1)
## p = Parser(strict=True) unless(isinstance(inner.defects[0], Errors.StartBoundaryNotFound))
## # Note, under a future non-strict parsing mode, this would parse the
## # message into the intended message tree.
## self.assertRaises(Errors.BoundaryError, p.parsestr, data)
def test_multipart_no_boundary(self): def test_multipart_no_boundary(self):
fp = openfile(findfile('msg_25.txt')) unless = self.failUnless
try: msg = self._msgobj('msg_25.txt')
self.assertRaises(Errors.BoundaryError, unless(isinstance(msg.get_payload(), str))
email.message_from_file, fp) self.assertEqual(len(msg.defects), 1)
finally: unless(isinstance(msg.defects[0], Errors.NoBoundaryInMultipart))
fp.close()
def test_invalid_content_type(self): def test_invalid_content_type(self):
eq = self.assertEqual eq = self.assertEqual
@ -1245,22 +1370,18 @@ message 2
--BOUNDARY-- --BOUNDARY--
""") """)
## XXX: No longer fails with the new parser. Should it ? def test_no_separating_blank_line(self):
## def test_no_separating_blank_line(self): eq = self.ndiffAssertEqual
## eq = self.ndiffAssertEqual msg = self._msgobj('msg_35.txt')
## msg = self._msgobj('msg_35.txt') eq(msg.as_string(), """\
## eq(msg.as_string(), """\ From: aperson@dom.ain
## From: aperson@dom.ain To: bperson@dom.ain
## To: bperson@dom.ain Subject: here's something interesting
## Subject: here's something interesting
## counter to RFC 2822, there's no separating newline here
## counter to RFC 2822, there's no separating newline here """)
## """)
## # strict=True should raise an exception
## self.assertRaises(Errors.HeaderParseError,
## self._msgobj, 'msg_35.txt', True)
##
##
# Test RFC 2047 header encoding and decoding # Test RFC 2047 header encoding and decoding
class TestRFC2047(unittest.TestCase): class TestRFC2047(unittest.TestCase):
@ -1351,7 +1472,7 @@ class TestMIMEMessage(TestEmailBase):
r = MIMEMessage(m) r = MIMEMessage(m)
eq(r.get_type(), 'message/rfc822') eq(r.get_type(), 'message/rfc822')
payload = r.get_payload() payload = r.get_payload()
unless(type(payload), ListType) unless(isinstance(payload, list))
eq(len(payload), 1) eq(len(payload), 1)
subpart = payload[0] subpart = payload[0]
unless(subpart is m) unless(subpart is m)
@ -1392,7 +1513,7 @@ Here is the body of the message.
msg = self._msgobj('msg_11.txt') msg = self._msgobj('msg_11.txt')
eq(msg.get_type(), 'message/rfc822') eq(msg.get_type(), 'message/rfc822')
payload = msg.get_payload() payload = msg.get_payload()
unless(isinstance(payload, ListType)) unless(isinstance(payload, list))
eq(len(payload), 1) eq(len(payload), 1)
submsg = payload[0] submsg = payload[0]
self.failUnless(isinstance(submsg, Message)) self.failUnless(isinstance(submsg, Message))
@ -1449,7 +1570,7 @@ Your message cannot be delivered to the following recipients:
subpart = msg.get_payload(2) subpart = msg.get_payload(2)
eq(subpart.get_type(), 'message/rfc822') eq(subpart.get_type(), 'message/rfc822')
payload = subpart.get_payload() payload = subpart.get_payload()
unless(isinstance(payload, ListType)) unless(isinstance(payload, list))
eq(len(payload), 1) eq(len(payload), 1)
subsubpart = payload[0] subsubpart = payload[0]
unless(isinstance(subsubpart, Message)) unless(isinstance(subsubpart, Message))
@ -1468,7 +1589,7 @@ Your message cannot be delivered to the following recipients:
msg['From'] = 'aperson@dom.ain' msg['From'] = 'aperson@dom.ain'
msg['To'] = 'bperson@dom.ain' msg['To'] = 'bperson@dom.ain'
msg['Subject'] = 'Test' msg['Subject'] = 'Test'
msg.preamble = 'MIME message\n' msg.preamble = 'MIME message'
msg.epilogue = 'End of MIME message\n' msg.epilogue = 'End of MIME message\n'
msg1 = MIMEText('One') msg1 = MIMEText('One')
msg2 = MIMEText('Two') msg2 = MIMEText('Two')
@ -1560,7 +1681,7 @@ Two
neq = self.ndiffAssertEqual neq = self.ndiffAssertEqual
# Set up container # Set up container
container = MIMEMultipart('digest', 'BOUNDARY') container = MIMEMultipart('digest', 'BOUNDARY')
container.epilogue = '\n' container.epilogue = ''
# Set up subparts # Set up subparts
subpart1a = MIMEText('message 1\n') subpart1a = MIMEText('message 1\n')
subpart2a = MIMEText('message 2\n') subpart2a = MIMEText('message 2\n')
@ -1729,6 +1850,10 @@ class TestIdempotent(TestEmailBase):
msg, text = self._msgobj('msg_34.txt') msg, text = self._msgobj('msg_34.txt')
self._idempotent(msg, text) self._idempotent(msg, text)
def test_nested_multipart_mixeds(self):
msg, text = self._msgobj('msg_12a.txt')
self._idempotent(msg, text)
def test_content_type(self): def test_content_type(self):
eq = self.assertEquals eq = self.assertEquals
unless = self.failUnless unless = self.failUnless
@ -1741,8 +1866,8 @@ class TestIdempotent(TestEmailBase):
params[pk] = pv params[pk] = pv
eq(params['report-type'], 'delivery-status') eq(params['report-type'], 'delivery-status')
eq(params['boundary'], 'D1690A7AC1.996856090/mail.example.com') eq(params['boundary'], 'D1690A7AC1.996856090/mail.example.com')
eq(msg.preamble, 'This is a MIME-encapsulated message.\n\n') eq(msg.preamble, 'This is a MIME-encapsulated message.\n')
eq(msg.epilogue, '\n\n') eq(msg.epilogue, '\n')
eq(len(msg.get_payload()), 3) eq(len(msg.get_payload()), 3)
# Make sure the subparts are what we expect # Make sure the subparts are what we expect
msg1 = msg.get_payload(0) msg1 = msg.get_payload(0)
@ -1755,7 +1880,7 @@ class TestIdempotent(TestEmailBase):
eq(msg3.get_type(), 'message/rfc822') eq(msg3.get_type(), 'message/rfc822')
self.failUnless(isinstance(msg3, Message)) self.failUnless(isinstance(msg3, Message))
payload = msg3.get_payload() payload = msg3.get_payload()
unless(isinstance(payload, ListType)) unless(isinstance(payload, list))
eq(len(payload), 1) eq(len(payload), 1)
msg4 = payload[0] msg4 = payload[0]
unless(isinstance(msg4, Message)) unless(isinstance(msg4, Message))
@ -1770,12 +1895,12 @@ class TestIdempotent(TestEmailBase):
# Make sure the payload is a list of exactly one sub-Message, and that # Make sure the payload is a list of exactly one sub-Message, and that
# that submessage has a type of text/plain # that submessage has a type of text/plain
payload = msg.get_payload() payload = msg.get_payload()
unless(isinstance(payload, ListType)) unless(isinstance(payload, list))
eq(len(payload), 1) eq(len(payload), 1)
msg1 = payload[0] msg1 = payload[0]
self.failUnless(isinstance(msg1, Message)) self.failUnless(isinstance(msg1, Message))
eq(msg1.get_type(), 'text/plain') eq(msg1.get_type(), 'text/plain')
self.failUnless(isinstance(msg1.get_payload(), StringType)) self.failUnless(isinstance(msg1.get_payload(), str))
eq(msg1.get_payload(), '\n') eq(msg1.get_payload(), '\n')
@ -2027,12 +2152,13 @@ class TestMiscellaneous(unittest.TestCase):
class TestIterators(TestEmailBase): class TestIterators(TestEmailBase):
def test_body_line_iterator(self): def test_body_line_iterator(self):
eq = self.assertEqual eq = self.assertEqual
neq = self.ndiffAssertEqual
# First a simple non-multipart message # First a simple non-multipart message
msg = self._msgobj('msg_01.txt') msg = self._msgobj('msg_01.txt')
it = Iterators.body_line_iterator(msg) it = Iterators.body_line_iterator(msg)
lines = list(it) lines = list(it)
eq(len(lines), 6) eq(len(lines), 6)
eq(EMPTYSTRING.join(lines), msg.get_payload()) neq(EMPTYSTRING.join(lines), msg.get_payload())
# Now a more complicated multipart # Now a more complicated multipart
msg = self._msgobj('msg_02.txt') msg = self._msgobj('msg_02.txt')
it = Iterators.body_line_iterator(msg) it = Iterators.body_line_iterator(msg)
@ -2040,7 +2166,7 @@ class TestIterators(TestEmailBase):
eq(len(lines), 43) eq(len(lines), 43)
fp = openfile('msg_19.txt') fp = openfile('msg_19.txt')
try: try:
eq(EMPTYSTRING.join(lines), fp.read()) neq(EMPTYSTRING.join(lines), fp.read())
finally: finally:
fp.close() fp.close()
@ -2094,8 +2220,8 @@ class TestParsers(TestEmailBase):
eq(msg['from'], 'ppp-request@zzz.org') eq(msg['from'], 'ppp-request@zzz.org')
eq(msg['to'], 'ppp@zzz.org') eq(msg['to'], 'ppp@zzz.org')
eq(msg.get_type(), 'multipart/mixed') eq(msg.get_type(), 'multipart/mixed')
eq(msg.is_multipart(), 0) self.failIf(msg.is_multipart())
self.failUnless(isinstance(msg.get_payload(), StringType)) self.failUnless(isinstance(msg.get_payload(), str))
def test_whitespace_continuaton(self): def test_whitespace_continuaton(self):
eq = self.assertEqual eq = self.assertEqual