diff --git a/pyas2lib/as2.py b/pyas2lib/as2.py index ff84f56..4a42abc 100644 --- a/pyas2lib/as2.py +++ b/pyas2lib/as2.py @@ -381,7 +381,11 @@ def build( self.payload = email_message.Message() self.payload.set_payload(data) self.payload.set_type(content_type) - encoders.encode_7or8bit(self.payload) + + if content_type.lower().startswith("application/octet-stream"): + self.payload["Content-Transfer-Encoding"] = "binary" + else: + encoders.encode_7or8bit(self.payload) if filename: self.payload.add_header( diff --git a/pyas2lib/tests/test_utils.py b/pyas2lib/tests/test_utils.py index 23b31d2..72a9e7a 100644 --- a/pyas2lib/tests/test_utils.py +++ b/pyas2lib/tests/test_utils.py @@ -15,8 +15,10 @@ def test_quoting(): assert utils.quote_as2name("PYAS2 LIB") == '"PYAS2 LIB"' -def test_bytes_generator(): - """Test the email bytes generator class.""" +def test_mime_to_bytes_empty_message(): + """ + It will generate the headers with an empty body + """ message = Message() message.set_type("application/pkcs7-mime") assert ( @@ -25,6 +27,67 @@ def test_bytes_generator(): ) +def test_mime_to_bytes_unix_text(): + """ + For non-binary content types, + it converts unix new lines to network newlines. + """ + message = Message() + message.set_type("application/xml") + message.set_payload("Some line.\nAnother line.") + + result = utils.mime_to_bytes(message) + + assert ( + b"MIME-Version: 1.0\r\n" + b"Content-Type: application/xml\r\n" + b"\r\n" + b"Some line.\r\n" + b"Another line." + ) == result + + +def test_mime_to_bytes_octet_stream(): + """ + For binary octet-stream content types, + it does not converts unix new lines to network newlines. + """ + message = Message() + message.set_type("application/octet-stream") + message.set_payload("Some line.\nAnother line.\n") + + result = utils.mime_to_bytes(message) + + assert ( + b"MIME-Version: 1.0\r\n" + b"Content-Type: application/octet-stream\r\n" + b"\r\n" + b"Some line.\n" + b"Another line.\n" + ) == result + + +def test_mime_to_bytes_binary(): + """ + It makes no conversion for binary content encoding. + """ + message = Message() + message.set_type("any/type") + message["Content-Transfer-Encoding"] = "binary" + message.set_payload("Some line.\nAnother line.\n") + + result = utils.mime_to_bytes(message) + + assert ( + b"MIME-Version: 1.0\r\n" + b"Content-Type: any/type\r\n" + b"Content-Transfer-Encoding: binary\r\n" + b"\r\n" + b"Some line.\n" + b"Another line.\n" + ) == result + + def test_make_boundary(): """Test the function for creating a boundary for multipart messages.""" assert utils.make_mime_boundary(text="123456") is not None diff --git a/pyas2lib/utils.py b/pyas2lib/utils.py index 200294c..dd2bbe6 100644 --- a/pyas2lib/utils.py +++ b/pyas2lib/utils.py @@ -45,9 +45,9 @@ def _handle_text(self, msg): Handle writing the binary messages to prevent default behaviour of newline replacements. """ - if msg.get_content_type() == "application/octet-stream" or ( - msg.get("Content-Transfer-Encoding") == "binary" - and msg.get_content_subtype() in ["pkcs7-mime", "pkcs7-signature",] + if ( + msg.get_content_type() == "application/octet-stream" + or msg.get("Content-Transfer-Encoding") == "binary" ): payload = msg.get_payload(decode=True) if payload is None: