|
1 | 1 | import io |
2 | 2 | import email |
3 | 3 | import unittest |
4 | | -from email.message import Message |
| 4 | +from email.message import Message, EmailMessage |
5 | 5 | from email.policy import default |
6 | 6 | from test.test_email import TestEmailBase |
7 | 7 |
|
@@ -39,38 +39,71 @@ def test_only_split_on_cr_lf(self): |
39 | 39 | # The unicode line splitter splits on unicode linebreaks, which are |
40 | 40 | # more numerous than allowed by the email RFCs; make sure we are only |
41 | 41 | # splitting on those two. |
42 | | - msg = self.parser( |
43 | | - "Next-Line: not\x85broken\r\n" |
44 | | - "Null: not\x00broken\r\n" |
45 | | - "Vertical-Tab: not\vbroken\r\n" |
46 | | - "Form-Feed: not\fbroken\r\n" |
47 | | - "File-Separator: not\x1Cbroken\r\n" |
48 | | - "Group-Separator: not\x1Dbroken\r\n" |
49 | | - "Record-Separator: not\x1Ebroken\r\n" |
50 | | - "Line-Separator: not\u2028broken\r\n" |
51 | | - "Paragraph-Separator: not\u2029broken\r\n" |
52 | | - "\r\n", |
53 | | - policy=default, |
54 | | - ) |
55 | | - self.assertEqual(msg.items(), [ |
56 | | - ("Next-Line", "not\x85broken"), |
57 | | - ("Null", "not\x00broken"), |
58 | | - ("Vertical-Tab", "not\vbroken"), |
59 | | - ("Form-Feed", "not\fbroken"), |
60 | | - ("File-Separator", "not\x1Cbroken"), |
61 | | - ("Group-Separator", "not\x1Dbroken"), |
62 | | - ("Record-Separator", "not\x1Ebroken"), |
63 | | - ("Line-Separator", "not\u2028broken"), |
64 | | - ("Paragraph-Separator", "not\u2029broken"), |
65 | | - ]) |
66 | | - self.assertEqual(msg.get_payload(), "") |
| 42 | + for parser in self.parsers: |
| 43 | + with self.subTest(parser=parser.__name__): |
| 44 | + msg = parser( |
| 45 | + "Next-Line: not\x85broken\r\n" |
| 46 | + "Null: not\x00broken\r\n" |
| 47 | + "Vertical-Tab: not\vbroken\r\n" |
| 48 | + "Form-Feed: not\fbroken\r\n" |
| 49 | + "File-Separator: not\x1Cbroken\r\n" |
| 50 | + "Group-Separator: not\x1Dbroken\r\n" |
| 51 | + "Record-Separator: not\x1Ebroken\r\n" |
| 52 | + "Line-Separator: not\u2028broken\r\n" |
| 53 | + "Paragraph-Separator: not\u2029broken\r\n" |
| 54 | + "\r\n", |
| 55 | + policy=default, |
| 56 | + ) |
| 57 | + self.assertEqual(msg.items(), [ |
| 58 | + ("Next-Line", "not\x85broken"), |
| 59 | + ("Null", "not\x00broken"), |
| 60 | + ("Vertical-Tab", "not\vbroken"), |
| 61 | + ("Form-Feed", "not\fbroken"), |
| 62 | + ("File-Separator", "not\x1Cbroken"), |
| 63 | + ("Group-Separator", "not\x1Dbroken"), |
| 64 | + ("Record-Separator", "not\x1Ebroken"), |
| 65 | + ("Line-Separator", "not\u2028broken"), |
| 66 | + ("Paragraph-Separator", "not\u2029broken"), |
| 67 | + ]) |
| 68 | + self.assertEqual(msg.get_payload(), "") |
| 69 | + |
| 70 | + class MyMessage(EmailMessage): |
| 71 | + pass |
| 72 | + |
| 73 | + def test_custom_message_factory_on_policy(self): |
| 74 | + for parser in self.parsers: |
| 75 | + with self.subTest(parser=parser.__name__): |
| 76 | + MyPolicy = default.clone(message_factory=self.MyMessage) |
| 77 | + msg = parser("To: foo\n\ntest", policy=MyPolicy) |
| 78 | + self.assertIsInstance(msg, self.MyMessage) |
| 79 | + |
| 80 | + def test_factory_arg_overrides_policy(self): |
| 81 | + for parser in self.parsers: |
| 82 | + with self.subTest(parser=parser.__name__): |
| 83 | + MyPolicy = default.clone(message_factory=self.MyMessage) |
| 84 | + msg = parser("To: foo\n\ntest", Message, policy=MyPolicy) |
| 85 | + self.assertNotIsInstance(msg, self.MyMessage) |
| 86 | + self.assertIsInstance(msg, Message) |
| 87 | + |
| 88 | +# Play some games to get nice output in subTest. This code could be clearer |
| 89 | +# if staticmethod supported __name__. |
| 90 | + |
| 91 | +def message_from_file(s, *args, **kw): |
| 92 | + f = io.StringIO(s) |
| 93 | + return email.message_from_file(f, *args, **kw) |
67 | 94 |
|
68 | 95 | class TestParser(TestParserBase, TestEmailBase): |
69 | | - parser = staticmethod(email.message_from_string) |
| 96 | + parsers = (email.message_from_string, message_from_file) |
| 97 | + |
| 98 | +def message_from_bytes(s, *args, **kw): |
| 99 | + return email.message_from_bytes(s.encode(), *args, **kw) |
| 100 | + |
| 101 | +def message_from_binary_file(s, *args, **kw): |
| 102 | + f = io.BytesIO(s.encode()) |
| 103 | + return email.message_from_binary_file(f, *args, **kw) |
70 | 104 |
|
71 | 105 | class TestBytesParser(TestParserBase, TestEmailBase): |
72 | | - def parser(self, s, *args, **kw): |
73 | | - return email.message_from_bytes(s.encode(), *args, **kw) |
| 106 | + parsers = (message_from_bytes, message_from_binary_file) |
74 | 107 |
|
75 | 108 |
|
76 | 109 | if __name__ == '__main__': |
|
0 commit comments