diff options
author | Mark Sapiro <mark@msapiro.net> | 2020-10-19 22:49:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-19 22:49:19 (GMT) |
commit | bf838227c35212709dc43b3c3c57f8e1655c1d24 (patch) | |
tree | 31a22425e9887d5a9451f4a5f49299fdd1b2ef13 /Lib/test | |
parent | 1438c2ac773e87d1f9c97fc22f2346e16bf48773 (diff) | |
download | cpython-bf838227c35212709dc43b3c3c57f8e1655c1d24.zip cpython-bf838227c35212709dc43b3c3c57f8e1655c1d24.tar.gz cpython-bf838227c35212709dc43b3c3c57f8e1655c1d24.tar.bz2 |
bpo-27321 Fix email.generator.py to not replace a non-existent header. (GH-18074)
This PR replaces #1977. The reason for the replacement is two-fold.
The fix itself is different is that if the CTE header doesn't exist in the original message, it is inserted. This is important because the new CTE could be quoted-printable whereas the original is implicit 8bit.
Also the tests are different. The test_nonascii_as_string_without_cte test in #1977 doesn't actually test the issue in that it passes without the fix. The test_nonascii_as_string_without_content_type_and_cte test is improved here, and even though it doesn't fail without the fix, it is included for completeness.
Automerge-Triggered-By: @warsaw
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_email/test_email.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index ba4ed69..044b938 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -312,6 +312,41 @@ class TestMessageAPI(TestEmailBase): g.flatten(msg) self.assertEqual(fullrepr, s.getvalue()) + def test_nonascii_as_string_without_cte(self): + m = textwrap.dedent("""\ + MIME-Version: 1.0 + Content-type: text/plain; charset="iso-8859-1" + + Test if non-ascii messages with no Content-Transfer-Encoding set + can be as_string'd: + Föö bär + """) + source = m.encode('iso-8859-1') + expected = textwrap.dedent("""\ + MIME-Version: 1.0 + Content-type: text/plain; charset="iso-8859-1" + Content-Transfer-Encoding: quoted-printable + + Test if non-ascii messages with no Content-Transfer-Encoding set + can be as_string'd: + F=F6=F6 b=E4r + """) + msg = email.message_from_bytes(source) + self.assertEqual(msg.as_string(), expected) + + def test_nonascii_as_string_without_content_type_and_cte(self): + m = textwrap.dedent("""\ + MIME-Version: 1.0 + + Test if non-ascii messages with no Content-Type nor + Content-Transfer-Encoding set can be as_string'd: + Föö bär + """) + source = m.encode('iso-8859-1') + expected = source.decode('ascii', 'replace') + msg = email.message_from_bytes(source) + self.assertEqual(msg.as_string(), expected) + def test_as_bytes(self): msg = self._msgobj('msg_01.txt') with openfile('msg_01.txt') as fp: |