Benjamin Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2010 Python Software Foundation |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2 | # Contact: email-sig@python.org |
| 3 | # email package unit tests |
| 4 | |
| 5 | import os |
| 6 | import sys |
| 7 | import time |
| 8 | import base64 |
| 9 | import difflib |
| 10 | import unittest |
| 11 | import warnings |
| 12 | |
| 13 | from io import StringIO |
| 14 | from itertools import chain |
| 15 | |
| 16 | import email |
| 17 | |
| 18 | from email.charset import Charset |
| 19 | from email.header import Header, decode_header, make_header |
| 20 | from email.parser import Parser, HeaderParser |
| 21 | from email.generator import Generator, DecodedGenerator |
| 22 | from email.message import Message |
| 23 | from email.mime.application import MIMEApplication |
| 24 | from email.mime.audio import MIMEAudio |
| 25 | from email.mime.text import MIMEText |
| 26 | from email.mime.image import MIMEImage |
| 27 | from email.mime.base import MIMEBase |
| 28 | from email.mime.message import MIMEMessage |
| 29 | from email.mime.multipart import MIMEMultipart |
| 30 | from email import utils |
| 31 | from email import errors |
| 32 | from email import encoders |
| 33 | from email import iterators |
| 34 | from email import base64mime |
| 35 | from email import quoprimime |
| 36 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 37 | from test.support import findfile, run_unittest |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 38 | from email.test import __file__ as landmark |
| 39 | |
| 40 | |
| 41 | NL = '\n' |
| 42 | EMPTYSTRING = '' |
| 43 | SPACE = ' ' |
| 44 | |
| 45 | |
| 46 | |
| 47 | def openfile(filename, *args, **kws): |
| 48 | path = os.path.join(os.path.dirname(landmark), 'data', filename) |
| 49 | return open(path, *args, **kws) |
| 50 | |
| 51 | |
| 52 | |
| 53 | # Base test class |
| 54 | class TestEmailBase(unittest.TestCase): |
| 55 | def ndiffAssertEqual(self, first, second): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 56 | """Like assertEqual except use ndiff for readable output.""" |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 57 | if first != second: |
| 58 | sfirst = str(first) |
| 59 | ssecond = str(second) |
| 60 | rfirst = [repr(line) for line in sfirst.splitlines()] |
| 61 | rsecond = [repr(line) for line in ssecond.splitlines()] |
| 62 | diff = difflib.ndiff(rfirst, rsecond) |
| 63 | raise self.failureException(NL + NL.join(diff)) |
| 64 | |
| 65 | def _msgobj(self, filename): |
| 66 | with openfile(findfile(filename)) as fp: |
| 67 | return email.message_from_file(fp) |
| 68 | |
| 69 | |
| 70 | |
| 71 | # Test various aspects of the Message class's API |
| 72 | class TestMessageAPI(TestEmailBase): |
| 73 | def test_get_all(self): |
| 74 | eq = self.assertEqual |
| 75 | msg = self._msgobj('msg_20.txt') |
| 76 | eq(msg.get_all('cc'), ['ccc@zzz.org', 'ddd@zzz.org', 'eee@zzz.org']) |
| 77 | eq(msg.get_all('xx', 'n/a'), 'n/a') |
| 78 | |
| 79 | def test_getset_charset(self): |
| 80 | eq = self.assertEqual |
| 81 | msg = Message() |
| 82 | eq(msg.get_charset(), None) |
| 83 | charset = Charset('iso-8859-1') |
| 84 | msg.set_charset(charset) |
| 85 | eq(msg['mime-version'], '1.0') |
| 86 | eq(msg.get_content_type(), 'text/plain') |
| 87 | eq(msg['content-type'], 'text/plain; charset="iso-8859-1"') |
| 88 | eq(msg.get_param('charset'), 'iso-8859-1') |
| 89 | eq(msg['content-transfer-encoding'], 'quoted-printable') |
| 90 | eq(msg.get_charset().input_charset, 'iso-8859-1') |
| 91 | # Remove the charset |
| 92 | msg.set_charset(None) |
| 93 | eq(msg.get_charset(), None) |
| 94 | eq(msg['content-type'], 'text/plain') |
| 95 | # Try adding a charset when there's already MIME headers present |
| 96 | msg = Message() |
| 97 | msg['MIME-Version'] = '2.0' |
| 98 | msg['Content-Type'] = 'text/x-weird' |
| 99 | msg['Content-Transfer-Encoding'] = 'quinted-puntable' |
| 100 | msg.set_charset(charset) |
| 101 | eq(msg['mime-version'], '2.0') |
| 102 | eq(msg['content-type'], 'text/x-weird; charset="iso-8859-1"') |
| 103 | eq(msg['content-transfer-encoding'], 'quinted-puntable') |
| 104 | |
| 105 | def test_set_charset_from_string(self): |
| 106 | eq = self.assertEqual |
| 107 | msg = Message() |
| 108 | msg.set_charset('us-ascii') |
| 109 | eq(msg.get_charset().input_charset, 'us-ascii') |
| 110 | eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 111 | |
| 112 | def test_set_payload_with_charset(self): |
| 113 | msg = Message() |
| 114 | charset = Charset('iso-8859-1') |
| 115 | msg.set_payload('This is a string payload', charset) |
| 116 | self.assertEqual(msg.get_charset().input_charset, 'iso-8859-1') |
| 117 | |
| 118 | def test_get_charsets(self): |
| 119 | eq = self.assertEqual |
| 120 | |
| 121 | msg = self._msgobj('msg_08.txt') |
| 122 | charsets = msg.get_charsets() |
| 123 | eq(charsets, [None, 'us-ascii', 'iso-8859-1', 'iso-8859-2', 'koi8-r']) |
| 124 | |
| 125 | msg = self._msgobj('msg_09.txt') |
| 126 | charsets = msg.get_charsets('dingbat') |
| 127 | eq(charsets, ['dingbat', 'us-ascii', 'iso-8859-1', 'dingbat', |
| 128 | 'koi8-r']) |
| 129 | |
| 130 | msg = self._msgobj('msg_12.txt') |
| 131 | charsets = msg.get_charsets() |
| 132 | eq(charsets, [None, 'us-ascii', 'iso-8859-1', None, 'iso-8859-2', |
| 133 | 'iso-8859-3', 'us-ascii', 'koi8-r']) |
| 134 | |
| 135 | def test_get_filename(self): |
| 136 | eq = self.assertEqual |
| 137 | |
| 138 | msg = self._msgobj('msg_04.txt') |
| 139 | filenames = [p.get_filename() for p in msg.get_payload()] |
| 140 | eq(filenames, ['msg.txt', 'msg.txt']) |
| 141 | |
| 142 | msg = self._msgobj('msg_07.txt') |
| 143 | subpart = msg.get_payload(1) |
| 144 | eq(subpart.get_filename(), 'dingusfish.gif') |
| 145 | |
| 146 | def test_get_filename_with_name_parameter(self): |
| 147 | eq = self.assertEqual |
| 148 | |
| 149 | msg = self._msgobj('msg_44.txt') |
| 150 | filenames = [p.get_filename() for p in msg.get_payload()] |
| 151 | eq(filenames, ['msg.txt', 'msg.txt']) |
| 152 | |
| 153 | def test_get_boundary(self): |
| 154 | eq = self.assertEqual |
| 155 | msg = self._msgobj('msg_07.txt') |
| 156 | # No quotes! |
| 157 | eq(msg.get_boundary(), 'BOUNDARY') |
| 158 | |
| 159 | def test_set_boundary(self): |
| 160 | eq = self.assertEqual |
| 161 | # This one has no existing boundary parameter, but the Content-Type: |
| 162 | # header appears fifth. |
| 163 | msg = self._msgobj('msg_01.txt') |
| 164 | msg.set_boundary('BOUNDARY') |
| 165 | header, value = msg.items()[4] |
| 166 | eq(header.lower(), 'content-type') |
| 167 | eq(value, 'text/plain; charset="us-ascii"; boundary="BOUNDARY"') |
| 168 | # This one has a Content-Type: header, with a boundary, stuck in the |
| 169 | # middle of its headers. Make sure the order is preserved; it should |
| 170 | # be fifth. |
| 171 | msg = self._msgobj('msg_04.txt') |
| 172 | msg.set_boundary('BOUNDARY') |
| 173 | header, value = msg.items()[4] |
| 174 | eq(header.lower(), 'content-type') |
| 175 | eq(value, 'multipart/mixed; boundary="BOUNDARY"') |
| 176 | # And this one has no Content-Type: header at all. |
| 177 | msg = self._msgobj('msg_03.txt') |
| 178 | self.assertRaises(errors.HeaderParseError, |
| 179 | msg.set_boundary, 'BOUNDARY') |
| 180 | |
R. David Murray | 57c45ac | 2010-02-21 04:39:40 +0000 | [diff] [blame] | 181 | def test_message_rfc822_only(self): |
| 182 | # Issue 7970: message/rfc822 not in multipart parsed by |
| 183 | # HeaderParser caused an exception when flattened. |
| 184 | fp = openfile(findfile('msg_46.txt')) |
| 185 | msgdata = fp.read() |
| 186 | parser = HeaderParser() |
| 187 | msg = parser.parsestr(msgdata) |
| 188 | out = StringIO() |
| 189 | gen = Generator(out, True, 0) |
| 190 | gen.flatten(msg, False) |
| 191 | self.assertEqual(out.getvalue(), msgdata) |
| 192 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 193 | def test_get_decoded_payload(self): |
| 194 | eq = self.assertEqual |
| 195 | msg = self._msgobj('msg_10.txt') |
| 196 | # The outer message is a multipart |
| 197 | eq(msg.get_payload(decode=True), None) |
| 198 | # Subpart 1 is 7bit encoded |
| 199 | eq(msg.get_payload(0).get_payload(decode=True), |
| 200 | b'This is a 7bit encoded message.\n') |
| 201 | # Subpart 2 is quopri |
| 202 | eq(msg.get_payload(1).get_payload(decode=True), |
| 203 | b'\xa1This is a Quoted Printable encoded message!\n') |
| 204 | # Subpart 3 is base64 |
| 205 | eq(msg.get_payload(2).get_payload(decode=True), |
| 206 | b'This is a Base64 encoded message.') |
R. David Murray | 57a4b98 | 2010-03-08 02:17:03 +0000 | [diff] [blame] | 207 | # Subpart 4 is base64 with a trailing newline, which |
| 208 | # used to be stripped (issue 7143). |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 209 | eq(msg.get_payload(3).get_payload(decode=True), |
R. David Murray | 57a4b98 | 2010-03-08 02:17:03 +0000 | [diff] [blame] | 210 | b'This is a Base64 encoded message.\n') |
| 211 | # Subpart 5 has no Content-Transfer-Encoding: header. |
| 212 | eq(msg.get_payload(4).get_payload(decode=True), |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 213 | b'This has no Content-Transfer-Encoding: header.\n') |
| 214 | |
| 215 | def test_get_decoded_uu_payload(self): |
| 216 | eq = self.assertEqual |
| 217 | msg = Message() |
| 218 | msg.set_payload('begin 666 -\n+:&5L;&\\@=V]R;&0 \n \nend\n') |
| 219 | for cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'): |
| 220 | msg['content-transfer-encoding'] = cte |
| 221 | eq(msg.get_payload(decode=True), b'hello world') |
| 222 | # Now try some bogus data |
| 223 | msg.set_payload('foo') |
| 224 | eq(msg.get_payload(decode=True), b'foo') |
| 225 | |
| 226 | def test_decoded_generator(self): |
| 227 | eq = self.assertEqual |
| 228 | msg = self._msgobj('msg_07.txt') |
| 229 | with openfile('msg_17.txt') as fp: |
| 230 | text = fp.read() |
| 231 | s = StringIO() |
| 232 | g = DecodedGenerator(s) |
| 233 | g.flatten(msg) |
| 234 | eq(s.getvalue(), text) |
| 235 | |
| 236 | def test__contains__(self): |
| 237 | msg = Message() |
| 238 | msg['From'] = 'Me' |
| 239 | msg['to'] = 'You' |
| 240 | # Check for case insensitivity |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 241 | self.assertTrue('from' in msg) |
| 242 | self.assertTrue('From' in msg) |
| 243 | self.assertTrue('FROM' in msg) |
| 244 | self.assertTrue('to' in msg) |
| 245 | self.assertTrue('To' in msg) |
| 246 | self.assertTrue('TO' in msg) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 247 | |
| 248 | def test_as_string(self): |
| 249 | eq = self.ndiffAssertEqual |
| 250 | msg = self._msgobj('msg_01.txt') |
| 251 | with openfile('msg_01.txt') as fp: |
| 252 | text = fp.read() |
| 253 | eq(text, str(msg)) |
| 254 | fullrepr = msg.as_string(unixfrom=True) |
| 255 | lines = fullrepr.split('\n') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 256 | self.assertTrue(lines[0].startswith('From ')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 257 | eq(text, NL.join(lines[1:])) |
| 258 | |
| 259 | def test_bad_param(self): |
| 260 | msg = email.message_from_string("Content-Type: blarg; baz; boo\n") |
| 261 | self.assertEqual(msg.get_param('baz'), '') |
| 262 | |
| 263 | def test_missing_filename(self): |
| 264 | msg = email.message_from_string("From: foo\n") |
| 265 | self.assertEqual(msg.get_filename(), None) |
| 266 | |
| 267 | def test_bogus_filename(self): |
| 268 | msg = email.message_from_string( |
| 269 | "Content-Disposition: blarg; filename\n") |
| 270 | self.assertEqual(msg.get_filename(), '') |
| 271 | |
| 272 | def test_missing_boundary(self): |
| 273 | msg = email.message_from_string("From: foo\n") |
| 274 | self.assertEqual(msg.get_boundary(), None) |
| 275 | |
| 276 | def test_get_params(self): |
| 277 | eq = self.assertEqual |
| 278 | msg = email.message_from_string( |
| 279 | 'X-Header: foo=one; bar=two; baz=three\n') |
| 280 | eq(msg.get_params(header='x-header'), |
| 281 | [('foo', 'one'), ('bar', 'two'), ('baz', 'three')]) |
| 282 | msg = email.message_from_string( |
| 283 | 'X-Header: foo; bar=one; baz=two\n') |
| 284 | eq(msg.get_params(header='x-header'), |
| 285 | [('foo', ''), ('bar', 'one'), ('baz', 'two')]) |
| 286 | eq(msg.get_params(), None) |
| 287 | msg = email.message_from_string( |
| 288 | 'X-Header: foo; bar="one"; baz=two\n') |
| 289 | eq(msg.get_params(header='x-header'), |
| 290 | [('foo', ''), ('bar', 'one'), ('baz', 'two')]) |
| 291 | |
| 292 | def test_get_param_liberal(self): |
| 293 | msg = Message() |
| 294 | msg['Content-Type'] = 'Content-Type: Multipart/mixed; boundary = "CPIMSSMTPC06p5f3tG"' |
| 295 | self.assertEqual(msg.get_param('boundary'), 'CPIMSSMTPC06p5f3tG') |
| 296 | |
| 297 | def test_get_param(self): |
| 298 | eq = self.assertEqual |
| 299 | msg = email.message_from_string( |
| 300 | "X-Header: foo=one; bar=two; baz=three\n") |
| 301 | eq(msg.get_param('bar', header='x-header'), 'two') |
| 302 | eq(msg.get_param('quuz', header='x-header'), None) |
| 303 | eq(msg.get_param('quuz'), None) |
| 304 | msg = email.message_from_string( |
| 305 | 'X-Header: foo; bar="one"; baz=two\n') |
| 306 | eq(msg.get_param('foo', header='x-header'), '') |
| 307 | eq(msg.get_param('bar', header='x-header'), 'one') |
| 308 | eq(msg.get_param('baz', header='x-header'), 'two') |
| 309 | # XXX: We are not RFC-2045 compliant! We cannot parse: |
| 310 | # msg["Content-Type"] = 'text/plain; weird="hey; dolly? [you] @ <\\"home\\">?"' |
| 311 | # msg.get_param("weird") |
| 312 | # yet. |
| 313 | |
| 314 | def test_get_param_funky_continuation_lines(self): |
| 315 | msg = self._msgobj('msg_22.txt') |
| 316 | self.assertEqual(msg.get_payload(1).get_param('name'), 'wibble.JPG') |
| 317 | |
| 318 | def test_get_param_with_semis_in_quotes(self): |
| 319 | msg = email.message_from_string( |
| 320 | 'Content-Type: image/pjpeg; name="Jim&&Jill"\n') |
| 321 | self.assertEqual(msg.get_param('name'), 'Jim&&Jill') |
| 322 | self.assertEqual(msg.get_param('name', unquote=False), |
| 323 | '"Jim&&Jill"') |
| 324 | |
R. David Murray | d48739f | 2010-04-14 18:59:18 +0000 | [diff] [blame] | 325 | def test_get_param_with_quotes(self): |
| 326 | msg = email.message_from_string( |
| 327 | 'Content-Type: foo; bar*0="baz\\"foobar"; bar*1="\\"baz"') |
| 328 | self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz') |
| 329 | msg = email.message_from_string( |
| 330 | "Content-Type: foo; bar*0=\"baz\\\"foobar\"; bar*1=\"\\\"baz\"") |
| 331 | self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz') |
| 332 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 333 | def test_field_containment(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 334 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 335 | msg = email.message_from_string('Header: exists') |
| 336 | unless('header' in msg) |
| 337 | unless('Header' in msg) |
| 338 | unless('HEADER' in msg) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 339 | self.assertFalse('headerx' in msg) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 340 | |
| 341 | def test_set_param(self): |
| 342 | eq = self.assertEqual |
| 343 | msg = Message() |
| 344 | msg.set_param('charset', 'iso-2022-jp') |
| 345 | eq(msg.get_param('charset'), 'iso-2022-jp') |
| 346 | msg.set_param('importance', 'high value') |
| 347 | eq(msg.get_param('importance'), 'high value') |
| 348 | eq(msg.get_param('importance', unquote=False), '"high value"') |
| 349 | eq(msg.get_params(), [('text/plain', ''), |
| 350 | ('charset', 'iso-2022-jp'), |
| 351 | ('importance', 'high value')]) |
| 352 | eq(msg.get_params(unquote=False), [('text/plain', ''), |
| 353 | ('charset', '"iso-2022-jp"'), |
| 354 | ('importance', '"high value"')]) |
| 355 | msg.set_param('charset', 'iso-9999-xx', header='X-Jimmy') |
| 356 | eq(msg.get_param('charset', header='X-Jimmy'), 'iso-9999-xx') |
| 357 | |
| 358 | def test_del_param(self): |
| 359 | eq = self.assertEqual |
| 360 | msg = self._msgobj('msg_05.txt') |
| 361 | eq(msg.get_params(), |
| 362 | [('multipart/report', ''), ('report-type', 'delivery-status'), |
| 363 | ('boundary', 'D1690A7AC1.996856090/mail.example.com')]) |
| 364 | old_val = msg.get_param("report-type") |
| 365 | msg.del_param("report-type") |
| 366 | eq(msg.get_params(), |
| 367 | [('multipart/report', ''), |
| 368 | ('boundary', 'D1690A7AC1.996856090/mail.example.com')]) |
| 369 | msg.set_param("report-type", old_val) |
| 370 | eq(msg.get_params(), |
| 371 | [('multipart/report', ''), |
| 372 | ('boundary', 'D1690A7AC1.996856090/mail.example.com'), |
| 373 | ('report-type', old_val)]) |
| 374 | |
| 375 | def test_del_param_on_other_header(self): |
| 376 | msg = Message() |
| 377 | msg.add_header('Content-Disposition', 'attachment', filename='bud.gif') |
| 378 | msg.del_param('filename', 'content-disposition') |
| 379 | self.assertEqual(msg['content-disposition'], 'attachment') |
| 380 | |
| 381 | def test_set_type(self): |
| 382 | eq = self.assertEqual |
| 383 | msg = Message() |
| 384 | self.assertRaises(ValueError, msg.set_type, 'text') |
| 385 | msg.set_type('text/plain') |
| 386 | eq(msg['content-type'], 'text/plain') |
| 387 | msg.set_param('charset', 'us-ascii') |
| 388 | eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 389 | msg.set_type('text/html') |
| 390 | eq(msg['content-type'], 'text/html; charset="us-ascii"') |
| 391 | |
| 392 | def test_set_type_on_other_header(self): |
| 393 | msg = Message() |
| 394 | msg['X-Content-Type'] = 'text/plain' |
| 395 | msg.set_type('application/octet-stream', 'X-Content-Type') |
| 396 | self.assertEqual(msg['x-content-type'], 'application/octet-stream') |
| 397 | |
| 398 | def test_get_content_type_missing(self): |
| 399 | msg = Message() |
| 400 | self.assertEqual(msg.get_content_type(), 'text/plain') |
| 401 | |
| 402 | def test_get_content_type_missing_with_default_type(self): |
| 403 | msg = Message() |
| 404 | msg.set_default_type('message/rfc822') |
| 405 | self.assertEqual(msg.get_content_type(), 'message/rfc822') |
| 406 | |
| 407 | def test_get_content_type_from_message_implicit(self): |
| 408 | msg = self._msgobj('msg_30.txt') |
| 409 | self.assertEqual(msg.get_payload(0).get_content_type(), |
| 410 | 'message/rfc822') |
| 411 | |
| 412 | def test_get_content_type_from_message_explicit(self): |
| 413 | msg = self._msgobj('msg_28.txt') |
| 414 | self.assertEqual(msg.get_payload(0).get_content_type(), |
| 415 | 'message/rfc822') |
| 416 | |
| 417 | def test_get_content_type_from_message_text_plain_implicit(self): |
| 418 | msg = self._msgobj('msg_03.txt') |
| 419 | self.assertEqual(msg.get_content_type(), 'text/plain') |
| 420 | |
| 421 | def test_get_content_type_from_message_text_plain_explicit(self): |
| 422 | msg = self._msgobj('msg_01.txt') |
| 423 | self.assertEqual(msg.get_content_type(), 'text/plain') |
| 424 | |
| 425 | def test_get_content_maintype_missing(self): |
| 426 | msg = Message() |
| 427 | self.assertEqual(msg.get_content_maintype(), 'text') |
| 428 | |
| 429 | def test_get_content_maintype_missing_with_default_type(self): |
| 430 | msg = Message() |
| 431 | msg.set_default_type('message/rfc822') |
| 432 | self.assertEqual(msg.get_content_maintype(), 'message') |
| 433 | |
| 434 | def test_get_content_maintype_from_message_implicit(self): |
| 435 | msg = self._msgobj('msg_30.txt') |
| 436 | self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message') |
| 437 | |
| 438 | def test_get_content_maintype_from_message_explicit(self): |
| 439 | msg = self._msgobj('msg_28.txt') |
| 440 | self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message') |
| 441 | |
| 442 | def test_get_content_maintype_from_message_text_plain_implicit(self): |
| 443 | msg = self._msgobj('msg_03.txt') |
| 444 | self.assertEqual(msg.get_content_maintype(), 'text') |
| 445 | |
| 446 | def test_get_content_maintype_from_message_text_plain_explicit(self): |
| 447 | msg = self._msgobj('msg_01.txt') |
| 448 | self.assertEqual(msg.get_content_maintype(), 'text') |
| 449 | |
| 450 | def test_get_content_subtype_missing(self): |
| 451 | msg = Message() |
| 452 | self.assertEqual(msg.get_content_subtype(), 'plain') |
| 453 | |
| 454 | def test_get_content_subtype_missing_with_default_type(self): |
| 455 | msg = Message() |
| 456 | msg.set_default_type('message/rfc822') |
| 457 | self.assertEqual(msg.get_content_subtype(), 'rfc822') |
| 458 | |
| 459 | def test_get_content_subtype_from_message_implicit(self): |
| 460 | msg = self._msgobj('msg_30.txt') |
| 461 | self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822') |
| 462 | |
| 463 | def test_get_content_subtype_from_message_explicit(self): |
| 464 | msg = self._msgobj('msg_28.txt') |
| 465 | self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822') |
| 466 | |
| 467 | def test_get_content_subtype_from_message_text_plain_implicit(self): |
| 468 | msg = self._msgobj('msg_03.txt') |
| 469 | self.assertEqual(msg.get_content_subtype(), 'plain') |
| 470 | |
| 471 | def test_get_content_subtype_from_message_text_plain_explicit(self): |
| 472 | msg = self._msgobj('msg_01.txt') |
| 473 | self.assertEqual(msg.get_content_subtype(), 'plain') |
| 474 | |
| 475 | def test_get_content_maintype_error(self): |
| 476 | msg = Message() |
| 477 | msg['Content-Type'] = 'no-slash-in-this-string' |
| 478 | self.assertEqual(msg.get_content_maintype(), 'text') |
| 479 | |
| 480 | def test_get_content_subtype_error(self): |
| 481 | msg = Message() |
| 482 | msg['Content-Type'] = 'no-slash-in-this-string' |
| 483 | self.assertEqual(msg.get_content_subtype(), 'plain') |
| 484 | |
| 485 | def test_replace_header(self): |
| 486 | eq = self.assertEqual |
| 487 | msg = Message() |
| 488 | msg.add_header('First', 'One') |
| 489 | msg.add_header('Second', 'Two') |
| 490 | msg.add_header('Third', 'Three') |
| 491 | eq(msg.keys(), ['First', 'Second', 'Third']) |
| 492 | eq(msg.values(), ['One', 'Two', 'Three']) |
| 493 | msg.replace_header('Second', 'Twenty') |
| 494 | eq(msg.keys(), ['First', 'Second', 'Third']) |
| 495 | eq(msg.values(), ['One', 'Twenty', 'Three']) |
| 496 | msg.add_header('First', 'Eleven') |
| 497 | msg.replace_header('First', 'One Hundred') |
| 498 | eq(msg.keys(), ['First', 'Second', 'Third', 'First']) |
| 499 | eq(msg.values(), ['One Hundred', 'Twenty', 'Three', 'Eleven']) |
| 500 | self.assertRaises(KeyError, msg.replace_header, 'Fourth', 'Missing') |
| 501 | |
| 502 | def test_broken_base64_payload(self): |
| 503 | x = 'AwDp0P7//y6LwKEAcPa/6Q=9' |
| 504 | msg = Message() |
| 505 | msg['content-type'] = 'audio/x-midi' |
| 506 | msg['content-transfer-encoding'] = 'base64' |
| 507 | msg.set_payload(x) |
| 508 | self.assertEqual(msg.get_payload(decode=True), |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 509 | bytes(x, 'raw-unicode-escape')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 510 | |
| 511 | |
| 512 | |
| 513 | # Test the email.encoders module |
| 514 | class TestEncoders(unittest.TestCase): |
| 515 | def test_encode_empty_payload(self): |
| 516 | eq = self.assertEqual |
| 517 | msg = Message() |
| 518 | msg.set_charset('us-ascii') |
| 519 | eq(msg['content-transfer-encoding'], '7bit') |
| 520 | |
| 521 | def test_default_cte(self): |
| 522 | eq = self.assertEqual |
Ezio Melotti | c303c12 | 2010-04-22 11:57:12 +0000 | [diff] [blame] | 523 | # 7bit data and the default us-ascii _charset |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 524 | msg = MIMEText('hello world') |
| 525 | eq(msg['content-transfer-encoding'], '7bit') |
Ezio Melotti | c303c12 | 2010-04-22 11:57:12 +0000 | [diff] [blame] | 526 | # Similar, but with 8bit data |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 527 | msg = MIMEText('hello \xf8 world') |
| 528 | eq(msg['content-transfer-encoding'], '8bit') |
| 529 | # And now with a different charset |
| 530 | msg = MIMEText('hello \xf8 world', _charset='iso-8859-1') |
| 531 | eq(msg['content-transfer-encoding'], 'quoted-printable') |
| 532 | |
R. David Murray | e85200d | 2010-05-06 01:41:14 +0000 | [diff] [blame] | 533 | def test_encode7or8bit(self): |
| 534 | # Make sure a charset whose input character set is 8bit but |
| 535 | # whose output character set is 7bit gets a transfer-encoding |
| 536 | # of 7bit. |
| 537 | eq = self.assertEqual |
R. David Murray | 850fc85 | 2010-06-03 01:58:28 +0000 | [diff] [blame] | 538 | msg = MIMEText('æ–‡', _charset='euc-jp') |
R. David Murray | e85200d | 2010-05-06 01:41:14 +0000 | [diff] [blame] | 539 | eq(msg['content-transfer-encoding'], '7bit') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 540 | |
| 541 | |
| 542 | # Test long header wrapping |
| 543 | class TestLongHeaders(TestEmailBase): |
| 544 | def test_split_long_continuation(self): |
| 545 | eq = self.ndiffAssertEqual |
| 546 | msg = email.message_from_string("""\ |
| 547 | Subject: bug demonstration |
| 548 | \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 549 | \tmore text |
| 550 | |
| 551 | test |
| 552 | """) |
| 553 | sfp = StringIO() |
| 554 | g = Generator(sfp) |
| 555 | g.flatten(msg) |
| 556 | eq(sfp.getvalue(), """\ |
| 557 | Subject: bug demonstration |
| 558 | \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 559 | \tmore text |
| 560 | |
| 561 | test |
| 562 | """) |
| 563 | |
| 564 | def test_another_long_almost_unsplittable_header(self): |
| 565 | eq = self.ndiffAssertEqual |
| 566 | hstr = """\ |
| 567 | bug demonstration |
| 568 | \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 569 | \tmore text""" |
| 570 | h = Header(hstr, continuation_ws='\t') |
| 571 | eq(h.encode(), """\ |
| 572 | bug demonstration |
| 573 | \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 574 | \tmore text""") |
| 575 | h = Header(hstr.replace('\t', ' ')) |
| 576 | eq(h.encode(), """\ |
| 577 | bug demonstration |
| 578 | 12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 579 | more text""") |
| 580 | |
| 581 | def test_long_nonstring(self): |
| 582 | eq = self.ndiffAssertEqual |
| 583 | g = Charset("iso-8859-1") |
| 584 | cz = Charset("iso-8859-2") |
| 585 | utf8 = Charset("utf-8") |
| 586 | g_head = (b'Die Mieter treten hier ein werden mit einem Foerderband ' |
| 587 | b'komfortabel den Korridor entlang, an s\xfcdl\xfcndischen ' |
| 588 | b'Wandgem\xe4lden vorbei, gegen die rotierenden Klingen ' |
| 589 | b'bef\xf6rdert. ') |
| 590 | cz_head = (b'Finan\xe8ni metropole se hroutily pod tlakem jejich ' |
| 591 | b'd\xf9vtipu.. ') |
| 592 | utf8_head = ('\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f' |
| 593 | '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00' |
| 594 | '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c' |
| 595 | '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067' |
| 596 | '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das ' |
| 597 | 'Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder ' |
| 598 | 'die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066' |
| 599 | '\u3044\u307e\u3059\u3002') |
| 600 | h = Header(g_head, g, header_name='Subject') |
| 601 | h.append(cz_head, cz) |
| 602 | h.append(utf8_head, utf8) |
| 603 | msg = Message() |
| 604 | msg['Subject'] = h |
| 605 | sfp = StringIO() |
| 606 | g = Generator(sfp) |
| 607 | g.flatten(msg) |
| 608 | eq(sfp.getvalue(), """\ |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 609 | Subject: =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerderb?= |
| 610 | =?iso-8859-1?q?and_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndischen?= |
| 611 | =?iso-8859-1?q?_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Klingen_bef?= |
| 612 | =?iso-8859-1?q?=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se_hrouti?= |
| 613 | =?iso-8859-2?q?ly_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= =?utf-8?b?5q2j56K6?= |
| 614 | =?utf-8?b?44Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb44KT44CC5LiA?= |
| 615 | =?utf-8?b?6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go44Gv44Gn44Gf44KJ?= |
| 616 | =?utf-8?b?44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGFzIE51bnN0dWNr?= |
| 617 | =?utf-8?b?IGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWloZXJodW5kIGRhcyBPZGVyIGRp?= |
| 618 | =?utf-8?b?ZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI3jgajoqIDjgaPjgabjgYTjgb7jgZk=?= |
| 619 | =?utf-8?b?44CC?= |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 620 | |
| 621 | """) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 622 | eq(h.encode(maxlinelen=76), """\ |
| 623 | =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerde?= |
| 624 | =?iso-8859-1?q?rband_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndis?= |
| 625 | =?iso-8859-1?q?chen_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Klinge?= |
| 626 | =?iso-8859-1?q?n_bef=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se?= |
| 627 | =?iso-8859-2?q?_hroutily_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= |
| 628 | =?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb?= |
| 629 | =?utf-8?b?44KT44CC5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go?= |
| 630 | =?utf-8?b?44Gv44Gn44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBp?= |
| 631 | =?utf-8?b?c3QgZGFzIE51bnN0dWNrIGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWlo?= |
| 632 | =?utf-8?b?ZXJodW5kIGRhcyBPZGVyIGRpZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI0=?= |
| 633 | =?utf-8?b?44Go6KiA44Gj44Gm44GE44G+44GZ44CC?=""") |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 634 | |
| 635 | def test_long_header_encode(self): |
| 636 | eq = self.ndiffAssertEqual |
| 637 | h = Header('wasnipoop; giraffes="very-long-necked-animals"; ' |
| 638 | 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"', |
| 639 | header_name='X-Foobar-Spoink-Defrobnit') |
| 640 | eq(h.encode(), '''\ |
| 641 | wasnipoop; giraffes="very-long-necked-animals"; |
| 642 | spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') |
| 643 | |
| 644 | def test_long_header_encode_with_tab_continuation_is_just_a_hint(self): |
| 645 | eq = self.ndiffAssertEqual |
| 646 | h = Header('wasnipoop; giraffes="very-long-necked-animals"; ' |
| 647 | 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"', |
| 648 | header_name='X-Foobar-Spoink-Defrobnit', |
| 649 | continuation_ws='\t') |
| 650 | eq(h.encode(), '''\ |
| 651 | wasnipoop; giraffes="very-long-necked-animals"; |
| 652 | spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') |
| 653 | |
| 654 | def test_long_header_encode_with_tab_continuation(self): |
| 655 | eq = self.ndiffAssertEqual |
| 656 | h = Header('wasnipoop; giraffes="very-long-necked-animals";\t' |
| 657 | 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"', |
| 658 | header_name='X-Foobar-Spoink-Defrobnit', |
| 659 | continuation_ws='\t') |
| 660 | eq(h.encode(), '''\ |
| 661 | wasnipoop; giraffes="very-long-necked-animals"; |
| 662 | \tspooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') |
| 663 | |
| 664 | def test_header_splitter(self): |
| 665 | eq = self.ndiffAssertEqual |
| 666 | msg = MIMEText('') |
| 667 | # It'd be great if we could use add_header() here, but that doesn't |
| 668 | # guarantee an order of the parameters. |
| 669 | msg['X-Foobar-Spoink-Defrobnit'] = ( |
| 670 | 'wasnipoop; giraffes="very-long-necked-animals"; ' |
| 671 | 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"') |
| 672 | sfp = StringIO() |
| 673 | g = Generator(sfp) |
| 674 | g.flatten(msg) |
| 675 | eq(sfp.getvalue(), '''\ |
| 676 | Content-Type: text/plain; charset="us-ascii" |
| 677 | MIME-Version: 1.0 |
| 678 | Content-Transfer-Encoding: 7bit |
| 679 | X-Foobar-Spoink-Defrobnit: wasnipoop; giraffes="very-long-necked-animals"; |
| 680 | spooge="yummy"; hippos="gargantuan"; marshmallows="gooey" |
| 681 | |
| 682 | ''') |
| 683 | |
| 684 | def test_no_semis_header_splitter(self): |
| 685 | eq = self.ndiffAssertEqual |
| 686 | msg = Message() |
| 687 | msg['From'] = 'test@dom.ain' |
| 688 | msg['References'] = SPACE.join('<%d@dom.ain>' % i for i in range(10)) |
| 689 | msg.set_payload('Test') |
| 690 | sfp = StringIO() |
| 691 | g = Generator(sfp) |
| 692 | g.flatten(msg) |
| 693 | eq(sfp.getvalue(), """\ |
| 694 | From: test@dom.ain |
| 695 | References: <0@dom.ain> <1@dom.ain> <2@dom.ain> <3@dom.ain> <4@dom.ain> |
| 696 | <5@dom.ain> <6@dom.ain> <7@dom.ain> <8@dom.ain> <9@dom.ain> |
| 697 | |
| 698 | Test""") |
| 699 | |
| 700 | def test_no_split_long_header(self): |
| 701 | eq = self.ndiffAssertEqual |
| 702 | hstr = 'References: ' + 'x' * 80 |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 703 | h = Header(hstr) |
| 704 | # These come on two lines because Headers are really field value |
| 705 | # classes and don't really know about their field names. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 706 | eq(h.encode(), """\ |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 707 | References: |
| 708 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx""") |
| 709 | h = Header('x' * 80) |
| 710 | eq(h.encode(), 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 711 | |
| 712 | def test_splitting_multiple_long_lines(self): |
| 713 | eq = self.ndiffAssertEqual |
| 714 | hstr = """\ |
| 715 | from babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <mailman-admin@babylon.socal-raves.org>; Sat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 716 | \tfrom babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <mailman-admin@babylon.socal-raves.org>; Sat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 717 | \tfrom babylon.socal-raves.org (localhost [127.0.0.1]); by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; for <mailman-admin@babylon.socal-raves.org>; Sat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 718 | """ |
| 719 | h = Header(hstr, continuation_ws='\t') |
| 720 | eq(h.encode(), """\ |
| 721 | from babylon.socal-raves.org (localhost [127.0.0.1]); |
| 722 | by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; |
| 723 | for <mailman-admin@babylon.socal-raves.org>; |
| 724 | Sat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 725 | \tfrom babylon.socal-raves.org (localhost [127.0.0.1]); |
| 726 | by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; |
| 727 | for <mailman-admin@babylon.socal-raves.org>; |
| 728 | Sat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 729 | \tfrom babylon.socal-raves.org (localhost [127.0.0.1]); |
| 730 | by babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; |
| 731 | for <mailman-admin@babylon.socal-raves.org>; |
| 732 | Sat, 2 Feb 2002 17:00:06 -0800 (PST)""") |
| 733 | |
| 734 | def test_splitting_first_line_only_is_long(self): |
| 735 | eq = self.ndiffAssertEqual |
| 736 | hstr = """\ |
| 737 | from modemcable093.139-201-24.que.mc.videotron.ca ([24.201.139.93] helo=cthulhu.gerg.ca) |
| 738 | \tby kronos.mems-exchange.org with esmtp (Exim 4.05) |
| 739 | \tid 17k4h5-00034i-00 |
| 740 | \tfor test@mems-exchange.org; Wed, 28 Aug 2002 11:25:20 -0400""" |
| 741 | h = Header(hstr, maxlinelen=78, header_name='Received', |
| 742 | continuation_ws='\t') |
| 743 | eq(h.encode(), """\ |
| 744 | from modemcable093.139-201-24.que.mc.videotron.ca ([24.201.139.93] |
| 745 | helo=cthulhu.gerg.ca) |
| 746 | \tby kronos.mems-exchange.org with esmtp (Exim 4.05) |
| 747 | \tid 17k4h5-00034i-00 |
| 748 | \tfor test@mems-exchange.org; Wed, 28 Aug 2002 11:25:20 -0400""") |
| 749 | |
| 750 | def test_long_8bit_header(self): |
| 751 | eq = self.ndiffAssertEqual |
| 752 | msg = Message() |
| 753 | h = Header('Britische Regierung gibt', 'iso-8859-1', |
| 754 | header_name='Subject') |
| 755 | h.append('gr\xfcnes Licht f\xfcr Offshore-Windkraftprojekte') |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 756 | eq(h.encode(maxlinelen=76), """\ |
| 757 | =?iso-8859-1?q?Britische_Regierung_gibt_gr=FCnes_Licht_f=FCr_Offs?= |
| 758 | =?iso-8859-1?q?hore-Windkraftprojekte?=""") |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 759 | msg['Subject'] = h |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 760 | eq(msg.as_string(maxheaderlen=76), """\ |
| 761 | Subject: =?iso-8859-1?q?Britische_Regierung_gibt_gr=FCnes_Licht_f=FCr_Offs?= |
| 762 | =?iso-8859-1?q?hore-Windkraftprojekte?= |
| 763 | |
| 764 | """) |
| 765 | eq(msg.as_string(maxheaderlen=0), """\ |
| 766 | Subject: =?iso-8859-1?q?Britische_Regierung_gibt_gr=FCnes_Licht_f=FCr_Offshore-Windkraftprojekte?= |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 767 | |
| 768 | """) |
| 769 | |
| 770 | def test_long_8bit_header_no_charset(self): |
| 771 | eq = self.ndiffAssertEqual |
| 772 | msg = Message() |
Barry Warsaw | 8c57104 | 2007-08-30 19:17:18 +0000 | [diff] [blame] | 773 | header_string = ('Britische Regierung gibt gr\xfcnes Licht ' |
| 774 | 'f\xfcr Offshore-Windkraftprojekte ' |
| 775 | '<a-very-long-address@example.com>') |
| 776 | msg['Reply-To'] = header_string |
| 777 | self.assertRaises(UnicodeEncodeError, msg.as_string) |
| 778 | msg = Message() |
| 779 | msg['Reply-To'] = Header(header_string, 'utf-8', |
| 780 | header_name='Reply-To') |
| 781 | eq(msg.as_string(maxheaderlen=78), """\ |
| 782 | Reply-To: =?utf-8?q?Britische_Regierung_gibt_gr=C3=BCnes_Licht_f=C3=BCr_Offs?= |
| 783 | =?utf-8?q?hore-Windkraftprojekte_=3Ca-very-long-address=40example=2Ecom=3E?= |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 784 | |
| 785 | """) |
| 786 | |
| 787 | def test_long_to_header(self): |
| 788 | eq = self.ndiffAssertEqual |
| 789 | to = ('"Someone Test #A" <someone@eecs.umich.edu>,' |
| 790 | '<someone@eecs.umich.edu>,' |
| 791 | '"Someone Test #B" <someone@umich.edu>, ' |
| 792 | '"Someone Test #C" <someone@eecs.umich.edu>, ' |
| 793 | '"Someone Test #D" <someone@eecs.umich.edu>') |
| 794 | msg = Message() |
| 795 | msg['To'] = to |
| 796 | eq(msg.as_string(maxheaderlen=78), '''\ |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 797 | To: "Someone Test #A" <someone@eecs.umich.edu>,<someone@eecs.umich.edu>, |
Barry Warsaw | 70d61ce | 2009-03-30 23:12:30 +0000 | [diff] [blame] | 798 | "Someone Test #B" <someone@umich.edu>, |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 799 | "Someone Test #C" <someone@eecs.umich.edu>, |
| 800 | "Someone Test #D" <someone@eecs.umich.edu> |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 801 | |
| 802 | ''') |
| 803 | |
| 804 | def test_long_line_after_append(self): |
| 805 | eq = self.ndiffAssertEqual |
| 806 | s = 'This is an example of string which has almost the limit of header length.' |
| 807 | h = Header(s) |
| 808 | h.append('Add another line.') |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 809 | eq(h.encode(maxlinelen=76), """\ |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 810 | This is an example of string which has almost the limit of header length. |
| 811 | Add another line.""") |
| 812 | |
| 813 | def test_shorter_line_with_append(self): |
| 814 | eq = self.ndiffAssertEqual |
| 815 | s = 'This is a shorter line.' |
| 816 | h = Header(s) |
| 817 | h.append('Add another sentence. (Surprise?)') |
| 818 | eq(h.encode(), |
| 819 | 'This is a shorter line. Add another sentence. (Surprise?)') |
| 820 | |
| 821 | def test_long_field_name(self): |
| 822 | eq = self.ndiffAssertEqual |
| 823 | fn = 'X-Very-Very-Very-Long-Header-Name' |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 824 | gs = ('Die Mieter treten hier ein werden mit einem Foerderband ' |
| 825 | 'komfortabel den Korridor entlang, an s\xfcdl\xfcndischen ' |
| 826 | 'Wandgem\xe4lden vorbei, gegen die rotierenden Klingen ' |
| 827 | 'bef\xf6rdert. ') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 828 | h = Header(gs, 'iso-8859-1', header_name=fn) |
| 829 | # BAW: this seems broken because the first line is too long |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 830 | eq(h.encode(maxlinelen=76), """\ |
| 831 | =?iso-8859-1?q?Die_Mieter_treten_hier_e?= |
| 832 | =?iso-8859-1?q?in_werden_mit_einem_Foerderband_komfortabel_den_Korridor_e?= |
| 833 | =?iso-8859-1?q?ntlang=2C_an_s=FCdl=FCndischen_Wandgem=E4lden_vorbei=2C_ge?= |
| 834 | =?iso-8859-1?q?gen_die_rotierenden_Klingen_bef=F6rdert=2E_?=""") |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 835 | |
| 836 | def test_long_received_header(self): |
| 837 | h = ('from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) ' |
| 838 | 'by hrothgar.la.mastaler.com (tmda-ofmipd) with ESMTP; ' |
| 839 | 'Wed, 05 Mar 2003 18:10:18 -0700') |
| 840 | msg = Message() |
| 841 | msg['Received-1'] = Header(h, continuation_ws='\t') |
| 842 | msg['Received-2'] = h |
Barry Warsaw | bef9d21 | 2007-08-31 10:55:37 +0000 | [diff] [blame] | 843 | # This should be splitting on spaces not semicolons. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 844 | self.ndiffAssertEqual(msg.as_string(maxheaderlen=78), """\ |
Barry Warsaw | bef9d21 | 2007-08-31 10:55:37 +0000 | [diff] [blame] | 845 | Received-1: from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) by hrothgar.la.mastaler.com (tmda-ofmipd) with ESMTP; |
| 846 | Wed, 05 Mar 2003 18:10:18 -0700 |
| 847 | Received-2: from FOO.TLD (vizworld.acl.foo.tld [123.452.678.9]) by hrothgar.la.mastaler.com (tmda-ofmipd) with ESMTP; |
| 848 | Wed, 05 Mar 2003 18:10:18 -0700 |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 849 | |
| 850 | """) |
| 851 | |
| 852 | def test_string_headerinst_eq(self): |
| 853 | h = ('<15975.17901.207240.414604@sgigritzmann1.mathematik.' |
| 854 | 'tu-muenchen.de> (David Bremner\'s message of ' |
| 855 | '"Thu, 6 Mar 2003 13:58:21 +0100")') |
| 856 | msg = Message() |
| 857 | msg['Received-1'] = Header(h, header_name='Received-1', |
| 858 | continuation_ws='\t') |
| 859 | msg['Received-2'] = h |
Barry Warsaw | bef9d21 | 2007-08-31 10:55:37 +0000 | [diff] [blame] | 860 | # XXX This should be splitting on spaces not commas. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 861 | self.ndiffAssertEqual(msg.as_string(maxheaderlen=78), """\ |
Barry Warsaw | bef9d21 | 2007-08-31 10:55:37 +0000 | [diff] [blame] | 862 | Received-1: <15975.17901.207240.414604@sgigritzmann1.mathematik.tu-muenchen.de> (David Bremner's message of \"Thu, |
| 863 | 6 Mar 2003 13:58:21 +0100\") |
| 864 | Received-2: <15975.17901.207240.414604@sgigritzmann1.mathematik.tu-muenchen.de> (David Bremner's message of \"Thu, |
| 865 | 6 Mar 2003 13:58:21 +0100\") |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 866 | |
| 867 | """) |
| 868 | |
| 869 | def test_long_unbreakable_lines_with_continuation(self): |
| 870 | eq = self.ndiffAssertEqual |
| 871 | msg = Message() |
| 872 | t = """\ |
| 873 | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9 |
| 874 | locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp""" |
| 875 | msg['Face-1'] = t |
| 876 | msg['Face-2'] = Header(t, header_name='Face-2') |
Barry Warsaw | bef9d21 | 2007-08-31 10:55:37 +0000 | [diff] [blame] | 877 | # XXX This splitting is all wrong. It the first value line should be |
| 878 | # snug against the field name. |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 879 | eq(msg.as_string(maxheaderlen=78), """\ |
Barry Warsaw | c5a6a30 | 2007-08-31 11:19:21 +0000 | [diff] [blame] | 880 | Face-1:\x20 |
Barry Warsaw | 70d61ce | 2009-03-30 23:12:30 +0000 | [diff] [blame] | 881 | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9 |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 882 | locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp |
Barry Warsaw | c5a6a30 | 2007-08-31 11:19:21 +0000 | [diff] [blame] | 883 | Face-2:\x20 |
Barry Warsaw | bef9d21 | 2007-08-31 10:55:37 +0000 | [diff] [blame] | 884 | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEUAAAAkHiJeRUIcGBi9 |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 885 | locQDQ4zJykFBAXJfWDjAAACYUlEQVR4nF2TQY/jIAyFc6lydlG5x8Nyp1Y69wj1PN2I5gzp |
| 886 | |
| 887 | """) |
| 888 | |
| 889 | def test_another_long_multiline_header(self): |
| 890 | eq = self.ndiffAssertEqual |
| 891 | m = ('Received: from siimage.com ' |
| 892 | '([172.25.1.3]) by zima.siliconimage.com with ' |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 893 | 'Microsoft SMTPSVC(5.0.2195.4905); ' |
| 894 | 'Wed, 16 Oct 2002 07:41:11 -0700') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 895 | msg = email.message_from_string(m) |
| 896 | eq(msg.as_string(maxheaderlen=78), '''\ |
Barry Warsaw | bef9d21 | 2007-08-31 10:55:37 +0000 | [diff] [blame] | 897 | Received: from siimage.com ([172.25.1.3]) by zima.siliconimage.com with Microsoft SMTPSVC(5.0.2195.4905); |
| 898 | Wed, 16 Oct 2002 07:41:11 -0700 |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 899 | |
| 900 | ''') |
| 901 | |
| 902 | def test_long_lines_with_different_header(self): |
| 903 | eq = self.ndiffAssertEqual |
| 904 | h = ('List-Unsubscribe: ' |
| 905 | '<http://lists.sourceforge.net/lists/listinfo/spamassassin-talk>,' |
| 906 | ' <mailto:spamassassin-talk-request@lists.sourceforge.net' |
| 907 | '?subject=unsubscribe>') |
| 908 | msg = Message() |
| 909 | msg['List'] = h |
| 910 | msg['List'] = Header(h, header_name='List') |
| 911 | eq(msg.as_string(maxheaderlen=78), """\ |
| 912 | List: List-Unsubscribe: <http://lists.sourceforge.net/lists/listinfo/spamassassin-talk>, |
Barry Warsaw | bef9d21 | 2007-08-31 10:55:37 +0000 | [diff] [blame] | 913 | <mailto:spamassassin-talk-request@lists.sourceforge.net?subject=unsubscribe> |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 914 | List: List-Unsubscribe: <http://lists.sourceforge.net/lists/listinfo/spamassassin-talk>, |
Barry Warsaw | bef9d21 | 2007-08-31 10:55:37 +0000 | [diff] [blame] | 915 | <mailto:spamassassin-talk-request@lists.sourceforge.net?subject=unsubscribe> |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 916 | |
| 917 | """) |
| 918 | |
| 919 | |
| 920 | |
| 921 | # Test mangling of "From " lines in the body of a message |
| 922 | class TestFromMangling(unittest.TestCase): |
| 923 | def setUp(self): |
| 924 | self.msg = Message() |
| 925 | self.msg['From'] = 'aaa@bbb.org' |
| 926 | self.msg.set_payload("""\ |
| 927 | From the desk of A.A.A.: |
| 928 | Blah blah blah |
| 929 | """) |
| 930 | |
| 931 | def test_mangled_from(self): |
| 932 | s = StringIO() |
| 933 | g = Generator(s, mangle_from_=True) |
| 934 | g.flatten(self.msg) |
| 935 | self.assertEqual(s.getvalue(), """\ |
| 936 | From: aaa@bbb.org |
| 937 | |
| 938 | >From the desk of A.A.A.: |
| 939 | Blah blah blah |
| 940 | """) |
| 941 | |
| 942 | def test_dont_mangle_from(self): |
| 943 | s = StringIO() |
| 944 | g = Generator(s, mangle_from_=False) |
| 945 | g.flatten(self.msg) |
| 946 | self.assertEqual(s.getvalue(), """\ |
| 947 | From: aaa@bbb.org |
| 948 | |
| 949 | From the desk of A.A.A.: |
| 950 | Blah blah blah |
| 951 | """) |
| 952 | |
| 953 | |
| 954 | |
| 955 | # Test the basic MIMEAudio class |
| 956 | class TestMIMEAudio(unittest.TestCase): |
| 957 | def setUp(self): |
| 958 | # Make sure we pick up the audiotest.au that lives in email/test/data. |
| 959 | # In Python, there's an audiotest.au living in Lib/test but that isn't |
| 960 | # included in some binary distros that don't include the test |
| 961 | # package. The trailing empty string on the .join() is significant |
| 962 | # since findfile() will do a dirname(). |
| 963 | datadir = os.path.join(os.path.dirname(landmark), 'data', '') |
| 964 | with open(findfile('audiotest.au', datadir), 'rb') as fp: |
| 965 | self._audiodata = fp.read() |
| 966 | self._au = MIMEAudio(self._audiodata) |
| 967 | |
| 968 | def test_guess_minor_type(self): |
| 969 | self.assertEqual(self._au.get_content_type(), 'audio/basic') |
| 970 | |
| 971 | def test_encoding(self): |
| 972 | payload = self._au.get_payload() |
R. David Murray | 7da8f06 | 2010-06-04 16:11:08 +0000 | [diff] [blame^] | 973 | self.assertEqual(base64.decodebytes(bytes(payload, 'ascii')), |
| 974 | self._audiodata) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 975 | |
| 976 | def test_checkSetMinor(self): |
| 977 | au = MIMEAudio(self._audiodata, 'fish') |
| 978 | self.assertEqual(au.get_content_type(), 'audio/fish') |
| 979 | |
| 980 | def test_add_header(self): |
| 981 | eq = self.assertEqual |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 982 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 983 | self._au.add_header('Content-Disposition', 'attachment', |
| 984 | filename='audiotest.au') |
| 985 | eq(self._au['content-disposition'], |
| 986 | 'attachment; filename="audiotest.au"') |
| 987 | eq(self._au.get_params(header='content-disposition'), |
| 988 | [('attachment', ''), ('filename', 'audiotest.au')]) |
| 989 | eq(self._au.get_param('filename', header='content-disposition'), |
| 990 | 'audiotest.au') |
| 991 | missing = [] |
| 992 | eq(self._au.get_param('attachment', header='content-disposition'), '') |
| 993 | unless(self._au.get_param('foo', failobj=missing, |
| 994 | header='content-disposition') is missing) |
| 995 | # Try some missing stuff |
| 996 | unless(self._au.get_param('foobar', missing) is missing) |
| 997 | unless(self._au.get_param('attachment', missing, |
| 998 | header='foobar') is missing) |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | # Test the basic MIMEImage class |
| 1003 | class TestMIMEImage(unittest.TestCase): |
| 1004 | def setUp(self): |
| 1005 | with openfile('PyBanner048.gif', 'rb') as fp: |
| 1006 | self._imgdata = fp.read() |
| 1007 | self._im = MIMEImage(self._imgdata) |
| 1008 | |
| 1009 | def test_guess_minor_type(self): |
| 1010 | self.assertEqual(self._im.get_content_type(), 'image/gif') |
| 1011 | |
| 1012 | def test_encoding(self): |
| 1013 | payload = self._im.get_payload() |
R. David Murray | 7da8f06 | 2010-06-04 16:11:08 +0000 | [diff] [blame^] | 1014 | self.assertEqual(base64.decodebytes(bytes(payload, 'ascii')), |
| 1015 | self._imgdata) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1016 | |
| 1017 | def test_checkSetMinor(self): |
| 1018 | im = MIMEImage(self._imgdata, 'fish') |
| 1019 | self.assertEqual(im.get_content_type(), 'image/fish') |
| 1020 | |
| 1021 | def test_add_header(self): |
| 1022 | eq = self.assertEqual |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1023 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1024 | self._im.add_header('Content-Disposition', 'attachment', |
| 1025 | filename='dingusfish.gif') |
| 1026 | eq(self._im['content-disposition'], |
| 1027 | 'attachment; filename="dingusfish.gif"') |
| 1028 | eq(self._im.get_params(header='content-disposition'), |
| 1029 | [('attachment', ''), ('filename', 'dingusfish.gif')]) |
| 1030 | eq(self._im.get_param('filename', header='content-disposition'), |
| 1031 | 'dingusfish.gif') |
| 1032 | missing = [] |
| 1033 | eq(self._im.get_param('attachment', header='content-disposition'), '') |
| 1034 | unless(self._im.get_param('foo', failobj=missing, |
| 1035 | header='content-disposition') is missing) |
| 1036 | # Try some missing stuff |
| 1037 | unless(self._im.get_param('foobar', missing) is missing) |
| 1038 | unless(self._im.get_param('attachment', missing, |
| 1039 | header='foobar') is missing) |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | # Test the basic MIMEApplication class |
| 1044 | class TestMIMEApplication(unittest.TestCase): |
| 1045 | def test_headers(self): |
| 1046 | eq = self.assertEqual |
Barry Warsaw | 8b2af27 | 2007-08-31 03:04:26 +0000 | [diff] [blame] | 1047 | msg = MIMEApplication(b'\xfa\xfb\xfc\xfd\xfe\xff') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1048 | eq(msg.get_content_type(), 'application/octet-stream') |
| 1049 | eq(msg['content-transfer-encoding'], 'base64') |
| 1050 | |
| 1051 | def test_body(self): |
| 1052 | eq = self.assertEqual |
Barry Warsaw | 8c57104 | 2007-08-30 19:17:18 +0000 | [diff] [blame] | 1053 | bytes = b'\xfa\xfb\xfc\xfd\xfe\xff' |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1054 | msg = MIMEApplication(bytes) |
R. David Murray | 7da8f06 | 2010-06-04 16:11:08 +0000 | [diff] [blame^] | 1055 | eq(msg.get_payload(), '+vv8/f7/') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1056 | eq(msg.get_payload(decode=True), bytes) |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | # Test the basic MIMEText class |
| 1061 | class TestMIMEText(unittest.TestCase): |
| 1062 | def setUp(self): |
| 1063 | self._msg = MIMEText('hello there') |
| 1064 | |
| 1065 | def test_types(self): |
| 1066 | eq = self.assertEqual |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1067 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1068 | eq(self._msg.get_content_type(), 'text/plain') |
| 1069 | eq(self._msg.get_param('charset'), 'us-ascii') |
| 1070 | missing = [] |
| 1071 | unless(self._msg.get_param('foobar', missing) is missing) |
| 1072 | unless(self._msg.get_param('charset', missing, header='foobar') |
| 1073 | is missing) |
| 1074 | |
| 1075 | def test_payload(self): |
| 1076 | self.assertEqual(self._msg.get_payload(), 'hello there') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1077 | self.assertTrue(not self._msg.is_multipart()) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1078 | |
| 1079 | def test_charset(self): |
| 1080 | eq = self.assertEqual |
| 1081 | msg = MIMEText('hello there', _charset='us-ascii') |
| 1082 | eq(msg.get_charset().input_charset, 'us-ascii') |
| 1083 | eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 1084 | |
R. David Murray | 850fc85 | 2010-06-03 01:58:28 +0000 | [diff] [blame] | 1085 | def test_7bit_input(self): |
| 1086 | eq = self.assertEqual |
| 1087 | msg = MIMEText('hello there', _charset='us-ascii') |
| 1088 | eq(msg.get_charset().input_charset, 'us-ascii') |
| 1089 | eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 1090 | |
| 1091 | def test_7bit_input_no_charset(self): |
| 1092 | eq = self.assertEqual |
| 1093 | msg = MIMEText('hello there') |
| 1094 | eq(msg.get_charset(), 'us-ascii') |
| 1095 | eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 1096 | self.assertTrue('hello there' in msg.as_string()) |
| 1097 | |
| 1098 | def test_utf8_input(self): |
| 1099 | teststr = '\u043a\u0438\u0440\u0438\u043b\u0438\u0446\u0430' |
| 1100 | eq = self.assertEqual |
| 1101 | msg = MIMEText(teststr, _charset='utf-8') |
| 1102 | eq(msg.get_charset().output_charset, 'utf-8') |
| 1103 | eq(msg['content-type'], 'text/plain; charset="utf-8"') |
| 1104 | eq(msg.get_payload(decode=True), teststr.encode('utf-8')) |
| 1105 | |
| 1106 | @unittest.skip("can't fix because of backward compat in email5, " |
| 1107 | "will fix in email6") |
| 1108 | def test_utf8_input_no_charset(self): |
| 1109 | teststr = '\u043a\u0438\u0440\u0438\u043b\u0438\u0446\u0430' |
| 1110 | self.assertRaises(UnicodeEncodeError, MIMEText, teststr) |
| 1111 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1112 | |
| 1113 | |
| 1114 | # Test complicated multipart/* messages |
| 1115 | class TestMultipart(TestEmailBase): |
| 1116 | def setUp(self): |
| 1117 | with openfile('PyBanner048.gif', 'rb') as fp: |
| 1118 | data = fp.read() |
| 1119 | container = MIMEBase('multipart', 'mixed', boundary='BOUNDARY') |
| 1120 | image = MIMEImage(data, name='dingusfish.gif') |
| 1121 | image.add_header('content-disposition', 'attachment', |
| 1122 | filename='dingusfish.gif') |
| 1123 | intro = MIMEText('''\ |
| 1124 | Hi there, |
| 1125 | |
| 1126 | This is the dingus fish. |
| 1127 | ''') |
| 1128 | container.attach(intro) |
| 1129 | container.attach(image) |
| 1130 | container['From'] = 'Barry <barry@digicool.com>' |
| 1131 | container['To'] = 'Dingus Lovers <cravindogs@cravindogs.com>' |
| 1132 | container['Subject'] = 'Here is your dingus fish' |
| 1133 | |
| 1134 | now = 987809702.54848599 |
| 1135 | timetuple = time.localtime(now) |
| 1136 | if timetuple[-1] == 0: |
| 1137 | tzsecs = time.timezone |
| 1138 | else: |
| 1139 | tzsecs = time.altzone |
| 1140 | if tzsecs > 0: |
| 1141 | sign = '-' |
| 1142 | else: |
| 1143 | sign = '+' |
| 1144 | tzoffset = ' %s%04d' % (sign, tzsecs / 36) |
| 1145 | container['Date'] = time.strftime( |
| 1146 | '%a, %d %b %Y %H:%M:%S', |
| 1147 | time.localtime(now)) + tzoffset |
| 1148 | self._msg = container |
| 1149 | self._im = image |
| 1150 | self._txt = intro |
| 1151 | |
| 1152 | def test_hierarchy(self): |
| 1153 | # convenience |
| 1154 | eq = self.assertEqual |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1155 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1156 | raises = self.assertRaises |
| 1157 | # tests |
| 1158 | m = self._msg |
| 1159 | unless(m.is_multipart()) |
| 1160 | eq(m.get_content_type(), 'multipart/mixed') |
| 1161 | eq(len(m.get_payload()), 2) |
| 1162 | raises(IndexError, m.get_payload, 2) |
| 1163 | m0 = m.get_payload(0) |
| 1164 | m1 = m.get_payload(1) |
| 1165 | unless(m0 is self._txt) |
| 1166 | unless(m1 is self._im) |
| 1167 | eq(m.get_payload(), [m0, m1]) |
| 1168 | unless(not m0.is_multipart()) |
| 1169 | unless(not m1.is_multipart()) |
| 1170 | |
| 1171 | def test_empty_multipart_idempotent(self): |
| 1172 | text = """\ |
| 1173 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1174 | MIME-Version: 1.0 |
| 1175 | Subject: A subject |
| 1176 | To: aperson@dom.ain |
| 1177 | From: bperson@dom.ain |
| 1178 | |
| 1179 | |
| 1180 | --BOUNDARY |
| 1181 | |
| 1182 | |
| 1183 | --BOUNDARY-- |
| 1184 | """ |
| 1185 | msg = Parser().parsestr(text) |
| 1186 | self.ndiffAssertEqual(text, msg.as_string()) |
| 1187 | |
| 1188 | def test_no_parts_in_a_multipart_with_none_epilogue(self): |
| 1189 | outer = MIMEBase('multipart', 'mixed') |
| 1190 | outer['Subject'] = 'A subject' |
| 1191 | outer['To'] = 'aperson@dom.ain' |
| 1192 | outer['From'] = 'bperson@dom.ain' |
| 1193 | outer.set_boundary('BOUNDARY') |
| 1194 | self.ndiffAssertEqual(outer.as_string(), '''\ |
| 1195 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1196 | MIME-Version: 1.0 |
| 1197 | Subject: A subject |
| 1198 | To: aperson@dom.ain |
| 1199 | From: bperson@dom.ain |
| 1200 | |
| 1201 | --BOUNDARY |
| 1202 | |
| 1203 | --BOUNDARY--''') |
| 1204 | |
| 1205 | def test_no_parts_in_a_multipart_with_empty_epilogue(self): |
| 1206 | outer = MIMEBase('multipart', 'mixed') |
| 1207 | outer['Subject'] = 'A subject' |
| 1208 | outer['To'] = 'aperson@dom.ain' |
| 1209 | outer['From'] = 'bperson@dom.ain' |
| 1210 | outer.preamble = '' |
| 1211 | outer.epilogue = '' |
| 1212 | outer.set_boundary('BOUNDARY') |
| 1213 | self.ndiffAssertEqual(outer.as_string(), '''\ |
| 1214 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1215 | MIME-Version: 1.0 |
| 1216 | Subject: A subject |
| 1217 | To: aperson@dom.ain |
| 1218 | From: bperson@dom.ain |
| 1219 | |
| 1220 | |
| 1221 | --BOUNDARY |
| 1222 | |
| 1223 | --BOUNDARY-- |
| 1224 | ''') |
| 1225 | |
| 1226 | def test_one_part_in_a_multipart(self): |
| 1227 | eq = self.ndiffAssertEqual |
| 1228 | outer = MIMEBase('multipart', 'mixed') |
| 1229 | outer['Subject'] = 'A subject' |
| 1230 | outer['To'] = 'aperson@dom.ain' |
| 1231 | outer['From'] = 'bperson@dom.ain' |
| 1232 | outer.set_boundary('BOUNDARY') |
| 1233 | msg = MIMEText('hello world') |
| 1234 | outer.attach(msg) |
| 1235 | eq(outer.as_string(), '''\ |
| 1236 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1237 | MIME-Version: 1.0 |
| 1238 | Subject: A subject |
| 1239 | To: aperson@dom.ain |
| 1240 | From: bperson@dom.ain |
| 1241 | |
| 1242 | --BOUNDARY |
| 1243 | Content-Type: text/plain; charset="us-ascii" |
| 1244 | MIME-Version: 1.0 |
| 1245 | Content-Transfer-Encoding: 7bit |
| 1246 | |
| 1247 | hello world |
| 1248 | --BOUNDARY--''') |
| 1249 | |
| 1250 | def test_seq_parts_in_a_multipart_with_empty_preamble(self): |
| 1251 | eq = self.ndiffAssertEqual |
| 1252 | outer = MIMEBase('multipart', 'mixed') |
| 1253 | outer['Subject'] = 'A subject' |
| 1254 | outer['To'] = 'aperson@dom.ain' |
| 1255 | outer['From'] = 'bperson@dom.ain' |
| 1256 | outer.preamble = '' |
| 1257 | msg = MIMEText('hello world') |
| 1258 | outer.attach(msg) |
| 1259 | outer.set_boundary('BOUNDARY') |
| 1260 | eq(outer.as_string(), '''\ |
| 1261 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1262 | MIME-Version: 1.0 |
| 1263 | Subject: A subject |
| 1264 | To: aperson@dom.ain |
| 1265 | From: bperson@dom.ain |
| 1266 | |
| 1267 | |
| 1268 | --BOUNDARY |
| 1269 | Content-Type: text/plain; charset="us-ascii" |
| 1270 | MIME-Version: 1.0 |
| 1271 | Content-Transfer-Encoding: 7bit |
| 1272 | |
| 1273 | hello world |
| 1274 | --BOUNDARY--''') |
| 1275 | |
| 1276 | |
| 1277 | def test_seq_parts_in_a_multipart_with_none_preamble(self): |
| 1278 | eq = self.ndiffAssertEqual |
| 1279 | outer = MIMEBase('multipart', 'mixed') |
| 1280 | outer['Subject'] = 'A subject' |
| 1281 | outer['To'] = 'aperson@dom.ain' |
| 1282 | outer['From'] = 'bperson@dom.ain' |
| 1283 | outer.preamble = None |
| 1284 | msg = MIMEText('hello world') |
| 1285 | outer.attach(msg) |
| 1286 | outer.set_boundary('BOUNDARY') |
| 1287 | eq(outer.as_string(), '''\ |
| 1288 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1289 | MIME-Version: 1.0 |
| 1290 | Subject: A subject |
| 1291 | To: aperson@dom.ain |
| 1292 | From: bperson@dom.ain |
| 1293 | |
| 1294 | --BOUNDARY |
| 1295 | Content-Type: text/plain; charset="us-ascii" |
| 1296 | MIME-Version: 1.0 |
| 1297 | Content-Transfer-Encoding: 7bit |
| 1298 | |
| 1299 | hello world |
| 1300 | --BOUNDARY--''') |
| 1301 | |
| 1302 | |
| 1303 | def test_seq_parts_in_a_multipart_with_none_epilogue(self): |
| 1304 | eq = self.ndiffAssertEqual |
| 1305 | outer = MIMEBase('multipart', 'mixed') |
| 1306 | outer['Subject'] = 'A subject' |
| 1307 | outer['To'] = 'aperson@dom.ain' |
| 1308 | outer['From'] = 'bperson@dom.ain' |
| 1309 | outer.epilogue = None |
| 1310 | msg = MIMEText('hello world') |
| 1311 | outer.attach(msg) |
| 1312 | outer.set_boundary('BOUNDARY') |
| 1313 | eq(outer.as_string(), '''\ |
| 1314 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1315 | MIME-Version: 1.0 |
| 1316 | Subject: A subject |
| 1317 | To: aperson@dom.ain |
| 1318 | From: bperson@dom.ain |
| 1319 | |
| 1320 | --BOUNDARY |
| 1321 | Content-Type: text/plain; charset="us-ascii" |
| 1322 | MIME-Version: 1.0 |
| 1323 | Content-Transfer-Encoding: 7bit |
| 1324 | |
| 1325 | hello world |
| 1326 | --BOUNDARY--''') |
| 1327 | |
| 1328 | |
| 1329 | def test_seq_parts_in_a_multipart_with_empty_epilogue(self): |
| 1330 | eq = self.ndiffAssertEqual |
| 1331 | outer = MIMEBase('multipart', 'mixed') |
| 1332 | outer['Subject'] = 'A subject' |
| 1333 | outer['To'] = 'aperson@dom.ain' |
| 1334 | outer['From'] = 'bperson@dom.ain' |
| 1335 | outer.epilogue = '' |
| 1336 | msg = MIMEText('hello world') |
| 1337 | outer.attach(msg) |
| 1338 | outer.set_boundary('BOUNDARY') |
| 1339 | eq(outer.as_string(), '''\ |
| 1340 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1341 | MIME-Version: 1.0 |
| 1342 | Subject: A subject |
| 1343 | To: aperson@dom.ain |
| 1344 | From: bperson@dom.ain |
| 1345 | |
| 1346 | --BOUNDARY |
| 1347 | Content-Type: text/plain; charset="us-ascii" |
| 1348 | MIME-Version: 1.0 |
| 1349 | Content-Transfer-Encoding: 7bit |
| 1350 | |
| 1351 | hello world |
| 1352 | --BOUNDARY-- |
| 1353 | ''') |
| 1354 | |
| 1355 | |
| 1356 | def test_seq_parts_in_a_multipart_with_nl_epilogue(self): |
| 1357 | eq = self.ndiffAssertEqual |
| 1358 | outer = MIMEBase('multipart', 'mixed') |
| 1359 | outer['Subject'] = 'A subject' |
| 1360 | outer['To'] = 'aperson@dom.ain' |
| 1361 | outer['From'] = 'bperson@dom.ain' |
| 1362 | outer.epilogue = '\n' |
| 1363 | msg = MIMEText('hello world') |
| 1364 | outer.attach(msg) |
| 1365 | outer.set_boundary('BOUNDARY') |
| 1366 | eq(outer.as_string(), '''\ |
| 1367 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1368 | MIME-Version: 1.0 |
| 1369 | Subject: A subject |
| 1370 | To: aperson@dom.ain |
| 1371 | From: bperson@dom.ain |
| 1372 | |
| 1373 | --BOUNDARY |
| 1374 | Content-Type: text/plain; charset="us-ascii" |
| 1375 | MIME-Version: 1.0 |
| 1376 | Content-Transfer-Encoding: 7bit |
| 1377 | |
| 1378 | hello world |
| 1379 | --BOUNDARY-- |
| 1380 | |
| 1381 | ''') |
| 1382 | |
| 1383 | def test_message_external_body(self): |
| 1384 | eq = self.assertEqual |
| 1385 | msg = self._msgobj('msg_36.txt') |
| 1386 | eq(len(msg.get_payload()), 2) |
| 1387 | msg1 = msg.get_payload(1) |
| 1388 | eq(msg1.get_content_type(), 'multipart/alternative') |
| 1389 | eq(len(msg1.get_payload()), 2) |
| 1390 | for subpart in msg1.get_payload(): |
| 1391 | eq(subpart.get_content_type(), 'message/external-body') |
| 1392 | eq(len(subpart.get_payload()), 1) |
| 1393 | subsubpart = subpart.get_payload(0) |
| 1394 | eq(subsubpart.get_content_type(), 'text/plain') |
| 1395 | |
| 1396 | def test_double_boundary(self): |
| 1397 | # msg_37.txt is a multipart that contains two dash-boundary's in a |
| 1398 | # row. Our interpretation of RFC 2046 calls for ignoring the second |
| 1399 | # and subsequent boundaries. |
| 1400 | msg = self._msgobj('msg_37.txt') |
| 1401 | self.assertEqual(len(msg.get_payload()), 3) |
| 1402 | |
| 1403 | def test_nested_inner_contains_outer_boundary(self): |
| 1404 | eq = self.ndiffAssertEqual |
| 1405 | # msg_38.txt has an inner part that contains outer boundaries. My |
| 1406 | # interpretation of RFC 2046 (based on sections 5.1 and 5.1.2) say |
| 1407 | # these are illegal and should be interpreted as unterminated inner |
| 1408 | # parts. |
| 1409 | msg = self._msgobj('msg_38.txt') |
| 1410 | sfp = StringIO() |
| 1411 | iterators._structure(msg, sfp) |
| 1412 | eq(sfp.getvalue(), """\ |
| 1413 | multipart/mixed |
| 1414 | multipart/mixed |
| 1415 | multipart/alternative |
| 1416 | text/plain |
| 1417 | text/plain |
| 1418 | text/plain |
| 1419 | text/plain |
| 1420 | """) |
| 1421 | |
| 1422 | def test_nested_with_same_boundary(self): |
| 1423 | eq = self.ndiffAssertEqual |
| 1424 | # msg 39.txt is similarly evil in that it's got inner parts that use |
| 1425 | # the same boundary as outer parts. Again, I believe the way this is |
| 1426 | # parsed is closest to the spirit of RFC 2046 |
| 1427 | msg = self._msgobj('msg_39.txt') |
| 1428 | sfp = StringIO() |
| 1429 | iterators._structure(msg, sfp) |
| 1430 | eq(sfp.getvalue(), """\ |
| 1431 | multipart/mixed |
| 1432 | multipart/mixed |
| 1433 | multipart/alternative |
| 1434 | application/octet-stream |
| 1435 | application/octet-stream |
| 1436 | text/plain |
| 1437 | """) |
| 1438 | |
| 1439 | def test_boundary_in_non_multipart(self): |
| 1440 | msg = self._msgobj('msg_40.txt') |
| 1441 | self.assertEqual(msg.as_string(), '''\ |
| 1442 | MIME-Version: 1.0 |
| 1443 | Content-Type: text/html; boundary="--961284236552522269" |
| 1444 | |
| 1445 | ----961284236552522269 |
| 1446 | Content-Type: text/html; |
| 1447 | Content-Transfer-Encoding: 7Bit |
| 1448 | |
| 1449 | <html></html> |
| 1450 | |
| 1451 | ----961284236552522269-- |
| 1452 | ''') |
| 1453 | |
| 1454 | def test_boundary_with_leading_space(self): |
| 1455 | eq = self.assertEqual |
| 1456 | msg = email.message_from_string('''\ |
| 1457 | MIME-Version: 1.0 |
| 1458 | Content-Type: multipart/mixed; boundary=" XXXX" |
| 1459 | |
| 1460 | -- XXXX |
| 1461 | Content-Type: text/plain |
| 1462 | |
| 1463 | |
| 1464 | -- XXXX |
| 1465 | Content-Type: text/plain |
| 1466 | |
| 1467 | -- XXXX-- |
| 1468 | ''') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1469 | self.assertTrue(msg.is_multipart()) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1470 | eq(msg.get_boundary(), ' XXXX') |
| 1471 | eq(len(msg.get_payload()), 2) |
| 1472 | |
| 1473 | def test_boundary_without_trailing_newline(self): |
| 1474 | m = Parser().parsestr("""\ |
| 1475 | Content-Type: multipart/mixed; boundary="===============0012394164==" |
| 1476 | MIME-Version: 1.0 |
| 1477 | |
| 1478 | --===============0012394164== |
| 1479 | Content-Type: image/file1.jpg |
| 1480 | MIME-Version: 1.0 |
| 1481 | Content-Transfer-Encoding: base64 |
| 1482 | |
| 1483 | YXNkZg== |
| 1484 | --===============0012394164==--""") |
| 1485 | self.assertEquals(m.get_payload(0).get_payload(), 'YXNkZg==') |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | # Test some badly formatted messages |
| 1490 | class TestNonConformant(TestEmailBase): |
| 1491 | def test_parse_missing_minor_type(self): |
| 1492 | eq = self.assertEqual |
| 1493 | msg = self._msgobj('msg_14.txt') |
| 1494 | eq(msg.get_content_type(), 'text/plain') |
| 1495 | eq(msg.get_content_maintype(), 'text') |
| 1496 | eq(msg.get_content_subtype(), 'plain') |
| 1497 | |
| 1498 | def test_same_boundary_inner_outer(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1499 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1500 | msg = self._msgobj('msg_15.txt') |
| 1501 | # XXX We can probably eventually do better |
| 1502 | inner = msg.get_payload(0) |
| 1503 | unless(hasattr(inner, 'defects')) |
| 1504 | self.assertEqual(len(inner.defects), 1) |
| 1505 | unless(isinstance(inner.defects[0], |
| 1506 | errors.StartBoundaryNotFoundDefect)) |
| 1507 | |
| 1508 | def test_multipart_no_boundary(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1509 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1510 | msg = self._msgobj('msg_25.txt') |
| 1511 | unless(isinstance(msg.get_payload(), str)) |
| 1512 | self.assertEqual(len(msg.defects), 2) |
| 1513 | unless(isinstance(msg.defects[0], errors.NoBoundaryInMultipartDefect)) |
| 1514 | unless(isinstance(msg.defects[1], |
| 1515 | errors.MultipartInvariantViolationDefect)) |
| 1516 | |
| 1517 | def test_invalid_content_type(self): |
| 1518 | eq = self.assertEqual |
| 1519 | neq = self.ndiffAssertEqual |
| 1520 | msg = Message() |
| 1521 | # RFC 2045, $5.2 says invalid yields text/plain |
| 1522 | msg['Content-Type'] = 'text' |
| 1523 | eq(msg.get_content_maintype(), 'text') |
| 1524 | eq(msg.get_content_subtype(), 'plain') |
| 1525 | eq(msg.get_content_type(), 'text/plain') |
| 1526 | # Clear the old value and try something /really/ invalid |
| 1527 | del msg['content-type'] |
| 1528 | msg['Content-Type'] = 'foo' |
| 1529 | eq(msg.get_content_maintype(), 'text') |
| 1530 | eq(msg.get_content_subtype(), 'plain') |
| 1531 | eq(msg.get_content_type(), 'text/plain') |
| 1532 | # Still, make sure that the message is idempotently generated |
| 1533 | s = StringIO() |
| 1534 | g = Generator(s) |
| 1535 | g.flatten(msg) |
| 1536 | neq(s.getvalue(), 'Content-Type: foo\n\n') |
| 1537 | |
| 1538 | def test_no_start_boundary(self): |
| 1539 | eq = self.ndiffAssertEqual |
| 1540 | msg = self._msgobj('msg_31.txt') |
| 1541 | eq(msg.get_payload(), """\ |
| 1542 | --BOUNDARY |
| 1543 | Content-Type: text/plain |
| 1544 | |
| 1545 | message 1 |
| 1546 | |
| 1547 | --BOUNDARY |
| 1548 | Content-Type: text/plain |
| 1549 | |
| 1550 | message 2 |
| 1551 | |
| 1552 | --BOUNDARY-- |
| 1553 | """) |
| 1554 | |
| 1555 | def test_no_separating_blank_line(self): |
| 1556 | eq = self.ndiffAssertEqual |
| 1557 | msg = self._msgobj('msg_35.txt') |
| 1558 | eq(msg.as_string(), """\ |
| 1559 | From: aperson@dom.ain |
| 1560 | To: bperson@dom.ain |
| 1561 | Subject: here's something interesting |
| 1562 | |
| 1563 | counter to RFC 2822, there's no separating newline here |
| 1564 | """) |
| 1565 | |
| 1566 | def test_lying_multipart(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1567 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1568 | msg = self._msgobj('msg_41.txt') |
| 1569 | unless(hasattr(msg, 'defects')) |
| 1570 | self.assertEqual(len(msg.defects), 2) |
| 1571 | unless(isinstance(msg.defects[0], errors.NoBoundaryInMultipartDefect)) |
| 1572 | unless(isinstance(msg.defects[1], |
| 1573 | errors.MultipartInvariantViolationDefect)) |
| 1574 | |
| 1575 | def test_missing_start_boundary(self): |
| 1576 | outer = self._msgobj('msg_42.txt') |
| 1577 | # The message structure is: |
| 1578 | # |
| 1579 | # multipart/mixed |
| 1580 | # text/plain |
| 1581 | # message/rfc822 |
| 1582 | # multipart/mixed [*] |
| 1583 | # |
| 1584 | # [*] This message is missing its start boundary |
| 1585 | bad = outer.get_payload(1).get_payload(0) |
| 1586 | self.assertEqual(len(bad.defects), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1587 | self.assertTrue(isinstance(bad.defects[0], |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1588 | errors.StartBoundaryNotFoundDefect)) |
| 1589 | |
| 1590 | def test_first_line_is_continuation_header(self): |
| 1591 | eq = self.assertEqual |
| 1592 | m = ' Line 1\nLine 2\nLine 3' |
| 1593 | msg = email.message_from_string(m) |
| 1594 | eq(msg.keys(), []) |
| 1595 | eq(msg.get_payload(), 'Line 2\nLine 3') |
| 1596 | eq(len(msg.defects), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1597 | self.assertTrue(isinstance(msg.defects[0], |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1598 | errors.FirstHeaderLineIsContinuationDefect)) |
| 1599 | eq(msg.defects[0].line, ' Line 1\n') |
| 1600 | |
| 1601 | |
| 1602 | |
| 1603 | # Test RFC 2047 header encoding and decoding |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 1604 | class TestRFC2047(TestEmailBase): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1605 | def test_rfc2047_multiline(self): |
| 1606 | eq = self.assertEqual |
| 1607 | s = """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz |
| 1608 | foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?=""" |
| 1609 | dh = decode_header(s) |
| 1610 | eq(dh, [ |
| 1611 | (b'Re:', None), |
| 1612 | (b'r\x8aksm\x9arg\x8cs', 'mac-iceland'), |
| 1613 | (b'baz foo bar', None), |
| 1614 | (b'r\x8aksm\x9arg\x8cs', 'mac-iceland')]) |
| 1615 | header = make_header(dh) |
| 1616 | eq(str(header), |
| 1617 | 'Re: r\xe4ksm\xf6rg\xe5s baz foo bar r\xe4ksm\xf6rg\xe5s') |
Barry Warsaw | 00b3422 | 2007-08-31 02:35:00 +0000 | [diff] [blame] | 1618 | self.ndiffAssertEqual(header.encode(maxlinelen=76), """\ |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 1619 | Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?= |
| 1620 | =?mac-iceland?q?=9Arg=8Cs?=""") |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1621 | |
| 1622 | def test_whitespace_eater_unicode(self): |
| 1623 | eq = self.assertEqual |
| 1624 | s = '=?ISO-8859-1?Q?Andr=E9?= Pirard <pirard@dom.ain>' |
| 1625 | dh = decode_header(s) |
| 1626 | eq(dh, [(b'Andr\xe9', 'iso-8859-1'), |
| 1627 | (b'Pirard <pirard@dom.ain>', None)]) |
| 1628 | header = str(make_header(dh)) |
| 1629 | eq(header, 'Andr\xe9 Pirard <pirard@dom.ain>') |
| 1630 | |
| 1631 | def test_whitespace_eater_unicode_2(self): |
| 1632 | eq = self.assertEqual |
| 1633 | s = 'The =?iso-8859-1?b?cXVpY2sgYnJvd24gZm94?= jumped over the =?iso-8859-1?b?bGF6eSBkb2c=?=' |
| 1634 | dh = decode_header(s) |
| 1635 | eq(dh, [(b'The', None), (b'quick brown fox', 'iso-8859-1'), |
| 1636 | (b'jumped over the', None), (b'lazy dog', 'iso-8859-1')]) |
| 1637 | hu = str(make_header(dh)) |
| 1638 | eq(hu, 'The quick brown fox jumped over the lazy dog') |
| 1639 | |
| 1640 | def test_rfc2047_missing_whitespace(self): |
| 1641 | s = 'Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord' |
| 1642 | dh = decode_header(s) |
| 1643 | self.assertEqual(dh, [(s, None)]) |
| 1644 | |
| 1645 | def test_rfc2047_with_whitespace(self): |
| 1646 | s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord' |
| 1647 | dh = decode_header(s) |
| 1648 | self.assertEqual(dh, [(b'Sm', None), (b'\xf6', 'iso-8859-1'), |
| 1649 | (b'rg', None), (b'\xe5', 'iso-8859-1'), |
| 1650 | (b'sbord', None)]) |
| 1651 | |
| 1652 | |
| 1653 | |
| 1654 | # Test the MIMEMessage class |
| 1655 | class TestMIMEMessage(TestEmailBase): |
| 1656 | def setUp(self): |
| 1657 | with openfile('msg_11.txt') as fp: |
| 1658 | self._text = fp.read() |
| 1659 | |
| 1660 | def test_type_error(self): |
| 1661 | self.assertRaises(TypeError, MIMEMessage, 'a plain string') |
| 1662 | |
| 1663 | def test_valid_argument(self): |
| 1664 | eq = self.assertEqual |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1665 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1666 | subject = 'A sub-message' |
| 1667 | m = Message() |
| 1668 | m['Subject'] = subject |
| 1669 | r = MIMEMessage(m) |
| 1670 | eq(r.get_content_type(), 'message/rfc822') |
| 1671 | payload = r.get_payload() |
| 1672 | unless(isinstance(payload, list)) |
| 1673 | eq(len(payload), 1) |
| 1674 | subpart = payload[0] |
| 1675 | unless(subpart is m) |
| 1676 | eq(subpart['subject'], subject) |
| 1677 | |
| 1678 | def test_bad_multipart(self): |
| 1679 | eq = self.assertEqual |
| 1680 | msg1 = Message() |
| 1681 | msg1['Subject'] = 'subpart 1' |
| 1682 | msg2 = Message() |
| 1683 | msg2['Subject'] = 'subpart 2' |
| 1684 | r = MIMEMessage(msg1) |
| 1685 | self.assertRaises(errors.MultipartConversionError, r.attach, msg2) |
| 1686 | |
| 1687 | def test_generate(self): |
| 1688 | # First craft the message to be encapsulated |
| 1689 | m = Message() |
| 1690 | m['Subject'] = 'An enclosed message' |
| 1691 | m.set_payload('Here is the body of the message.\n') |
| 1692 | r = MIMEMessage(m) |
| 1693 | r['Subject'] = 'The enclosing message' |
| 1694 | s = StringIO() |
| 1695 | g = Generator(s) |
| 1696 | g.flatten(r) |
| 1697 | self.assertEqual(s.getvalue(), """\ |
| 1698 | Content-Type: message/rfc822 |
| 1699 | MIME-Version: 1.0 |
| 1700 | Subject: The enclosing message |
| 1701 | |
| 1702 | Subject: An enclosed message |
| 1703 | |
| 1704 | Here is the body of the message. |
| 1705 | """) |
| 1706 | |
| 1707 | def test_parse_message_rfc822(self): |
| 1708 | eq = self.assertEqual |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1709 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1710 | msg = self._msgobj('msg_11.txt') |
| 1711 | eq(msg.get_content_type(), 'message/rfc822') |
| 1712 | payload = msg.get_payload() |
| 1713 | unless(isinstance(payload, list)) |
| 1714 | eq(len(payload), 1) |
| 1715 | submsg = payload[0] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1716 | self.assertTrue(isinstance(submsg, Message)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1717 | eq(submsg['subject'], 'An enclosed message') |
| 1718 | eq(submsg.get_payload(), 'Here is the body of the message.\n') |
| 1719 | |
| 1720 | def test_dsn(self): |
| 1721 | eq = self.assertEqual |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1722 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1723 | # msg 16 is a Delivery Status Notification, see RFC 1894 |
| 1724 | msg = self._msgobj('msg_16.txt') |
| 1725 | eq(msg.get_content_type(), 'multipart/report') |
| 1726 | unless(msg.is_multipart()) |
| 1727 | eq(len(msg.get_payload()), 3) |
| 1728 | # Subpart 1 is a text/plain, human readable section |
| 1729 | subpart = msg.get_payload(0) |
| 1730 | eq(subpart.get_content_type(), 'text/plain') |
| 1731 | eq(subpart.get_payload(), """\ |
| 1732 | This report relates to a message you sent with the following header fields: |
| 1733 | |
| 1734 | Message-id: <002001c144a6$8752e060$56104586@oxy.edu> |
| 1735 | Date: Sun, 23 Sep 2001 20:10:55 -0700 |
| 1736 | From: "Ian T. Henry" <henryi@oxy.edu> |
| 1737 | To: SoCal Raves <scr@socal-raves.org> |
| 1738 | Subject: [scr] yeah for Ians!! |
| 1739 | |
| 1740 | Your message cannot be delivered to the following recipients: |
| 1741 | |
| 1742 | Recipient address: jangel1@cougar.noc.ucla.edu |
| 1743 | Reason: recipient reached disk quota |
| 1744 | |
| 1745 | """) |
| 1746 | # Subpart 2 contains the machine parsable DSN information. It |
| 1747 | # consists of two blocks of headers, represented by two nested Message |
| 1748 | # objects. |
| 1749 | subpart = msg.get_payload(1) |
| 1750 | eq(subpart.get_content_type(), 'message/delivery-status') |
| 1751 | eq(len(subpart.get_payload()), 2) |
| 1752 | # message/delivery-status should treat each block as a bunch of |
| 1753 | # headers, i.e. a bunch of Message objects. |
| 1754 | dsn1 = subpart.get_payload(0) |
| 1755 | unless(isinstance(dsn1, Message)) |
| 1756 | eq(dsn1['original-envelope-id'], '0GK500B4HD0888@cougar.noc.ucla.edu') |
| 1757 | eq(dsn1.get_param('dns', header='reporting-mta'), '') |
| 1758 | # Try a missing one <wink> |
| 1759 | eq(dsn1.get_param('nsd', header='reporting-mta'), None) |
| 1760 | dsn2 = subpart.get_payload(1) |
| 1761 | unless(isinstance(dsn2, Message)) |
| 1762 | eq(dsn2['action'], 'failed') |
| 1763 | eq(dsn2.get_params(header='original-recipient'), |
| 1764 | [('rfc822', ''), ('jangel1@cougar.noc.ucla.edu', '')]) |
| 1765 | eq(dsn2.get_param('rfc822', header='final-recipient'), '') |
| 1766 | # Subpart 3 is the original message |
| 1767 | subpart = msg.get_payload(2) |
| 1768 | eq(subpart.get_content_type(), 'message/rfc822') |
| 1769 | payload = subpart.get_payload() |
| 1770 | unless(isinstance(payload, list)) |
| 1771 | eq(len(payload), 1) |
| 1772 | subsubpart = payload[0] |
| 1773 | unless(isinstance(subsubpart, Message)) |
| 1774 | eq(subsubpart.get_content_type(), 'text/plain') |
| 1775 | eq(subsubpart['message-id'], |
| 1776 | '<002001c144a6$8752e060$56104586@oxy.edu>') |
| 1777 | |
| 1778 | def test_epilogue(self): |
| 1779 | eq = self.ndiffAssertEqual |
| 1780 | with openfile('msg_21.txt') as fp: |
| 1781 | text = fp.read() |
| 1782 | msg = Message() |
| 1783 | msg['From'] = 'aperson@dom.ain' |
| 1784 | msg['To'] = 'bperson@dom.ain' |
| 1785 | msg['Subject'] = 'Test' |
| 1786 | msg.preamble = 'MIME message' |
| 1787 | msg.epilogue = 'End of MIME message\n' |
| 1788 | msg1 = MIMEText('One') |
| 1789 | msg2 = MIMEText('Two') |
| 1790 | msg.add_header('Content-Type', 'multipart/mixed', boundary='BOUNDARY') |
| 1791 | msg.attach(msg1) |
| 1792 | msg.attach(msg2) |
| 1793 | sfp = StringIO() |
| 1794 | g = Generator(sfp) |
| 1795 | g.flatten(msg) |
| 1796 | eq(sfp.getvalue(), text) |
| 1797 | |
| 1798 | def test_no_nl_preamble(self): |
| 1799 | eq = self.ndiffAssertEqual |
| 1800 | msg = Message() |
| 1801 | msg['From'] = 'aperson@dom.ain' |
| 1802 | msg['To'] = 'bperson@dom.ain' |
| 1803 | msg['Subject'] = 'Test' |
| 1804 | msg.preamble = 'MIME message' |
| 1805 | msg.epilogue = '' |
| 1806 | msg1 = MIMEText('One') |
| 1807 | msg2 = MIMEText('Two') |
| 1808 | msg.add_header('Content-Type', 'multipart/mixed', boundary='BOUNDARY') |
| 1809 | msg.attach(msg1) |
| 1810 | msg.attach(msg2) |
| 1811 | eq(msg.as_string(), """\ |
| 1812 | From: aperson@dom.ain |
| 1813 | To: bperson@dom.ain |
| 1814 | Subject: Test |
| 1815 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 1816 | |
| 1817 | MIME message |
| 1818 | --BOUNDARY |
| 1819 | Content-Type: text/plain; charset="us-ascii" |
| 1820 | MIME-Version: 1.0 |
| 1821 | Content-Transfer-Encoding: 7bit |
| 1822 | |
| 1823 | One |
| 1824 | --BOUNDARY |
| 1825 | Content-Type: text/plain; charset="us-ascii" |
| 1826 | MIME-Version: 1.0 |
| 1827 | Content-Transfer-Encoding: 7bit |
| 1828 | |
| 1829 | Two |
| 1830 | --BOUNDARY-- |
| 1831 | """) |
| 1832 | |
| 1833 | def test_default_type(self): |
| 1834 | eq = self.assertEqual |
| 1835 | with openfile('msg_30.txt') as fp: |
| 1836 | msg = email.message_from_file(fp) |
| 1837 | container1 = msg.get_payload(0) |
| 1838 | eq(container1.get_default_type(), 'message/rfc822') |
| 1839 | eq(container1.get_content_type(), 'message/rfc822') |
| 1840 | container2 = msg.get_payload(1) |
| 1841 | eq(container2.get_default_type(), 'message/rfc822') |
| 1842 | eq(container2.get_content_type(), 'message/rfc822') |
| 1843 | container1a = container1.get_payload(0) |
| 1844 | eq(container1a.get_default_type(), 'text/plain') |
| 1845 | eq(container1a.get_content_type(), 'text/plain') |
| 1846 | container2a = container2.get_payload(0) |
| 1847 | eq(container2a.get_default_type(), 'text/plain') |
| 1848 | eq(container2a.get_content_type(), 'text/plain') |
| 1849 | |
| 1850 | def test_default_type_with_explicit_container_type(self): |
| 1851 | eq = self.assertEqual |
| 1852 | with openfile('msg_28.txt') as fp: |
| 1853 | msg = email.message_from_file(fp) |
| 1854 | container1 = msg.get_payload(0) |
| 1855 | eq(container1.get_default_type(), 'message/rfc822') |
| 1856 | eq(container1.get_content_type(), 'message/rfc822') |
| 1857 | container2 = msg.get_payload(1) |
| 1858 | eq(container2.get_default_type(), 'message/rfc822') |
| 1859 | eq(container2.get_content_type(), 'message/rfc822') |
| 1860 | container1a = container1.get_payload(0) |
| 1861 | eq(container1a.get_default_type(), 'text/plain') |
| 1862 | eq(container1a.get_content_type(), 'text/plain') |
| 1863 | container2a = container2.get_payload(0) |
| 1864 | eq(container2a.get_default_type(), 'text/plain') |
| 1865 | eq(container2a.get_content_type(), 'text/plain') |
| 1866 | |
| 1867 | def test_default_type_non_parsed(self): |
| 1868 | eq = self.assertEqual |
| 1869 | neq = self.ndiffAssertEqual |
| 1870 | # Set up container |
| 1871 | container = MIMEMultipart('digest', 'BOUNDARY') |
| 1872 | container.epilogue = '' |
| 1873 | # Set up subparts |
| 1874 | subpart1a = MIMEText('message 1\n') |
| 1875 | subpart2a = MIMEText('message 2\n') |
| 1876 | subpart1 = MIMEMessage(subpart1a) |
| 1877 | subpart2 = MIMEMessage(subpart2a) |
| 1878 | container.attach(subpart1) |
| 1879 | container.attach(subpart2) |
| 1880 | eq(subpart1.get_content_type(), 'message/rfc822') |
| 1881 | eq(subpart1.get_default_type(), 'message/rfc822') |
| 1882 | eq(subpart2.get_content_type(), 'message/rfc822') |
| 1883 | eq(subpart2.get_default_type(), 'message/rfc822') |
| 1884 | neq(container.as_string(0), '''\ |
| 1885 | Content-Type: multipart/digest; boundary="BOUNDARY" |
| 1886 | MIME-Version: 1.0 |
| 1887 | |
| 1888 | --BOUNDARY |
| 1889 | Content-Type: message/rfc822 |
| 1890 | MIME-Version: 1.0 |
| 1891 | |
| 1892 | Content-Type: text/plain; charset="us-ascii" |
| 1893 | MIME-Version: 1.0 |
| 1894 | Content-Transfer-Encoding: 7bit |
| 1895 | |
| 1896 | message 1 |
| 1897 | |
| 1898 | --BOUNDARY |
| 1899 | Content-Type: message/rfc822 |
| 1900 | MIME-Version: 1.0 |
| 1901 | |
| 1902 | Content-Type: text/plain; charset="us-ascii" |
| 1903 | MIME-Version: 1.0 |
| 1904 | Content-Transfer-Encoding: 7bit |
| 1905 | |
| 1906 | message 2 |
| 1907 | |
| 1908 | --BOUNDARY-- |
| 1909 | ''') |
| 1910 | del subpart1['content-type'] |
| 1911 | del subpart1['mime-version'] |
| 1912 | del subpart2['content-type'] |
| 1913 | del subpart2['mime-version'] |
| 1914 | eq(subpart1.get_content_type(), 'message/rfc822') |
| 1915 | eq(subpart1.get_default_type(), 'message/rfc822') |
| 1916 | eq(subpart2.get_content_type(), 'message/rfc822') |
| 1917 | eq(subpart2.get_default_type(), 'message/rfc822') |
| 1918 | neq(container.as_string(0), '''\ |
| 1919 | Content-Type: multipart/digest; boundary="BOUNDARY" |
| 1920 | MIME-Version: 1.0 |
| 1921 | |
| 1922 | --BOUNDARY |
| 1923 | |
| 1924 | Content-Type: text/plain; charset="us-ascii" |
| 1925 | MIME-Version: 1.0 |
| 1926 | Content-Transfer-Encoding: 7bit |
| 1927 | |
| 1928 | message 1 |
| 1929 | |
| 1930 | --BOUNDARY |
| 1931 | |
| 1932 | Content-Type: text/plain; charset="us-ascii" |
| 1933 | MIME-Version: 1.0 |
| 1934 | Content-Transfer-Encoding: 7bit |
| 1935 | |
| 1936 | message 2 |
| 1937 | |
| 1938 | --BOUNDARY-- |
| 1939 | ''') |
| 1940 | |
| 1941 | def test_mime_attachments_in_constructor(self): |
| 1942 | eq = self.assertEqual |
| 1943 | text1 = MIMEText('') |
| 1944 | text2 = MIMEText('') |
| 1945 | msg = MIMEMultipart(_subparts=(text1, text2)) |
| 1946 | eq(len(msg.get_payload()), 2) |
| 1947 | eq(msg.get_payload(0), text1) |
| 1948 | eq(msg.get_payload(1), text2) |
| 1949 | |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 1950 | def test_default_multipart_constructor(self): |
| 1951 | msg = MIMEMultipart() |
| 1952 | self.assertTrue(msg.is_multipart()) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1953 | |
| 1954 | |
| 1955 | # A general test of parser->model->generator idempotency. IOW, read a message |
| 1956 | # in, parse it into a message object tree, then without touching the tree, |
| 1957 | # regenerate the plain text. The original text and the transformed text |
| 1958 | # should be identical. Note: that we ignore the Unix-From since that may |
| 1959 | # contain a changed date. |
| 1960 | class TestIdempotent(TestEmailBase): |
| 1961 | def _msgobj(self, filename): |
| 1962 | with openfile(filename) as fp: |
| 1963 | data = fp.read() |
| 1964 | msg = email.message_from_string(data) |
| 1965 | return msg, data |
| 1966 | |
| 1967 | def _idempotent(self, msg, text): |
| 1968 | eq = self.ndiffAssertEqual |
| 1969 | s = StringIO() |
| 1970 | g = Generator(s, maxheaderlen=0) |
| 1971 | g.flatten(msg) |
| 1972 | eq(text, s.getvalue()) |
| 1973 | |
| 1974 | def test_parse_text_message(self): |
| 1975 | eq = self.assertEquals |
| 1976 | msg, text = self._msgobj('msg_01.txt') |
| 1977 | eq(msg.get_content_type(), 'text/plain') |
| 1978 | eq(msg.get_content_maintype(), 'text') |
| 1979 | eq(msg.get_content_subtype(), 'plain') |
| 1980 | eq(msg.get_params()[1], ('charset', 'us-ascii')) |
| 1981 | eq(msg.get_param('charset'), 'us-ascii') |
| 1982 | eq(msg.preamble, None) |
| 1983 | eq(msg.epilogue, None) |
| 1984 | self._idempotent(msg, text) |
| 1985 | |
| 1986 | def test_parse_untyped_message(self): |
| 1987 | eq = self.assertEquals |
| 1988 | msg, text = self._msgobj('msg_03.txt') |
| 1989 | eq(msg.get_content_type(), 'text/plain') |
| 1990 | eq(msg.get_params(), None) |
| 1991 | eq(msg.get_param('charset'), None) |
| 1992 | self._idempotent(msg, text) |
| 1993 | |
| 1994 | def test_simple_multipart(self): |
| 1995 | msg, text = self._msgobj('msg_04.txt') |
| 1996 | self._idempotent(msg, text) |
| 1997 | |
| 1998 | def test_MIME_digest(self): |
| 1999 | msg, text = self._msgobj('msg_02.txt') |
| 2000 | self._idempotent(msg, text) |
| 2001 | |
| 2002 | def test_long_header(self): |
| 2003 | msg, text = self._msgobj('msg_27.txt') |
| 2004 | self._idempotent(msg, text) |
| 2005 | |
| 2006 | def test_MIME_digest_with_part_headers(self): |
| 2007 | msg, text = self._msgobj('msg_28.txt') |
| 2008 | self._idempotent(msg, text) |
| 2009 | |
| 2010 | def test_mixed_with_image(self): |
| 2011 | msg, text = self._msgobj('msg_06.txt') |
| 2012 | self._idempotent(msg, text) |
| 2013 | |
| 2014 | def test_multipart_report(self): |
| 2015 | msg, text = self._msgobj('msg_05.txt') |
| 2016 | self._idempotent(msg, text) |
| 2017 | |
| 2018 | def test_dsn(self): |
| 2019 | msg, text = self._msgobj('msg_16.txt') |
| 2020 | self._idempotent(msg, text) |
| 2021 | |
| 2022 | def test_preamble_epilogue(self): |
| 2023 | msg, text = self._msgobj('msg_21.txt') |
| 2024 | self._idempotent(msg, text) |
| 2025 | |
| 2026 | def test_multipart_one_part(self): |
| 2027 | msg, text = self._msgobj('msg_23.txt') |
| 2028 | self._idempotent(msg, text) |
| 2029 | |
| 2030 | def test_multipart_no_parts(self): |
| 2031 | msg, text = self._msgobj('msg_24.txt') |
| 2032 | self._idempotent(msg, text) |
| 2033 | |
| 2034 | def test_no_start_boundary(self): |
| 2035 | msg, text = self._msgobj('msg_31.txt') |
| 2036 | self._idempotent(msg, text) |
| 2037 | |
| 2038 | def test_rfc2231_charset(self): |
| 2039 | msg, text = self._msgobj('msg_32.txt') |
| 2040 | self._idempotent(msg, text) |
| 2041 | |
| 2042 | def test_more_rfc2231_parameters(self): |
| 2043 | msg, text = self._msgobj('msg_33.txt') |
| 2044 | self._idempotent(msg, text) |
| 2045 | |
| 2046 | def test_text_plain_in_a_multipart_digest(self): |
| 2047 | msg, text = self._msgobj('msg_34.txt') |
| 2048 | self._idempotent(msg, text) |
| 2049 | |
| 2050 | def test_nested_multipart_mixeds(self): |
| 2051 | msg, text = self._msgobj('msg_12a.txt') |
| 2052 | self._idempotent(msg, text) |
| 2053 | |
| 2054 | def test_message_external_body_idempotent(self): |
| 2055 | msg, text = self._msgobj('msg_36.txt') |
| 2056 | self._idempotent(msg, text) |
| 2057 | |
| 2058 | def test_content_type(self): |
| 2059 | eq = self.assertEquals |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2060 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2061 | # Get a message object and reset the seek pointer for other tests |
| 2062 | msg, text = self._msgobj('msg_05.txt') |
| 2063 | eq(msg.get_content_type(), 'multipart/report') |
| 2064 | # Test the Content-Type: parameters |
| 2065 | params = {} |
| 2066 | for pk, pv in msg.get_params(): |
| 2067 | params[pk] = pv |
| 2068 | eq(params['report-type'], 'delivery-status') |
| 2069 | eq(params['boundary'], 'D1690A7AC1.996856090/mail.example.com') |
| 2070 | eq(msg.preamble, 'This is a MIME-encapsulated message.\n') |
| 2071 | eq(msg.epilogue, '\n') |
| 2072 | eq(len(msg.get_payload()), 3) |
| 2073 | # Make sure the subparts are what we expect |
| 2074 | msg1 = msg.get_payload(0) |
| 2075 | eq(msg1.get_content_type(), 'text/plain') |
| 2076 | eq(msg1.get_payload(), 'Yadda yadda yadda\n') |
| 2077 | msg2 = msg.get_payload(1) |
| 2078 | eq(msg2.get_content_type(), 'text/plain') |
| 2079 | eq(msg2.get_payload(), 'Yadda yadda yadda\n') |
| 2080 | msg3 = msg.get_payload(2) |
| 2081 | eq(msg3.get_content_type(), 'message/rfc822') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2082 | self.assertTrue(isinstance(msg3, Message)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2083 | payload = msg3.get_payload() |
| 2084 | unless(isinstance(payload, list)) |
| 2085 | eq(len(payload), 1) |
| 2086 | msg4 = payload[0] |
| 2087 | unless(isinstance(msg4, Message)) |
| 2088 | eq(msg4.get_payload(), 'Yadda yadda yadda\n') |
| 2089 | |
| 2090 | def test_parser(self): |
| 2091 | eq = self.assertEquals |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2092 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2093 | msg, text = self._msgobj('msg_06.txt') |
| 2094 | # Check some of the outer headers |
| 2095 | eq(msg.get_content_type(), 'message/rfc822') |
| 2096 | # Make sure the payload is a list of exactly one sub-Message, and that |
| 2097 | # that submessage has a type of text/plain |
| 2098 | payload = msg.get_payload() |
| 2099 | unless(isinstance(payload, list)) |
| 2100 | eq(len(payload), 1) |
| 2101 | msg1 = payload[0] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2102 | self.assertTrue(isinstance(msg1, Message)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2103 | eq(msg1.get_content_type(), 'text/plain') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2104 | self.assertTrue(isinstance(msg1.get_payload(), str)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2105 | eq(msg1.get_payload(), '\n') |
| 2106 | |
| 2107 | |
| 2108 | |
| 2109 | # Test various other bits of the package's functionality |
| 2110 | class TestMiscellaneous(TestEmailBase): |
| 2111 | def test_message_from_string(self): |
| 2112 | with openfile('msg_01.txt') as fp: |
| 2113 | text = fp.read() |
| 2114 | msg = email.message_from_string(text) |
| 2115 | s = StringIO() |
| 2116 | # Don't wrap/continue long headers since we're trying to test |
| 2117 | # idempotency. |
| 2118 | g = Generator(s, maxheaderlen=0) |
| 2119 | g.flatten(msg) |
| 2120 | self.assertEqual(text, s.getvalue()) |
| 2121 | |
| 2122 | def test_message_from_file(self): |
| 2123 | with openfile('msg_01.txt') as fp: |
| 2124 | text = fp.read() |
| 2125 | fp.seek(0) |
| 2126 | msg = email.message_from_file(fp) |
| 2127 | s = StringIO() |
| 2128 | # Don't wrap/continue long headers since we're trying to test |
| 2129 | # idempotency. |
| 2130 | g = Generator(s, maxheaderlen=0) |
| 2131 | g.flatten(msg) |
| 2132 | self.assertEqual(text, s.getvalue()) |
| 2133 | |
| 2134 | def test_message_from_string_with_class(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2135 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2136 | with openfile('msg_01.txt') as fp: |
| 2137 | text = fp.read() |
| 2138 | |
| 2139 | # Create a subclass |
| 2140 | class MyMessage(Message): |
| 2141 | pass |
| 2142 | |
| 2143 | msg = email.message_from_string(text, MyMessage) |
| 2144 | unless(isinstance(msg, MyMessage)) |
| 2145 | # Try something more complicated |
| 2146 | with openfile('msg_02.txt') as fp: |
| 2147 | text = fp.read() |
| 2148 | msg = email.message_from_string(text, MyMessage) |
| 2149 | for subpart in msg.walk(): |
| 2150 | unless(isinstance(subpart, MyMessage)) |
| 2151 | |
| 2152 | def test_message_from_file_with_class(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2153 | unless = self.assertTrue |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2154 | # Create a subclass |
| 2155 | class MyMessage(Message): |
| 2156 | pass |
| 2157 | |
| 2158 | with openfile('msg_01.txt') as fp: |
| 2159 | msg = email.message_from_file(fp, MyMessage) |
| 2160 | unless(isinstance(msg, MyMessage)) |
| 2161 | # Try something more complicated |
| 2162 | with openfile('msg_02.txt') as fp: |
| 2163 | msg = email.message_from_file(fp, MyMessage) |
| 2164 | for subpart in msg.walk(): |
| 2165 | unless(isinstance(subpart, MyMessage)) |
| 2166 | |
| 2167 | def test__all__(self): |
| 2168 | module = __import__('email') |
| 2169 | # Can't use sorted() here due to Python 2.3 compatibility |
| 2170 | all = module.__all__[:] |
| 2171 | all.sort() |
| 2172 | self.assertEqual(all, [ |
| 2173 | 'base64mime', 'charset', 'encoders', 'errors', 'generator', |
| 2174 | 'header', 'iterators', 'message', 'message_from_file', |
| 2175 | 'message_from_string', 'mime', 'parser', |
| 2176 | 'quoprimime', 'utils', |
| 2177 | ]) |
| 2178 | |
| 2179 | def test_formatdate(self): |
| 2180 | now = time.time() |
| 2181 | self.assertEqual(utils.parsedate(utils.formatdate(now))[:6], |
| 2182 | time.gmtime(now)[:6]) |
| 2183 | |
| 2184 | def test_formatdate_localtime(self): |
| 2185 | now = time.time() |
| 2186 | self.assertEqual( |
| 2187 | utils.parsedate(utils.formatdate(now, localtime=True))[:6], |
| 2188 | time.localtime(now)[:6]) |
| 2189 | |
| 2190 | def test_formatdate_usegmt(self): |
| 2191 | now = time.time() |
| 2192 | self.assertEqual( |
| 2193 | utils.formatdate(now, localtime=False), |
| 2194 | time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now))) |
| 2195 | self.assertEqual( |
| 2196 | utils.formatdate(now, localtime=False, usegmt=True), |
| 2197 | time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now))) |
| 2198 | |
| 2199 | def test_parsedate_none(self): |
| 2200 | self.assertEqual(utils.parsedate(''), None) |
| 2201 | |
| 2202 | def test_parsedate_compact(self): |
| 2203 | # The FWS after the comma is optional |
| 2204 | self.assertEqual(utils.parsedate('Wed,3 Apr 2002 14:58:26 +0800'), |
| 2205 | utils.parsedate('Wed, 3 Apr 2002 14:58:26 +0800')) |
| 2206 | |
| 2207 | def test_parsedate_no_dayofweek(self): |
| 2208 | eq = self.assertEqual |
| 2209 | eq(utils.parsedate_tz('25 Feb 2003 13:47:26 -0800'), |
| 2210 | (2003, 2, 25, 13, 47, 26, 0, 1, -1, -28800)) |
| 2211 | |
| 2212 | def test_parsedate_compact_no_dayofweek(self): |
| 2213 | eq = self.assertEqual |
| 2214 | eq(utils.parsedate_tz('5 Feb 2003 13:47:26 -0800'), |
| 2215 | (2003, 2, 5, 13, 47, 26, 0, 1, -1, -28800)) |
| 2216 | |
| 2217 | def test_parsedate_acceptable_to_time_functions(self): |
| 2218 | eq = self.assertEqual |
| 2219 | timetup = utils.parsedate('5 Feb 2003 13:47:26 -0800') |
| 2220 | t = int(time.mktime(timetup)) |
| 2221 | eq(time.localtime(t)[:6], timetup[:6]) |
| 2222 | eq(int(time.strftime('%Y', timetup)), 2003) |
| 2223 | timetup = utils.parsedate_tz('5 Feb 2003 13:47:26 -0800') |
| 2224 | t = int(time.mktime(timetup[:9])) |
| 2225 | eq(time.localtime(t)[:6], timetup[:6]) |
| 2226 | eq(int(time.strftime('%Y', timetup[:9])), 2003) |
| 2227 | |
| 2228 | def test_parseaddr_empty(self): |
| 2229 | self.assertEqual(utils.parseaddr('<>'), ('', '')) |
| 2230 | self.assertEqual(utils.formataddr(utils.parseaddr('<>')), '') |
| 2231 | |
| 2232 | def test_noquote_dump(self): |
| 2233 | self.assertEqual( |
| 2234 | utils.formataddr(('A Silly Person', 'person@dom.ain')), |
| 2235 | 'A Silly Person <person@dom.ain>') |
| 2236 | |
| 2237 | def test_escape_dump(self): |
| 2238 | self.assertEqual( |
| 2239 | utils.formataddr(('A (Very) Silly Person', 'person@dom.ain')), |
| 2240 | r'"A \(Very\) Silly Person" <person@dom.ain>') |
| 2241 | a = r'A \(Special\) Person' |
| 2242 | b = 'person@dom.ain' |
| 2243 | self.assertEqual(utils.parseaddr(utils.formataddr((a, b))), (a, b)) |
| 2244 | |
| 2245 | def test_escape_backslashes(self): |
| 2246 | self.assertEqual( |
| 2247 | utils.formataddr(('Arthur \Backslash\ Foobar', 'person@dom.ain')), |
| 2248 | r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>') |
| 2249 | a = r'Arthur \Backslash\ Foobar' |
| 2250 | b = 'person@dom.ain' |
| 2251 | self.assertEqual(utils.parseaddr(utils.formataddr((a, b))), (a, b)) |
| 2252 | |
| 2253 | def test_name_with_dot(self): |
| 2254 | x = 'John X. Doe <jxd@example.com>' |
| 2255 | y = '"John X. Doe" <jxd@example.com>' |
| 2256 | a, b = ('John X. Doe', 'jxd@example.com') |
| 2257 | self.assertEqual(utils.parseaddr(x), (a, b)) |
| 2258 | self.assertEqual(utils.parseaddr(y), (a, b)) |
| 2259 | # formataddr() quotes the name if there's a dot in it |
| 2260 | self.assertEqual(utils.formataddr((a, b)), y) |
| 2261 | |
| 2262 | def test_multiline_from_comment(self): |
| 2263 | x = """\ |
| 2264 | Foo |
| 2265 | \tBar <foo@example.com>""" |
| 2266 | self.assertEqual(utils.parseaddr(x), ('Foo Bar', 'foo@example.com')) |
| 2267 | |
| 2268 | def test_quote_dump(self): |
| 2269 | self.assertEqual( |
| 2270 | utils.formataddr(('A Silly; Person', 'person@dom.ain')), |
| 2271 | r'"A Silly; Person" <person@dom.ain>') |
| 2272 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2273 | def test_charset_richcomparisons(self): |
| 2274 | eq = self.assertEqual |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2275 | ne = self.assertNotEqual |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2276 | cset1 = Charset() |
| 2277 | cset2 = Charset() |
| 2278 | eq(cset1, 'us-ascii') |
| 2279 | eq(cset1, 'US-ASCII') |
| 2280 | eq(cset1, 'Us-AsCiI') |
| 2281 | eq('us-ascii', cset1) |
| 2282 | eq('US-ASCII', cset1) |
| 2283 | eq('Us-AsCiI', cset1) |
| 2284 | ne(cset1, 'usascii') |
| 2285 | ne(cset1, 'USASCII') |
| 2286 | ne(cset1, 'UsAsCiI') |
| 2287 | ne('usascii', cset1) |
| 2288 | ne('USASCII', cset1) |
| 2289 | ne('UsAsCiI', cset1) |
| 2290 | eq(cset1, cset2) |
| 2291 | eq(cset2, cset1) |
| 2292 | |
| 2293 | def test_getaddresses(self): |
| 2294 | eq = self.assertEqual |
| 2295 | eq(utils.getaddresses(['aperson@dom.ain (Al Person)', |
| 2296 | 'Bud Person <bperson@dom.ain>']), |
| 2297 | [('Al Person', 'aperson@dom.ain'), |
| 2298 | ('Bud Person', 'bperson@dom.ain')]) |
| 2299 | |
| 2300 | def test_getaddresses_nasty(self): |
| 2301 | eq = self.assertEqual |
| 2302 | eq(utils.getaddresses(['foo: ;']), [('', '')]) |
| 2303 | eq(utils.getaddresses( |
| 2304 | ['[]*-- =~$']), |
| 2305 | [('', ''), ('', ''), ('', '*--')]) |
| 2306 | eq(utils.getaddresses( |
| 2307 | ['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']), |
| 2308 | [('', ''), ('Jason R. Mastaler', 'jason@dom.ain')]) |
| 2309 | |
| 2310 | def test_getaddresses_embedded_comment(self): |
| 2311 | """Test proper handling of a nested comment""" |
| 2312 | eq = self.assertEqual |
| 2313 | addrs = utils.getaddresses(['User ((nested comment)) <foo@bar.com>']) |
| 2314 | eq(addrs[0][1], 'foo@bar.com') |
| 2315 | |
| 2316 | def test_utils_quote_unquote(self): |
| 2317 | eq = self.assertEqual |
| 2318 | msg = Message() |
| 2319 | msg.add_header('content-disposition', 'attachment', |
| 2320 | filename='foo\\wacky"name') |
| 2321 | eq(msg.get_filename(), 'foo\\wacky"name') |
| 2322 | |
| 2323 | def test_get_body_encoding_with_bogus_charset(self): |
| 2324 | charset = Charset('not a charset') |
| 2325 | self.assertEqual(charset.get_body_encoding(), 'base64') |
| 2326 | |
| 2327 | def test_get_body_encoding_with_uppercase_charset(self): |
| 2328 | eq = self.assertEqual |
| 2329 | msg = Message() |
| 2330 | msg['Content-Type'] = 'text/plain; charset=UTF-8' |
| 2331 | eq(msg['content-type'], 'text/plain; charset=UTF-8') |
| 2332 | charsets = msg.get_charsets() |
| 2333 | eq(len(charsets), 1) |
| 2334 | eq(charsets[0], 'utf-8') |
| 2335 | charset = Charset(charsets[0]) |
| 2336 | eq(charset.get_body_encoding(), 'base64') |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 2337 | msg.set_payload(b'hello world', charset=charset) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2338 | eq(msg.get_payload(), 'aGVsbG8gd29ybGQ=\n') |
| 2339 | eq(msg.get_payload(decode=True), b'hello world') |
| 2340 | eq(msg['content-transfer-encoding'], 'base64') |
| 2341 | # Try another one |
| 2342 | msg = Message() |
| 2343 | msg['Content-Type'] = 'text/plain; charset="US-ASCII"' |
| 2344 | charsets = msg.get_charsets() |
| 2345 | eq(len(charsets), 1) |
| 2346 | eq(charsets[0], 'us-ascii') |
| 2347 | charset = Charset(charsets[0]) |
| 2348 | eq(charset.get_body_encoding(), encoders.encode_7or8bit) |
| 2349 | msg.set_payload('hello world', charset=charset) |
| 2350 | eq(msg.get_payload(), 'hello world') |
| 2351 | eq(msg['content-transfer-encoding'], '7bit') |
| 2352 | |
| 2353 | def test_charsets_case_insensitive(self): |
| 2354 | lc = Charset('us-ascii') |
| 2355 | uc = Charset('US-ASCII') |
| 2356 | self.assertEqual(lc.get_body_encoding(), uc.get_body_encoding()) |
| 2357 | |
| 2358 | def test_partial_falls_inside_message_delivery_status(self): |
| 2359 | eq = self.ndiffAssertEqual |
| 2360 | # The Parser interface provides chunks of data to FeedParser in 8192 |
| 2361 | # byte gulps. SF bug #1076485 found one of those chunks inside |
| 2362 | # message/delivery-status header block, which triggered an |
| 2363 | # unreadline() of NeedMoreData. |
| 2364 | msg = self._msgobj('msg_43.txt') |
| 2365 | sfp = StringIO() |
| 2366 | iterators._structure(msg, sfp) |
| 2367 | eq(sfp.getvalue(), """\ |
| 2368 | multipart/report |
| 2369 | text/plain |
| 2370 | message/delivery-status |
| 2371 | text/plain |
| 2372 | text/plain |
| 2373 | text/plain |
| 2374 | text/plain |
| 2375 | text/plain |
| 2376 | text/plain |
| 2377 | text/plain |
| 2378 | text/plain |
| 2379 | text/plain |
| 2380 | text/plain |
| 2381 | text/plain |
| 2382 | text/plain |
| 2383 | text/plain |
| 2384 | text/plain |
| 2385 | text/plain |
| 2386 | text/plain |
| 2387 | text/plain |
| 2388 | text/plain |
| 2389 | text/plain |
| 2390 | text/plain |
| 2391 | text/plain |
| 2392 | text/plain |
| 2393 | text/plain |
| 2394 | text/plain |
| 2395 | text/plain |
| 2396 | text/plain |
| 2397 | text/rfc822-headers |
| 2398 | """) |
| 2399 | |
| 2400 | |
| 2401 | |
| 2402 | # Test the iterator/generators |
| 2403 | class TestIterators(TestEmailBase): |
| 2404 | def test_body_line_iterator(self): |
| 2405 | eq = self.assertEqual |
| 2406 | neq = self.ndiffAssertEqual |
| 2407 | # First a simple non-multipart message |
| 2408 | msg = self._msgobj('msg_01.txt') |
| 2409 | it = iterators.body_line_iterator(msg) |
| 2410 | lines = list(it) |
| 2411 | eq(len(lines), 6) |
| 2412 | neq(EMPTYSTRING.join(lines), msg.get_payload()) |
| 2413 | # Now a more complicated multipart |
| 2414 | msg = self._msgobj('msg_02.txt') |
| 2415 | it = iterators.body_line_iterator(msg) |
| 2416 | lines = list(it) |
| 2417 | eq(len(lines), 43) |
| 2418 | with openfile('msg_19.txt') as fp: |
| 2419 | neq(EMPTYSTRING.join(lines), fp.read()) |
| 2420 | |
| 2421 | def test_typed_subpart_iterator(self): |
| 2422 | eq = self.assertEqual |
| 2423 | msg = self._msgobj('msg_04.txt') |
| 2424 | it = iterators.typed_subpart_iterator(msg, 'text') |
| 2425 | lines = [] |
| 2426 | subparts = 0 |
| 2427 | for subpart in it: |
| 2428 | subparts += 1 |
| 2429 | lines.append(subpart.get_payload()) |
| 2430 | eq(subparts, 2) |
| 2431 | eq(EMPTYSTRING.join(lines), """\ |
| 2432 | a simple kind of mirror |
| 2433 | to reflect upon our own |
| 2434 | a simple kind of mirror |
| 2435 | to reflect upon our own |
| 2436 | """) |
| 2437 | |
| 2438 | def test_typed_subpart_iterator_default_type(self): |
| 2439 | eq = self.assertEqual |
| 2440 | msg = self._msgobj('msg_03.txt') |
| 2441 | it = iterators.typed_subpart_iterator(msg, 'text', 'plain') |
| 2442 | lines = [] |
| 2443 | subparts = 0 |
| 2444 | for subpart in it: |
| 2445 | subparts += 1 |
| 2446 | lines.append(subpart.get_payload()) |
| 2447 | eq(subparts, 1) |
| 2448 | eq(EMPTYSTRING.join(lines), """\ |
| 2449 | |
| 2450 | Hi, |
| 2451 | |
| 2452 | Do you like this message? |
| 2453 | |
| 2454 | -Me |
| 2455 | """) |
| 2456 | |
| 2457 | |
| 2458 | |
| 2459 | class TestParsers(TestEmailBase): |
| 2460 | def test_header_parser(self): |
| 2461 | eq = self.assertEqual |
| 2462 | # Parse only the headers of a complex multipart MIME document |
| 2463 | with openfile('msg_02.txt') as fp: |
| 2464 | msg = HeaderParser().parse(fp) |
| 2465 | eq(msg['from'], 'ppp-request@zzz.org') |
| 2466 | eq(msg['to'], 'ppp@zzz.org') |
| 2467 | eq(msg.get_content_type(), 'multipart/mixed') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2468 | self.assertFalse(msg.is_multipart()) |
| 2469 | self.assertTrue(isinstance(msg.get_payload(), str)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2470 | |
| 2471 | def test_whitespace_continuation(self): |
| 2472 | eq = self.assertEqual |
| 2473 | # This message contains a line after the Subject: header that has only |
| 2474 | # whitespace, but it is not empty! |
| 2475 | msg = email.message_from_string("""\ |
| 2476 | From: aperson@dom.ain |
| 2477 | To: bperson@dom.ain |
| 2478 | Subject: the next line has a space on it |
| 2479 | \x20 |
| 2480 | Date: Mon, 8 Apr 2002 15:09:19 -0400 |
| 2481 | Message-ID: spam |
| 2482 | |
| 2483 | Here's the message body |
| 2484 | """) |
| 2485 | eq(msg['subject'], 'the next line has a space on it\n ') |
| 2486 | eq(msg['message-id'], 'spam') |
| 2487 | eq(msg.get_payload(), "Here's the message body\n") |
| 2488 | |
| 2489 | def test_whitespace_continuation_last_header(self): |
| 2490 | eq = self.assertEqual |
| 2491 | # Like the previous test, but the subject line is the last |
| 2492 | # header. |
| 2493 | msg = email.message_from_string("""\ |
| 2494 | From: aperson@dom.ain |
| 2495 | To: bperson@dom.ain |
| 2496 | Date: Mon, 8 Apr 2002 15:09:19 -0400 |
| 2497 | Message-ID: spam |
| 2498 | Subject: the next line has a space on it |
| 2499 | \x20 |
| 2500 | |
| 2501 | Here's the message body |
| 2502 | """) |
| 2503 | eq(msg['subject'], 'the next line has a space on it\n ') |
| 2504 | eq(msg['message-id'], 'spam') |
| 2505 | eq(msg.get_payload(), "Here's the message body\n") |
| 2506 | |
| 2507 | def test_crlf_separation(self): |
| 2508 | eq = self.assertEqual |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2509 | with openfile('msg_26.txt', newline='\n') as fp: |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2510 | msg = Parser().parse(fp) |
| 2511 | eq(len(msg.get_payload()), 2) |
| 2512 | part1 = msg.get_payload(0) |
| 2513 | eq(part1.get_content_type(), 'text/plain') |
| 2514 | eq(part1.get_payload(), 'Simple email with attachment.\r\n\r\n') |
| 2515 | part2 = msg.get_payload(1) |
| 2516 | eq(part2.get_content_type(), 'application/riscos') |
| 2517 | |
| 2518 | def test_multipart_digest_with_extra_mime_headers(self): |
| 2519 | eq = self.assertEqual |
| 2520 | neq = self.ndiffAssertEqual |
| 2521 | with openfile('msg_28.txt') as fp: |
| 2522 | msg = email.message_from_file(fp) |
| 2523 | # Structure is: |
| 2524 | # multipart/digest |
| 2525 | # message/rfc822 |
| 2526 | # text/plain |
| 2527 | # message/rfc822 |
| 2528 | # text/plain |
| 2529 | eq(msg.is_multipart(), 1) |
| 2530 | eq(len(msg.get_payload()), 2) |
| 2531 | part1 = msg.get_payload(0) |
| 2532 | eq(part1.get_content_type(), 'message/rfc822') |
| 2533 | eq(part1.is_multipart(), 1) |
| 2534 | eq(len(part1.get_payload()), 1) |
| 2535 | part1a = part1.get_payload(0) |
| 2536 | eq(part1a.is_multipart(), 0) |
| 2537 | eq(part1a.get_content_type(), 'text/plain') |
| 2538 | neq(part1a.get_payload(), 'message 1\n') |
| 2539 | # next message/rfc822 |
| 2540 | part2 = msg.get_payload(1) |
| 2541 | eq(part2.get_content_type(), 'message/rfc822') |
| 2542 | eq(part2.is_multipart(), 1) |
| 2543 | eq(len(part2.get_payload()), 1) |
| 2544 | part2a = part2.get_payload(0) |
| 2545 | eq(part2a.is_multipart(), 0) |
| 2546 | eq(part2a.get_content_type(), 'text/plain') |
| 2547 | neq(part2a.get_payload(), 'message 2\n') |
| 2548 | |
| 2549 | def test_three_lines(self): |
| 2550 | # A bug report by Andrew McNamara |
| 2551 | lines = ['From: Andrew Person <aperson@dom.ain', |
| 2552 | 'Subject: Test', |
| 2553 | 'Date: Tue, 20 Aug 2002 16:43:45 +1000'] |
| 2554 | msg = email.message_from_string(NL.join(lines)) |
| 2555 | self.assertEqual(msg['date'], 'Tue, 20 Aug 2002 16:43:45 +1000') |
| 2556 | |
| 2557 | def test_strip_line_feed_and_carriage_return_in_headers(self): |
| 2558 | eq = self.assertEqual |
| 2559 | # For [ 1002475 ] email message parser doesn't handle \r\n correctly |
| 2560 | value1 = 'text' |
| 2561 | value2 = 'more text' |
| 2562 | m = 'Header: %s\r\nNext-Header: %s\r\n\r\nBody\r\n\r\n' % ( |
| 2563 | value1, value2) |
| 2564 | msg = email.message_from_string(m) |
| 2565 | eq(msg.get('Header'), value1) |
| 2566 | eq(msg.get('Next-Header'), value2) |
| 2567 | |
| 2568 | def test_rfc2822_header_syntax(self): |
| 2569 | eq = self.assertEqual |
| 2570 | m = '>From: foo\nFrom: bar\n!"#QUX;~: zoo\n\nbody' |
| 2571 | msg = email.message_from_string(m) |
| 2572 | eq(len(msg), 3) |
| 2573 | eq(sorted(field for field in msg), ['!"#QUX;~', '>From', 'From']) |
| 2574 | eq(msg.get_payload(), 'body') |
| 2575 | |
| 2576 | def test_rfc2822_space_not_allowed_in_header(self): |
| 2577 | eq = self.assertEqual |
| 2578 | m = '>From foo@example.com 11:25:53\nFrom: bar\n!"#QUX;~: zoo\n\nbody' |
| 2579 | msg = email.message_from_string(m) |
| 2580 | eq(len(msg.keys()), 0) |
| 2581 | |
| 2582 | def test_rfc2822_one_character_header(self): |
| 2583 | eq = self.assertEqual |
| 2584 | m = 'A: first header\nB: second header\nCC: third header\n\nbody' |
| 2585 | msg = email.message_from_string(m) |
| 2586 | headers = msg.keys() |
| 2587 | headers.sort() |
| 2588 | eq(headers, ['A', 'B', 'CC']) |
| 2589 | eq(msg.get_payload(), 'body') |
| 2590 | |
| 2591 | |
| 2592 | |
| 2593 | class TestBase64(unittest.TestCase): |
| 2594 | def test_len(self): |
| 2595 | eq = self.assertEqual |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2596 | eq(base64mime.header_length('hello'), |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 2597 | len(base64mime.body_encode(b'hello', eol=''))) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2598 | for size in range(15): |
| 2599 | if size == 0 : bsize = 0 |
| 2600 | elif size <= 3 : bsize = 4 |
| 2601 | elif size <= 6 : bsize = 8 |
| 2602 | elif size <= 9 : bsize = 12 |
| 2603 | elif size <= 12: bsize = 16 |
| 2604 | else : bsize = 20 |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2605 | eq(base64mime.header_length('x' * size), bsize) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2606 | |
| 2607 | def test_decode(self): |
| 2608 | eq = self.assertEqual |
Barry Warsaw | 2cc1f6d | 2007-08-30 14:28:55 +0000 | [diff] [blame] | 2609 | eq(base64mime.decode(''), b'') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2610 | eq(base64mime.decode('aGVsbG8='), b'hello') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2611 | |
| 2612 | def test_encode(self): |
| 2613 | eq = self.assertEqual |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 2614 | eq(base64mime.body_encode(b''), b'') |
| 2615 | eq(base64mime.body_encode(b'hello'), 'aGVsbG8=\n') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2616 | # Test the binary flag |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 2617 | eq(base64mime.body_encode(b'hello\n'), 'aGVsbG8K\n') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2618 | # Test the maxlinelen arg |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 2619 | eq(base64mime.body_encode(b'xxxx ' * 20, maxlinelen=40), """\ |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2620 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg |
| 2621 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg |
| 2622 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg |
| 2623 | eHh4eCB4eHh4IA== |
| 2624 | """) |
| 2625 | # Test the eol argument |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 2626 | eq(base64mime.body_encode(b'xxxx ' * 20, maxlinelen=40, eol='\r\n'), |
Barry Warsaw | 7aa02e6 | 2007-08-31 03:26:19 +0000 | [diff] [blame] | 2627 | """\ |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2628 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r |
| 2629 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r |
| 2630 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r |
| 2631 | eHh4eCB4eHh4IA==\r |
| 2632 | """) |
| 2633 | |
| 2634 | def test_header_encode(self): |
| 2635 | eq = self.assertEqual |
| 2636 | he = base64mime.header_encode |
| 2637 | eq(he('hello'), '=?iso-8859-1?b?aGVsbG8=?=') |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2638 | eq(he('hello\r\nworld'), '=?iso-8859-1?b?aGVsbG8NCndvcmxk?=') |
| 2639 | eq(he('hello\nworld'), '=?iso-8859-1?b?aGVsbG8Kd29ybGQ=?=') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2640 | # Test the charset option |
| 2641 | eq(he('hello', charset='iso-8859-2'), '=?iso-8859-2?b?aGVsbG8=?=') |
| 2642 | eq(he('hello\nworld'), '=?iso-8859-1?b?aGVsbG8Kd29ybGQ=?=') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2643 | |
| 2644 | |
| 2645 | |
| 2646 | class TestQuopri(unittest.TestCase): |
| 2647 | def setUp(self): |
| 2648 | # Set of characters (as byte integers) that don't need to be encoded |
| 2649 | # in headers. |
| 2650 | self.hlit = list(chain( |
| 2651 | range(ord('a'), ord('z') + 1), |
| 2652 | range(ord('A'), ord('Z') + 1), |
| 2653 | range(ord('0'), ord('9') + 1), |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2654 | (c for c in b'!*+-/'))) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2655 | # Set of characters (as byte integers) that do need to be encoded in |
| 2656 | # headers. |
| 2657 | self.hnon = [c for c in range(256) if c not in self.hlit] |
| 2658 | assert len(self.hlit) + len(self.hnon) == 256 |
| 2659 | # Set of characters (as byte integers) that don't need to be encoded |
| 2660 | # in bodies. |
| 2661 | self.blit = list(range(ord(' '), ord('~') + 1)) |
| 2662 | self.blit.append(ord('\t')) |
| 2663 | self.blit.remove(ord('=')) |
| 2664 | # Set of characters (as byte integers) that do need to be encoded in |
| 2665 | # bodies. |
| 2666 | self.bnon = [c for c in range(256) if c not in self.blit] |
| 2667 | assert len(self.blit) + len(self.bnon) == 256 |
| 2668 | |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2669 | def test_quopri_header_check(self): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2670 | for c in self.hlit: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2671 | self.assertFalse(quoprimime.header_check(c), |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2672 | 'Should not be header quopri encoded: %s' % chr(c)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2673 | for c in self.hnon: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2674 | self.assertTrue(quoprimime.header_check(c), |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2675 | 'Should be header quopri encoded: %s' % chr(c)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2676 | |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2677 | def test_quopri_body_check(self): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2678 | for c in self.blit: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2679 | self.assertFalse(quoprimime.body_check(c), |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2680 | 'Should not be body quopri encoded: %s' % chr(c)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2681 | for c in self.bnon: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2682 | self.assertTrue(quoprimime.body_check(c), |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2683 | 'Should be body quopri encoded: %s' % chr(c)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2684 | |
| 2685 | def test_header_quopri_len(self): |
| 2686 | eq = self.assertEqual |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2687 | eq(quoprimime.header_length(b'hello'), 5) |
| 2688 | # RFC 2047 chrome is not included in header_length(). |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2689 | eq(len(quoprimime.header_encode(b'hello', charset='xxx')), |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2690 | quoprimime.header_length(b'hello') + |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2691 | # =?xxx?q?...?= means 10 extra characters |
| 2692 | 10) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2693 | eq(quoprimime.header_length(b'h@e@l@l@o@'), 20) |
| 2694 | # RFC 2047 chrome is not included in header_length(). |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2695 | eq(len(quoprimime.header_encode(b'h@e@l@l@o@', charset='xxx')), |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2696 | quoprimime.header_length(b'h@e@l@l@o@') + |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2697 | # =?xxx?q?...?= means 10 extra characters |
| 2698 | 10) |
| 2699 | for c in self.hlit: |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2700 | eq(quoprimime.header_length(bytes([c])), 1, |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2701 | 'expected length 1 for %r' % chr(c)) |
| 2702 | for c in self.hnon: |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2703 | # Space is special; it's encoded to _ |
| 2704 | if c == ord(' '): |
| 2705 | continue |
| 2706 | eq(quoprimime.header_length(bytes([c])), 3, |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2707 | 'expected length 3 for %r' % chr(c)) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2708 | eq(quoprimime.header_length(b' '), 1) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2709 | |
| 2710 | def test_body_quopri_len(self): |
| 2711 | eq = self.assertEqual |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2712 | for c in self.blit: |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2713 | eq(quoprimime.body_length(bytes([c])), 1) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2714 | for c in self.bnon: |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2715 | eq(quoprimime.body_length(bytes([c])), 3) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2716 | |
| 2717 | def test_quote_unquote_idempotent(self): |
| 2718 | for x in range(256): |
| 2719 | c = chr(x) |
| 2720 | self.assertEqual(quoprimime.unquote(quoprimime.quote(c)), c) |
| 2721 | |
| 2722 | def test_header_encode(self): |
| 2723 | eq = self.assertEqual |
| 2724 | he = quoprimime.header_encode |
| 2725 | eq(he(b'hello'), '=?iso-8859-1?q?hello?=') |
| 2726 | eq(he(b'hello', charset='iso-8859-2'), '=?iso-8859-2?q?hello?=') |
| 2727 | eq(he(b'hello\nworld'), '=?iso-8859-1?q?hello=0Aworld?=') |
| 2728 | # Test a non-ASCII character |
| 2729 | eq(he(b'hello\xc7there'), '=?iso-8859-1?q?hello=C7there?=') |
| 2730 | |
| 2731 | def test_decode(self): |
| 2732 | eq = self.assertEqual |
| 2733 | eq(quoprimime.decode(''), '') |
| 2734 | eq(quoprimime.decode('hello'), 'hello') |
| 2735 | eq(quoprimime.decode('hello', 'X'), 'hello') |
| 2736 | eq(quoprimime.decode('hello\nworld', 'X'), 'helloXworld') |
| 2737 | |
| 2738 | def test_encode(self): |
| 2739 | eq = self.assertEqual |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2740 | eq(quoprimime.body_encode(''), '') |
| 2741 | eq(quoprimime.body_encode('hello'), 'hello') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2742 | # Test the binary flag |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2743 | eq(quoprimime.body_encode('hello\r\nworld'), 'hello\nworld') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2744 | # Test the maxlinelen arg |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2745 | eq(quoprimime.body_encode('xxxx ' * 20, maxlinelen=40), """\ |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2746 | xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx= |
| 2747 | xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx= |
| 2748 | x xxxx xxxx xxxx xxxx=20""") |
| 2749 | # Test the eol argument |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2750 | eq(quoprimime.body_encode('xxxx ' * 20, maxlinelen=40, eol='\r\n'), |
| 2751 | """\ |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2752 | xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx=\r |
| 2753 | xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx=\r |
| 2754 | x xxxx xxxx xxxx xxxx=20""") |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2755 | eq(quoprimime.body_encode("""\ |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2756 | one line |
| 2757 | |
| 2758 | two line"""), """\ |
| 2759 | one line |
| 2760 | |
| 2761 | two line""") |
| 2762 | |
| 2763 | |
| 2764 | |
| 2765 | # Test the Charset class |
| 2766 | class TestCharset(unittest.TestCase): |
| 2767 | def tearDown(self): |
| 2768 | from email import charset as CharsetModule |
| 2769 | try: |
| 2770 | del CharsetModule.CHARSETS['fake'] |
| 2771 | except KeyError: |
| 2772 | pass |
| 2773 | |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2774 | def test_codec_encodeable(self): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2775 | eq = self.assertEqual |
| 2776 | # Make sure us-ascii = no Unicode conversion |
| 2777 | c = Charset('us-ascii') |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2778 | eq(c.header_encode('Hello World!'), 'Hello World!') |
| 2779 | # Test 8-bit idempotency with us-ascii |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2780 | s = '\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa' |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2781 | self.assertRaises(UnicodeError, c.header_encode, s) |
| 2782 | c = Charset('utf-8') |
| 2783 | eq(c.header_encode(s), '=?utf-8?b?wqTCosKkwqTCpMKmwqTCqMKkwqo=?=') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2784 | |
| 2785 | def test_body_encode(self): |
| 2786 | eq = self.assertEqual |
| 2787 | # Try a charset with QP body encoding |
| 2788 | c = Charset('iso-8859-1') |
Barry Warsaw | 7aa02e6 | 2007-08-31 03:26:19 +0000 | [diff] [blame] | 2789 | eq('hello w=F6rld', c.body_encode('hello w\xf6rld')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2790 | # Try a charset with Base64 body encoding |
| 2791 | c = Charset('utf-8') |
Martin v. Löwis | 15b16a3 | 2008-12-02 06:00:15 +0000 | [diff] [blame] | 2792 | eq('aGVsbG8gd29ybGQ=\n', c.body_encode(b'hello world')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2793 | # Try a charset with None body encoding |
| 2794 | c = Charset('us-ascii') |
Barry Warsaw | 7aa02e6 | 2007-08-31 03:26:19 +0000 | [diff] [blame] | 2795 | eq('hello world', c.body_encode('hello world')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2796 | # Try the convert argument, where input codec != output codec |
| 2797 | c = Charset('euc-jp') |
| 2798 | # With apologies to Tokio Kikuchi ;) |
Barry Warsaw | bef9d21 | 2007-08-31 10:55:37 +0000 | [diff] [blame] | 2799 | # XXX FIXME |
| 2800 | ## try: |
| 2801 | ## eq('\x1b$B5FCO;~IW\x1b(B', |
| 2802 | ## c.body_encode('\xb5\xc6\xc3\xcf\xbb\xfe\xc9\xd7')) |
| 2803 | ## eq('\xb5\xc6\xc3\xcf\xbb\xfe\xc9\xd7', |
| 2804 | ## c.body_encode('\xb5\xc6\xc3\xcf\xbb\xfe\xc9\xd7', False)) |
| 2805 | ## except LookupError: |
| 2806 | ## # We probably don't have the Japanese codecs installed |
| 2807 | ## pass |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2808 | # Testing SF bug #625509, which we have to fake, since there are no |
| 2809 | # built-in encodings where the header encoding is QP but the body |
| 2810 | # encoding is not. |
| 2811 | from email import charset as CharsetModule |
| 2812 | CharsetModule.add_charset('fake', CharsetModule.QP, None) |
| 2813 | c = Charset('fake') |
Barry Warsaw | 7aa02e6 | 2007-08-31 03:26:19 +0000 | [diff] [blame] | 2814 | eq('hello w\xf6rld', c.body_encode('hello w\xf6rld')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2815 | |
| 2816 | def test_unicode_charset_name(self): |
| 2817 | charset = Charset('us-ascii') |
| 2818 | self.assertEqual(str(charset), 'us-ascii') |
| 2819 | self.assertRaises(errors.CharsetError, Charset, 'asc\xffii') |
| 2820 | |
| 2821 | |
| 2822 | |
| 2823 | # Test multilingual MIME headers. |
| 2824 | class TestHeader(TestEmailBase): |
| 2825 | def test_simple(self): |
| 2826 | eq = self.ndiffAssertEqual |
| 2827 | h = Header('Hello World!') |
| 2828 | eq(h.encode(), 'Hello World!') |
| 2829 | h.append(' Goodbye World!') |
| 2830 | eq(h.encode(), 'Hello World! Goodbye World!') |
| 2831 | |
| 2832 | def test_simple_surprise(self): |
| 2833 | eq = self.ndiffAssertEqual |
| 2834 | h = Header('Hello World!') |
| 2835 | eq(h.encode(), 'Hello World!') |
| 2836 | h.append('Goodbye World!') |
| 2837 | eq(h.encode(), 'Hello World! Goodbye World!') |
| 2838 | |
| 2839 | def test_header_needs_no_decoding(self): |
| 2840 | h = 'no decoding needed' |
| 2841 | self.assertEqual(decode_header(h), [(h, None)]) |
| 2842 | |
| 2843 | def test_long(self): |
| 2844 | h = Header("I am the very model of a modern Major-General; I've information vegetable, animal, and mineral; I know the kings of England, and I quote the fights historical from Marathon to Waterloo, in order categorical; I'm very well acquainted, too, with matters mathematical; I understand equations, both the simple and quadratical; about binomial theorem I'm teeming with a lot o' news, with many cheerful facts about the square of the hypotenuse.", |
| 2845 | maxlinelen=76) |
| 2846 | for l in h.encode(splitchars=' ').split('\n '): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2847 | self.assertTrue(len(l) <= 76) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2848 | |
| 2849 | def test_multilingual(self): |
| 2850 | eq = self.ndiffAssertEqual |
| 2851 | g = Charset("iso-8859-1") |
| 2852 | cz = Charset("iso-8859-2") |
| 2853 | utf8 = Charset("utf-8") |
| 2854 | g_head = (b'Die Mieter treten hier ein werden mit einem ' |
| 2855 | b'Foerderband komfortabel den Korridor entlang, ' |
| 2856 | b'an s\xfcdl\xfcndischen Wandgem\xe4lden vorbei, ' |
| 2857 | b'gegen die rotierenden Klingen bef\xf6rdert. ') |
| 2858 | cz_head = (b'Finan\xe8ni metropole se hroutily pod tlakem jejich ' |
| 2859 | b'd\xf9vtipu.. ') |
| 2860 | utf8_head = ('\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f' |
| 2861 | '\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00' |
| 2862 | '\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c' |
| 2863 | '\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067' |
| 2864 | '\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das ' |
| 2865 | 'Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder ' |
| 2866 | 'die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066' |
| 2867 | '\u3044\u307e\u3059\u3002') |
| 2868 | h = Header(g_head, g) |
| 2869 | h.append(cz_head, cz) |
| 2870 | h.append(utf8_head, utf8) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2871 | enc = h.encode(maxlinelen=76) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2872 | eq(enc, """\ |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2873 | =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerderband_kom?= |
| 2874 | =?iso-8859-1?q?fortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndischen_Wand?= |
| 2875 | =?iso-8859-1?q?gem=E4lden_vorbei=2C_gegen_die_rotierenden_Klingen_bef=F6r?= |
| 2876 | =?iso-8859-1?q?dert=2E_?= =?iso-8859-2?q?Finan=E8ni_metropole_se_hroutily?= |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2877 | =?iso-8859-2?q?_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= =?utf-8?b?5q2j56K6?= |
| 2878 | =?utf-8?b?44Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE44G+44Gb44KT44CC?= |
| 2879 | =?utf-8?b?5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB44GC44Go44Gv44Gn?= |
| 2880 | =?utf-8?b?44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGFz?= |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2881 | =?utf-8?b?IE51bnN0dWNrIGdpdCB1bmQgU2xvdGVybWV5ZXI/IEphISBCZWloZXJodW5k?= |
| 2882 | =?utf-8?b?IGRhcyBPZGVyIGRpZSBGbGlwcGVyd2FsZHQgZ2Vyc3B1dC7jgI3jgajoqIA=?= |
| 2883 | =?utf-8?b?44Gj44Gm44GE44G+44GZ44CC?=""") |
| 2884 | decoded = decode_header(enc) |
| 2885 | eq(len(decoded), 3) |
| 2886 | eq(decoded[0], (g_head, 'iso-8859-1')) |
| 2887 | eq(decoded[1], (cz_head, 'iso-8859-2')) |
| 2888 | eq(decoded[2], (utf8_head.encode('utf-8'), 'utf-8')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2889 | ustr = str(h) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2890 | eq(ustr, |
| 2891 | (b'Die Mieter treten hier ein werden mit einem Foerderband ' |
| 2892 | b'komfortabel den Korridor entlang, an s\xc3\xbcdl\xc3\xbcndischen ' |
| 2893 | b'Wandgem\xc3\xa4lden vorbei, gegen die rotierenden Klingen ' |
| 2894 | b'bef\xc3\xb6rdert. Finan\xc4\x8dni metropole se hroutily pod ' |
| 2895 | b'tlakem jejich d\xc5\xafvtipu.. \xe6\xad\xa3\xe7\xa2\xba\xe3\x81' |
| 2896 | b'\xab\xe8\xa8\x80\xe3\x81\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3' |
| 2897 | b'\xe3\x81\xaf\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3' |
| 2898 | b'\x81\xbe\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83' |
| 2899 | b'\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8\xaa\x9e' |
| 2900 | b'\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81\xe3\x81\x82\xe3' |
| 2901 | b'\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81\x9f\xe3\x82\x89\xe3\x82' |
| 2902 | b'\x81\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82\xe5\xae\x9f\xe9\x9a\x9b' |
| 2903 | b'\xe3\x81\xab\xe3\x81\xaf\xe3\x80\x8cWenn ist das Nunstuck git ' |
| 2904 | b'und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt ' |
| 2905 | b'gersput.\xe3\x80\x8d\xe3\x81\xa8\xe8\xa8\x80\xe3\x81\xa3\xe3\x81' |
| 2906 | b'\xa6\xe3\x81\x84\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82' |
| 2907 | ).decode('utf-8')) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2908 | # Test make_header() |
| 2909 | newh = make_header(decode_header(enc)) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2910 | eq(newh, h) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2911 | |
| 2912 | def test_empty_header_encode(self): |
| 2913 | h = Header() |
| 2914 | self.assertEqual(h.encode(), '') |
Barry Warsaw | 8b3d659 | 2007-08-30 02:10:49 +0000 | [diff] [blame] | 2915 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2916 | def test_header_ctor_default_args(self): |
| 2917 | eq = self.ndiffAssertEqual |
| 2918 | h = Header() |
| 2919 | eq(h, '') |
| 2920 | h.append('foo', Charset('iso-8859-1')) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2921 | eq(h, 'foo') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2922 | |
| 2923 | def test_explicit_maxlinelen(self): |
| 2924 | eq = self.ndiffAssertEqual |
| 2925 | hstr = ('A very long line that must get split to something other ' |
| 2926 | 'than at the 76th character boundary to test the non-default ' |
| 2927 | 'behavior') |
| 2928 | h = Header(hstr) |
| 2929 | eq(h.encode(), '''\ |
| 2930 | A very long line that must get split to something other than at the 76th |
| 2931 | character boundary to test the non-default behavior''') |
| 2932 | eq(str(h), hstr) |
| 2933 | h = Header(hstr, header_name='Subject') |
| 2934 | eq(h.encode(), '''\ |
| 2935 | A very long line that must get split to something other than at the |
| 2936 | 76th character boundary to test the non-default behavior''') |
| 2937 | eq(str(h), hstr) |
| 2938 | h = Header(hstr, maxlinelen=1024, header_name='Subject') |
| 2939 | eq(h.encode(), hstr) |
| 2940 | eq(str(h), hstr) |
| 2941 | |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2942 | def test_quopri_splittable(self): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 2943 | eq = self.ndiffAssertEqual |
| 2944 | h = Header(charset='iso-8859-1', maxlinelen=20) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 2945 | x = 'xxxx ' * 20 |
| 2946 | h.append(x) |
| 2947 | s = h.encode() |
| 2948 | eq(s, """\ |
| 2949 | =?iso-8859-1?q?xxx?= |
| 2950 | =?iso-8859-1?q?x_?= |
| 2951 | =?iso-8859-1?q?xx?= |
| 2952 | =?iso-8859-1?q?xx?= |
| 2953 | =?iso-8859-1?q?_x?= |
| 2954 | =?iso-8859-1?q?xx?= |
| 2955 | =?iso-8859-1?q?x_?= |
| 2956 | =?iso-8859-1?q?xx?= |
| 2957 | =?iso-8859-1?q?xx?= |
| 2958 | =?iso-8859-1?q?_x?= |
| 2959 | =?iso-8859-1?q?xx?= |
| 2960 | =?iso-8859-1?q?x_?= |
| 2961 | =?iso-8859-1?q?xx?= |
| 2962 | =?iso-8859-1?q?xx?= |
| 2963 | =?iso-8859-1?q?_x?= |
| 2964 | =?iso-8859-1?q?xx?= |
| 2965 | =?iso-8859-1?q?x_?= |
| 2966 | =?iso-8859-1?q?xx?= |
| 2967 | =?iso-8859-1?q?xx?= |
| 2968 | =?iso-8859-1?q?_x?= |
| 2969 | =?iso-8859-1?q?xx?= |
| 2970 | =?iso-8859-1?q?x_?= |
| 2971 | =?iso-8859-1?q?xx?= |
| 2972 | =?iso-8859-1?q?xx?= |
| 2973 | =?iso-8859-1?q?_x?= |
| 2974 | =?iso-8859-1?q?xx?= |
| 2975 | =?iso-8859-1?q?x_?= |
| 2976 | =?iso-8859-1?q?xx?= |
| 2977 | =?iso-8859-1?q?xx?= |
| 2978 | =?iso-8859-1?q?_x?= |
| 2979 | =?iso-8859-1?q?xx?= |
| 2980 | =?iso-8859-1?q?x_?= |
| 2981 | =?iso-8859-1?q?xx?= |
| 2982 | =?iso-8859-1?q?xx?= |
| 2983 | =?iso-8859-1?q?_x?= |
| 2984 | =?iso-8859-1?q?xx?= |
| 2985 | =?iso-8859-1?q?x_?= |
| 2986 | =?iso-8859-1?q?xx?= |
| 2987 | =?iso-8859-1?q?xx?= |
| 2988 | =?iso-8859-1?q?_x?= |
| 2989 | =?iso-8859-1?q?xx?= |
| 2990 | =?iso-8859-1?q?x_?= |
| 2991 | =?iso-8859-1?q?xx?= |
| 2992 | =?iso-8859-1?q?xx?= |
| 2993 | =?iso-8859-1?q?_x?= |
| 2994 | =?iso-8859-1?q?xx?= |
| 2995 | =?iso-8859-1?q?x_?= |
| 2996 | =?iso-8859-1?q?xx?= |
| 2997 | =?iso-8859-1?q?xx?= |
| 2998 | =?iso-8859-1?q?_?=""") |
| 2999 | eq(x, str(make_header(decode_header(s)))) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 3000 | h = Header(charset='iso-8859-1', maxlinelen=40) |
| 3001 | h.append('xxxx ' * 20) |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 3002 | s = h.encode() |
| 3003 | eq(s, """\ |
| 3004 | =?iso-8859-1?q?xxxx_xxxx_xxxx_xxxx_xxx?= |
| 3005 | =?iso-8859-1?q?x_xxxx_xxxx_xxxx_xxxx_?= |
| 3006 | =?iso-8859-1?q?xxxx_xxxx_xxxx_xxxx_xx?= |
| 3007 | =?iso-8859-1?q?xx_xxxx_xxxx_xxxx_xxxx?= |
| 3008 | =?iso-8859-1?q?_xxxx_xxxx_?=""") |
| 3009 | eq(x, str(make_header(decode_header(s)))) |
| 3010 | |
| 3011 | def test_base64_splittable(self): |
| 3012 | eq = self.ndiffAssertEqual |
| 3013 | h = Header(charset='koi8-r', maxlinelen=20) |
| 3014 | x = 'xxxx ' * 20 |
| 3015 | h.append(x) |
| 3016 | s = h.encode() |
| 3017 | eq(s, """\ |
| 3018 | =?koi8-r?b?eHh4?= |
| 3019 | =?koi8-r?b?eCB4?= |
| 3020 | =?koi8-r?b?eHh4?= |
| 3021 | =?koi8-r?b?IHh4?= |
| 3022 | =?koi8-r?b?eHgg?= |
| 3023 | =?koi8-r?b?eHh4?= |
| 3024 | =?koi8-r?b?eCB4?= |
| 3025 | =?koi8-r?b?eHh4?= |
| 3026 | =?koi8-r?b?IHh4?= |
| 3027 | =?koi8-r?b?eHgg?= |
| 3028 | =?koi8-r?b?eHh4?= |
| 3029 | =?koi8-r?b?eCB4?= |
| 3030 | =?koi8-r?b?eHh4?= |
| 3031 | =?koi8-r?b?IHh4?= |
| 3032 | =?koi8-r?b?eHgg?= |
| 3033 | =?koi8-r?b?eHh4?= |
| 3034 | =?koi8-r?b?eCB4?= |
| 3035 | =?koi8-r?b?eHh4?= |
| 3036 | =?koi8-r?b?IHh4?= |
| 3037 | =?koi8-r?b?eHgg?= |
| 3038 | =?koi8-r?b?eHh4?= |
| 3039 | =?koi8-r?b?eCB4?= |
| 3040 | =?koi8-r?b?eHh4?= |
| 3041 | =?koi8-r?b?IHh4?= |
| 3042 | =?koi8-r?b?eHgg?= |
| 3043 | =?koi8-r?b?eHh4?= |
| 3044 | =?koi8-r?b?eCB4?= |
| 3045 | =?koi8-r?b?eHh4?= |
| 3046 | =?koi8-r?b?IHh4?= |
| 3047 | =?koi8-r?b?eHgg?= |
| 3048 | =?koi8-r?b?eHh4?= |
| 3049 | =?koi8-r?b?eCB4?= |
| 3050 | =?koi8-r?b?eHh4?= |
| 3051 | =?koi8-r?b?IA==?=""") |
| 3052 | eq(x, str(make_header(decode_header(s)))) |
| 3053 | h = Header(charset='koi8-r', maxlinelen=40) |
| 3054 | h.append(x) |
| 3055 | s = h.encode() |
| 3056 | eq(s, """\ |
| 3057 | =?koi8-r?b?eHh4eCB4eHh4IHh4eHggeHh4?= |
| 3058 | =?koi8-r?b?eCB4eHh4IHh4eHggeHh4eCB4?= |
| 3059 | =?koi8-r?b?eHh4IHh4eHggeHh4eCB4eHh4?= |
| 3060 | =?koi8-r?b?IHh4eHggeHh4eCB4eHh4IHh4?= |
| 3061 | =?koi8-r?b?eHggeHh4eCB4eHh4IHh4eHgg?= |
| 3062 | =?koi8-r?b?eHh4eCB4eHh4IA==?=""") |
| 3063 | eq(x, str(make_header(decode_header(s)))) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 3064 | |
| 3065 | def test_us_ascii_header(self): |
| 3066 | eq = self.assertEqual |
| 3067 | s = 'hello' |
| 3068 | x = decode_header(s) |
| 3069 | eq(x, [('hello', None)]) |
| 3070 | h = make_header(x) |
| 3071 | eq(s, h.encode()) |
| 3072 | |
| 3073 | def test_string_charset(self): |
| 3074 | eq = self.assertEqual |
| 3075 | h = Header() |
| 3076 | h.append('hello', 'iso-8859-1') |
Guido van Rossum | 9604e66 | 2007-08-30 03:46:43 +0000 | [diff] [blame] | 3077 | eq(h, 'hello') |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 3078 | |
| 3079 | ## def test_unicode_error(self): |
| 3080 | ## raises = self.assertRaises |
| 3081 | ## raises(UnicodeError, Header, u'[P\xf6stal]', 'us-ascii') |
| 3082 | ## raises(UnicodeError, Header, '[P\xf6stal]', 'us-ascii') |
| 3083 | ## h = Header() |
| 3084 | ## raises(UnicodeError, h.append, u'[P\xf6stal]', 'us-ascii') |
| 3085 | ## raises(UnicodeError, h.append, '[P\xf6stal]', 'us-ascii') |
| 3086 | ## raises(UnicodeError, Header, u'\u83ca\u5730\u6642\u592b', 'iso-8859-1') |
| 3087 | |
| 3088 | def test_utf8_shortest(self): |
| 3089 | eq = self.assertEqual |
| 3090 | h = Header('p\xf6stal', 'utf-8') |
| 3091 | eq(h.encode(), '=?utf-8?q?p=C3=B6stal?=') |
| 3092 | h = Header('\u83ca\u5730\u6642\u592b', 'utf-8') |
| 3093 | eq(h.encode(), '=?utf-8?b?6I+K5Zyw5pmC5aSr?=') |
| 3094 | |
| 3095 | def test_bad_8bit_header(self): |
| 3096 | raises = self.assertRaises |
| 3097 | eq = self.assertEqual |
| 3098 | x = b'Ynwp4dUEbay Auction Semiar- No Charge \x96 Earn Big' |
| 3099 | raises(UnicodeError, Header, x) |
| 3100 | h = Header() |
| 3101 | raises(UnicodeError, h.append, x) |
| 3102 | e = x.decode('utf-8', 'replace') |
| 3103 | eq(str(Header(x, errors='replace')), e) |
| 3104 | h.append(x, errors='replace') |
| 3105 | eq(str(h), e) |
| 3106 | |
| 3107 | def test_encoded_adjacent_nonencoded(self): |
| 3108 | eq = self.assertEqual |
| 3109 | h = Header() |
| 3110 | h.append('hello', 'iso-8859-1') |
| 3111 | h.append('world') |
| 3112 | s = h.encode() |
| 3113 | eq(s, '=?iso-8859-1?q?hello?= world') |
| 3114 | h = make_header(decode_header(s)) |
| 3115 | eq(h.encode(), s) |
| 3116 | |
| 3117 | def test_whitespace_eater(self): |
| 3118 | eq = self.assertEqual |
| 3119 | s = 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztk=?= =?koi8-r?q?=CA?= zz.' |
| 3120 | parts = decode_header(s) |
| 3121 | eq(parts, [(b'Subject:', None), (b'\xf0\xd2\xcf\xd7\xc5\xd2\xcb\xc1 \xce\xc1 \xc6\xc9\xce\xc1\xcc\xd8\xce\xd9\xca', 'koi8-r'), (b'zz.', None)]) |
| 3122 | hdr = make_header(parts) |
| 3123 | eq(hdr.encode(), |
| 3124 | 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztnK?= zz.') |
| 3125 | |
| 3126 | def test_broken_base64_header(self): |
| 3127 | raises = self.assertRaises |
| 3128 | s = 'Subject: =?EUC-KR?B?CSixpLDtKSC/7Liuvsax4iC6uLmwMcijIKHaILzSwd/H0SC8+LCjwLsgv7W/+Mj3IQ?=' |
| 3129 | raises(errors.HeaderParseError, decode_header, s) |
| 3130 | |
| 3131 | |
| 3132 | |
| 3133 | # Test RFC 2231 header parameters (en/de)coding |
| 3134 | class TestRFC2231(TestEmailBase): |
| 3135 | def test_get_param(self): |
| 3136 | eq = self.assertEqual |
| 3137 | msg = self._msgobj('msg_29.txt') |
| 3138 | eq(msg.get_param('title'), |
| 3139 | ('us-ascii', 'en', 'This is even more ***fun*** isn\'t it!')) |
| 3140 | eq(msg.get_param('title', unquote=False), |
| 3141 | ('us-ascii', 'en', '"This is even more ***fun*** isn\'t it!"')) |
| 3142 | |
| 3143 | def test_set_param(self): |
| 3144 | eq = self.ndiffAssertEqual |
| 3145 | msg = Message() |
| 3146 | msg.set_param('title', 'This is even more ***fun*** isn\'t it!', |
| 3147 | charset='us-ascii') |
| 3148 | eq(msg.get_param('title'), |
| 3149 | ('us-ascii', '', 'This is even more ***fun*** isn\'t it!')) |
| 3150 | msg.set_param('title', 'This is even more ***fun*** isn\'t it!', |
| 3151 | charset='us-ascii', language='en') |
| 3152 | eq(msg.get_param('title'), |
| 3153 | ('us-ascii', 'en', 'This is even more ***fun*** isn\'t it!')) |
| 3154 | msg = self._msgobj('msg_01.txt') |
| 3155 | msg.set_param('title', 'This is even more ***fun*** isn\'t it!', |
| 3156 | charset='us-ascii', language='en') |
| 3157 | eq(msg.as_string(maxheaderlen=78), """\ |
| 3158 | Return-Path: <bbb@zzz.org> |
| 3159 | Delivered-To: bbb@zzz.org |
| 3160 | Received: by mail.zzz.org (Postfix, from userid 889) |
| 3161 | \tid 27CEAD38CC; Fri, 4 May 2001 14:05:44 -0400 (EDT) |
| 3162 | MIME-Version: 1.0 |
| 3163 | Content-Transfer-Encoding: 7bit |
| 3164 | Message-ID: <15090.61304.110929.45684@aaa.zzz.org> |
| 3165 | From: bbb@ddd.com (John X. Doe) |
| 3166 | To: bbb@zzz.org |
| 3167 | Subject: This is a test message |
| 3168 | Date: Fri, 4 May 2001 14:05:44 -0400 |
| 3169 | Content-Type: text/plain; charset=us-ascii; |
| 3170 | title*="us-ascii'en'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20isn%27t%20it%21" |
| 3171 | |
| 3172 | |
| 3173 | Hi, |
| 3174 | |
| 3175 | Do you like this message? |
| 3176 | |
| 3177 | -Me |
| 3178 | """) |
| 3179 | |
| 3180 | def test_del_param(self): |
| 3181 | eq = self.ndiffAssertEqual |
| 3182 | msg = self._msgobj('msg_01.txt') |
| 3183 | msg.set_param('foo', 'bar', charset='us-ascii', language='en') |
| 3184 | msg.set_param('title', 'This is even more ***fun*** isn\'t it!', |
| 3185 | charset='us-ascii', language='en') |
| 3186 | msg.del_param('foo', header='Content-Type') |
| 3187 | eq(msg.as_string(maxheaderlen=78), """\ |
| 3188 | Return-Path: <bbb@zzz.org> |
| 3189 | Delivered-To: bbb@zzz.org |
| 3190 | Received: by mail.zzz.org (Postfix, from userid 889) |
| 3191 | \tid 27CEAD38CC; Fri, 4 May 2001 14:05:44 -0400 (EDT) |
| 3192 | MIME-Version: 1.0 |
| 3193 | Content-Transfer-Encoding: 7bit |
| 3194 | Message-ID: <15090.61304.110929.45684@aaa.zzz.org> |
| 3195 | From: bbb@ddd.com (John X. Doe) |
| 3196 | To: bbb@zzz.org |
| 3197 | Subject: This is a test message |
| 3198 | Date: Fri, 4 May 2001 14:05:44 -0400 |
| 3199 | Content-Type: text/plain; charset="us-ascii"; |
| 3200 | title*="us-ascii'en'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20isn%27t%20it%21" |
| 3201 | |
| 3202 | |
| 3203 | Hi, |
| 3204 | |
| 3205 | Do you like this message? |
| 3206 | |
| 3207 | -Me |
| 3208 | """) |
| 3209 | |
| 3210 | def test_rfc2231_get_content_charset(self): |
| 3211 | eq = self.assertEqual |
| 3212 | msg = self._msgobj('msg_32.txt') |
| 3213 | eq(msg.get_content_charset(), 'us-ascii') |
| 3214 | |
| 3215 | def test_rfc2231_no_language_or_charset(self): |
| 3216 | m = '''\ |
| 3217 | Content-Transfer-Encoding: 8bit |
| 3218 | Content-Disposition: inline; filename="file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEMP_nsmail.htm" |
| 3219 | Content-Type: text/html; NAME*0=file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEM; NAME*1=P_nsmail.htm |
| 3220 | |
| 3221 | ''' |
| 3222 | msg = email.message_from_string(m) |
| 3223 | param = msg.get_param('NAME') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 3224 | self.assertFalse(isinstance(param, tuple)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 3225 | self.assertEqual( |
| 3226 | param, |
| 3227 | 'file____C__DOCUMENTS_20AND_20SETTINGS_FABIEN_LOCAL_20SETTINGS_TEMP_nsmail.htm') |
| 3228 | |
| 3229 | def test_rfc2231_no_language_or_charset_in_filename(self): |
| 3230 | m = '''\ |
| 3231 | Content-Disposition: inline; |
| 3232 | \tfilename*0*="''This%20is%20even%20more%20"; |
| 3233 | \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 3234 | \tfilename*2="is it not.pdf" |
| 3235 | |
| 3236 | ''' |
| 3237 | msg = email.message_from_string(m) |
| 3238 | self.assertEqual(msg.get_filename(), |
| 3239 | 'This is even more ***fun*** is it not.pdf') |
| 3240 | |
| 3241 | def test_rfc2231_no_language_or_charset_in_filename_encoded(self): |
| 3242 | m = '''\ |
| 3243 | Content-Disposition: inline; |
| 3244 | \tfilename*0*="''This%20is%20even%20more%20"; |
| 3245 | \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 3246 | \tfilename*2="is it not.pdf" |
| 3247 | |
| 3248 | ''' |
| 3249 | msg = email.message_from_string(m) |
| 3250 | self.assertEqual(msg.get_filename(), |
| 3251 | 'This is even more ***fun*** is it not.pdf') |
| 3252 | |
| 3253 | def test_rfc2231_partly_encoded(self): |
| 3254 | m = '''\ |
| 3255 | Content-Disposition: inline; |
| 3256 | \tfilename*0="''This%20is%20even%20more%20"; |
| 3257 | \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 3258 | \tfilename*2="is it not.pdf" |
| 3259 | |
| 3260 | ''' |
| 3261 | msg = email.message_from_string(m) |
| 3262 | self.assertEqual( |
| 3263 | msg.get_filename(), |
| 3264 | 'This%20is%20even%20more%20***fun*** is it not.pdf') |
| 3265 | |
| 3266 | def test_rfc2231_partly_nonencoded(self): |
| 3267 | m = '''\ |
| 3268 | Content-Disposition: inline; |
| 3269 | \tfilename*0="This%20is%20even%20more%20"; |
| 3270 | \tfilename*1="%2A%2A%2Afun%2A%2A%2A%20"; |
| 3271 | \tfilename*2="is it not.pdf" |
| 3272 | |
| 3273 | ''' |
| 3274 | msg = email.message_from_string(m) |
| 3275 | self.assertEqual( |
| 3276 | msg.get_filename(), |
| 3277 | 'This%20is%20even%20more%20%2A%2A%2Afun%2A%2A%2A%20is it not.pdf') |
| 3278 | |
| 3279 | def test_rfc2231_no_language_or_charset_in_boundary(self): |
| 3280 | m = '''\ |
| 3281 | Content-Type: multipart/alternative; |
| 3282 | \tboundary*0*="''This%20is%20even%20more%20"; |
| 3283 | \tboundary*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 3284 | \tboundary*2="is it not.pdf" |
| 3285 | |
| 3286 | ''' |
| 3287 | msg = email.message_from_string(m) |
| 3288 | self.assertEqual(msg.get_boundary(), |
| 3289 | 'This is even more ***fun*** is it not.pdf') |
| 3290 | |
| 3291 | def test_rfc2231_no_language_or_charset_in_charset(self): |
| 3292 | # This is a nonsensical charset value, but tests the code anyway |
| 3293 | m = '''\ |
| 3294 | Content-Type: text/plain; |
| 3295 | \tcharset*0*="This%20is%20even%20more%20"; |
| 3296 | \tcharset*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 3297 | \tcharset*2="is it not.pdf" |
| 3298 | |
| 3299 | ''' |
| 3300 | msg = email.message_from_string(m) |
| 3301 | self.assertEqual(msg.get_content_charset(), |
| 3302 | 'this is even more ***fun*** is it not.pdf') |
| 3303 | |
| 3304 | def test_rfc2231_bad_encoding_in_filename(self): |
| 3305 | m = '''\ |
| 3306 | Content-Disposition: inline; |
| 3307 | \tfilename*0*="bogus'xx'This%20is%20even%20more%20"; |
| 3308 | \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 3309 | \tfilename*2="is it not.pdf" |
| 3310 | |
| 3311 | ''' |
| 3312 | msg = email.message_from_string(m) |
| 3313 | self.assertEqual(msg.get_filename(), |
| 3314 | 'This is even more ***fun*** is it not.pdf') |
| 3315 | |
| 3316 | def test_rfc2231_bad_encoding_in_charset(self): |
| 3317 | m = """\ |
| 3318 | Content-Type: text/plain; charset*=bogus''utf-8%E2%80%9D |
| 3319 | |
| 3320 | """ |
| 3321 | msg = email.message_from_string(m) |
| 3322 | # This should return None because non-ascii characters in the charset |
| 3323 | # are not allowed. |
| 3324 | self.assertEqual(msg.get_content_charset(), None) |
| 3325 | |
| 3326 | def test_rfc2231_bad_character_in_charset(self): |
| 3327 | m = """\ |
| 3328 | Content-Type: text/plain; charset*=ascii''utf-8%E2%80%9D |
| 3329 | |
| 3330 | """ |
| 3331 | msg = email.message_from_string(m) |
| 3332 | # This should return None because non-ascii characters in the charset |
| 3333 | # are not allowed. |
| 3334 | self.assertEqual(msg.get_content_charset(), None) |
| 3335 | |
| 3336 | def test_rfc2231_bad_character_in_filename(self): |
| 3337 | m = '''\ |
| 3338 | Content-Disposition: inline; |
| 3339 | \tfilename*0*="ascii'xx'This%20is%20even%20more%20"; |
| 3340 | \tfilename*1*="%2A%2A%2Afun%2A%2A%2A%20"; |
| 3341 | \tfilename*2*="is it not.pdf%E2" |
| 3342 | |
| 3343 | ''' |
| 3344 | msg = email.message_from_string(m) |
| 3345 | self.assertEqual(msg.get_filename(), |
| 3346 | 'This is even more ***fun*** is it not.pdf\ufffd') |
| 3347 | |
| 3348 | def test_rfc2231_unknown_encoding(self): |
| 3349 | m = """\ |
| 3350 | Content-Transfer-Encoding: 8bit |
| 3351 | Content-Disposition: inline; filename*=X-UNKNOWN''myfile.txt |
| 3352 | |
| 3353 | """ |
| 3354 | msg = email.message_from_string(m) |
| 3355 | self.assertEqual(msg.get_filename(), 'myfile.txt') |
| 3356 | |
| 3357 | def test_rfc2231_single_tick_in_filename_extended(self): |
| 3358 | eq = self.assertEqual |
| 3359 | m = """\ |
| 3360 | Content-Type: application/x-foo; |
| 3361 | \tname*0*=\"Frank's\"; name*1*=\" Document\" |
| 3362 | |
| 3363 | """ |
| 3364 | msg = email.message_from_string(m) |
| 3365 | charset, language, s = msg.get_param('name') |
| 3366 | eq(charset, None) |
| 3367 | eq(language, None) |
| 3368 | eq(s, "Frank's Document") |
| 3369 | |
| 3370 | def test_rfc2231_single_tick_in_filename(self): |
| 3371 | m = """\ |
| 3372 | Content-Type: application/x-foo; name*0=\"Frank's\"; name*1=\" Document\" |
| 3373 | |
| 3374 | """ |
| 3375 | msg = email.message_from_string(m) |
| 3376 | param = msg.get_param('name') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 3377 | self.assertFalse(isinstance(param, tuple)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 3378 | self.assertEqual(param, "Frank's Document") |
| 3379 | |
| 3380 | def test_rfc2231_tick_attack_extended(self): |
| 3381 | eq = self.assertEqual |
| 3382 | m = """\ |
| 3383 | Content-Type: application/x-foo; |
| 3384 | \tname*0*=\"us-ascii'en-us'Frank's\"; name*1*=\" Document\" |
| 3385 | |
| 3386 | """ |
| 3387 | msg = email.message_from_string(m) |
| 3388 | charset, language, s = msg.get_param('name') |
| 3389 | eq(charset, 'us-ascii') |
| 3390 | eq(language, 'en-us') |
| 3391 | eq(s, "Frank's Document") |
| 3392 | |
| 3393 | def test_rfc2231_tick_attack(self): |
| 3394 | m = """\ |
| 3395 | Content-Type: application/x-foo; |
| 3396 | \tname*0=\"us-ascii'en-us'Frank's\"; name*1=\" Document\" |
| 3397 | |
| 3398 | """ |
| 3399 | msg = email.message_from_string(m) |
| 3400 | param = msg.get_param('name') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 3401 | self.assertFalse(isinstance(param, tuple)) |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 3402 | self.assertEqual(param, "us-ascii'en-us'Frank's Document") |
| 3403 | |
| 3404 | def test_rfc2231_no_extended_values(self): |
| 3405 | eq = self.assertEqual |
| 3406 | m = """\ |
| 3407 | Content-Type: application/x-foo; name=\"Frank's Document\" |
| 3408 | |
| 3409 | """ |
| 3410 | msg = email.message_from_string(m) |
| 3411 | eq(msg.get_param('name'), "Frank's Document") |
| 3412 | |
| 3413 | def test_rfc2231_encoded_then_unencoded_segments(self): |
| 3414 | eq = self.assertEqual |
| 3415 | m = """\ |
| 3416 | Content-Type: application/x-foo; |
| 3417 | \tname*0*=\"us-ascii'en-us'My\"; |
| 3418 | \tname*1=\" Document\"; |
| 3419 | \tname*2*=\" For You\" |
| 3420 | |
| 3421 | """ |
| 3422 | msg = email.message_from_string(m) |
| 3423 | charset, language, s = msg.get_param('name') |
| 3424 | eq(charset, 'us-ascii') |
| 3425 | eq(language, 'en-us') |
| 3426 | eq(s, 'My Document For You') |
| 3427 | |
| 3428 | def test_rfc2231_unencoded_then_encoded_segments(self): |
| 3429 | eq = self.assertEqual |
| 3430 | m = """\ |
| 3431 | Content-Type: application/x-foo; |
| 3432 | \tname*0=\"us-ascii'en-us'My\"; |
| 3433 | \tname*1*=\" Document\"; |
| 3434 | \tname*2*=\" For You\" |
| 3435 | |
| 3436 | """ |
| 3437 | msg = email.message_from_string(m) |
| 3438 | charset, language, s = msg.get_param('name') |
| 3439 | eq(charset, 'us-ascii') |
| 3440 | eq(language, 'en-us') |
| 3441 | eq(s, 'My Document For You') |
| 3442 | |
| 3443 | |
| 3444 | |
R. David Murray | a8f480f | 2010-01-16 18:30:03 +0000 | [diff] [blame] | 3445 | # Tests to ensure that signed parts of an email are completely preserved, as |
| 3446 | # required by RFC1847 section 2.1. Note that these are incomplete, because the |
| 3447 | # email package does not currently always preserve the body. See issue 1670765. |
| 3448 | class TestSigned(TestEmailBase): |
| 3449 | |
| 3450 | def _msg_and_obj(self, filename): |
| 3451 | with openfile(findfile(filename)) as fp: |
| 3452 | original = fp.read() |
| 3453 | msg = email.message_from_string(original) |
| 3454 | return original, msg |
| 3455 | |
| 3456 | def _signed_parts_eq(self, original, result): |
| 3457 | # Extract the first mime part of each message |
| 3458 | import re |
| 3459 | repart = re.compile(r'^--([^\n]+)\n(.*?)\n--\1$', re.S | re.M) |
| 3460 | inpart = repart.search(original).group(2) |
| 3461 | outpart = repart.search(result).group(2) |
| 3462 | self.assertEqual(outpart, inpart) |
| 3463 | |
| 3464 | def test_long_headers_as_string(self): |
| 3465 | original, msg = self._msg_and_obj('msg_45.txt') |
| 3466 | result = msg.as_string() |
| 3467 | self._signed_parts_eq(original, result) |
| 3468 | |
| 3469 | def test_long_headers_as_string_maxheaderlen(self): |
| 3470 | original, msg = self._msg_and_obj('msg_45.txt') |
| 3471 | result = msg.as_string(maxheaderlen=60) |
| 3472 | self._signed_parts_eq(original, result) |
| 3473 | |
| 3474 | def test_long_headers_flatten(self): |
| 3475 | original, msg = self._msg_and_obj('msg_45.txt') |
| 3476 | fp = StringIO() |
| 3477 | Generator(fp).flatten(msg) |
| 3478 | result = fp.getvalue() |
| 3479 | self._signed_parts_eq(original, result) |
| 3480 | |
| 3481 | |
| 3482 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 3483 | def _testclasses(): |
| 3484 | mod = sys.modules[__name__] |
| 3485 | return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')] |
| 3486 | |
| 3487 | |
| 3488 | def suite(): |
| 3489 | suite = unittest.TestSuite() |
| 3490 | for testclass in _testclasses(): |
| 3491 | suite.addTest(unittest.makeSuite(testclass)) |
| 3492 | return suite |
| 3493 | |
| 3494 | |
| 3495 | def test_main(): |
| 3496 | for testclass in _testclasses(): |
| 3497 | run_unittest(testclass) |
| 3498 | |
| 3499 | |
| 3500 | |
| 3501 | if __name__ == '__main__': |
| 3502 | unittest.main(defaultTest='suite') |