From a30b5e75774830f076846ab0812b8aeff795aeba Mon Sep 17 00:00:00 2001 From: "Piotr F. Mieszkowski" Date: Fri, 19 May 2023 20:30:00 +0200 Subject: [PATCH] Handle missing Content-Type properly - ContentManager sets default Content-Type even if it was missing in the original message. - Make sure that when Content-Type is missing, copying parameters doesn't raise an error. - Add a unit-test to check that. --- lacre/core.py | 2 +- test/modules/test_lacre_core.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lacre/core.py b/lacre/core.py index 31742a0..06cee5a 100644 --- a/lacre/core.py +++ b/lacre/core.py @@ -209,7 +209,7 @@ def _rewrap_payload(message: EmailMessage) -> MIMEPart: wrapper.set_type(message.get_content_type()) # Copy all Content-Type parameters. - for (pname, pvalue) in message.get_params(): + for (pname, pvalue) in message.get_params(failobj=list()): # Skip MIME type that's also returned by get_params(). if not '/' in pname: wrapper.set_param(pname, pvalue) diff --git a/test/modules/test_lacre_core.py b/test/modules/test_lacre_core.py index 5591308..49897b7 100644 --- a/test/modules/test_lacre_core.py +++ b/test/modules/test_lacre_core.py @@ -40,3 +40,17 @@ class LacreCoreTest(unittest.TestCase): 'only content and content-type should be copied') self.assertEqual(rewrapped.get_content_type(), 'text/plain', 'rewrapped part should have initial message\'s content-type') + + def test_payload_wrapping_wo_content_type(self): + m = EmailMessage() + m.set_payload('This is a payload.\r\n' + + '\r\n' + + 'It has two paragraphs.\r\n') + m['Subject'] = 'Source message' + + rewrapped = lacre.core._rewrap_payload(m) + + self.assertFalse('Subject' in rewrapped, + 'only content and content-type should be copied') + self.assertEqual(rewrapped.get_content_type(), 'text/plain', + 'rewrapped part should have initial message\'s content-type')