@@ -92,6 +92,44 @@ def test_set_payload_with_charset(self):
9292 msg .set_payload ('This is a string payload' , charset )
9393 self .assertEqual (msg .get_charset ().input_charset , 'iso-8859-1' )
9494
95+ def test_set_payload_with_8bit_data_and_charset (self ):
96+ data = b'\xd0 \x90 \xd0 \x91 \xd0 \x92 '
97+ charset = Charset ('utf-8' )
98+ msg = Message ()
99+ msg .set_payload (data , charset )
100+ self .assertEqual (msg ['content-transfer-encoding' ], 'base64' )
101+ self .assertEqual (msg .get_payload (decode = True ), data )
102+ self .assertEqual (msg .get_payload (), '0JDQkdCS\n ' )
103+
104+ def test_set_payload_with_non_ascii_and_charset_body_encoding_none (self ):
105+ data = b'\xd0 \x90 \xd0 \x91 \xd0 \x92 '
106+ charset = Charset ('utf-8' )
107+ charset .body_encoding = None # Disable base64 encoding
108+ msg = Message ()
109+ msg .set_payload (data .decode ('utf-8' ), charset )
110+ self .assertEqual (msg ['content-transfer-encoding' ], '8bit' )
111+ self .assertEqual (msg .get_payload (decode = True ), data )
112+
113+ def test_set_payload_with_8bit_data_and_charset_body_encoding_none (self ):
114+ data = b'\xd0 \x90 \xd0 \x91 \xd0 \x92 '
115+ charset = Charset ('utf-8' )
116+ charset .body_encoding = None # Disable base64 encoding
117+ msg = Message ()
118+ msg .set_payload (data , charset )
119+ self .assertEqual (msg ['content-transfer-encoding' ], '8bit' )
120+ self .assertEqual (msg .get_payload (decode = True ), data )
121+
122+ def test_set_payload_to_list (self ):
123+ msg = Message ()
124+ msg .set_payload ([])
125+ self .assertEqual (msg .get_payload (), [])
126+
127+ def test_set_payload_with_non_ascii_and_no_charset_raises (self ):
128+ data = b'\xd0 \x90 \xd0 \x91 \xd0 \x92 ' .decode ('utf-8' )
129+ msg = Message ()
130+ with self .assertRaises (TypeError ):
131+ msg .set_payload (data )
132+
95133 def test_get_charsets (self ):
96134 eq = self .assertEqual
97135
@@ -558,20 +596,10 @@ def test_broken_base64_payload(self):
558596 self .assertIsInstance (msg .defects [0 ],
559597 errors .InvalidBase64CharactersDefect )
560598
561- def test_broken_unicode_payload (self ):
562- # This test improves coverage but is not a compliance test.
563- # The behavior in this situation is currently undefined by the API.
564- x = 'this is a br\xf6 ken thing to do'
565- msg = Message ()
566- msg ['content-type' ] = 'text/plain'
567- msg ['content-transfer-encoding' ] = '8bit'
568- msg .set_payload (x )
569- self .assertEqual (msg .get_payload (decode = True ),
570- bytes (x , 'raw-unicode-escape' ))
571-
572599 def test_questionable_bytes_payload (self ):
573600 # This test improves coverage but is not a compliance test,
574- # since it involves poking inside the black box.
601+ # since it involves poking inside the black box in a way
602+ # that actually breaks the model invariants.
575603 x = 'this is a quéstionable thing to do' .encode ('utf-8' )
576604 msg = Message ()
577605 msg ['content-type' ] = 'text/plain; charset="utf-8"'
0 commit comments