Walter Dörwald | 8891021 | 2003-05-22 17:32:40 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
Martin v. Löwis | efffd28 | 2000-09-30 17:03:19 +0000 | [diff] [blame] | 3 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4 | import string, StringIO, mimetools |
Walter Dörwald | 8891021 | 2003-05-22 17:32:40 +0000 | [diff] [blame] | 5 | |
| 6 | msgtext1 = mimetools.Message(StringIO.StringIO( |
| 7 | """Content-Type: text/plain; charset=iso-8859-1; format=flowed |
| 8 | Content-Transfer-Encoding: 8bit |
| 9 | |
| 10 | Foo! |
| 11 | """)) |
| 12 | |
| 13 | class MimeToolsTest(unittest.TestCase): |
| 14 | |
| 15 | def test_decodeencode(self): |
| 16 | start = string.ascii_letters + "=" + string.digits + "\n" |
| 17 | for enc in ['7bit','8bit','base64','quoted-printable', |
| 18 | 'uuencode', 'x-uuencode', 'uue', 'x-uue']: |
| 19 | i = StringIO.StringIO(start) |
| 20 | o = StringIO.StringIO() |
| 21 | mimetools.encode(i, o, enc) |
| 22 | i = StringIO.StringIO(o.getvalue()) |
| 23 | o = StringIO.StringIO() |
| 24 | mimetools.decode(i, o, enc) |
| 25 | self.assertEqual(o.getvalue(), start) |
| 26 | |
| 27 | def test_boundary(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 28 | s = set([""]) |
Walter Dörwald | 8891021 | 2003-05-22 17:32:40 +0000 | [diff] [blame] | 29 | for i in xrange(100): |
| 30 | nb = mimetools.choose_boundary() |
| 31 | self.assert_(nb not in s) |
| 32 | s.add(nb) |
| 33 | |
| 34 | def test_message(self): |
| 35 | msg = mimetools.Message(StringIO.StringIO(msgtext1)) |
| 36 | self.assertEqual(msg.gettype(), "text/plain") |
| 37 | self.assertEqual(msg.getmaintype(), "text") |
| 38 | self.assertEqual(msg.getsubtype(), "plain") |
| 39 | self.assertEqual(msg.getplist(), ["charset=iso-8859-1", "format=flowed"]) |
| 40 | self.assertEqual(msg.getparamnames(), ["charset", "format"]) |
| 41 | self.assertEqual(msg.getparam("charset"), "iso-8859-1") |
| 42 | self.assertEqual(msg.getparam("format"), "flowed") |
| 43 | self.assertEqual(msg.getparam("spam"), None) |
| 44 | self.assertEqual(msg.getencoding(), "8bit") |
| 45 | |
| 46 | def test_main(): |
| 47 | test_support.run_unittest(MimeToolsTest) |
| 48 | |
| 49 | if __name__=="__main__": |
| 50 | test_main() |