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 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 4 | import string, mimetools |
| 5 | import io |
Walter Dörwald | 8891021 | 2003-05-22 17:32:40 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 7 | msgtext1 = mimetools.Message(io.StringIO( |
Walter Dörwald | 8891021 | 2003-05-22 17:32:40 +0000 | [diff] [blame] | 8 | """Content-Type: text/plain; charset=iso-8859-1; format=flowed |
| 9 | Content-Transfer-Encoding: 8bit |
| 10 | |
| 11 | Foo! |
| 12 | """)) |
| 13 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 14 | sample = bytes(string.ascii_letters + "=" + string.digits + "\n", "ASCII") |
| 15 | |
Walter Dörwald | 8891021 | 2003-05-22 17:32:40 +0000 | [diff] [blame] | 16 | class MimeToolsTest(unittest.TestCase): |
| 17 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 18 | 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örwald | 8891021 | 2003-05-22 17:32:40 +0000 | [diff] [blame] | 52 | |
| 53 | def test_boundary(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 54 | s = set([""]) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 55 | for i in range(100): |
Walter Dörwald | 8891021 | 2003-05-22 17:32:40 +0000 | [diff] [blame] | 56 | nb = mimetools.choose_boundary() |
| 57 | self.assert_(nb not in s) |
| 58 | s.add(nb) |
| 59 | |
| 60 | def test_message(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 61 | msg = mimetools.Message(io.StringIO(msgtext1)) |
Walter Dörwald | 8891021 | 2003-05-22 17:32:40 +0000 | [diff] [blame] | 62 | 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 | |
| 72 | def test_main(): |
| 73 | test_support.run_unittest(MimeToolsTest) |
| 74 | |
| 75 | if __name__=="__main__": |
| 76 | test_main() |