ยปCore Development>Code coverage>Lib/test/test_email/test_parser.py

Python code coverage for Lib/test/test_email/test_parser.py

#countcontent
1n/aimport io
2n/aimport email
3n/aimport unittest
4n/afrom email.message import Message, EmailMessage
5n/afrom email.policy import default
6n/afrom test.test_email import TestEmailBase
7n/a
8n/a
9n/aclass TestCustomMessage(TestEmailBase):
10n/a
11n/a class MyMessage(Message):
12n/a def __init__(self, policy):
13n/a self.check_policy = policy
14n/a super().__init__()
15n/a
16n/a MyPolicy = TestEmailBase.policy.clone(linesep='boo')
17n/a
18n/a def test_custom_message_gets_policy_if_possible_from_string(self):
19n/a msg = email.message_from_string("Subject: bogus\n\nmsg\n",
20n/a self.MyMessage,
21n/a policy=self.MyPolicy)
22n/a self.assertIsInstance(msg, self.MyMessage)
23n/a self.assertIs(msg.check_policy, self.MyPolicy)
24n/a
25n/a def test_custom_message_gets_policy_if_possible_from_file(self):
26n/a source_file = io.StringIO("Subject: bogus\n\nmsg\n")
27n/a msg = email.message_from_file(source_file,
28n/a self.MyMessage,
29n/a policy=self.MyPolicy)
30n/a self.assertIsInstance(msg, self.MyMessage)
31n/a self.assertIs(msg.check_policy, self.MyPolicy)
32n/a
33n/a # XXX add tests for other functions that take Message arg.
34n/a
35n/a
36n/aclass TestParserBase:
37n/a
38n/a def test_only_split_on_cr_lf(self):
39n/a # The unicode line splitter splits on unicode linebreaks, which are
40n/a # more numerous than allowed by the email RFCs; make sure we are only
41n/a # splitting on those two.
42n/a for parser in self.parsers:
43n/a with self.subTest(parser=parser.__name__):
44n/a msg = parser(
45n/a "Next-Line: not\x85broken\r\n"
46n/a "Null: not\x00broken\r\n"
47n/a "Vertical-Tab: not\vbroken\r\n"
48n/a "Form-Feed: not\fbroken\r\n"
49n/a "File-Separator: not\x1Cbroken\r\n"
50n/a "Group-Separator: not\x1Dbroken\r\n"
51n/a "Record-Separator: not\x1Ebroken\r\n"
52n/a "Line-Separator: not\u2028broken\r\n"
53n/a "Paragraph-Separator: not\u2029broken\r\n"
54n/a "\r\n",
55n/a policy=default,
56n/a )
57n/a self.assertEqual(msg.items(), [
58n/a ("Next-Line", "not\x85broken"),
59n/a ("Null", "not\x00broken"),
60n/a ("Vertical-Tab", "not\vbroken"),
61n/a ("Form-Feed", "not\fbroken"),
62n/a ("File-Separator", "not\x1Cbroken"),
63n/a ("Group-Separator", "not\x1Dbroken"),
64n/a ("Record-Separator", "not\x1Ebroken"),
65n/a ("Line-Separator", "not\u2028broken"),
66n/a ("Paragraph-Separator", "not\u2029broken"),
67n/a ])
68n/a self.assertEqual(msg.get_payload(), "")
69n/a
70n/a class MyMessage(EmailMessage):
71n/a pass
72n/a
73n/a def test_custom_message_factory_on_policy(self):
74n/a for parser in self.parsers:
75n/a with self.subTest(parser=parser.__name__):
76n/a MyPolicy = default.clone(message_factory=self.MyMessage)
77n/a msg = parser("To: foo\n\ntest", policy=MyPolicy)
78n/a self.assertIsInstance(msg, self.MyMessage)
79n/a
80n/a def test_factory_arg_overrides_policy(self):
81n/a for parser in self.parsers:
82n/a with self.subTest(parser=parser.__name__):
83n/a MyPolicy = default.clone(message_factory=self.MyMessage)
84n/a msg = parser("To: foo\n\ntest", Message, policy=MyPolicy)
85n/a self.assertNotIsInstance(msg, self.MyMessage)
86n/a self.assertIsInstance(msg, Message)
87n/a
88n/a# Play some games to get nice output in subTest. This code could be clearer
89n/a# if staticmethod supported __name__.
90n/a
91n/adef message_from_file(s, *args, **kw):
92n/a f = io.StringIO(s)
93n/a return email.message_from_file(f, *args, **kw)
94n/a
95n/aclass TestParser(TestParserBase, TestEmailBase):
96n/a parsers = (email.message_from_string, message_from_file)
97n/a
98n/adef message_from_bytes(s, *args, **kw):
99n/a return email.message_from_bytes(s.encode(), *args, **kw)
100n/a
101n/adef message_from_binary_file(s, *args, **kw):
102n/a f = io.BytesIO(s.encode())
103n/a return email.message_from_binary_file(f, *args, **kw)
104n/a
105n/aclass TestBytesParser(TestParserBase, TestEmailBase):
106n/a parsers = (message_from_bytes, message_from_binary_file)
107n/a
108n/a
109n/aif __name__ == '__main__':
110n/a unittest.main()