77import unittest
88import base64
99from cStringIO import StringIO
10- from types import StringType
10+ from types import StringType , ListType
1111import warnings
1212
1313import email
@@ -191,7 +191,7 @@ def test_decoded_generator(self):
191191 fp .close ()
192192 s = StringIO ()
193193 g = DecodedGenerator (s )
194- g (msg )
194+ g . flatten (msg )
195195 eq (s .getvalue (), text )
196196
197197 def test__contains__ (self ):
@@ -403,7 +403,7 @@ def test_header_splitter(self):
403403 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"' )
404404 sfp = StringIO ()
405405 g = Generator (sfp )
406- g (msg )
406+ g . flatten (msg )
407407 self .assertEqual (sfp .getvalue (), '''\
408408 Content-Type: text/plain; charset="us-ascii"
409409MIME-Version: 1.0
@@ -423,7 +423,7 @@ def test_no_semis_header_splitter(self):
423423 msg .set_payload ('Test' )
424424 sfp = StringIO ()
425425 g = Generator (sfp )
426- g (msg )
426+ g . flatten (msg )
427427 self .assertEqual (sfp .getvalue (), """\
428428429429@@ -439,7 +439,7 @@ def test_no_split_long_header(self):
439439 msg .set_payload ('Test' )
440440 sfp = StringIO ()
441441 g = Generator (sfp )
442- g (msg )
442+ g . flatten (msg )
443443 self .assertEqual (sfp .getvalue (), """\
444444445445References: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
@@ -477,15 +477,15 @@ class TestFromMangling(unittest.TestCase):
477477 def setUp (self ):
478478 self .msg = Message ()
479479 self .
msg [
'From' ]
= '[email protected] ' 480- self .msg .add_payload ("""\
480+ self .msg .set_payload ("""\
481481 From the desk of A.A.A.:
482482Blah blah blah
483483""" )
484484
485485 def test_mangled_from (self ):
486486 s = StringIO ()
487487 g = Generator (s , mangle_from_ = 1 )
488- g (self .msg )
488+ g . flatten (self .msg )
489489 self .assertEqual (s .getvalue (), """\
490490491491
@@ -496,7 +496,7 @@ def test_mangled_from(self):
496496 def test_dont_mangle_from (self ):
497497 s = StringIO ()
498498 g = Generator (s , mangle_from_ = 0 )
499- g (self .msg )
499+ g . flatten (self .msg )
500500 self .assertEqual (s .getvalue (), """\
501501502502
@@ -658,8 +658,8 @@ def setUp(self):
658658
659659This is the dingus fish.
660660''' )
661- container .add_payload (intro )
662- container .add_payload (image )
661+ container .attach (intro )
662+ container .attach (image )
663663 container [
'From' ]
= 'Barry <[email protected] >' 664664 container [
'To' ]
= 'Dingus Lovers <[email protected] >' 665665 container ['Subject' ] = 'Here is your dingus fish'
@@ -851,24 +851,38 @@ def test_type_error(self):
851851
852852 def test_valid_argument (self ):
853853 eq = self .assertEqual
854+ unless = self .failUnless
854855 subject = 'A sub-message'
855856 m = Message ()
856857 m ['Subject' ] = subject
857858 r = MIMEMessage (m )
858859 eq (r .get_type (), 'message/rfc822' )
859- self .failUnless (r .get_payload () is m )
860- eq (r .get_payload ()['subject' ], subject )
860+ payload = r .get_payload ()
861+ unless (type (payload ), ListType )
862+ eq (len (payload ), 1 )
863+ subpart = payload [0 ]
864+ unless (subpart is m )
865+ eq (subpart ['subject' ], subject )
866+
867+ def test_bad_multipart (self ):
868+ eq = self .assertEqual
869+ msg1 = Message ()
870+ msg1 ['Subject' ] = 'subpart 1'
871+ msg2 = Message ()
872+ msg2 ['Subject' ] = 'subpart 2'
873+ r = MIMEMessage (msg1 )
874+ self .assertRaises (Errors .MultipartConversionError , r .attach , msg2 )
861875
862876 def test_generate (self ):
863877 # First craft the message to be encapsulated
864878 m = Message ()
865879 m ['Subject' ] = 'An enclosed message'
866- m .add_payload ('Here is the body of the message.\n ' )
880+ m .set_payload ('Here is the body of the message.\n ' )
867881 r = MIMEMessage (m )
868882 r ['Subject' ] = 'The enclosing message'
869883 s = StringIO ()
870884 g = Generator (s )
871- g (r )
885+ g . flatten (r )
872886 self .assertEqual (s .getvalue (), """\
873887 Content-Type: message/rfc822
874888MIME-Version: 1.0
@@ -881,10 +895,13 @@ def test_generate(self):
881895
882896 def test_parse_message_rfc822 (self ):
883897 eq = self .assertEqual
898+ unless = self .failUnless
884899 msg = self ._msgobj ('msg_11.txt' )
885900 eq (msg .get_type (), 'message/rfc822' )
886- eq (len (msg .get_payload ()), 1 )
887- submsg = msg .get_payload ()
901+ payload = msg .get_payload ()
902+ unless (isinstance (payload , ListType ))
903+ eq (len (payload ), 1 )
904+ submsg = payload [0 ]
888905 self .failUnless (isinstance (submsg , Message ))
889906 eq (submsg ['subject' ], 'An enclosed message' )
890907 eq (submsg .get_payload (), 'Here is the body of the message.\n ' )
@@ -938,7 +955,10 @@ def test_dsn(self):
938955 # Subpart 3 is the original message
939956 subpart = msg .get_payload (2 )
940957 eq (subpart .get_type (), 'message/rfc822' )
941- subsubpart = subpart .get_payload ()
958+ payload = subpart .get_payload ()
959+ unless (isinstance (payload , ListType ))
960+ eq (len (payload ), 1 )
961+ subsubpart = payload [0 ]
942962 unless (isinstance (subsubpart , Message ))
943963 eq (subsubpart .get_type (), 'text/plain' )
944964 eq (subsubpart ['message-id' ],
@@ -959,11 +979,11 @@ def test_epilogue(self):
959979 msg1 = MIMEText ('One' )
960980 msg2 = MIMEText ('Two' )
961981 msg .add_header ('Content-Type' , 'multipart/mixed' , boundary = 'BOUNDARY' )
962- msg .add_payload (msg1 )
963- msg .add_payload (msg2 )
982+ msg .attach (msg1 )
983+ msg .attach (msg2 )
964984 sfp = StringIO ()
965985 g = Generator (sfp )
966- g (msg )
986+ g . flatten (msg )
967987 self .assertEqual (sfp .getvalue (), text )
968988
969989
@@ -987,7 +1007,7 @@ def _idempotent(self, msg, text):
9871007 eq = self .assertEquals
9881008 s = StringIO ()
9891009 g = Generator (s , maxheaderlen = 0 )
990- g (msg )
1010+ g . flatten (msg )
9911011 eq (text , s .getvalue ())
9921012
9931013 def test_parse_text_message (self ):
@@ -1044,6 +1064,7 @@ def test_multipart_no_parts(self):
10441064
10451065 def test_content_type (self ):
10461066 eq = self .assertEquals
1067+ unless = self .failUnless
10471068 # Get a message object and reset the seek pointer for other tests
10481069 msg , text = self ._msgobj ('msg_05.txt' )
10491070 eq (msg .get_type (), 'multipart/report' )
@@ -1066,18 +1087,25 @@ def test_content_type(self):
10661087 msg3 = msg .get_payload (2 )
10671088 eq (msg3 .get_type (), 'message/rfc822' )
10681089 self .failUnless (isinstance (msg3 , Message ))
1069- msg4 = msg3 .get_payload ()
1070- self .failUnless (isinstance (msg4 , Message ))
1090+ payload = msg3 .get_payload ()
1091+ unless (isinstance (payload , ListType ))
1092+ eq (len (payload ), 1 )
1093+ msg4 = payload [0 ]
1094+ unless (isinstance (msg4 , Message ))
10711095 eq (msg4 .get_payload (), 'Yadda yadda yadda\n ' )
10721096
10731097 def test_parser (self ):
10741098 eq = self .assertEquals
1099+ unless = self .failUnless
10751100 msg , text = self ._msgobj ('msg_06.txt' )
10761101 # Check some of the outer headers
10771102 eq (msg .get_type (), 'message/rfc822' )
1078- # Make sure there's exactly one thing in the payload and that's a
1079- # sub-Message object of type text/plain
1080- msg1 = msg .get_payload ()
1103+ # Make sure the payload is a list of exactly one sub-Message, and that
1104+ # that submessage has a type of text/plain
1105+ payload = msg .get_payload ()
1106+ unless (isinstance (payload , ListType ))
1107+ eq (len (payload ), 1 )
1108+ msg1 = payload [0 ]
10811109 self .failUnless (isinstance (msg1 , Message ))
10821110 eq (msg1 .get_type (), 'text/plain' )
10831111 self .failUnless (isinstance (msg1 .get_payload (), StringType ))
@@ -1097,7 +1125,7 @@ def test_message_from_string(self):
10971125 # Don't wrap/continue long headers since we're trying to test
10981126 # idempotency.
10991127 g = Generator (s , maxheaderlen = 0 )
1100- g (msg )
1128+ g . flatten (msg )
11011129 self .assertEqual (text , s .getvalue ())
11021130
11031131 def test_message_from_file (self ):
@@ -1110,7 +1138,7 @@ def test_message_from_file(self):
11101138 # Don't wrap/continue long headers since we're trying to test
11111139 # idempotency.
11121140 g = Generator (s , maxheaderlen = 0 )
1113- g (msg )
1141+ g . flatten (msg )
11141142 self .assertEqual (text , s .getvalue ())
11151143 finally :
11161144 fp .close ()
0 commit comments