/Lib/test/test_email/test_email.py
Python | 1193 lines | 1123 code | 39 blank | 31 comment | 10 complexity | 13e42dedca567722c7382803d8688c3a MD5 | raw file
- # Copyright (C) 2001-2010 Python Software Foundation
- # Contact: email-sig@python.org
- # email package unit tests
- import re
- import time
- import base64
- import unittest
- import textwrap
- from io import StringIO, BytesIO
- from itertools import chain
- from random import choice
- from socket import getfqdn
- from threading import Thread
- import email
- import email.policy
- from email.charset import Charset
- from email.header import Header, decode_header, make_header
- from email.parser import Parser, HeaderParser
- from email.generator import Generator, DecodedGenerator, BytesGenerator
- from email.message import Message
- from email.mime.application import MIMEApplication
- from email.mime.audio import MIMEAudio
- from email.mime.text import MIMEText
- from email.mime.image import MIMEImage
- from email.mime.base import MIMEBase
- from email.mime.message import MIMEMessage
- from email.mime.multipart import MIMEMultipart
- from email.mime.nonmultipart import MIMENonMultipart
- from email import utils
- from email import errors
- from email import encoders
- from email import iterators
- from email import base64mime
- from email import quoprimime
- from test.support import unlink, start_threads
- from test.test_email import openfile, TestEmailBase
- # These imports are documented to work, but we are testing them using a
- # different path, so we import them here just to make sure they are importable.
- from email.parser import FeedParser, BytesFeedParser
- NL = '\n'
- EMPTYSTRING = ''
- SPACE = ' '
- # Test various aspects of the Message class's API
- class TestMessageAPI(TestEmailBase):
- def test_get_all(self):
- eq = self.assertEqual
- msg = self._msgobj('msg_20.txt')
- eq(msg.get_all('cc'), ['ccc@zzz.org', 'ddd@zzz.org', 'eee@zzz.org'])
- eq(msg.get_all('xx', 'n/a'), 'n/a')
- def test_getset_charset(self):
- eq = self.assertEqual
- msg = Message()
- eq(msg.get_charset(), None)
- charset = Charset('iso-8859-1')
- msg.set_charset(charset)
- eq(msg['mime-version'], '1.0')
- eq(msg.get_content_type(), 'text/plain')
- eq(msg['content-type'], 'text/plain; charset="iso-8859-1"')
- eq(msg.get_param('charset'), 'iso-8859-1')
- eq(msg['content-transfer-encoding'], 'quoted-printable')
- eq(msg.get_charset().input_charset, 'iso-8859-1')
- # Remove the charset
- msg.set_charset(None)
- eq(msg.get_charset(), None)
- eq(msg['content-type'], 'text/plain')
- # Try adding a charset when there's already MIME headers present
- msg = Message()
- msg['MIME-Version'] = '2.0'
- msg['Content-Type'] = 'text/x-weird'
- msg['Content-Transfer-Encoding'] = 'quinted-puntable'
- msg.set_charset(charset)
- eq(msg['mime-version'], '2.0')
- eq(msg['content-type'], 'text/x-weird; charset="iso-8859-1"')
- eq(msg['content-transfer-encoding'], 'quinted-puntable')
- def test_set_charset_from_string(self):
- eq = self.assertEqual
- msg = Message()
- msg.set_charset('us-ascii')
- eq(msg.get_charset().input_charset, 'us-ascii')
- eq(msg['content-type'], 'text/plain; charset="us-ascii"')
- def test_set_payload_with_charset(self):
- msg = Message()
- charset = Charset('iso-8859-1')
- msg.set_payload('This is a string payload', charset)
- self.assertEqual(msg.get_charset().input_charset, 'iso-8859-1')
- def test_set_payload_with_8bit_data_and_charset(self):
- data = b'\xd0\x90\xd0\x91\xd0\x92'
- charset = Charset('utf-8')
- msg = Message()
- msg.set_payload(data, charset)
- self.assertEqual(msg['content-transfer-encoding'], 'base64')
- self.assertEqual(msg.get_payload(decode=True), data)
- self.assertEqual(msg.get_payload(), '0JDQkdCS\n')
- def test_set_payload_with_non_ascii_and_charset_body_encoding_none(self):
- data = b'\xd0\x90\xd0\x91\xd0\x92'
- charset = Charset('utf-8')
- charset.body_encoding = None # Disable base64 encoding
- msg = Message()
- msg.set_payload(data.decode('utf-8'), charset)
- self.assertEqual(msg['content-transfer-encoding'], '8bit')
- self.assertEqual(msg.get_payload(decode=True), data)
- def test_set_payload_with_8bit_data_and_charset_body_encoding_none(self):
- data = b'\xd0\x90\xd0\x91\xd0\x92'
- charset = Charset('utf-8')
- charset.body_encoding = None # Disable base64 encoding
- msg = Message()
- msg.set_payload(data, charset)
- self.assertEqual(msg['content-transfer-encoding'], '8bit')
- self.assertEqual(msg.get_payload(decode=True), data)
- def test_set_payload_to_list(self):
- msg = Message()
- msg.set_payload([])
- self.assertEqual(msg.get_payload(), [])
- def test_attach_when_payload_is_string(self):
- msg = Message()
- msg['Content-Type'] = 'multipart/mixed'
- msg.set_payload('string payload')
- sub_msg = MIMEMessage(Message())
- self.assertRaisesRegex(TypeError, "[Aa]ttach.*non-multipart",
- msg.attach, sub_msg)
- def test_get_charsets(self):
- eq = self.assertEqual
- msg = self._msgobj('msg_08.txt')
- charsets = msg.get_charsets()
- eq(charsets, [None, 'us-ascii', 'iso-8859-1', 'iso-8859-2', 'koi8-r'])
- msg = self._msgobj('msg_09.txt')
- charsets = msg.get_charsets('dingbat')
- eq(charsets, ['dingbat', 'us-ascii', 'iso-8859-1', 'dingbat',
- 'koi8-r'])
- msg = self._msgobj('msg_12.txt')
- charsets = msg.get_charsets()
- eq(charsets, [None, 'us-ascii', 'iso-8859-1', None, 'iso-8859-2',
- 'iso-8859-3', 'us-ascii', 'koi8-r'])
- def test_get_filename(self):
- eq = self.assertEqual
- msg = self._msgobj('msg_04.txt')
- filenames = [p.get_filename() for p in msg.get_payload()]
- eq(filenames, ['msg.txt', 'msg.txt'])
- msg = self._msgobj('msg_07.txt')
- subpart = msg.get_payload(1)
- eq(subpart.get_filename(), 'dingusfish.gif')
- def test_get_filename_with_name_parameter(self):
- eq = self.assertEqual
- msg = self._msgobj('msg_44.txt')
- filenames = [p.get_filename() for p in msg.get_payload()]
- eq(filenames, ['msg.txt', 'msg.txt'])
- def test_get_boundary(self):
- eq = self.assertEqual
- msg = self._msgobj('msg_07.txt')
- # No quotes!
- eq(msg.get_boundary(), 'BOUNDARY')
- def test_set_boundary(self):
- eq = self.assertEqual
- # This one has no existing boundary parameter, but the Content-Type:
- # header appears fifth.
- msg = self._msgobj('msg_01.txt')
- msg.set_boundary('BOUNDARY')
- header, value = msg.items()[4]
- eq(header.lower(), 'content-type')
- eq(value, 'text/plain; charset="us-ascii"; boundary="BOUNDARY"')
- # This one has a Content-Type: header, with a boundary, stuck in the
- # middle of its headers. Make sure the order is preserved; it should
- # be fifth.
- msg = self._msgobj('msg_04.txt')
- msg.set_boundary('BOUNDARY')
- header, value = msg.items()[4]
- eq(header.lower(), 'content-type')
- eq(value, 'multipart/mixed; boundary="BOUNDARY"')
- # And this one has no Content-Type: header at all.
- msg = self._msgobj('msg_03.txt')
- self.assertRaises(errors.HeaderParseError,
- msg.set_boundary, 'BOUNDARY')
- def test_make_boundary(self):
- msg = MIMEMultipart('form-data')
- # Note that when the boundary gets created is an implementation
- # detail and might change.
- self.assertEqual(msg.items()[0][1], 'multipart/form-data')
- # Trigger creation of boundary
- msg.as_string()
- self.assertEqual(msg.items()[0][1][:33],
- 'multipart/form-data; boundary="==')
- # XXX: there ought to be tests of the uniqueness of the boundary, too.
- def test_message_rfc822_only(self):
- # Issue 7970: message/rfc822 not in multipart parsed by
- # HeaderParser caused an exception when flattened.
- with openfile('msg_46.txt') as fp:
- msgdata = fp.read()
- parser = HeaderParser()
- msg = parser.parsestr(msgdata)
- out = StringIO()
- gen = Generator(out, True, 0)
- gen.flatten(msg, False)
- self.assertEqual(out.getvalue(), msgdata)
- def test_byte_message_rfc822_only(self):
- # Make sure new bytes header parser also passes this.
- with openfile('msg_46.txt') as fp:
- msgdata = fp.read().encode('ascii')
- parser = email.parser.BytesHeaderParser()
- msg = parser.parsebytes(msgdata)
- out = BytesIO()
- gen = email.generator.BytesGenerator(out)
- gen.flatten(msg)
- self.assertEqual(out.getvalue(), msgdata)
- def test_get_decoded_payload(self):
- eq = self.assertEqual
- msg = self._msgobj('msg_10.txt')
- # The outer message is a multipart
- eq(msg.get_payload(decode=True), None)
- # Subpart 1 is 7bit encoded
- eq(msg.get_payload(0).get_payload(decode=True),
- b'This is a 7bit encoded message.\n')
- # Subpart 2 is quopri
- eq(msg.get_payload(1).get_payload(decode=True),
- b'\xa1This is a Quoted Printable encoded message!\n')
- # Subpart 3 is base64
- eq(msg.get_payload(2).get_payload(decode=True),
- b'This is a Base64 encoded message.')
- # Subpart 4 is base64 with a trailing newline, which
- # used to be stripped (issue 7143).
- eq(msg.get_payload(3).get_payload(decode=True),
- b'This is a Base64 encoded message.\n')
- # Subpart 5 has no Content-Transfer-Encoding: header.
- eq(msg.get_payload(4).get_payload(decode=True),
- b'This has no Content-Transfer-Encoding: header.\n')
- def test_get_decoded_uu_payload(self):
- eq = self.assertEqual
- msg = Message()
- msg.set_payload('begin 666 -\n+:&5L;&\\@=V]R;&0 \n \nend\n')
- for cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
- msg['content-transfer-encoding'] = cte
- eq(msg.get_payload(decode=True), b'hello world')
- # Now try some bogus data
- msg.set_payload('foo')
- eq(msg.get_payload(decode=True), b'foo')
- def test_get_payload_n_raises_on_non_multipart(self):
- msg = Message()
- self.assertRaises(TypeError, msg.get_payload, 1)
- def test_decoded_generator(self):
- eq = self.assertEqual
- msg = self._msgobj('msg_07.txt')
- with openfile('msg_17.txt') as fp:
- text = fp.read()
- s = StringIO()
- g = DecodedGenerator(s)
- g.flatten(msg)
- eq(s.getvalue(), text)
- def test__contains__(self):
- msg = Message()
- msg['From'] = 'Me'
- msg['to'] = 'You'
- # Check for case insensitivity
- self.assertIn('from', msg)
- self.assertIn('From', msg)
- self.assertIn('FROM', msg)
- self.assertIn('to', msg)
- self.assertIn('To', msg)
- self.assertIn('TO', msg)
- def test_as_string(self):
- msg = self._msgobj('msg_01.txt')
- with openfile('msg_01.txt') as fp:
- text = fp.read()
- self.assertEqual(text, str(msg))
- fullrepr = msg.as_string(unixfrom=True)
- lines = fullrepr.split('\n')
- self.assertTrue(lines[0].startswith('From '))
- self.assertEqual(text, NL.join(lines[1:]))
- def test_as_string_policy(self):
- msg = self._msgobj('msg_01.txt')
- newpolicy = msg.policy.clone(linesep='\r\n')
- fullrepr = msg.as_string(policy=newpolicy)
- s = StringIO()
- g = Generator(s, policy=newpolicy)
- g.flatten(msg)
- self.assertEqual(fullrepr, s.getvalue())
- def test_as_bytes(self):
- msg = self._msgobj('msg_01.txt')
- with openfile('msg_01.txt') as fp:
- data = fp.read().encode('ascii')
- self.assertEqual(data, bytes(msg))
- fullrepr = msg.as_bytes(unixfrom=True)
- lines = fullrepr.split(b'\n')
- self.assertTrue(lines[0].startswith(b'From '))
- self.assertEqual(data, b'\n'.join(lines[1:]))
- def test_as_bytes_policy(self):
- msg = self._msgobj('msg_01.txt')
- newpolicy = msg.policy.clone(linesep='\r\n')
- fullrepr = msg.as_bytes(policy=newpolicy)
- s = BytesIO()
- g = BytesGenerator(s,policy=newpolicy)
- g.flatten(msg)
- self.assertEqual(fullrepr, s.getvalue())
- # test_headerregistry.TestContentTypeHeader.bad_params
- def test_bad_param(self):
- msg = email.message_from_string("Content-Type: blarg; baz; boo\n")
- self.assertEqual(msg.get_param('baz'), '')
- def test_missing_filename(self):
- msg = email.message_from_string("From: foo\n")
- self.assertEqual(msg.get_filename(), None)
- def test_bogus_filename(self):
- msg = email.message_from_string(
- "Content-Disposition: blarg; filename\n")
- self.assertEqual(msg.get_filename(), '')
- def test_missing_boundary(self):
- msg = email.message_from_string("From: foo\n")
- self.assertEqual(msg.get_boundary(), None)
- def test_get_params(self):
- eq = self.assertEqual
- msg = email.message_from_string(
- 'X-Header: foo=one; bar=two; baz=three\n')
- eq(msg.get_params(header='x-header'),
- [('foo', 'one'), ('bar', 'two'), ('baz', 'three')])
- msg = email.message_from_string(
- 'X-Header: foo; bar=one; baz=two\n')
- eq(msg.get_params(header='x-header'),
- [('foo', ''), ('bar', 'one'), ('baz', 'two')])
- eq(msg.get_params(), None)
- msg = email.message_from_string(
- 'X-Header: foo; bar="one"; baz=two\n')
- eq(msg.get_params(header='x-header'),
- [('foo', ''), ('bar', 'one'), ('baz', 'two')])
- # test_headerregistry.TestContentTypeHeader.spaces_around_param_equals
- def test_get_param_liberal(self):
- msg = Message()
- msg['Content-Type'] = 'Content-Type: Multipart/mixed; boundary = "CPIMSSMTPC06p5f3tG"'
- self.assertEqual(msg.get_param('boundary'), 'CPIMSSMTPC06p5f3tG')
- def test_get_param(self):
- eq = self.assertEqual
- msg = email.message_from_string(
- "X-Header: foo=one; bar=two; baz=three\n")
- eq(msg.get_param('bar', header='x-header'), 'two')
- eq(msg.get_param('quuz', header='x-header'), None)
- eq(msg.get_param('quuz'), None)
- msg = email.message_from_string(
- 'X-Header: foo; bar="one"; baz=two\n')
- eq(msg.get_param('foo', header='x-header'), '')
- eq(msg.get_param('bar', header='x-header'), 'one')
- eq(msg.get_param('baz', header='x-header'), 'two')
- # XXX: We are not RFC-2045 compliant! We cannot parse:
- # msg["Content-Type"] = 'text/plain; weird="hey; dolly? [you] @ <\\"home\\">?"'
- # msg.get_param("weird")
- # yet.
- # test_headerregistry.TestContentTypeHeader.spaces_around_semis
- def test_get_param_funky_continuation_lines(self):
- msg = self._msgobj('msg_22.txt')
- self.assertEqual(msg.get_payload(1).get_param('name'), 'wibble.JPG')
- # test_headerregistry.TestContentTypeHeader.semis_inside_quotes
- def test_get_param_with_semis_in_quotes(self):
- msg = email.message_from_string(
- 'Content-Type: image/pjpeg; name="Jim&&Jill"\n')
- self.assertEqual(msg.get_param('name'), 'Jim&&Jill')
- self.assertEqual(msg.get_param('name', unquote=False),
- '"Jim&&Jill"')
- # test_headerregistry.TestContentTypeHeader.quotes_inside_rfc2231_value
- def test_get_param_with_quotes(self):
- msg = email.message_from_string(
- 'Content-Type: foo; bar*0="baz\\"foobar"; bar*1="\\"baz"')
- self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz')
- msg = email.message_from_string(
- "Content-Type: foo; bar*0=\"baz\\\"foobar\"; bar*1=\"\\\"baz\"")
- self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz')
- def test_field_containment(self):
- msg = email.message_from_string('Header: exists')
- self.assertIn('header', msg)
- self.assertIn('Header', msg)
- self.assertIn('HEADER', msg)
- self.assertNotIn('headerx', msg)
- def test_set_param(self):
- eq = self.assertEqual
- msg = Message()
- msg.set_param('charset', 'iso-2022-jp')
- eq(msg.get_param('charset'), 'iso-2022-jp')
- msg.set_param('importance', 'high value')
- eq(msg.get_param('importance'), 'high value')
- eq(msg.get_param('importance', unquote=False), '"high value"')
- eq(msg.get_params(), [('text/plain', ''),
- ('charset', 'iso-2022-jp'),
- ('importance', 'high value')])
- eq(msg.get_params(unquote=False), [('text/plain', ''),
- ('charset', '"iso-2022-jp"'),
- ('importance', '"high value"')])
- msg.set_param('charset', 'iso-9999-xx', header='X-Jimmy')
- eq(msg.get_param('charset', header='X-Jimmy'), 'iso-9999-xx')
- def test_del_param(self):
- eq = self.assertEqual
- msg = self._msgobj('msg_05.txt')
- eq(msg.get_params(),
- [('multipart/report', ''), ('report-type', 'delivery-status'),
- ('boundary', 'D1690A7AC1.996856090/mail.example.com')])
- old_val = msg.get_param("report-type")
- msg.del_param("report-type")
- eq(msg.get_params(),
- [('multipart/report', ''),
- ('boundary', 'D1690A7AC1.996856090/mail.example.com')])
- msg.set_param("report-type", old_val)
- eq(msg.get_params(),
- [('multipart/report', ''),
- ('boundary', 'D1690A7AC1.996856090/mail.example.com'),
- ('report-type', old_val)])
- def test_del_param_on_other_header(self):
- msg = Message()
- msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')
- msg.del_param('filename', 'content-disposition')
- self.assertEqual(msg['content-disposition'], 'attachment')
- def test_del_param_on_nonexistent_header(self):
- msg = Message()
- # Deleting param on empty msg should not raise exception.
- msg.del_param('filename', 'content-disposition')
- def test_del_nonexistent_param(self):
- msg = Message()
- msg.add_header('Content-Type', 'text/plain', charset='utf-8')
- existing_header = msg['Content-Type']
- msg.del_param('foobar', header='Content-Type')
- self.assertEqual(msg['Content-Type'], existing_header)
- def test_set_type(self):
- eq = self.assertEqual
- msg = Message()
- self.assertRaises(ValueError, msg.set_type, 'text')
- msg.set_type('text/plain')
- eq(msg['content-type'], 'text/plain')
- msg.set_param('charset', 'us-ascii')
- eq(msg['content-type'], 'text/plain; charset="us-ascii"')
- msg.set_type('text/html')
- eq(msg['content-type'], 'text/html; charset="us-ascii"')
- def test_set_type_on_other_header(self):
- msg = Message()
- msg['X-Content-Type'] = 'text/plain'
- msg.set_type('application/octet-stream', 'X-Content-Type')
- self.assertEqual(msg['x-content-type'], 'application/octet-stream')
- def test_get_content_type_missing(self):
- msg = Message()
- self.assertEqual(msg.get_content_type(), 'text/plain')
- def test_get_content_type_missing_with_default_type(self):
- msg = Message()
- msg.set_default_type('message/rfc822')
- self.assertEqual(msg.get_content_type(), 'message/rfc822')
- def test_get_content_type_from_message_implicit(self):
- msg = self._msgobj('msg_30.txt')
- self.assertEqual(msg.get_payload(0).get_content_type(),
- 'message/rfc822')
- def test_get_content_type_from_message_explicit(self):
- msg = self._msgobj('msg_28.txt')
- self.assertEqual(msg.get_payload(0).get_content_type(),
- 'message/rfc822')
- def test_get_content_type_from_message_text_plain_implicit(self):
- msg = self._msgobj('msg_03.txt')
- self.assertEqual(msg.get_content_type(), 'text/plain')
- def test_get_content_type_from_message_text_plain_explicit(self):
- msg = self._msgobj('msg_01.txt')
- self.assertEqual(msg.get_content_type(), 'text/plain')
- def test_get_content_maintype_missing(self):
- msg = Message()
- self.assertEqual(msg.get_content_maintype(), 'text')
- def test_get_content_maintype_missing_with_default_type(self):
- msg = Message()
- msg.set_default_type('message/rfc822')
- self.assertEqual(msg.get_content_maintype(), 'message')
- def test_get_content_maintype_from_message_implicit(self):
- msg = self._msgobj('msg_30.txt')
- self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message')
- def test_get_content_maintype_from_message_explicit(self):
- msg = self._msgobj('msg_28.txt')
- self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message')
- def test_get_content_maintype_from_message_text_plain_implicit(self):
- msg = self._msgobj('msg_03.txt')
- self.assertEqual(msg.get_content_maintype(), 'text')
- def test_get_content_maintype_from_message_text_plain_explicit(self):
- msg = self._msgobj('msg_01.txt')
- self.assertEqual(msg.get_content_maintype(), 'text')
- def test_get_content_subtype_missing(self):
- msg = Message()
- self.assertEqual(msg.get_content_subtype(), 'plain')
- def test_get_content_subtype_missing_with_default_type(self):
- msg = Message()
- msg.set_default_type('message/rfc822')
- self.assertEqual(msg.get_content_subtype(), 'rfc822')
- def test_get_content_subtype_from_message_implicit(self):
- msg = self._msgobj('msg_30.txt')
- self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822')
- def test_get_content_subtype_from_message_explicit(self):
- msg = self._msgobj('msg_28.txt')
- self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822')
- def test_get_content_subtype_from_message_text_plain_implicit(self):
- msg = self._msgobj('msg_03.txt')
- self.assertEqual(msg.get_content_subtype(), 'plain')
- def test_get_content_subtype_from_message_text_plain_explicit(self):
- msg = self._msgobj('msg_01.txt')
- self.assertEqual(msg.get_content_subtype(), 'plain')
- def test_get_content_maintype_error(self):
- msg = Message()
- msg['Content-Type'] = 'no-slash-in-this-string'
- self.assertEqual(msg.get_content_maintype(), 'text')
- def test_get_content_subtype_error(self):
- msg = Message()
- msg['Content-Type'] = 'no-slash-in-this-string'
- self.assertEqual(msg.get_content_subtype(), 'plain')
- def test_replace_header(self):
- eq = self.assertEqual
- msg = Message()
- msg.add_header('First', 'One')
- msg.add_header('Second', 'Two')
- msg.add_header('Third', 'Three')
- eq(msg.keys(), ['First', 'Second', 'Third'])
- eq(msg.values(), ['One', 'Two', 'Three'])
- msg.replace_header('Second', 'Twenty')
- eq(msg.keys(), ['First', 'Second', 'Third'])
- eq(msg.values(), ['One', 'Twenty', 'Three'])
- msg.add_header('First', 'Eleven')
- msg.replace_header('First', 'One Hundred')
- eq(msg.keys(), ['First', 'Second', 'Third', 'First'])
- eq(msg.values(), ['One Hundred', 'Twenty', 'Three', 'Eleven'])
- self.assertRaises(KeyError, msg.replace_header, 'Fourth', 'Missing')
- def test_get_content_disposition(self):
- msg = Message()
- self.assertIsNone(msg.get_content_disposition())
- msg.add_header('Content-Disposition', 'attachment',
- filename='random.avi')
- self.assertEqual(msg.get_content_disposition(), 'attachment')
- msg.replace_header('Content-Disposition', 'inline')
- self.assertEqual(msg.get_content_disposition(), 'inline')
- msg.replace_header('Content-Disposition', 'InlinE')
- self.assertEqual(msg.get_content_disposition(), 'inline')
- # test_defect_handling:test_invalid_chars_in_base64_payload
- def test_broken_base64_payload(self):
- x = 'AwDp0P7//y6LwKEAcPa/6Q=9'
- msg = Message()
- msg['content-type'] = 'audio/x-midi'
- msg['content-transfer-encoding'] = 'base64'
- msg.set_payload(x)
- self.assertEqual(msg.get_payload(decode=True),
- (b'\x03\x00\xe9\xd0\xfe\xff\xff.\x8b\xc0'
- b'\xa1\x00p\xf6\xbf\xe9\x0f'))
- self.assertIsInstance(msg.defects[0],
- errors.InvalidBase64CharactersDefect)
- def test_broken_unicode_payload(self):
- # This test improves coverage but is not a compliance test.
- # The behavior in this situation is currently undefined by the API.
- x = 'this is a br\xf6ken thing to do'
- msg = Message()
- msg['content-type'] = 'text/plain'
- msg['content-transfer-encoding'] = '8bit'
- msg.set_payload(x)
- self.assertEqual(msg.get_payload(decode=True),
- bytes(x, 'raw-unicode-escape'))
- def test_questionable_bytes_payload(self):
- # This test improves coverage but is not a compliance test,
- # since it involves poking inside the black box.
- x = 'this is a quéstionable thing to do'.encode('utf-8')
- msg = Message()
- msg['content-type'] = 'text/plain; charset="utf-8"'
- msg['content-transfer-encoding'] = '8bit'
- msg._payload = x
- self.assertEqual(msg.get_payload(decode=True), x)
- # Issue 1078919
- def test_ascii_add_header(self):
- msg = Message()
- msg.add_header('Content-Disposition', 'attachment',
- filename='bud.gif')
- self.assertEqual('attachment; filename="bud.gif"',
- msg['Content-Disposition'])
- def test_noascii_add_header(self):
- msg = Message()
- msg.add_header('Content-Disposition', 'attachment',
- filename="Fußballer.ppt")
- self.assertEqual(
- 'attachment; filename*=utf-8\'\'Fu%C3%9Fballer.ppt',
- msg['Content-Disposition'])
- def test_nonascii_add_header_via_triple(self):
- msg = Message()
- msg.add_header('Content-Disposition', 'attachment',
- filename=('iso-8859-1', '', 'Fußballer.ppt'))
- self.assertEqual(
- 'attachment; filename*=iso-8859-1\'\'Fu%DFballer.ppt',
- msg['Content-Disposition'])
- def test_ascii_add_header_with_tspecial(self):
- msg = Message()
- msg.add_header('Content-Disposition', 'attachment',
- filename="windows [filename].ppt")
- self.assertEqual(
- 'attachment; filename="windows [filename].ppt"',
- msg['Content-Disposition'])
- def test_nonascii_add_header_with_tspecial(self):
- msg = Message()
- msg.add_header('Content-Disposition', 'attachment',
- filename="Fußballer [filename].ppt")
- self.assertEqual(
- "attachment; filename*=utf-8''Fu%C3%9Fballer%20%5Bfilename%5D.ppt",
- msg['Content-Disposition'])
- def test_binary_quopri_payload(self):
- for charset in ('latin-1', 'ascii'):
- msg = Message()
- msg['content-type'] = 'text/plain; charset=%s' % charset
- msg['content-transfer-encoding'] = 'quoted-printable'
- msg.set_payload(b'foo=e6=96=87bar')
- self.assertEqual(
- msg.get_payload(decode=True),
- b'foo\xe6\x96\x87bar',
- 'get_payload returns wrong result with charset %s.' % charset)
- def test_binary_base64_payload(self):
- for charset in ('latin-1', 'ascii'):
- msg = Message()
- msg['content-type'] = 'text/plain; charset=%s' % charset
- msg['content-transfer-encoding'] = 'base64'
- msg.set_payload(b'Zm9v5paHYmFy')
- self.assertEqual(
- msg.get_payload(decode=True),
- b'foo\xe6\x96\x87bar',
- 'get_payload returns wrong result with charset %s.' % charset)
- def test_binary_uuencode_payload(self):
- for charset in ('latin-1', 'ascii'):
- for encoding in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
- msg = Message()
- msg['content-type'] = 'text/plain; charset=%s' % charset
- msg['content-transfer-encoding'] = encoding
- msg.set_payload(b"begin 666 -\n)9F]OYI:'8F%R\n \nend\n")
- self.assertEqual(
- msg.get_payload(decode=True),
- b'foo\xe6\x96\x87bar',
- str(('get_payload returns wrong result ',
- 'with charset {0} and encoding {1}.')).\
- format(charset, encoding))
- def test_add_header_with_name_only_param(self):
- msg = Message()
- msg.add_header('Content-Disposition', 'inline', foo_bar=None)
- self.assertEqual("inline; foo-bar", msg['Content-Disposition'])
- def test_add_header_with_no_value(self):
- msg = Message()
- msg.add_header('X-Status', None)
- self.assertEqual('', msg['X-Status'])
- # Issue 5871: reject an attempt to embed a header inside a header value
- # (header injection attack).
- def test_embedded_header_via_Header_rejected(self):
- msg = Message()
- msg['Dummy'] = Header('dummy\nX-Injected-Header: test')
- self.assertRaises(errors.HeaderParseError, msg.as_string)
- def test_embedded_header_via_string_rejected(self):
- msg = Message()
- msg['Dummy'] = 'dummy\nX-Injected-Header: test'
- self.assertRaises(errors.HeaderParseError, msg.as_string)
- def test_unicode_header_defaults_to_utf8_encoding(self):
- # Issue 14291
- m = MIMEText('abc\n')
- m['Subject'] = 'É test'
- self.assertEqual(str(m),textwrap.dedent("""\
- Content-Type: text/plain; charset="us-ascii"
- MIME-Version: 1.0
- Content-Transfer-Encoding: 7bit
- Subject: =?utf-8?q?=C3=89_test?=
- abc
- """))
- def test_unicode_body_defaults_to_utf8_encoding(self):
- # Issue 14291
- m = MIMEText('É testabc\n')
- self.assertEqual(str(m),textwrap.dedent("""\
- Content-Type: text/plain; charset="utf-8"
- MIME-Version: 1.0
- Content-Transfer-Encoding: base64
- w4kgdGVzdGFiYwo=
- """))
- # Test the email.encoders module
- class TestEncoders(unittest.TestCase):
- def test_EncodersEncode_base64(self):
- with openfile('PyBanner048.gif', 'rb') as fp:
- bindata = fp.read()
- mimed = email.mime.image.MIMEImage(bindata)
- base64ed = mimed.get_payload()
- # the transfer-encoded body lines should all be <=76 characters
- lines = base64ed.split('\n')
- self.assertLessEqual(max([ len(x) for x in lines ]), 76)
- def test_encode_empty_payload(self):
- eq = self.assertEqual
- msg = Message()
- msg.set_charset('us-ascii')
- eq(msg['content-transfer-encoding'], '7bit')
- def test_default_cte(self):
- eq = self.assertEqual
- # 7bit data and the default us-ascii _charset
- msg = MIMEText('hello world')
- eq(msg['content-transfer-encoding'], '7bit')
- # Similar, but with 8bit data
- msg = MIMEText('hello \xf8 world')
- eq(msg['content-transfer-encoding'], 'base64')
- # And now with a different charset
- msg = MIMEText('hello \xf8 world', _charset='iso-8859-1')
- eq(msg['content-transfer-encoding'], 'quoted-printable')
- def test_encode7or8bit(self):
- # Make sure a charset whose input character set is 8bit but
- # whose output character set is 7bit gets a transfer-encoding
- # of 7bit.
- eq = self.assertEqual
- msg = MIMEText('文\n', _charset='euc-jp')
- eq(msg['content-transfer-encoding'], '7bit')
- eq(msg.as_string(), textwrap.dedent("""\
- MIME-Version: 1.0
- Content-Type: text/plain; charset="iso-2022-jp"
- Content-Transfer-Encoding: 7bit
- \x1b$BJ8\x1b(B
- """))
- def test_qp_encode_latin1(self):
- msg = MIMEText('\xe1\xf6\n', 'text', 'ISO-8859-1')
- self.assertEqual(str(msg), textwrap.dedent("""\
- MIME-Version: 1.0
- Content-Type: text/text; charset="iso-8859-1"
- Content-Transfer-Encoding: quoted-printable
- =E1=F6
- """))
- def test_qp_encode_non_latin1(self):
- # Issue 16948
- msg = MIMEText('\u017c\n', 'text', 'ISO-8859-2')
- self.assertEqual(str(msg), textwrap.dedent("""\
- MIME-Version: 1.0
- Content-Type: text/text; charset="iso-8859-2"
- Content-Transfer-Encoding: quoted-printable
- =BF
- """))
- # Test long header wrapping
- class TestLongHeaders(TestEmailBase):
- maxDiff = None
- def test_split_long_continuation(self):
- eq = self.ndiffAssertEqual
- msg = email.message_from_string("""\
- Subject: bug demonstration
- \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
- \tmore text
- test
- """)
- sfp = StringIO()
- g = Generator(sfp)
- g.flatten(msg)
- eq(sfp.getvalue(), """\
- Subject: bug demonstration
- \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
- \tmore text
- test
- """)
- def test_another_long_almost_unsplittable_header(self):
- eq = self.ndiffAssertEqual
- hstr = """\
- bug demonstration
- \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
- \tmore text"""
- h = Header(hstr, continuation_ws='\t')
- eq(h.encode(), """\
- bug demonstration
- \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
- \tmore text""")
- h = Header(hstr.replace('\t', ' '))
- eq(h.encode(), """\
- bug demonstration
- 12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789
- more text""")
- def test_long_nonstring(self):
- eq = self.ndiffAssertEqual
- g = Charset("iso-8859-1")
- cz = Charset("iso-8859-2")
- utf8 = Charset("utf-8")
- g_head = (b'Die Mieter treten hier ein werden mit einem Foerderband '
- b'komfortabel den Korridor entlang, an s\xfcdl\xfcndischen '
- b'Wandgem\xe4lden vorbei, gegen die rotierenden Klingen '
- b'bef\xf6rdert. ')
- cz_head = (b'Finan\xe8ni metropole se hroutily pod tlakem jejich '
- b'd\xf9vtipu.. ')
- utf8_head = ('\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f'
- '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00'
- '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c'
- '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067'
- '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das '
- 'Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder '
- 'die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066'
- '\u3044\u307e\u3059\u3002')
- h = Header(g_head, g, header_name='Subject')
- h.append(cz_head, cz)
- h.append(utf8_head, utf8)
- msg = Message()
- msg['Subject'] = h
- sfp = StringIO()
- g = Generator(sfp)
- g.flatten(msg)
- eq(sfp.getvalue(), """\
- Subject: =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerderb?=
- =?iso-8859-1?q?and_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndischen?=
- =?iso-8859-1?q?_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Klingen_bef?=
- =?iso-8859-1?q?=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se_hrouti?=
- =?iso-8859-2?q?ly_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= =?utf-8?b?5q2j56K6?=
- =?utf-8?b?44Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb44KT44CC5LiA?=
- =?utf-8?b?6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go44Gv44Gn44Gf44KJ?=
- =?utf-8?b?44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGFzIE51bnN0dWNr?=
- =?utf-8?b?IGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWloZXJodW5kIGRhcyBPZGVyIGRp?=
- =?utf-8?b?ZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI3jgajoqIDjgaPjgabjgYTjgb7jgZk=?=
- =?utf-8?b?44CC?=
- """)
- eq(h.encode(maxlinelen=76), """\
- =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerde?=
- =?iso-8859-1?q?rband_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndis?=
- =?iso-8859-1?q?chen_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Klinge?=
- =?iso-8859-1?q?n_bef=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se?=
- =?iso-8859-2?q?_hroutily_pod_tlakem_jejich_d=F9vtipu=2E=2E_?=
- =?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb?=
- =?utf-8?b?44KT44CC5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go?=
- =?utf-8?b?44Gv44Gn44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBp?=
- =?utf-8?b?c3QgZGFzIE51bnN0dWNrIGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWlo?=
- =?utf-8?b?ZXJodW5kIGRhcyBPZGVyIGRpZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI0=?=
- =?utf-8?b?44Go6KiA44Gj44Gm44GE44G+44GZ44CC?=""")
- def test_long_header_encode(self):
- eq = self.ndiffAssertEqual
- h = Header('wasnipoop; giraffes="very-long-necked-animals"; '
- 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"',
- header_name='X-Foobar-Spoink-Defrobnit')
- eq(h.encode(), '''\
- wasnipoop; giraffes="very-long-necked-animals";
- spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''')
- def test_long_header_encode_with_tab_continuation_is_just_a_hint(self):
- eq = self.ndiffAssertEqual
- h = Header('wasnipoop; giraffes="very-long-necked-animals"; '
- 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"',
- header_name='X-Foobar-Spoink-Defrobnit',
- continuation_ws='\t')
- eq(h.encode(), '''\
- wasnipoop; giraffes="very-long-necked-animals";
- spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''')
- def test_long_header_encode_with_tab_continuation(self):
- eq = self.ndiffAssertEqual
- h = Header('wasnipoop; giraffes="very-long-necked-animals";\t'
- 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"',
- header_name='X-Foobar-Spoink-Defrobnit',
- continuation_ws='\t')
- eq(h.encode(), '''\
- wasnipoop; giraffes="very-long-necked-animals";
- \tspooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''')
- def test_header_encode_with_different_output_charset(self):
- h = Header('文', 'euc-jp')
- self.assertEqual(h.encode(), "=?iso-2022-jp?b?GyRCSjgbKEI=?=")
- def test_long_header_encode_with_different_output_charset(self):
- h = Header(b'test-ja \xa4\xd8\xc5\xea\xb9\xc6\xa4\xb5\xa4\xec\xa4'
- b'\xbf\xa5\xe1\xa1\xbc\xa5\xeb\xa4\xcf\xbb\xca\xb2\xf1\xbc\xd4'
- b'\xa4\xce\xbe\xb5\xc7\xa7\xa4\xf2\xc2\xd4\xa4\xc3\xa4\xc6\xa4'
- b'\xa4\xa4\xde\xa4\xb9'.decode('euc-jp'), 'euc-jp')
- res = """\
- =?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKMnE8VCROPjUbKEI=?=
- =?iso-2022-jp?b?GyRCRyckckJUJEMkRiQkJF4kORsoQg==?="""
- self.assertEqual(h.encode(), res)
- def test_header_splitter(self):
- eq = self.ndiffAssertEqual
- msg = MIMEText('')
- # It'd be great if we could use add_header() here, but that doesn't
- # guarantee an order of the parameters.
- msg['X-Foobar-Spoink-Defrobnit'] = (
- 'wasnipoop; giraffes="very-long-necked-animals"; '
- 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"')
- sfp = StringIO()
- g = Generator(sfp)
- g.flatten(msg)
- eq(sfp.getvalue(), '''\
- Content-Type: text/plain; charset="us-ascii"
- MIME-Version: 1.0
- Content-Transfer-Encoding: 7bit
- X-Foobar-Spoink-Defrobnit: wasnipoop; giraffes="very-long-necked-animals";
- spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"
- ''')
- def test_no_semis_header_splitter(self):
- eq = self.ndiffAssertEqual
- msg = Message()
- msg['From'] = 'test@dom.ain'
- msg['References'] = SPACE.join('<%d@dom.ain>' % i for i in range(10))
- msg.set_payload('Test')
- sfp = StringIO()
- g = Generator(sfp)
- g.flatten(msg)
- eq(sfp.getvalue(), """\
- From: test@dom.ain
- References: <0@dom.ain> <1@dom.ain> <2@dom.ain> <3@dom.ain> <4@dom.ain>
- <5@dom.ain> <6@dom.ain> <7@dom.ain> <8@dom.ain> <9@dom.ain>
- Test""")
- def test_last_split_chunk_does_not_fit(self):
- eq = self.ndiffAssertEqual
- h = Header('Subject: the first part of this is short, but_the_second'
- '_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line'
- '_all_by_itself')
- eq(h.encode(), """\
- Subject: the first part of this is short,
- but_the_second_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself""")
- def test_splittable_leading_char_followed_by_overlong_unsplitable(self):
- eq = self.ndiffAssertEqual
- h = Header(', but_the_second'
- '_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line'
- '_all_by_itself')
- eq(h.encode(), """\
- ,
- but_the_second_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself""")
- def test_multiple_splittable_leading_char_followed_by_overlong_unsplitable(self):
- eq = self.ndiffAssertEqual
- h = Header(', , but_the_second'
- '_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line'
- '_all_by_itself')
- eq(h.encode(), """\
- , ,
- but_the_second_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself""")
- def test_trailing_splitable_on_overlong_unsplitable(self):
- eq = self.ndiffAssertEqual
- h = Header('this_part_does_not_fit_within_maxlinelen_and_thus_should_'
- 'be_on_a_line_all_by_itself;')
- eq(h.encode(), "this_part_does_not_fit_within_maxlinelen_and_thus_should_"
- "be_on_a_line_all_by_itself;")
- def test_trailing_splitable_on_overlong_unsplitable_with_leading_splitable(self):
- eq = self.ndiffAssertEqual
- h = Header('; '
- 'this_part_does_not_fit_within_maxlinelen_and_thus_should_'
- 'be_on_a_line_all_by_itself; ')
- eq(h.encode(), """\
- ;
- this_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself; """)
- def test_long_header_with_multiple_sequential_split_chars(self):
- eq = self.ndiffAssertEqual
- h = Header('This is a long line that has two whitespaces in a row. '
- 'This used to cause truncation of the header when folded')
- eq(h.encode(), """\
- This is a long line that has two whitespaces in a row. This used to cause
- truncation of the header when folded""")
- def test_splitter_split_on_punctuation_only_if_fws_with_header(self):
- eq = self.ndiffAssertEqual
- h = Header('thisverylongheaderhas;semicolons;and,commas,but'
- 'they;arenotlegal;fold,points')
- eq(h.encode(), "thisverylongheaderhas;semicolons;and,commas,butthey;"
- "arenotlegal;fold,points")
- def test_leading_splittable_in_the_middle_just_before_overlong_last_part(self):
- eq = self.ndiffAssertEqual
- h = Header('this is a test where we need to have more than one line '
- 'before; our final line that is just too big to fit;; '
- 'this_part_does_not_fit_within_maxlinelen_and_thus_should_'
- 'be_on_a_line_all_by_itself;')
- eq(h.encode(), """\
- this is a test where we need to have more than one line before;
- our final line that is just too big to fit;;
- this_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself;""")
- def test_overlong_last_part_followed_by_split_point(self):
- eq = self.ndiffAssertEqual
- h = Header('this_part_does_not_fit_within_maxlinelen_and_thus_should_'
- 'be_on_a_line_all_by_itself ')
- eq(h.encode(), "this_part_does_not_fit_within_maxlinelen_and_thus_"
- "should_be_on_a_line_all_by_itself ")
- def test_multiline_with_overlong_parts_separated_by_two_split_points(self):
- eq = self.ndiffAssertEqual
- h = Header('this_is_a__test_where_we_need_to_have_more_than_one_line_'
- 'before_our_final_line_; ; '
- 'this_part_does_not_fit_within_maxlinelen_and_thus_should_'
- 'be_on_a_line_all_by_itself; ')
- eq(h.encode(), """\
- this_is_a__test_where_we_need_to_have_more_than_one_line_before_our_final_line_;
- ;
- this_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself; """)
- def test_multiline_with_overlong_last_part_followed_by_split_point(self):
- eq = self.ndiffAssertEqual
- h = Header('this is a test where we need to have more than one line '
- 'before our final line; ; '
- 'this_part_does_not_fit_within_maxlinelen_and_thus_should_'
- 'be_on_a_line_all_by_itself; ')
- eq(h.encode(), """\
- this is a test where we need to have more than one line before our final line;
- ;
- this_part_does_not_fit_within_maxlinelen_and_thus_should_be_on_a_line_all_by_itself; """)
- def test_long_header_with_whitespace_runs(self):
- eq = self.ndiffAssertEqual
- msg = Message()
- msg['From'] = 'test@dom.ain'
- msg['References'] = SPACE.join(['<foo@dom.ain> '] * 10)
- msg.set_payload('Test')
- sfp = StringIO()
- g = Generator(sfp)
- g.flatten(msg)
- eq(sfp.getvalue(), """\
- From: test@dom.ain
- References: <foo@dom.ain> <foo@dom.ain> <foo@dom.ain> <foo@dom.ain>
- <foo@dom.ain> <foo@dom.ain> <foo@dom.ain> <foo@dom.ain>
- <foo@dom.ain> <foo@dom.ain>\x20\x20
- Test""")
- def test_long_run_with_semi_header_splitter(self):
- eq = self.ndiffAssertEqual
- msg = Message()
- msg['From'] = 'test@dom.ain'
- msg['References'] = SPACE.join(['<foo@dom.ain>'] * 10) + '; abc'
- msg.set_payload('Test')
- sfp = StringIO()
- g = Generator(sfp)
- g.flatten(msg)
- eq(sfp.getvalue(), """\
- From: test@dom.ain
- References: <foo@dom.ain> <foo@dom.ain> <foo@dom.ain> <foo@dom.ain>
- <foo@dom.ain> <foo@dom.ain> <foo@dom.ain> <foo@dom.ain> <foo@dom.ain>
- <foo@dom.ain>; abc
- Test""")
- def test_splitter_split_on_punctuation_only_if_fws(self):
- eq = self.ndiffAssertEqual
- msg = Message()
- msg['From'] = 'test@dom.ain'
- msg['References'] = ('thisverylongheaderhas;semicolons;and,commas,but'
- 'they;arenotlegal;fold,points')
- msg.set_payload('Test')
- sfp = StringIO()
- g = Generator(sfp)
- g.flatten(msg)
- # XXX the space after the header should not be there.
- eq(sfp.getvalue(), """\
- From: test@dom.ain
- References:\x20
- thisverylongheaderhas;semicolons;and,commas,butthey;arenotlegal;fold,points
- Test""")
- def test_no_split_long_header(self):
- eq = self.ndiffAssertEqual
- hstr = 'References: ' + 'x' * 80
- h = Header(hstr)
- # These come on two lines because Headers are really field value
- # classes and don't really know about their field names.
- eq(h.encode(), """\
- References:
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx""")
- h = Header('x' * 80)
- eq(h.encode(), 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
- def test_splitting_multiple_long_lines(self):
- eq = self.ndiffAssertEqual
- hstr = """\
- from babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <mailman-admin@babylon.socal-raves.org>; Sat, 2 Feb 2002 17:00:06 -0800 (PST)
- \tfrom babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <mailman-admin@babylon.socal-raves.org>; Sat, 2 Feb 2002 17:00:06 -0800 (PST)
- \tfrom babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <mailman-admin@babylon.socal-raves.org>; Sat, 2 Feb 2002 17:00:06 -0800 (PST)
- """
- h = Header(hstr, continuation_ws='\t')
- eq(h.encode(), """\
- from babylon.socal-raves.org (localhost [127.0.0.1]);
- by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;
- for <mailman-admin@babylon.socal-raves.org>;
- Sat, 2 Feb 2002 17:00:06 -0800 (PST)
- \tfrom babylon.socal-raves.org (localhost [127.0.0.1]);
- by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;
- for <mailman-admin@babylon.socal-raves.org>;
- Sat, 2 Feb 2002 17:00:06 -0800 (PST)
- \tfrom babylon.socal-raves.org (localhost [127.0.0.1]);
- by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;
- for <mailman-admin@babylon.socal-raves.org>;
- Sat, 2 Feb 2002 17:00:06 -0800 (PST)""")
- def test_splitting_first_line_only_is_long(self):
- eq = self.ndiffAssertEqual
- hstr = """\
- from modemcable093.139-201-24.que.mc.videotron.ca ([24.201.139.93] helo=cthulhu.gerg.ca)
- \tby kronos.mems-exchange.org with esmtp (Exim 4.05)
- \tid 17k4h5-00034i-00
- \tfor test@mems-exchange.org; Wed, 28 Aug 2002 11:25:20 -0400"""
- h = Header(hstr, maxlinelen=78, header_name='Receiv