|
3 | 3 | import unittest |
4 | 4 | from email import errors |
5 | 5 | from email import policy |
| 6 | +from email import utils |
| 7 | +from email import message_from_string |
6 | 8 | from email.message import Message |
7 | 9 | from test.test_email import TestEmailBase, parameterize |
8 | 10 | from email import headerregistry |
@@ -1688,6 +1690,59 @@ def test_group_comparison(self): |
1688 | 1690 | self.assertTrue(g == ALWAYS_EQ) |
1689 | 1691 |
|
1690 | 1692 |
|
| 1693 | +class TestReferencesHeader(TestHeaderBase): |
| 1694 | + |
| 1695 | + def test_references_message_ids(self): |
| 1696 | + h = self.make_header('References', |
| 1697 | + '<a@example.com> (the first) <b@example.com>') |
| 1698 | + self.assertEqual(h.message_ids, ('<a@example.com>', '<b@example.com>')) |
| 1699 | + self.assertDefectsEqual(h.defects, []) |
| 1700 | + |
| 1701 | + def test_in_reply_to_message_ids(self): |
| 1702 | + h = self.make_header('In-Reply-To', ' <a@example.com> ') |
| 1703 | + self.assertEqual(h.message_ids, ('<a@example.com>',)) |
| 1704 | + |
| 1705 | + def test_message_ids_match_make_msgid(self): |
| 1706 | + msgid = utils.make_msgid() |
| 1707 | + h = self.make_header('In-Reply-To', msgid) |
| 1708 | + self.assertEqual(h.message_ids, (msgid,)) |
| 1709 | + |
| 1710 | + def test_message_ids_id_right_forms(self): |
| 1711 | + h = self.make_header('References', |
| 1712 | + '<a@[127.0.0.1]> <b@xn--fa-hia.example>') |
| 1713 | + self.assertEqual(h.message_ids, |
| 1714 | + ('<a@[127.0.0.1]>', '<b@xn--fa-hia.example>')) |
| 1715 | + |
| 1716 | + def test_message_ids_comma_separated(self): |
| 1717 | + h = self.make_header('References', '<a@example.com>,<b@example.com>') |
| 1718 | + self.assertEqual(h.message_ids, ('<a@example.com>', '<b@example.com>')) |
| 1719 | + self.assertDefectsEqual(h.defects, [errors.InvalidHeaderDefect]) |
| 1720 | + |
| 1721 | + def test_message_ids_skip_invalid(self): |
| 1722 | + h = self.make_header('References', '<a@example.com> not-a-msg-id') |
| 1723 | + self.assertEqual(h.message_ids, ('<a@example.com>',)) |
| 1724 | + self.assertDefectsEqual(h.defects, [errors.InvalidHeaderDefect]) |
| 1725 | + |
| 1726 | + def test_message_ids_from_parsed_message(self): |
| 1727 | + msg = message_from_string(textwrap.dedent('''\ |
| 1728 | + In-Reply-To: <b@example.com> |
| 1729 | + References: <a@example.com> |
| 1730 | + <b@example.com> |
| 1731 | +
|
| 1732 | + body |
| 1733 | + '''), policy=policy.default) |
| 1734 | + [in_reply_to] = msg['In-Reply-To'].message_ids |
| 1735 | + *_, last_reference = msg['References'].message_ids |
| 1736 | + self.assertEqual(in_reply_to, last_reference) |
| 1737 | + self.assertEqual(msg['References'].message_ids, |
| 1738 | + ('<a@example.com>', '<b@example.com>')) |
| 1739 | + |
| 1740 | + def test_message_ids_read_only(self): |
| 1741 | + h = self.make_header('References', '<a@example.com>') |
| 1742 | + with self.assertRaises(AttributeError): |
| 1743 | + h.message_ids = () |
| 1744 | + |
| 1745 | + |
1691 | 1746 | class TestFolding(TestHeaderBase): |
1692 | 1747 |
|
1693 | 1748 | def test_address_display_names(self): |
|
0 commit comments