1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 #
18 # $Id: test_multipart.py,v 1.4 2001-08-07 00:24:43 richard Exp $
20 import unittest, cStringIO
22 from roundup.mailgw import Message
24 class MultipartTestCase(unittest.TestCase):
25 def setUp(self):
26 self.fp = cStringIO.StringIO()
27 w = self.fp.write
28 w('Content-Type: multipart/mixed; boundary="foo"\r\n\r\n')
29 w('This is a multipart message. Ignore this bit.\r\n')
30 w('--foo\r\n')
32 w('Content-Type: text/plain\r\n\r\n')
33 w('Hello, world!\r\n')
34 w('\r\n')
35 w('Blah blah\r\n')
36 w('foo\r\n')
37 w('-foo\r\n')
38 w('--foo\r\n')
40 w('Content-Type: multipart/alternative; boundary="bar"\r\n\r\n')
41 w('This is a multipart message. Ignore this bit.\r\n')
42 w('--bar\r\n')
44 w('Content-Type: text/plain\r\n\r\n')
45 w('Hello, world!\r\n')
46 w('\r\n')
47 w('Blah blah\r\n')
48 w('--bar\r\n')
50 w('Content-Type: text/html\r\n\r\n')
51 w('<b>Hello, world!</b>\r\n')
52 w('--bar--\r\n')
53 w('--foo\r\n')
55 w('Content-Type: text/plain\r\n\r\n')
56 w('Last bit\n')
57 w('--foo--\r\n')
58 self.fp.seek(0)
60 def testMultipart(self):
61 m = Message(self.fp)
62 self.assert_(m is not None)
64 # skip the first bit
65 p = m.getPart()
66 self.assert_(p is not None)
67 self.assertEqual(p.fp.read(),
68 'This is a multipart message. Ignore this bit.\r\n')
70 # first text/plain
71 p = m.getPart()
72 self.assert_(p is not None)
73 self.assertEqual(p.gettype(), 'text/plain')
74 self.assertEqual(p.fp.read(),
75 'Hello, world!\r\n\r\nBlah blah\r\nfoo\r\n-foo\r\n')
77 # sub-multipart
78 p = m.getPart()
79 self.assert_(p is not None)
80 self.assertEqual(p.gettype(), 'multipart/alternative')
82 # sub-multipart text/plain
83 q = p.getPart()
84 self.assert_(q is not None)
85 q = p.getPart()
86 self.assert_(q is not None)
87 self.assertEqual(q.gettype(), 'text/plain')
88 self.assertEqual(q.fp.read(), 'Hello, world!\r\n\r\nBlah blah\r\n')
90 # sub-multipart text/html
91 q = p.getPart()
92 self.assert_(q is not None)
93 self.assertEqual(q.gettype(), 'text/html')
94 self.assertEqual(q.fp.read(), '<b>Hello, world!</b>\r\n')
96 # sub-multipart end
97 q = p.getPart()
98 self.assert_(q is None)
100 # final text/plain
101 p = m.getPart()
102 self.assert_(p is not None)
103 self.assertEqual(p.gettype(), 'text/plain')
104 self.assertEqual(p.fp.read(),
105 'Last bit\n')
107 # end
108 p = m.getPart()
109 self.assert_(p is None)
111 def suite():
112 return unittest.makeSuite(MultipartTestCase, 'test')
115 #
116 # $Log: not supported by cvs2svn $
117 # Revision 1.3 2001/08/07 00:15:51 richard
118 # Added the copyright/license notice to (nearly) all files at request of
119 # Bizar Software.
120 #
121 # Revision 1.2 2001/07/29 07:01:39 richard
122 # Added vim command to all source so that we don't get no steenkin' tabs :)
123 #
124 # Revision 1.1 2001/07/28 06:43:02 richard
125 # Multipart message class has the getPart method now. Added some tests for it.
126 #
127 #
128 # vim: set filetype=python ts=4 sw=4 et si