Skip to content

Commit

Permalink
Merge pull request #25 from abhishek-ram/chevah-fix-binary-messages
Browse files Browse the repository at this point in the history
Add support for binary messages
  • Loading branch information
abhishek-ram authored Nov 1, 2020
2 parents 8e811cb + 9f5d27c commit 0a33440
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
6 changes: 5 additions & 1 deletion pyas2lib/as2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
67 changes: 65 additions & 2 deletions pyas2lib/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pyas2lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0a33440

Please sign in to comment.