Mercurial > p > roundup > code
diff test/test_multipart.py @ 5493:725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
| author | Christof Meerwald <cmeerw@cmeerw.org> |
|---|---|
| date | Sun, 12 Aug 2018 16:15:10 +0100 |
| parents | 55f09ca366c4 |
| children | 081be318661b |
line wrap: on
line diff
--- a/test/test_multipart.py Sun Aug 12 16:05:42 2018 +0100 +++ b/test/test_multipart.py Sun Aug 12 16:15:10 2018 +0100 @@ -15,12 +15,30 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. +import email import unittest from roundup.anypy.strings import StringIO -from roundup.mailgw import Message +from roundup.mailgw import RoundupMessage + +def gen_message(spec): + """Create a basic MIME message according to 'spec'. + + Each line of a spec has one content-type, which is optionally indented. + The indentation signifies how deep in the MIME hierarchy the + content-type is. -class ExampleMessage(Message): + """ + + def getIndent(line): + """Get the current line's indentation, using four-space indents.""" + count = 0 + for char in line: + if char != ' ': + break + count += 1 + return count // 4 + # A note on message/rfc822: The content of such an attachment is an # email with at least one header line. RFC2046 tells us: """ A # media type of "message/rfc822" indicates that the body contains an @@ -42,36 +60,22 @@ 'application/pdf': ' name="foo.pdf"\nfoo\n', 'message/rfc822': '\nSubject: foo\n\nfoo\n'} - def __init__(self, spec): - """Create a basic MIME message according to 'spec'. - - Each line of a spec has one content-type, which is optionally indented. - The indentation signifies how deep in the MIME hierarchy the - content-type is. - - """ - parts = [] - for line in spec.splitlines(): - content_type = line.strip() - if not content_type: - continue + parts = [] + for line in spec.splitlines(): + content_type = line.strip() + if not content_type: + continue - indent = self.getIndent(line) - if indent: - parts.append('\n--boundary-%s\n' % indent) - parts.append('Content-type: %s;\n' % content_type) - parts.append(self.table[content_type] % {'indent': indent + 1}) - - Message.__init__(self, StringIO(''.join(parts))) + indent = getIndent(line) + if indent: + parts.append('\n--boundary-%s\n' % indent) + parts.append('Content-type: %s;\n' % content_type) + parts.append(table[content_type] % {'indent': indent + 1}) - def getIndent(self, line): - """Get the current line's indentation, using four-space indents.""" - count = 0 - for char in line: - if char != ' ': - break - count += 1 - return count // 4 + for i in range(indent, 0, -1): + parts.append('\n--boundary-%s--\n' % i) + + return email.message_from_file(StringIO(''.join(parts)), RoundupMessage) class MultipartTestCase(unittest.TestCase): def setUp(self): @@ -110,54 +114,49 @@ self.fp.seek(0) def testMultipart(self): - m = Message(self.fp) + m = email.message_from_file(self.fp, RoundupMessage) self.assert_(m is not None) - # skip the first bit - p = m.getpart() - self.assert_(p is not None) - self.assertEqual(p.fp.read(), - 'This is a multipart message. Ignore this bit.\r\n') + it = iter(m.get_payload()) # first text/plain - p = m.getpart() + p = next(it, None) self.assert_(p is not None) - self.assertEqual(p.gettype(), 'text/plain') - self.assertEqual(p.fp.read(), + self.assertEqual(p.get_content_type(), 'text/plain') + self.assertEqual(p.get_payload(), 'Hello, world!\r\n\r\nBlah blah\r\nfoo\r\n-foo\r\n') # sub-multipart - p = m.getpart() + p = next(it, None) self.assert_(p is not None) - self.assertEqual(p.gettype(), 'multipart/alternative') + self.assertEqual(p.get_content_type(), 'multipart/alternative') # sub-multipart text/plain - q = p.getpart() + qit = iter(p.get_payload()) + q = next(qit, None) self.assert_(q is not None) - q = p.getpart() - self.assert_(q is not None) - self.assertEqual(q.gettype(), 'text/plain') - self.assertEqual(q.fp.read(), 'Hello, world!\r\n\r\nBlah blah\r\n') + self.assertEqual(q.get_content_type(), 'text/plain') + self.assertEqual(q.get_payload(), 'Hello, world!\r\n\r\nBlah blah\r\n') # sub-multipart text/html - q = p.getpart() + q = next(qit, None) self.assert_(q is not None) - self.assertEqual(q.gettype(), 'text/html') - self.assertEqual(q.fp.read(), '<b>Hello, world!</b>\r\n') + self.assertEqual(q.get_content_type(), 'text/html') + self.assertEqual(q.get_payload(), '<b>Hello, world!</b>\r\n') # sub-multipart end - q = p.getpart() + q = next(qit, None) self.assert_(q is None) # final text/plain - p = m.getpart() + p = next(it, None) self.assert_(p is not None) - self.assertEqual(p.gettype(), 'text/plain') - self.assertEqual(p.fp.read(), + self.assertEqual(p.get_content_type(), 'text/plain') + self.assertEqual(p.get_payload(), 'Last bit\n') # end - p = m.getpart() + p = next(it, None) self.assert_(p is None) def TestExtraction(self, spec, expected, convert_html_with=False): @@ -167,7 +166,7 @@ else: html2text=None - self.assertEqual(ExampleMessage(spec).extract_content( + self.assertEqual(gen_message(spec).extract_content( html2text=html2text), expected) def testTextPlain(self):
