MCPcopy
hub / github.com/django/django / test_attach_text_as_bytes

Method test_attach_text_as_bytes

tests/mail/tests.py:1034–1049  ·  view source on GitHub ↗

For text/* attachments, EmailMessage.attach() decodes bytes as UTF-8 if possible and changes to DEFAULT_ATTACHMENT_MIME_TYPE if not.

(self)

Source from the content-addressed store, hash-verified

1032 self.assertEqual(actual.mimetype, expected_mimetype)
1033
1034 def test_attach_text_as_bytes(self):
1035 """
1036 For text/* attachments, EmailMessage.attach() decodes bytes as UTF-8
1037 if possible and changes to DEFAULT_ATTACHMENT_MIME_TYPE if not.
1038 """
1039 email = EmailMessage()
1040 # Mimetype guessing identifies these as text/plain from the .txt
1041 # extensions.
1042 email.attach("utf8.txt", "ütƒ-8\n".encode())
1043 email.attach("not-utf8.txt", b"\x86unknown-encoding\n")
1044 attachments = self.get_decoded_attachments(email)
1045 self.assertEqual(attachments[0], ("utf8.txt", "ütƒ-8\n", "text/plain"))
1046 self.assertEqual(
1047 attachments[1],
1048 ("not-utf8.txt", b"\x86unknown-encoding\n", "application/octet-stream"),
1049 )
1050
1051 def test_attach_text_as_bytes_using_property(self):
1052 """

Callers

nothing calls this directly

Calls 4

attachMethod · 0.95
EmailMessageClass · 0.90
encodeMethod · 0.45

Tested by

no test coverage detected