1 | n/a | # Copyright (C) 2002-2006 Python Software Foundation |
---|
2 | n/a | # Contact: email-sig@python.org |
---|
3 | n/a | # email package unit tests for (optional) Asian codecs |
---|
4 | n/a | |
---|
5 | n/a | import unittest |
---|
6 | n/a | |
---|
7 | n/a | from test.test_email import TestEmailBase |
---|
8 | n/a | from email.charset import Charset |
---|
9 | n/a | from email.header import Header, decode_header |
---|
10 | n/a | from email.message import Message |
---|
11 | n/a | |
---|
12 | n/a | # We're compatible with Python 2.3, but it doesn't have the built-in Asian |
---|
13 | n/a | # codecs, so we have to skip all these tests. |
---|
14 | n/a | try: |
---|
15 | n/a | str(b'foo', 'euc-jp') |
---|
16 | n/a | except LookupError: |
---|
17 | n/a | raise unittest.SkipTest |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | |
---|
21 | n/a | class TestEmailAsianCodecs(TestEmailBase): |
---|
22 | n/a | def test_japanese_codecs(self): |
---|
23 | n/a | eq = self.ndiffAssertEqual |
---|
24 | n/a | jcode = "euc-jp" |
---|
25 | n/a | gcode = "iso-8859-1" |
---|
26 | n/a | j = Charset(jcode) |
---|
27 | n/a | g = Charset(gcode) |
---|
28 | n/a | h = Header("Hello World!") |
---|
29 | n/a | jhello = str(b'\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc' |
---|
30 | n/a | b'\xa5\xeb\xa5\xc9\xa1\xaa', jcode) |
---|
31 | n/a | ghello = str(b'Gr\xfc\xdf Gott!', gcode) |
---|
32 | n/a | h.append(jhello, j) |
---|
33 | n/a | h.append(ghello, g) |
---|
34 | n/a | # BAW: This used to -- and maybe should -- fold the two iso-8859-1 |
---|
35 | n/a | # chunks into a single encoded word. However it doesn't violate the |
---|
36 | n/a | # standard to have them as two encoded chunks and maybe it's |
---|
37 | n/a | # reasonable <wink> for each .append() call to result in a separate |
---|
38 | n/a | # encoded word. |
---|
39 | n/a | eq(h.encode(), """\ |
---|
40 | n/a | Hello World! =?iso-2022-jp?b?GyRCJU8lbSE8JW8hPCVrJUkhKhsoQg==?= |
---|
41 | n/a | =?iso-8859-1?q?Gr=FC=DF_Gott!?=""") |
---|
42 | n/a | eq(decode_header(h.encode()), |
---|
43 | n/a | [(b'Hello World! ', None), |
---|
44 | n/a | (b'\x1b$B%O%m!<%o!<%k%I!*\x1b(B', 'iso-2022-jp'), |
---|
45 | n/a | (b'Gr\xfc\xdf Gott!', gcode)]) |
---|
46 | n/a | subject_bytes = (b'test-ja \xa4\xd8\xc5\xea\xb9\xc6\xa4\xb5' |
---|
47 | n/a | b'\xa4\xec\xa4\xbf\xa5\xe1\xa1\xbc\xa5\xeb\xa4\xcf\xbb\xca\xb2' |
---|
48 | n/a | b'\xf1\xbc\xd4\xa4\xce\xbe\xb5\xc7\xa7\xa4\xf2\xc2\xd4\xa4\xc3' |
---|
49 | n/a | b'\xa4\xc6\xa4\xa4\xa4\xde\xa4\xb9') |
---|
50 | n/a | subject = str(subject_bytes, jcode) |
---|
51 | n/a | h = Header(subject, j, header_name="Subject") |
---|
52 | n/a | # test a very long header |
---|
53 | n/a | enc = h.encode() |
---|
54 | n/a | # TK: splitting point may differ by codec design and/or Header encoding |
---|
55 | n/a | eq(enc , """\ |
---|
56 | n/a | =?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKGyhC?= |
---|
57 | n/a | =?iso-2022-jp?b?GyRCMnE8VCROPjVHJyRyQlQkQyRGJCQkXiQ5GyhC?=""") |
---|
58 | n/a | # TK: full decode comparison |
---|
59 | n/a | eq(str(h).encode(jcode), subject_bytes) |
---|
60 | n/a | |
---|
61 | n/a | def test_payload_encoding_utf8(self): |
---|
62 | n/a | jhello = str(b'\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc' |
---|
63 | n/a | b'\xa5\xeb\xa5\xc9\xa1\xaa', 'euc-jp') |
---|
64 | n/a | msg = Message() |
---|
65 | n/a | msg.set_payload(jhello, 'utf-8') |
---|
66 | n/a | ustr = msg.get_payload(decode=True).decode(msg.get_content_charset()) |
---|
67 | n/a | self.assertEqual(jhello, ustr) |
---|
68 | n/a | |
---|
69 | n/a | def test_payload_encoding(self): |
---|
70 | n/a | jcode = 'euc-jp' |
---|
71 | n/a | jhello = str(b'\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc' |
---|
72 | n/a | b'\xa5\xeb\xa5\xc9\xa1\xaa', jcode) |
---|
73 | n/a | msg = Message() |
---|
74 | n/a | msg.set_payload(jhello, jcode) |
---|
75 | n/a | ustr = msg.get_payload(decode=True).decode(msg.get_content_charset()) |
---|
76 | n/a | self.assertEqual(jhello, ustr) |
---|
77 | n/a | |
---|
78 | n/a | |
---|
79 | n/a | |
---|
80 | n/a | if __name__ == '__main__': |
---|
81 | n/a | unittest.main() |
---|