Home | History | Annotate | Download | only in test
      1 # Copyright (C) 2002-2006 Python Software Foundation
      2 # Contact: email-sig (at] python.org
      3 # email package unit tests for (optional) Asian codecs
      4 
      5 import unittest
      6 from test.test_support import 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 unittest.SkipTest
     19 
     20 
     21 
     23 class TestEmailAsianCodecs(TestEmailBase):
     24     def test_japanese_codecs(self):
     25         eq = self.ndiffAssertEqual
     26         j = Charset("euc-jp")
     27         g = Charset("iso-8859-1")
     28         h = Header("Hello World!")
     29         jhello = '\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc\xa5\xeb\xa5\xc9\xa1\xaa'
     30         ghello = 'Gr\xfc\xdf Gott!'
     31         h.append(jhello, j)
     32         h.append(ghello, g)
     33         # BAW: This used to -- and maybe should -- fold the two iso-8859-1
     34         # chunks into a single encoded word.  However it doesn't violate the
     35         # standard to have them as two encoded chunks and maybe it's
     36         # reasonable <wink> for each .append() call to result in a separate
     37         # encoded word.
     38         eq(h.encode(), """\
     39 Hello World! =?iso-2022-jp?b?GyRCJU8lbSE8JW8hPCVrJUkhKhsoQg==?=
     40  =?iso-8859-1?q?Gr=FC=DF?= =?iso-8859-1?q?_Gott!?=""")
     41         eq(decode_header(h.encode()),
     42            [('Hello World!', None),
     43             ('\x1b$B%O%m!<%o!<%k%I!*\x1b(B', 'iso-2022-jp'),
     44             ('Gr\xfc\xdf Gott!', 'iso-8859-1')])
     45         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'
     46         h = Header(long, j, header_name="Subject")
     47         # test a very long header
     48         enc = h.encode()
     49         # TK: splitting point may differ by codec design and/or Header encoding
     50         eq(enc , """\
     51 =?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKGyhC?=
     52  =?iso-2022-jp?b?GyRCMnE8VCROPjVHJyRyQlQkQyRGJCQkXiQ5GyhC?=""")
     53         # TK: full decode comparison
     54         eq(h.__unicode__().encode('euc-jp'), long)
     55 
     56     def test_payload_encoding(self):
     57         jhello = '\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc\xa5\xeb\xa5\xc9\xa1\xaa'
     58         jcode  = 'euc-jp'
     59         msg = Message()
     60         msg.set_payload(jhello, jcode)
     61         ustr = unicode(msg.get_payload(), msg.get_content_charset())
     62         self.assertEqual(jhello, ustr.encode(jcode))
     63 
     64 
     65 
     67 def suite():
     68     suite = unittest.TestSuite()
     69     suite.addTest(unittest.makeSuite(TestEmailAsianCodecs))
     70     return suite
     71 
     72 
     73 def test_main():
     74     run_unittest(TestEmailAsianCodecs)
     75 
     76 
     77 
     79 if __name__ == '__main__':
     80     unittest.main(defaultTest='suite')
     81