1 # $Id: test_multipart.py,v 1.1 2001-07-28 06:43:02 richard Exp $
3 import unittest, cStringIO
5 from roundup.mailgw import Message
7 class MultipartTestCase(unittest.TestCase):
8 def setUp(self):
9 self.fp = cStringIO.StringIO()
10 w = self.fp.write
11 w('Content-Type: multipart/mixed; boundary="foo"\r\n\r\n')
12 w('This is a multipart message. Ignore this bit.\r\n')
13 w('--foo\r\n')
15 w('Content-Type: text/plain\r\n\r\n')
16 w('Hello, world!\r\n')
17 w('\r\n')
18 w('Blah blah\r\n')
19 w('foo\r\n')
20 w('-foo\r\n')
21 w('--foo\r\n')
23 w('Content-Type: multipart/alternative; boundary="bar"\r\n\r\n')
24 w('This is a multipart message. Ignore this bit.\r\n')
25 w('--bar\r\n')
27 w('Content-Type: text/plain\r\n\r\n')
28 w('Hello, world!\r\n')
29 w('\r\n')
30 w('Blah blah\r\n')
31 w('--bar\r\n')
33 w('Content-Type: text/html\r\n\r\n')
34 w('<b>Hello, world!</b>\r\n')
35 w('--bar--\r\n')
36 w('--foo\r\n')
38 w('Content-Type: text/plain\r\n\r\n')
39 w('Last bit\n')
40 w('--foo--\r\n')
41 self.fp.seek(0)
43 def testMultipart(self):
44 m = Message(self.fp)
45 self.assert_(m is not None)
47 # skip the first bit
48 p = m.getPart()
49 self.assert_(p is not None)
50 self.assertEqual(p.fp.read(),
51 'This is a multipart message. Ignore this bit.\r\n')
53 # first text/plain
54 p = m.getPart()
55 self.assert_(p is not None)
56 self.assertEqual(p.gettype(), 'text/plain')
57 self.assertEqual(p.fp.read(),
58 'Hello, world!\r\n\r\nBlah blah\r\nfoo\r\n-foo\r\n')
60 # sub-multipart
61 p = m.getPart()
62 self.assert_(p is not None)
63 self.assertEqual(p.gettype(), 'multipart/alternative')
65 # sub-multipart text/plain
66 q = p.getPart()
67 self.assert_(q is not None)
68 q = p.getPart()
69 self.assert_(q is not None)
70 self.assertEqual(q.gettype(), 'text/plain')
71 self.assertEqual(q.fp.read(), 'Hello, world!\r\n\r\nBlah blah\r\n')
73 # sub-multipart text/html
74 q = p.getPart()
75 self.assert_(q is not None)
76 self.assertEqual(q.gettype(), 'text/html')
77 self.assertEqual(q.fp.read(), '<b>Hello, world!</b>\r\n')
79 # sub-multipart end
80 q = p.getPart()
81 self.assert_(q is None)
83 # final text/plain
84 p = m.getPart()
85 self.assert_(p is not None)
86 self.assertEqual(p.gettype(), 'text/plain')
87 self.assertEqual(p.fp.read(),
88 'Last bit\n')
90 # end
91 p = m.getPart()
92 self.assert_(p is None)
94 def suite():
95 return unittest.makeSuite(MultipartTestCase, 'test')
98 #
99 # $Log: not supported by cvs2svn $
100 #