| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 14 |
|
| 15 |
try: |
| 16 |
unicode('foo', 'euc-jp') |
| 17 |
except LookupError: |
| 18 |
raise unittest.SkipTest |
| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 47 |
enc = h.encode() |
| 48 |
|
| 49 |
eq(enc , """\ |
| 50 |
=?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKGyhC?= |
| 51 |
=?iso-2022-jp?b?GyRCMnE8VCROPjVHJyRyQlQkQyRGJCQkXiQ5GyhC?=""") |
| 52 |
|
| 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') |