/Lib/test/test_email/test_asian_codecs.py

https://github.com/albertz/CPython · Python · 81 lines · 57 code · 11 blank · 13 comment · 3 complexity · cfa2b6d440eb12729a5eb28ce06c9eb7 MD5 · raw file

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