blob: cf0e191e6db206ad53a7abf1d962fccd6ec8d893 [file] [log] [blame]
Walter Dörwald88910212003-05-22 17:32:40 +00001import unittest
2from test import test_support
Martin v. Löwisefffd282000-09-30 17:03:19 +00003
Guido van Rossum34d19282007-08-09 01:03:29 +00004import string, mimetools
5import io
Walter Dörwald88910212003-05-22 17:32:40 +00006
Guido van Rossum34d19282007-08-09 01:03:29 +00007msgtext1 = mimetools.Message(io.StringIO(
Walter Dörwald88910212003-05-22 17:32:40 +00008"""Content-Type: text/plain; charset=iso-8859-1; format=flowed
9Content-Transfer-Encoding: 8bit
10
11Foo!
12"""))
13
Guido van Rossum34d19282007-08-09 01:03:29 +000014sample = bytes(string.ascii_letters + "=" + string.digits + "\n", "ASCII")
15
Walter Dörwald88910212003-05-22 17:32:40 +000016class MimeToolsTest(unittest.TestCase):
17
Guido van Rossum34d19282007-08-09 01:03:29 +000018 def decode_encode_test(self, enc):
19 i = io.BytesIO(sample)
20 o = io.BytesIO()
21 mimetools.encode(i, o, enc)
22 i = io.BytesIO(o.getvalue())
23 o = io.BytesIO()
24 mimetools.decode(i, o, enc)
25 self.assertEqual(o.getvalue(), sample)
26
27 # Separate tests for better diagnostics
28
29 def test_7bit(self):
30 self.decode_encode_test('7bit')
31
32 def test_8bit(self):
33 self.decode_encode_test('8bit')
34
35 def test_base64(self):
36 self.decode_encode_test('base64')
37
38 def test_quoted_printable(self):
39 self.decode_encode_test('quoted-printable')
40
41 def test_uuencode(self):
42 self.decode_encode_test('uuencode')
43
44 def test_x_uuencode(self):
45 self.decode_encode_test('x-uuencode')
46
47 def test_uue(self):
48 self.decode_encode_test('uue')
49
50 def test_x_uue(self):
51 self.decode_encode_test('x-uue')
Walter Dörwald88910212003-05-22 17:32:40 +000052
53 def test_boundary(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054 s = set([""])
Guido van Rossum805365e2007-05-07 22:24:25 +000055 for i in range(100):
Walter Dörwald88910212003-05-22 17:32:40 +000056 nb = mimetools.choose_boundary()
57 self.assert_(nb not in s)
58 s.add(nb)
59
60 def test_message(self):
Guido van Rossum34d19282007-08-09 01:03:29 +000061 msg = mimetools.Message(io.StringIO(msgtext1))
Walter Dörwald88910212003-05-22 17:32:40 +000062 self.assertEqual(msg.gettype(), "text/plain")
63 self.assertEqual(msg.getmaintype(), "text")
64 self.assertEqual(msg.getsubtype(), "plain")
65 self.assertEqual(msg.getplist(), ["charset=iso-8859-1", "format=flowed"])
66 self.assertEqual(msg.getparamnames(), ["charset", "format"])
67 self.assertEqual(msg.getparam("charset"), "iso-8859-1")
68 self.assertEqual(msg.getparam("format"), "flowed")
69 self.assertEqual(msg.getparam("spam"), None)
70 self.assertEqual(msg.getencoding(), "8bit")
71
72def test_main():
73 test_support.run_unittest(MimeToolsTest)
74
75if __name__=="__main__":
76 test_main()