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