1 | n/a | """Test the parser and generator are inverses. |
---|
2 | n/a | |
---|
3 | n/a | Note that this is only strictly true if we are parsing RFC valid messages and |
---|
4 | n/a | producing RFC valid messages. |
---|
5 | n/a | """ |
---|
6 | n/a | |
---|
7 | n/a | import io |
---|
8 | n/a | import unittest |
---|
9 | n/a | from email import policy, message_from_bytes |
---|
10 | n/a | from email.message import EmailMessage |
---|
11 | n/a | from email.generator import BytesGenerator |
---|
12 | n/a | from test.test_email import TestEmailBase, parameterize |
---|
13 | n/a | |
---|
14 | n/a | # This is like textwrap.dedent for bytes, except that it uses \r\n for the line |
---|
15 | n/a | # separators on the rebuilt string. |
---|
16 | n/a | def dedent(bstr): |
---|
17 | n/a | lines = bstr.splitlines() |
---|
18 | n/a | if not lines[0].strip(): |
---|
19 | n/a | raise ValueError("First line must contain text") |
---|
20 | n/a | stripamt = len(lines[0]) - len(lines[0].lstrip()) |
---|
21 | n/a | return b'\r\n'.join( |
---|
22 | n/a | [x[stripamt:] if len(x)>=stripamt else b'' |
---|
23 | n/a | for x in lines]) |
---|
24 | n/a | |
---|
25 | n/a | |
---|
26 | n/a | @parameterize |
---|
27 | n/a | class TestInversion(TestEmailBase): |
---|
28 | n/a | |
---|
29 | n/a | policy = policy.default |
---|
30 | n/a | message = EmailMessage |
---|
31 | n/a | |
---|
32 | n/a | def msg_as_input(self, msg): |
---|
33 | n/a | m = message_from_bytes(msg, policy=policy.SMTP) |
---|
34 | n/a | b = io.BytesIO() |
---|
35 | n/a | g = BytesGenerator(b) |
---|
36 | n/a | g.flatten(m) |
---|
37 | n/a | self.assertEqual(b.getvalue(), msg) |
---|
38 | n/a | |
---|
39 | n/a | # XXX: spaces are not preserved correctly here yet in the general case. |
---|
40 | n/a | msg_params = { |
---|
41 | n/a | 'header_with_one_space_body': (dedent(b"""\ |
---|
42 | n/a | From: abc@xyz.com |
---|
43 | n/a | X-Status:\x20 |
---|
44 | n/a | Subject: test |
---|
45 | n/a | |
---|
46 | n/a | foo |
---|
47 | n/a | """),), |
---|
48 | n/a | |
---|
49 | n/a | } |
---|
50 | n/a | |
---|
51 | n/a | payload_params = { |
---|
52 | n/a | 'plain_text': dict(payload='This is a test\n'*20), |
---|
53 | n/a | 'base64_text': dict(payload=(('xy a'*40+'\n')*5), cte='base64'), |
---|
54 | n/a | 'qp_text': dict(payload=(('xy a'*40+'\n')*5), cte='quoted-printable'), |
---|
55 | n/a | } |
---|
56 | n/a | |
---|
57 | n/a | def payload_as_body(self, payload, **kw): |
---|
58 | n/a | msg = self._make_message() |
---|
59 | n/a | msg['From'] = 'foo' |
---|
60 | n/a | msg['To'] = 'bar' |
---|
61 | n/a | msg['Subject'] = 'payload round trip test' |
---|
62 | n/a | msg.set_content(payload, **kw) |
---|
63 | n/a | b = bytes(msg) |
---|
64 | n/a | msg2 = message_from_bytes(b, policy=self.policy) |
---|
65 | n/a | self.assertEqual(bytes(msg2), b) |
---|
66 | n/a | self.assertEqual(msg2.get_content(), payload) |
---|
67 | n/a | |
---|
68 | n/a | |
---|
69 | n/a | if __name__ == '__main__': |
---|
70 | n/a | unittest.main() |
---|