Barry Warsaw | 763af41 | 2002-01-27 06:48:47 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001,2002 Python Software Foundation |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 2 | # email package unit tests |
| 3 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 4 | import sys |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 5 | import os |
| 6 | import time |
| 7 | import unittest |
| 8 | import base64 |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 9 | import difflib |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 10 | from cStringIO import StringIO |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 11 | from types import StringType, ListType |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 12 | import warnings |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 13 | |
| 14 | import email |
| 15 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 16 | from email.Charset import Charset |
Barry Warsaw | c53b29e | 2002-07-09 16:36:36 +0000 | [diff] [blame] | 17 | from email.Header import Header, decode_header, make_header |
Barry Warsaw | bf7a59d | 2001-10-11 15:44:50 +0000 | [diff] [blame] | 18 | from email.Parser import Parser, HeaderParser |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 19 | from email.Generator import Generator, DecodedGenerator |
| 20 | from email.Message import Message |
Barry Warsaw | fee435a | 2001-10-09 19:23:57 +0000 | [diff] [blame] | 21 | from email.MIMEAudio import MIMEAudio |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 22 | from email.MIMEText import MIMEText |
| 23 | from email.MIMEImage import MIMEImage |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 24 | from email.MIMEBase import MIMEBase |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 25 | from email.MIMEMessage import MIMEMessage |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 26 | from email.MIMEMultipart import MIMEMultipart |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 27 | from email import Utils |
| 28 | from email import Errors |
| 29 | from email import Encoders |
| 30 | from email import Iterators |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 31 | from email import base64MIME |
| 32 | from email import quopriMIME |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 33 | |
Barry Warsaw | c9ad32c | 2002-04-15 22:14:06 +0000 | [diff] [blame] | 34 | import test_support |
Guido van Rossum | 78f0dd3 | 2001-12-07 21:07:08 +0000 | [diff] [blame] | 35 | from test_support import findfile, __file__ as test_support_file |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 36 | |
Barry Warsaw | c9ad32c | 2002-04-15 22:14:06 +0000 | [diff] [blame] | 37 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 38 | NL = '\n' |
| 39 | EMPTYSTRING = '' |
Barry Warsaw | 07227d1 | 2001-10-17 20:52:26 +0000 | [diff] [blame] | 40 | SPACE = ' ' |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 41 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 42 | # We don't care about DeprecationWarnings |
| 43 | warnings.filterwarnings('ignore', '', DeprecationWarning, __name__) |
| 44 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 45 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 46 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 47 | def openfile(filename): |
Guido van Rossum | 78f0dd3 | 2001-12-07 21:07:08 +0000 | [diff] [blame] | 48 | path = os.path.join(os.path.dirname(test_support_file), 'data', filename) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 49 | return open(path) |
| 50 | |
| 51 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 52 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 53 | # Base test class |
| 54 | class TestEmailBase(unittest.TestCase): |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 55 | if hasattr(difflib, 'ndiff'): |
| 56 | # Python 2.2 and beyond |
| 57 | def ndiffAssertEqual(self, first, second): |
| 58 | """Like failUnlessEqual except use ndiff for readable output.""" |
| 59 | if first <> second: |
Barry Warsaw | c53b29e | 2002-07-09 16:36:36 +0000 | [diff] [blame] | 60 | sfirst = str(first) |
| 61 | ssecond = str(second) |
| 62 | diff = difflib.ndiff(sfirst.splitlines(), ssecond.splitlines()) |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 63 | fp = StringIO() |
| 64 | print >> fp, NL, NL.join(diff) |
| 65 | raise self.failureException, fp.getvalue() |
| 66 | else: |
| 67 | # Python 2.1 |
| 68 | ndiffAssertEqual = unittest.TestCase.assertEqual |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 69 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 70 | def _msgobj(self, filename): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 71 | fp = openfile(findfile(filename)) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 72 | try: |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 73 | msg = email.message_from_file(fp) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 74 | finally: |
| 75 | fp.close() |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 76 | return msg |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 77 | |
| 78 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 79 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 80 | # Test various aspects of the Message class's API |
| 81 | class TestMessageAPI(TestEmailBase): |
Barry Warsaw | 2f6a0b0 | 2001-10-09 15:49:35 +0000 | [diff] [blame] | 82 | def test_get_all(self): |
| 83 | eq = self.assertEqual |
| 84 | msg = self._msgobj('msg_20.txt') |
| 85 | eq(msg.get_all('cc'), ['ccc@zzz.org', 'ddd@zzz.org', 'eee@zzz.org']) |
| 86 | eq(msg.get_all('xx', 'n/a'), 'n/a') |
| 87 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 88 | def test_getset_charset(self): |
| 89 | eq = self.assertEqual |
| 90 | msg = Message() |
| 91 | eq(msg.get_charset(), None) |
| 92 | charset = Charset('iso-8859-1') |
| 93 | msg.set_charset(charset) |
| 94 | eq(msg['mime-version'], '1.0') |
| 95 | eq(msg.get_type(), 'text/plain') |
| 96 | eq(msg['content-type'], 'text/plain; charset="iso-8859-1"') |
| 97 | eq(msg.get_param('charset'), 'iso-8859-1') |
| 98 | eq(msg['content-transfer-encoding'], 'quoted-printable') |
| 99 | eq(msg.get_charset().input_charset, 'iso-8859-1') |
| 100 | # Remove the charset |
| 101 | msg.set_charset(None) |
| 102 | eq(msg.get_charset(), None) |
| 103 | eq(msg['content-type'], 'text/plain') |
| 104 | # Try adding a charset when there's already MIME headers present |
| 105 | msg = Message() |
| 106 | msg['MIME-Version'] = '2.0' |
| 107 | msg['Content-Type'] = 'text/x-weird' |
| 108 | msg['Content-Transfer-Encoding'] = 'quinted-puntable' |
| 109 | msg.set_charset(charset) |
| 110 | eq(msg['mime-version'], '2.0') |
| 111 | eq(msg['content-type'], 'text/x-weird; charset="iso-8859-1"') |
| 112 | eq(msg['content-transfer-encoding'], 'quinted-puntable') |
| 113 | |
| 114 | def test_set_charset_from_string(self): |
| 115 | eq = self.assertEqual |
| 116 | msg = Message() |
| 117 | msg.set_charset('us-ascii') |
| 118 | eq(msg.get_charset().input_charset, 'us-ascii') |
| 119 | eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 120 | |
| 121 | def test_set_payload_with_charset(self): |
| 122 | msg = Message() |
| 123 | charset = Charset('iso-8859-1') |
| 124 | msg.set_payload('This is a string payload', charset) |
| 125 | self.assertEqual(msg.get_charset().input_charset, 'iso-8859-1') |
| 126 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 127 | def test_get_charsets(self): |
| 128 | eq = self.assertEqual |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 129 | |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 130 | msg = self._msgobj('msg_08.txt') |
| 131 | charsets = msg.get_charsets() |
| 132 | eq(charsets, [None, 'us-ascii', 'iso-8859-1', 'iso-8859-2', 'koi8-r']) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 133 | |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 134 | msg = self._msgobj('msg_09.txt') |
| 135 | charsets = msg.get_charsets('dingbat') |
| 136 | eq(charsets, ['dingbat', 'us-ascii', 'iso-8859-1', 'dingbat', |
| 137 | 'koi8-r']) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 138 | |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 139 | msg = self._msgobj('msg_12.txt') |
| 140 | charsets = msg.get_charsets() |
| 141 | eq(charsets, [None, 'us-ascii', 'iso-8859-1', None, 'iso-8859-2', |
| 142 | 'iso-8859-3', 'us-ascii', 'koi8-r']) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 143 | |
| 144 | def test_get_filename(self): |
| 145 | eq = self.assertEqual |
| 146 | |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 147 | msg = self._msgobj('msg_04.txt') |
| 148 | filenames = [p.get_filename() for p in msg.get_payload()] |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 149 | eq(filenames, ['msg.txt', 'msg.txt']) |
| 150 | |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 151 | msg = self._msgobj('msg_07.txt') |
| 152 | subpart = msg.get_payload(1) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 153 | eq(subpart.get_filename(), 'dingusfish.gif') |
| 154 | |
| 155 | def test_get_boundary(self): |
| 156 | eq = self.assertEqual |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 157 | msg = self._msgobj('msg_07.txt') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 158 | # No quotes! |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 159 | eq(msg.get_boundary(), 'BOUNDARY') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 160 | |
| 161 | def test_set_boundary(self): |
| 162 | eq = self.assertEqual |
| 163 | # This one has no existing boundary parameter, but the Content-Type: |
| 164 | # header appears fifth. |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 165 | msg = self._msgobj('msg_01.txt') |
| 166 | msg.set_boundary('BOUNDARY') |
| 167 | header, value = msg.items()[4] |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 168 | eq(header.lower(), 'content-type') |
Barry Warsaw | 9546e79 | 2002-06-29 05:58:45 +0000 | [diff] [blame] | 169 | eq(value, 'text/plain; charset="us-ascii"; boundary="BOUNDARY"') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 170 | # This one has a Content-Type: header, with a boundary, stuck in the |
| 171 | # middle of its headers. Make sure the order is preserved; it should |
| 172 | # be fifth. |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 173 | msg = self._msgobj('msg_04.txt') |
| 174 | msg.set_boundary('BOUNDARY') |
| 175 | header, value = msg.items()[4] |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 176 | eq(header.lower(), 'content-type') |
| 177 | eq(value, 'multipart/mixed; boundary="BOUNDARY"') |
| 178 | # And this one has no Content-Type: header at all. |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 179 | msg = self._msgobj('msg_03.txt') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 180 | self.assertRaises(Errors.HeaderParseError, |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 181 | msg.set_boundary, 'BOUNDARY') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 182 | |
| 183 | def test_get_decoded_payload(self): |
| 184 | eq = self.assertEqual |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 185 | msg = self._msgobj('msg_10.txt') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 186 | # The outer message is a multipart |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 187 | eq(msg.get_payload(decode=1), None) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 188 | # Subpart 1 is 7bit encoded |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 189 | eq(msg.get_payload(0).get_payload(decode=1), |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 190 | 'This is a 7bit encoded message.\n') |
| 191 | # Subpart 2 is quopri |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 192 | eq(msg.get_payload(1).get_payload(decode=1), |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 193 | '\xa1This is a Quoted Printable encoded message!\n') |
| 194 | # Subpart 3 is base64 |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 195 | eq(msg.get_payload(2).get_payload(decode=1), |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 196 | 'This is a Base64 encoded message.') |
| 197 | # Subpart 4 has no Content-Transfer-Encoding: header. |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 198 | eq(msg.get_payload(3).get_payload(decode=1), |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 199 | 'This has no Content-Transfer-Encoding: header.\n') |
| 200 | |
| 201 | def test_decoded_generator(self): |
| 202 | eq = self.assertEqual |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 203 | msg = self._msgobj('msg_07.txt') |
| 204 | fp = openfile('msg_17.txt') |
| 205 | try: |
| 206 | text = fp.read() |
| 207 | finally: |
| 208 | fp.close() |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 209 | s = StringIO() |
| 210 | g = DecodedGenerator(s) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 211 | g.flatten(msg) |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 212 | eq(s.getvalue(), text) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 213 | |
| 214 | def test__contains__(self): |
| 215 | msg = Message() |
| 216 | msg['From'] = 'Me' |
| 217 | msg['to'] = 'You' |
| 218 | # Check for case insensitivity |
| 219 | self.failUnless('from' in msg) |
| 220 | self.failUnless('From' in msg) |
| 221 | self.failUnless('FROM' in msg) |
| 222 | self.failUnless('to' in msg) |
| 223 | self.failUnless('To' in msg) |
| 224 | self.failUnless('TO' in msg) |
| 225 | |
| 226 | def test_as_string(self): |
| 227 | eq = self.assertEqual |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 228 | msg = self._msgobj('msg_01.txt') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 229 | fp = openfile('msg_01.txt') |
| 230 | try: |
| 231 | text = fp.read() |
| 232 | finally: |
| 233 | fp.close() |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 234 | eq(text, msg.as_string()) |
| 235 | fullrepr = str(msg) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 236 | lines = fullrepr.split('\n') |
| 237 | self.failUnless(lines[0].startswith('From ')) |
| 238 | eq(text, NL.join(lines[1:])) |
| 239 | |
| 240 | def test_bad_param(self): |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 241 | msg = email.message_from_string("Content-Type: blarg; baz; boo\n") |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 242 | self.assertEqual(msg.get_param('baz'), '') |
| 243 | |
| 244 | def test_missing_filename(self): |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 245 | msg = email.message_from_string("From: foo\n") |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 246 | self.assertEqual(msg.get_filename(), None) |
| 247 | |
| 248 | def test_bogus_filename(self): |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 249 | msg = email.message_from_string( |
| 250 | "Content-Disposition: blarg; filename\n") |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 251 | self.assertEqual(msg.get_filename(), '') |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 252 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 253 | def test_missing_boundary(self): |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 254 | msg = email.message_from_string("From: foo\n") |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 255 | self.assertEqual(msg.get_boundary(), None) |
| 256 | |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 257 | def test_get_params(self): |
| 258 | eq = self.assertEqual |
| 259 | msg = email.message_from_string( |
| 260 | 'X-Header: foo=one; bar=two; baz=three\n') |
| 261 | eq(msg.get_params(header='x-header'), |
| 262 | [('foo', 'one'), ('bar', 'two'), ('baz', 'three')]) |
| 263 | msg = email.message_from_string( |
| 264 | 'X-Header: foo; bar=one; baz=two\n') |
| 265 | eq(msg.get_params(header='x-header'), |
| 266 | [('foo', ''), ('bar', 'one'), ('baz', 'two')]) |
| 267 | eq(msg.get_params(), None) |
| 268 | msg = email.message_from_string( |
| 269 | 'X-Header: foo; bar="one"; baz=two\n') |
| 270 | eq(msg.get_params(header='x-header'), |
| 271 | [('foo', ''), ('bar', 'one'), ('baz', 'two')]) |
| 272 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 273 | def test_get_param_liberal(self): |
| 274 | msg = Message() |
| 275 | msg['Content-Type'] = 'Content-Type: Multipart/mixed; boundary = "CPIMSSMTPC06p5f3tG"' |
| 276 | self.assertEqual(msg.get_param('boundary'), 'CPIMSSMTPC06p5f3tG') |
| 277 | |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 278 | def test_get_param(self): |
| 279 | eq = self.assertEqual |
| 280 | msg = email.message_from_string( |
| 281 | "X-Header: foo=one; bar=two; baz=three\n") |
| 282 | eq(msg.get_param('bar', header='x-header'), 'two') |
| 283 | eq(msg.get_param('quuz', header='x-header'), None) |
| 284 | eq(msg.get_param('quuz'), None) |
| 285 | msg = email.message_from_string( |
| 286 | 'X-Header: foo; bar="one"; baz=two\n') |
| 287 | eq(msg.get_param('foo', header='x-header'), '') |
| 288 | eq(msg.get_param('bar', header='x-header'), 'one') |
| 289 | eq(msg.get_param('baz', header='x-header'), 'two') |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 290 | # XXX: We are not RFC-2045 compliant! We cannot parse: |
| 291 | # msg["Content-Type"] = 'text/plain; weird="hey; dolly? [you] @ <\\"home\\">?"' |
| 292 | # msg.get_param("weird") |
| 293 | # yet. |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 294 | |
Barry Warsaw | 2539cf5 | 2001-10-25 22:43:46 +0000 | [diff] [blame] | 295 | def test_get_param_funky_continuation_lines(self): |
| 296 | msg = self._msgobj('msg_22.txt') |
| 297 | self.assertEqual(msg.get_payload(1).get_param('name'), 'wibble.JPG') |
| 298 | |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 299 | def test_has_key(self): |
| 300 | msg = email.message_from_string('Header: exists') |
| 301 | self.failUnless(msg.has_key('header')) |
| 302 | self.failUnless(msg.has_key('Header')) |
| 303 | self.failUnless(msg.has_key('HEADER')) |
| 304 | self.failIf(msg.has_key('headeri')) |
| 305 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 306 | def test_set_param(self): |
| 307 | eq = self.assertEqual |
| 308 | msg = Message() |
| 309 | msg.set_param('charset', 'iso-2022-jp') |
| 310 | eq(msg.get_param('charset'), 'iso-2022-jp') |
| 311 | msg.set_param('importance', 'high value') |
| 312 | eq(msg.get_param('importance'), 'high value') |
| 313 | eq(msg.get_param('importance', unquote=0), '"high value"') |
| 314 | eq(msg.get_params(), [('text/plain', ''), |
| 315 | ('charset', 'iso-2022-jp'), |
| 316 | ('importance', 'high value')]) |
| 317 | eq(msg.get_params(unquote=0), [('text/plain', ''), |
| 318 | ('charset', '"iso-2022-jp"'), |
| 319 | ('importance', '"high value"')]) |
| 320 | msg.set_param('charset', 'iso-9999-xx', header='X-Jimmy') |
| 321 | eq(msg.get_param('charset', header='X-Jimmy'), 'iso-9999-xx') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 322 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 323 | def test_del_param(self): |
| 324 | eq = self.assertEqual |
| 325 | msg = self._msgobj('msg_05.txt') |
| 326 | eq(msg.get_params(), |
| 327 | [('multipart/report', ''), ('report-type', 'delivery-status'), |
| 328 | ('boundary', 'D1690A7AC1.996856090/mail.example.com')]) |
| 329 | old_val = msg.get_param("report-type") |
| 330 | msg.del_param("report-type") |
| 331 | eq(msg.get_params(), |
| 332 | [('multipart/report', ''), |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 333 | ('boundary', 'D1690A7AC1.996856090/mail.example.com')]) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 334 | msg.set_param("report-type", old_val) |
| 335 | eq(msg.get_params(), |
| 336 | [('multipart/report', ''), |
| 337 | ('boundary', 'D1690A7AC1.996856090/mail.example.com'), |
| 338 | ('report-type', old_val)]) |
| 339 | |
| 340 | def test_set_type(self): |
| 341 | eq = self.assertEqual |
| 342 | msg = Message() |
| 343 | self.assertRaises(ValueError, msg.set_type, 'text') |
| 344 | msg.set_type('text/plain') |
| 345 | eq(msg['content-type'], 'text/plain') |
| 346 | msg.set_param('charset', 'us-ascii') |
| 347 | eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 348 | msg.set_type('text/html') |
| 349 | eq(msg['content-type'], 'text/html; charset="us-ascii"') |
| 350 | |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 351 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 352 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 353 | # Test the email.Encoders module |
| 354 | class TestEncoders(unittest.TestCase): |
| 355 | def test_encode_noop(self): |
| 356 | eq = self.assertEqual |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 357 | msg = MIMEText('hello world', _encoder=Encoders.encode_noop) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 358 | eq(msg.get_payload(), 'hello world\n') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 359 | |
| 360 | def test_encode_7bit(self): |
| 361 | eq = self.assertEqual |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 362 | msg = MIMEText('hello world', _encoder=Encoders.encode_7or8bit) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 363 | eq(msg.get_payload(), 'hello world\n') |
| 364 | eq(msg['content-transfer-encoding'], '7bit') |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 365 | msg = MIMEText('hello \x7f world', _encoder=Encoders.encode_7or8bit) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 366 | eq(msg.get_payload(), 'hello \x7f world\n') |
| 367 | eq(msg['content-transfer-encoding'], '7bit') |
| 368 | |
| 369 | def test_encode_8bit(self): |
| 370 | eq = self.assertEqual |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 371 | msg = MIMEText('hello \x80 world', _encoder=Encoders.encode_7or8bit) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 372 | eq(msg.get_payload(), 'hello \x80 world\n') |
| 373 | eq(msg['content-transfer-encoding'], '8bit') |
| 374 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 375 | def test_encode_empty_payload(self): |
| 376 | eq = self.assertEqual |
| 377 | msg = Message() |
| 378 | msg.set_charset('us-ascii') |
| 379 | eq(msg['content-transfer-encoding'], '7bit') |
| 380 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 381 | def test_encode_base64(self): |
| 382 | eq = self.assertEqual |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 383 | msg = MIMEText('hello world', _encoder=Encoders.encode_base64) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 384 | eq(msg.get_payload(), 'aGVsbG8gd29ybGQK\n') |
| 385 | eq(msg['content-transfer-encoding'], 'base64') |
| 386 | |
| 387 | def test_encode_quoted_printable(self): |
| 388 | eq = self.assertEqual |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 389 | msg = MIMEText('hello world', _encoder=Encoders.encode_quopri) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 390 | eq(msg.get_payload(), 'hello=20world\n') |
| 391 | eq(msg['content-transfer-encoding'], 'quoted-printable') |
| 392 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 393 | def test_default_cte(self): |
| 394 | eq = self.assertEqual |
| 395 | msg = MIMEText('hello world') |
| 396 | eq(msg['content-transfer-encoding'], '7bit') |
| 397 | |
| 398 | def test_default_cte(self): |
| 399 | eq = self.assertEqual |
| 400 | # With no explicit _charset its us-ascii, and all are 7-bit |
| 401 | msg = MIMEText('hello world') |
| 402 | eq(msg['content-transfer-encoding'], '7bit') |
| 403 | # Similar, but with 8-bit data |
| 404 | msg = MIMEText('hello \xf8 world') |
| 405 | eq(msg['content-transfer-encoding'], '8bit') |
| 406 | # And now with a different charset |
| 407 | msg = MIMEText('hello \xf8 world', _charset='iso-8859-1') |
| 408 | eq(msg['content-transfer-encoding'], 'quoted-printable') |
| 409 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 410 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 411 | |
| 412 | # Test long header wrapping |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 413 | class TestLongHeaders(TestEmailBase): |
| 414 | def test_split_long_continuation(self): |
| 415 | eq = self.ndiffAssertEqual |
| 416 | msg = email.message_from_string("""\ |
| 417 | Subject: bug demonstration |
| 418 | \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 419 | \tmore text |
| 420 | |
| 421 | test |
| 422 | """) |
| 423 | sfp = StringIO() |
| 424 | g = Generator(sfp) |
| 425 | g.flatten(msg) |
| 426 | eq(sfp.getvalue(), """\ |
| 427 | Subject: bug demonstration |
| 428 | \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 429 | \tmore text |
| 430 | |
| 431 | test |
| 432 | """) |
| 433 | |
| 434 | def test_another_long_almost_unsplittable_header(self): |
| 435 | eq = self.ndiffAssertEqual |
| 436 | hstr = """\ |
| 437 | bug demonstration |
| 438 | \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 439 | \tmore text""" |
| 440 | h = Header(hstr, continuation_ws='\t') |
| 441 | eq(h.encode(), """\ |
| 442 | bug demonstration |
| 443 | \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 444 | \tmore text""") |
| 445 | h = Header(hstr) |
| 446 | eq(h.encode(), """\ |
| 447 | bug demonstration |
| 448 | 12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 |
| 449 | more text""") |
| 450 | |
| 451 | def test_long_nonstring(self): |
| 452 | eq = self.ndiffAssertEqual |
| 453 | g = Charset("iso-8859-1") |
| 454 | cz = Charset("iso-8859-2") |
| 455 | utf8 = Charset("utf-8") |
| 456 | g_head = "Die Mieter treten hier ein werden mit einem Foerderband komfortabel den Korridor entlang, an s\xfcdl\xfcndischen Wandgem\xe4lden vorbei, gegen die rotierenden Klingen bef\xf6rdert. " |
| 457 | cz_head = "Finan\xe8ni metropole se hroutily pod tlakem jejich d\xf9vtipu.. " |
| 458 | utf8_head = u"\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066\u3044\u307e\u3059\u3002".encode("utf-8") |
| 459 | h = Header(g_head, g) |
| 460 | h.append(cz_head, cz) |
| 461 | h.append(utf8_head, utf8) |
| 462 | msg = Message() |
| 463 | msg['Subject'] = h |
| 464 | sfp = StringIO() |
| 465 | g = Generator(sfp) |
| 466 | g.flatten(msg) |
| 467 | eq(sfp.getvalue(), '''\ |
| 468 | Subject: =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_eine?= |
| 469 | =?iso-8859-1?q?m_Foerderband_komfortabel_den_Korridor_ent?= |
| 470 | =?iso-8859-1?q?lang=2C_an_s=FCdl=FCndischen_Wandgem=E4lden_vorbei?= |
| 471 | =?iso-8859-1?q?=2C_gegen_die_rotierenden_Klingen_bef=F6rdert=2E_?= |
| 472 | =?iso-8859-2?q?Finan=E8ni_metropole_se_hroutil?= |
| 473 | =?iso-8859-2?q?y_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= |
| 474 | =?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv?= |
| 475 | =?utf-8?b?44GV44KM44Gm44GE44G+44Gb44KT44CC5LiA?= |
| 476 | =?utf-8?b?6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM?= |
| 477 | =?utf-8?b?44CB44GC44Go44Gv44Gn44Gf44KJ44KB44Gn?= |
| 478 | =?utf-8?b?44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGE=?= |
| 479 | =?utf-8?b?cyBOdW5zdHVjayBnaXQgdW5k?= |
| 480 | =?utf-8?b?IFNsb3Rlcm1leWVyPyBKYSEgQmVpaGVyaHVuZCBkYXMgT2Rl?= |
| 481 | =?utf-8?b?ciBkaWUgRmxpcHBlcndhbGR0?= |
| 482 | =?utf-8?b?IGdlcnNwdXQu44CN44Go6KiA44Gj44Gm44GE44G+44GZ44CC?= |
| 483 | |
| 484 | ''') |
| 485 | eq(h.encode(), '''\ |
| 486 | =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_eine?= |
| 487 | =?iso-8859-1?q?m_Foerderband_komfortabel_den_Korridor_ent?= |
| 488 | =?iso-8859-1?q?lang=2C_an_s=FCdl=FCndischen_Wandgem=E4lden_vorbei?= |
| 489 | =?iso-8859-1?q?=2C_gegen_die_rotierenden_Klingen_bef=F6rdert=2E_?= |
| 490 | =?iso-8859-2?q?Finan=E8ni_metropole_se_hroutil?= |
| 491 | =?iso-8859-2?q?y_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= |
| 492 | =?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv?= |
| 493 | =?utf-8?b?44GV44KM44Gm44GE44G+44Gb44KT44CC5LiA?= |
| 494 | =?utf-8?b?6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM?= |
| 495 | =?utf-8?b?44CB44GC44Go44Gv44Gn44Gf44KJ44KB44Gn?= |
| 496 | =?utf-8?b?44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGE=?= |
| 497 | =?utf-8?b?cyBOdW5zdHVjayBnaXQgdW5k?= |
| 498 | =?utf-8?b?IFNsb3Rlcm1leWVyPyBKYSEgQmVpaGVyaHVuZCBkYXMgT2Rl?= |
| 499 | =?utf-8?b?ciBkaWUgRmxpcHBlcndhbGR0?= |
| 500 | =?utf-8?b?IGdlcnNwdXQu44CN44Go6KiA44Gj44Gm44GE44G+44GZ44CC?=''') |
| 501 | |
| 502 | def test_long_header_encode(self): |
| 503 | eq = self.ndiffAssertEqual |
| 504 | h = Header('wasnipoop; giraffes="very-long-necked-animals"; ' |
| 505 | 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"', |
| 506 | header_name='X-Foobar-Spoink-Defrobnit') |
| 507 | eq(h.encode(), '''\ |
| 508 | wasnipoop; giraffes="very-long-necked-animals"; |
| 509 | spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') |
| 510 | |
| 511 | def test_long_header_encode_with_tab_continuation(self): |
| 512 | eq = self.ndiffAssertEqual |
| 513 | h = Header('wasnipoop; giraffes="very-long-necked-animals"; ' |
| 514 | 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"', |
| 515 | header_name='X-Foobar-Spoink-Defrobnit', |
| 516 | continuation_ws='\t') |
| 517 | eq(h.encode(), '''\ |
| 518 | wasnipoop; giraffes="very-long-necked-animals"; |
| 519 | \tspooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') |
| 520 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 521 | def test_header_splitter(self): |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 522 | eq = self.ndiffAssertEqual |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 523 | msg = MIMEText('') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 524 | # It'd be great if we could use add_header() here, but that doesn't |
| 525 | # guarantee an order of the parameters. |
| 526 | msg['X-Foobar-Spoink-Defrobnit'] = ( |
| 527 | 'wasnipoop; giraffes="very-long-necked-animals"; ' |
| 528 | 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"') |
| 529 | sfp = StringIO() |
| 530 | g = Generator(sfp) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 531 | g.flatten(msg) |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 532 | eq(sfp.getvalue(), '''\ |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 533 | Content-Type: text/plain; charset="us-ascii" |
| 534 | MIME-Version: 1.0 |
| 535 | Content-Transfer-Encoding: 7bit |
| 536 | X-Foobar-Spoink-Defrobnit: wasnipoop; giraffes="very-long-necked-animals"; |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 537 | \tspooge="yummy"; hippos="gargantuan"; marshmallows="gooey" |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 538 | |
| 539 | ''') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 540 | |
Barry Warsaw | 07227d1 | 2001-10-17 20:52:26 +0000 | [diff] [blame] | 541 | def test_no_semis_header_splitter(self): |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 542 | eq = self.ndiffAssertEqual |
Barry Warsaw | 07227d1 | 2001-10-17 20:52:26 +0000 | [diff] [blame] | 543 | msg = Message() |
| 544 | msg['From'] = 'test@dom.ain' |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 545 | msg['References'] = SPACE.join(['<%d@dom.ain>' % i for i in range(10)]) |
Barry Warsaw | 07227d1 | 2001-10-17 20:52:26 +0000 | [diff] [blame] | 546 | msg.set_payload('Test') |
| 547 | sfp = StringIO() |
| 548 | g = Generator(sfp) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 549 | g.flatten(msg) |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 550 | eq(sfp.getvalue(), """\ |
Barry Warsaw | 07227d1 | 2001-10-17 20:52:26 +0000 | [diff] [blame] | 551 | From: test@dom.ain |
| 552 | References: <0@dom.ain> <1@dom.ain> <2@dom.ain> <3@dom.ain> <4@dom.ain> |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 553 | \t<5@dom.ain> <6@dom.ain> <7@dom.ain> <8@dom.ain> <9@dom.ain> |
Barry Warsaw | 07227d1 | 2001-10-17 20:52:26 +0000 | [diff] [blame] | 554 | |
| 555 | Test""") |
| 556 | |
| 557 | def test_no_split_long_header(self): |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 558 | eq = self.ndiffAssertEqual |
| 559 | hstr = 'References: ' + 'x' * 80 |
| 560 | h = Header(hstr, continuation_ws='\t') |
| 561 | eq(h.encode(), """\ |
| 562 | References: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx""") |
Barry Warsaw | 07227d1 | 2001-10-17 20:52:26 +0000 | [diff] [blame] | 563 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 564 | def test_splitting_multiple_long_lines(self): |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 565 | eq = self.ndiffAssertEqual |
| 566 | hstr = """\ |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 567 | 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) |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 568 | \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) |
| 569 | \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) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 570 | """ |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 571 | h = Header(hstr, continuation_ws='\t') |
| 572 | eq(h.encode(), """\ |
| 573 | from babylon.socal-raves.org (localhost [127.0.0.1]); |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 574 | \tby babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; |
| 575 | \tfor <mailman-admin@babylon.socal-raves.org>; |
| 576 | \tSat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 577 | \tfrom babylon.socal-raves.org (localhost [127.0.0.1]); |
| 578 | \tby babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; |
| 579 | \tfor <mailman-admin@babylon.socal-raves.org>; |
| 580 | \tSat, 2 Feb 2002 17:00:06 -0800 (PST) |
| 581 | \tfrom babylon.socal-raves.org (localhost [127.0.0.1]); |
| 582 | \tby babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81; |
| 583 | \tfor <mailman-admin@babylon.socal-raves.org>; |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 584 | \tSat, 2 Feb 2002 17:00:06 -0800 (PST)""") |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 585 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 586 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 587 | |
| 588 | # Test mangling of "From " lines in the body of a message |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 589 | class TestFromMangling(unittest.TestCase): |
| 590 | def setUp(self): |
| 591 | self.msg = Message() |
| 592 | self.msg['From'] = 'aaa@bbb.org' |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 593 | self.msg.set_payload("""\ |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 594 | From the desk of A.A.A.: |
| 595 | Blah blah blah |
| 596 | """) |
| 597 | |
| 598 | def test_mangled_from(self): |
| 599 | s = StringIO() |
| 600 | g = Generator(s, mangle_from_=1) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 601 | g.flatten(self.msg) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 602 | self.assertEqual(s.getvalue(), """\ |
| 603 | From: aaa@bbb.org |
| 604 | |
| 605 | >From the desk of A.A.A.: |
| 606 | Blah blah blah |
| 607 | """) |
| 608 | |
| 609 | def test_dont_mangle_from(self): |
| 610 | s = StringIO() |
| 611 | g = Generator(s, mangle_from_=0) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 612 | g.flatten(self.msg) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 613 | self.assertEqual(s.getvalue(), """\ |
| 614 | From: aaa@bbb.org |
| 615 | |
| 616 | From the desk of A.A.A.: |
| 617 | Blah blah blah |
| 618 | """) |
| 619 | |
| 620 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 621 | |
Barry Warsaw | fee435a | 2001-10-09 19:23:57 +0000 | [diff] [blame] | 622 | # Test the basic MIMEAudio class |
| 623 | class TestMIMEAudio(unittest.TestCase): |
| 624 | def setUp(self): |
| 625 | # In Python, audiotest.au lives in Lib/test not Lib/test/data |
| 626 | fp = open(findfile('audiotest.au')) |
| 627 | try: |
| 628 | self._audiodata = fp.read() |
| 629 | finally: |
| 630 | fp.close() |
| 631 | self._au = MIMEAudio(self._audiodata) |
| 632 | |
| 633 | def test_guess_minor_type(self): |
| 634 | self.assertEqual(self._au.get_type(), 'audio/basic') |
| 635 | |
| 636 | def test_encoding(self): |
| 637 | payload = self._au.get_payload() |
| 638 | self.assertEqual(base64.decodestring(payload), self._audiodata) |
| 639 | |
| 640 | def checkSetMinor(self): |
| 641 | au = MIMEAudio(self._audiodata, 'fish') |
| 642 | self.assertEqual(im.get_type(), 'audio/fish') |
| 643 | |
| 644 | def test_custom_encoder(self): |
| 645 | eq = self.assertEqual |
| 646 | def encoder(msg): |
| 647 | orig = msg.get_payload() |
| 648 | msg.set_payload(0) |
| 649 | msg['Content-Transfer-Encoding'] = 'broken64' |
| 650 | au = MIMEAudio(self._audiodata, _encoder=encoder) |
| 651 | eq(au.get_payload(), 0) |
| 652 | eq(au['content-transfer-encoding'], 'broken64') |
| 653 | |
| 654 | def test_add_header(self): |
| 655 | eq = self.assertEqual |
| 656 | unless = self.failUnless |
| 657 | self._au.add_header('Content-Disposition', 'attachment', |
| 658 | filename='audiotest.au') |
| 659 | eq(self._au['content-disposition'], |
| 660 | 'attachment; filename="audiotest.au"') |
| 661 | eq(self._au.get_params(header='content-disposition'), |
| 662 | [('attachment', ''), ('filename', 'audiotest.au')]) |
| 663 | eq(self._au.get_param('filename', header='content-disposition'), |
| 664 | 'audiotest.au') |
| 665 | missing = [] |
| 666 | eq(self._au.get_param('attachment', header='content-disposition'), '') |
| 667 | unless(self._au.get_param('foo', failobj=missing, |
| 668 | header='content-disposition') is missing) |
| 669 | # Try some missing stuff |
| 670 | unless(self._au.get_param('foobar', missing) is missing) |
| 671 | unless(self._au.get_param('attachment', missing, |
| 672 | header='foobar') is missing) |
| 673 | |
| 674 | |
| 675 | |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 676 | # Test the basic MIMEImage class |
| 677 | class TestMIMEImage(unittest.TestCase): |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 678 | def setUp(self): |
| 679 | fp = openfile('PyBanner048.gif') |
| 680 | try: |
| 681 | self._imgdata = fp.read() |
| 682 | finally: |
| 683 | fp.close() |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 684 | self._im = MIMEImage(self._imgdata) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 685 | |
| 686 | def test_guess_minor_type(self): |
| 687 | self.assertEqual(self._im.get_type(), 'image/gif') |
| 688 | |
| 689 | def test_encoding(self): |
| 690 | payload = self._im.get_payload() |
| 691 | self.assertEqual(base64.decodestring(payload), self._imgdata) |
| 692 | |
| 693 | def checkSetMinor(self): |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 694 | im = MIMEImage(self._imgdata, 'fish') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 695 | self.assertEqual(im.get_type(), 'image/fish') |
| 696 | |
| 697 | def test_custom_encoder(self): |
| 698 | eq = self.assertEqual |
| 699 | def encoder(msg): |
| 700 | orig = msg.get_payload() |
| 701 | msg.set_payload(0) |
| 702 | msg['Content-Transfer-Encoding'] = 'broken64' |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 703 | im = MIMEImage(self._imgdata, _encoder=encoder) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 704 | eq(im.get_payload(), 0) |
| 705 | eq(im['content-transfer-encoding'], 'broken64') |
| 706 | |
| 707 | def test_add_header(self): |
| 708 | eq = self.assertEqual |
| 709 | unless = self.failUnless |
| 710 | self._im.add_header('Content-Disposition', 'attachment', |
| 711 | filename='dingusfish.gif') |
| 712 | eq(self._im['content-disposition'], |
| 713 | 'attachment; filename="dingusfish.gif"') |
| 714 | eq(self._im.get_params(header='content-disposition'), |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 715 | [('attachment', ''), ('filename', 'dingusfish.gif')]) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 716 | eq(self._im.get_param('filename', header='content-disposition'), |
| 717 | 'dingusfish.gif') |
| 718 | missing = [] |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 719 | eq(self._im.get_param('attachment', header='content-disposition'), '') |
| 720 | unless(self._im.get_param('foo', failobj=missing, |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 721 | header='content-disposition') is missing) |
| 722 | # Try some missing stuff |
| 723 | unless(self._im.get_param('foobar', missing) is missing) |
| 724 | unless(self._im.get_param('attachment', missing, |
| 725 | header='foobar') is missing) |
| 726 | |
| 727 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 728 | |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 729 | # Test the basic MIMEText class |
| 730 | class TestMIMEText(unittest.TestCase): |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 731 | def setUp(self): |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 732 | self._msg = MIMEText('hello there') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 733 | |
| 734 | def test_types(self): |
| 735 | eq = self.assertEqual |
| 736 | unless = self.failUnless |
| 737 | eq(self._msg.get_type(), 'text/plain') |
| 738 | eq(self._msg.get_param('charset'), 'us-ascii') |
| 739 | missing = [] |
| 740 | unless(self._msg.get_param('foobar', missing) is missing) |
| 741 | unless(self._msg.get_param('charset', missing, header='foobar') |
| 742 | is missing) |
| 743 | |
| 744 | def test_payload(self): |
| 745 | self.assertEqual(self._msg.get_payload(), 'hello there\n') |
| 746 | self.failUnless(not self._msg.is_multipart()) |
| 747 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 748 | def test_charset(self): |
| 749 | eq = self.assertEqual |
| 750 | msg = MIMEText('hello there', _charset='us-ascii') |
| 751 | eq(msg.get_charset().input_charset, 'us-ascii') |
| 752 | eq(msg['content-type'], 'text/plain; charset="us-ascii"') |
| 753 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 754 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 755 | |
| 756 | # Test a more complicated multipart/mixed type message |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 757 | class TestMultipartMixed(unittest.TestCase): |
| 758 | def setUp(self): |
| 759 | fp = openfile('PyBanner048.gif') |
| 760 | try: |
| 761 | data = fp.read() |
| 762 | finally: |
| 763 | fp.close() |
| 764 | |
| 765 | container = MIMEBase('multipart', 'mixed', boundary='BOUNDARY') |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 766 | image = MIMEImage(data, name='dingusfish.gif') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 767 | image.add_header('content-disposition', 'attachment', |
| 768 | filename='dingusfish.gif') |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 769 | intro = MIMEText('''\ |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 770 | Hi there, |
| 771 | |
| 772 | This is the dingus fish. |
| 773 | ''') |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 774 | container.attach(intro) |
| 775 | container.attach(image) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 776 | container['From'] = 'Barry <barry@digicool.com>' |
| 777 | container['To'] = 'Dingus Lovers <cravindogs@cravindogs.com>' |
| 778 | container['Subject'] = 'Here is your dingus fish' |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 779 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 780 | now = 987809702.54848599 |
| 781 | timetuple = time.localtime(now) |
| 782 | if timetuple[-1] == 0: |
| 783 | tzsecs = time.timezone |
| 784 | else: |
| 785 | tzsecs = time.altzone |
| 786 | if tzsecs > 0: |
| 787 | sign = '-' |
| 788 | else: |
| 789 | sign = '+' |
| 790 | tzoffset = ' %s%04d' % (sign, tzsecs / 36) |
| 791 | container['Date'] = time.strftime( |
| 792 | '%a, %d %b %Y %H:%M:%S', |
| 793 | time.localtime(now)) + tzoffset |
| 794 | self._msg = container |
| 795 | self._im = image |
| 796 | self._txt = intro |
| 797 | |
| 798 | def test_hierarchy(self): |
| 799 | # convenience |
| 800 | eq = self.assertEqual |
| 801 | unless = self.failUnless |
| 802 | raises = self.assertRaises |
| 803 | # tests |
| 804 | m = self._msg |
| 805 | unless(m.is_multipart()) |
| 806 | eq(m.get_type(), 'multipart/mixed') |
| 807 | eq(len(m.get_payload()), 2) |
| 808 | raises(IndexError, m.get_payload, 2) |
| 809 | m0 = m.get_payload(0) |
| 810 | m1 = m.get_payload(1) |
| 811 | unless(m0 is self._txt) |
| 812 | unless(m1 is self._im) |
| 813 | eq(m.get_payload(), [m0, m1]) |
| 814 | unless(not m0.is_multipart()) |
| 815 | unless(not m1.is_multipart()) |
| 816 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 817 | def test_no_parts_in_a_multipart(self): |
| 818 | outer = MIMEBase('multipart', 'mixed') |
| 819 | outer['Subject'] = 'A subject' |
| 820 | outer['To'] = 'aperson@dom.ain' |
| 821 | outer['From'] = 'bperson@dom.ain' |
| 822 | outer.preamble = '' |
| 823 | outer.epilogue = '' |
| 824 | outer.set_boundary('BOUNDARY') |
| 825 | msg = MIMEText('hello world') |
| 826 | self.assertEqual(outer.as_string(), '''\ |
| 827 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 828 | MIME-Version: 1.0 |
| 829 | Subject: A subject |
| 830 | To: aperson@dom.ain |
| 831 | From: bperson@dom.ain |
| 832 | |
| 833 | --BOUNDARY |
| 834 | |
| 835 | |
| 836 | --BOUNDARY-- |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 837 | ''') |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 838 | |
| 839 | def test_one_part_in_a_multipart(self): |
| 840 | outer = MIMEBase('multipart', 'mixed') |
| 841 | outer['Subject'] = 'A subject' |
| 842 | outer['To'] = 'aperson@dom.ain' |
| 843 | outer['From'] = 'bperson@dom.ain' |
| 844 | outer.preamble = '' |
| 845 | outer.epilogue = '' |
| 846 | outer.set_boundary('BOUNDARY') |
| 847 | msg = MIMEText('hello world') |
| 848 | outer.attach(msg) |
| 849 | self.assertEqual(outer.as_string(), '''\ |
| 850 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 851 | MIME-Version: 1.0 |
| 852 | Subject: A subject |
| 853 | To: aperson@dom.ain |
| 854 | From: bperson@dom.ain |
| 855 | |
| 856 | --BOUNDARY |
| 857 | Content-Type: text/plain; charset="us-ascii" |
| 858 | MIME-Version: 1.0 |
| 859 | Content-Transfer-Encoding: 7bit |
| 860 | |
| 861 | hello world |
| 862 | |
| 863 | --BOUNDARY-- |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 864 | ''') |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 865 | |
| 866 | def test_seq_parts_in_a_multipart(self): |
| 867 | outer = MIMEBase('multipart', 'mixed') |
| 868 | outer['Subject'] = 'A subject' |
| 869 | outer['To'] = 'aperson@dom.ain' |
| 870 | outer['From'] = 'bperson@dom.ain' |
| 871 | outer.preamble = '' |
| 872 | outer.epilogue = '' |
| 873 | msg = MIMEText('hello world') |
| 874 | outer.attach(msg) |
| 875 | outer.set_boundary('BOUNDARY') |
| 876 | self.assertEqual(outer.as_string(), '''\ |
| 877 | Content-Type: multipart/mixed; boundary="BOUNDARY" |
| 878 | MIME-Version: 1.0 |
| 879 | Subject: A subject |
| 880 | To: aperson@dom.ain |
| 881 | From: bperson@dom.ain |
| 882 | |
| 883 | --BOUNDARY |
| 884 | Content-Type: text/plain; charset="us-ascii" |
| 885 | MIME-Version: 1.0 |
| 886 | Content-Transfer-Encoding: 7bit |
| 887 | |
| 888 | hello world |
| 889 | |
| 890 | --BOUNDARY-- |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 891 | ''') |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 892 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 893 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 894 | |
| 895 | # Test some badly formatted messages |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 896 | class TestNonConformant(TestEmailBase): |
| 897 | def test_parse_missing_minor_type(self): |
| 898 | eq = self.assertEqual |
| 899 | msg = self._msgobj('msg_14.txt') |
| 900 | eq(msg.get_type(), 'text') |
| 901 | eq(msg.get_main_type(), 'text') |
| 902 | self.failUnless(msg.get_subtype() is None) |
| 903 | |
| 904 | def test_bogus_boundary(self): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 905 | fp = openfile(findfile('msg_15.txt')) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 906 | try: |
| 907 | data = fp.read() |
| 908 | finally: |
| 909 | fp.close() |
| 910 | p = Parser() |
| 911 | # Note, under a future non-strict parsing mode, this would parse the |
| 912 | # message into the intended message tree. |
| 913 | self.assertRaises(Errors.BoundaryError, p.parsestr, data) |
| 914 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 915 | def test_multipart_no_boundary(self): |
| 916 | fp = openfile(findfile('msg_25.txt')) |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 917 | try: |
| 918 | self.assertRaises(Errors.BoundaryError, |
| 919 | email.message_from_file, fp) |
| 920 | finally: |
| 921 | fp.close() |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 922 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 923 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 924 | |
| 925 | # Test RFC 2047 header encoding and decoding |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 926 | class TestRFC2047(unittest.TestCase): |
| 927 | def test_iso_8859_1(self): |
| 928 | eq = self.assertEqual |
| 929 | s = '=?iso-8859-1?q?this=20is=20some=20text?=' |
| 930 | eq(Utils.decode(s), 'this is some text') |
| 931 | s = '=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=' |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 932 | eq(Utils.decode(s), u'Keld J\xf8rn Simonsen') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 933 | s = '=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=' \ |
| 934 | '=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=' |
| 935 | eq(Utils.decode(s), 'If you can read this you understand the example.') |
| 936 | s = '=?iso-8859-8?b?7eXs+SDv4SDp7Oj08A==?=' |
| 937 | eq(Utils.decode(s), |
| 938 | u'\u05dd\u05d5\u05dc\u05e9 \u05df\u05d1 \u05d9\u05dc\u05d8\u05e4\u05e0') |
| 939 | s = '=?iso-8859-1?q?this=20is?= =?iso-8859-1?q?some=20text?=' |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 940 | eq(Utils.decode(s), u'this issome text') |
| 941 | s = '=?iso-8859-1?q?this=20is_?= =?iso-8859-1?q?some=20text?=' |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 942 | eq(Utils.decode(s), u'this is some text') |
| 943 | |
| 944 | def test_encode_header(self): |
| 945 | eq = self.assertEqual |
| 946 | s = 'this is some text' |
| 947 | eq(Utils.encode(s), '=?iso-8859-1?q?this=20is=20some=20text?=') |
| 948 | s = 'Keld_J\xf8rn_Simonsen' |
| 949 | eq(Utils.encode(s), '=?iso-8859-1?q?Keld_J=F8rn_Simonsen?=') |
| 950 | s1 = 'If you can read this yo' |
| 951 | s2 = 'u understand the example.' |
| 952 | eq(Utils.encode(s1, encoding='b'), |
| 953 | '=?iso-8859-1?b?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=') |
| 954 | eq(Utils.encode(s2, charset='iso-8859-2', encoding='b'), |
| 955 | '=?iso-8859-2?b?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=') |
| 956 | |
| 957 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 958 | |
| 959 | # Test the MIMEMessage class |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 960 | class TestMIMEMessage(TestEmailBase): |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 961 | def setUp(self): |
| 962 | fp = openfile('msg_11.txt') |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 963 | try: |
| 964 | self._text = fp.read() |
| 965 | finally: |
| 966 | fp.close() |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 967 | |
| 968 | def test_type_error(self): |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 969 | self.assertRaises(TypeError, MIMEMessage, 'a plain string') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 970 | |
| 971 | def test_valid_argument(self): |
| 972 | eq = self.assertEqual |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 973 | unless = self.failUnless |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 974 | subject = 'A sub-message' |
| 975 | m = Message() |
| 976 | m['Subject'] = subject |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 977 | r = MIMEMessage(m) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 978 | eq(r.get_type(), 'message/rfc822') |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 979 | payload = r.get_payload() |
| 980 | unless(type(payload), ListType) |
| 981 | eq(len(payload), 1) |
| 982 | subpart = payload[0] |
| 983 | unless(subpart is m) |
| 984 | eq(subpart['subject'], subject) |
| 985 | |
| 986 | def test_bad_multipart(self): |
| 987 | eq = self.assertEqual |
| 988 | msg1 = Message() |
| 989 | msg1['Subject'] = 'subpart 1' |
| 990 | msg2 = Message() |
| 991 | msg2['Subject'] = 'subpart 2' |
| 992 | r = MIMEMessage(msg1) |
| 993 | self.assertRaises(Errors.MultipartConversionError, r.attach, msg2) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 994 | |
| 995 | def test_generate(self): |
| 996 | # First craft the message to be encapsulated |
| 997 | m = Message() |
| 998 | m['Subject'] = 'An enclosed message' |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 999 | m.set_payload('Here is the body of the message.\n') |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1000 | r = MIMEMessage(m) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1001 | r['Subject'] = 'The enclosing message' |
| 1002 | s = StringIO() |
| 1003 | g = Generator(s) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1004 | g.flatten(r) |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1005 | self.assertEqual(s.getvalue(), """\ |
| 1006 | Content-Type: message/rfc822 |
| 1007 | MIME-Version: 1.0 |
| 1008 | Subject: The enclosing message |
| 1009 | |
| 1010 | Subject: An enclosed message |
| 1011 | |
| 1012 | Here is the body of the message. |
| 1013 | """) |
| 1014 | |
| 1015 | def test_parse_message_rfc822(self): |
| 1016 | eq = self.assertEqual |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1017 | unless = self.failUnless |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1018 | msg = self._msgobj('msg_11.txt') |
| 1019 | eq(msg.get_type(), 'message/rfc822') |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1020 | payload = msg.get_payload() |
| 1021 | unless(isinstance(payload, ListType)) |
| 1022 | eq(len(payload), 1) |
| 1023 | submsg = payload[0] |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1024 | self.failUnless(isinstance(submsg, Message)) |
| 1025 | eq(submsg['subject'], 'An enclosed message') |
| 1026 | eq(submsg.get_payload(), 'Here is the body of the message.\n') |
| 1027 | |
| 1028 | def test_dsn(self): |
| 1029 | eq = self.assertEqual |
| 1030 | unless = self.failUnless |
Barry Warsaw | c53b29e | 2002-07-09 16:36:36 +0000 | [diff] [blame] | 1031 | # msg 16 is a Delivery Status Notification, see RFC 1894 |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1032 | msg = self._msgobj('msg_16.txt') |
| 1033 | eq(msg.get_type(), 'multipart/report') |
| 1034 | unless(msg.is_multipart()) |
| 1035 | eq(len(msg.get_payload()), 3) |
| 1036 | # Subpart 1 is a text/plain, human readable section |
| 1037 | subpart = msg.get_payload(0) |
| 1038 | eq(subpart.get_type(), 'text/plain') |
| 1039 | eq(subpart.get_payload(), """\ |
| 1040 | This report relates to a message you sent with the following header fields: |
| 1041 | |
| 1042 | Message-id: <002001c144a6$8752e060$56104586@oxy.edu> |
| 1043 | Date: Sun, 23 Sep 2001 20:10:55 -0700 |
| 1044 | From: "Ian T. Henry" <henryi@oxy.edu> |
| 1045 | To: SoCal Raves <scr@socal-raves.org> |
| 1046 | Subject: [scr] yeah for Ians!! |
| 1047 | |
| 1048 | Your message cannot be delivered to the following recipients: |
| 1049 | |
| 1050 | Recipient address: jangel1@cougar.noc.ucla.edu |
| 1051 | Reason: recipient reached disk quota |
| 1052 | |
| 1053 | """) |
| 1054 | # Subpart 2 contains the machine parsable DSN information. It |
| 1055 | # consists of two blocks of headers, represented by two nested Message |
| 1056 | # objects. |
| 1057 | subpart = msg.get_payload(1) |
| 1058 | eq(subpart.get_type(), 'message/delivery-status') |
| 1059 | eq(len(subpart.get_payload()), 2) |
| 1060 | # message/delivery-status should treat each block as a bunch of |
| 1061 | # headers, i.e. a bunch of Message objects. |
| 1062 | dsn1 = subpart.get_payload(0) |
| 1063 | unless(isinstance(dsn1, Message)) |
| 1064 | eq(dsn1['original-envelope-id'], '0GK500B4HD0888@cougar.noc.ucla.edu') |
| 1065 | eq(dsn1.get_param('dns', header='reporting-mta'), '') |
| 1066 | # Try a missing one <wink> |
| 1067 | eq(dsn1.get_param('nsd', header='reporting-mta'), None) |
| 1068 | dsn2 = subpart.get_payload(1) |
| 1069 | unless(isinstance(dsn2, Message)) |
| 1070 | eq(dsn2['action'], 'failed') |
| 1071 | eq(dsn2.get_params(header='original-recipient'), |
| 1072 | [('rfc822', ''), ('jangel1@cougar.noc.ucla.edu', '')]) |
| 1073 | eq(dsn2.get_param('rfc822', header='final-recipient'), '') |
| 1074 | # Subpart 3 is the original message |
| 1075 | subpart = msg.get_payload(2) |
| 1076 | eq(subpart.get_type(), 'message/rfc822') |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1077 | payload = subpart.get_payload() |
| 1078 | unless(isinstance(payload, ListType)) |
| 1079 | eq(len(payload), 1) |
| 1080 | subsubpart = payload[0] |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1081 | unless(isinstance(subsubpart, Message)) |
| 1082 | eq(subsubpart.get_type(), 'text/plain') |
| 1083 | eq(subsubpart['message-id'], |
| 1084 | '<002001c144a6$8752e060$56104586@oxy.edu>') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1085 | |
Barry Warsaw | 1f0fa92 | 2001-10-19 04:08:59 +0000 | [diff] [blame] | 1086 | def test_epilogue(self): |
| 1087 | fp = openfile('msg_21.txt') |
| 1088 | try: |
| 1089 | text = fp.read() |
| 1090 | finally: |
| 1091 | fp.close() |
| 1092 | msg = Message() |
| 1093 | msg['From'] = 'aperson@dom.ain' |
| 1094 | msg['To'] = 'bperson@dom.ain' |
| 1095 | msg['Subject'] = 'Test' |
| 1096 | msg.preamble = 'MIME message\n' |
| 1097 | msg.epilogue = 'End of MIME message\n' |
| 1098 | msg1 = MIMEText('One') |
| 1099 | msg2 = MIMEText('Two') |
| 1100 | msg.add_header('Content-Type', 'multipart/mixed', boundary='BOUNDARY') |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1101 | msg.attach(msg1) |
| 1102 | msg.attach(msg2) |
Barry Warsaw | 1f0fa92 | 2001-10-19 04:08:59 +0000 | [diff] [blame] | 1103 | sfp = StringIO() |
| 1104 | g = Generator(sfp) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1105 | g.flatten(msg) |
Barry Warsaw | 1f0fa92 | 2001-10-19 04:08:59 +0000 | [diff] [blame] | 1106 | self.assertEqual(sfp.getvalue(), text) |
| 1107 | |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 1108 | def test_default_type(self): |
| 1109 | eq = self.assertEqual |
| 1110 | fp = openfile('msg_30.txt') |
| 1111 | try: |
| 1112 | msg = email.message_from_file(fp) |
| 1113 | finally: |
| 1114 | fp.close() |
| 1115 | container1 = msg.get_payload(0) |
| 1116 | eq(container1.get_default_type(), 'message/rfc822') |
| 1117 | eq(container1.get_type(), None) |
| 1118 | container2 = msg.get_payload(1) |
| 1119 | eq(container2.get_default_type(), 'message/rfc822') |
| 1120 | eq(container2.get_type(), None) |
| 1121 | container1a = container1.get_payload(0) |
| 1122 | eq(container1a.get_default_type(), 'text/plain') |
| 1123 | eq(container1a.get_type(), 'text/plain') |
| 1124 | container2a = container2.get_payload(0) |
| 1125 | eq(container2a.get_default_type(), 'text/plain') |
| 1126 | eq(container2a.get_type(), 'text/plain') |
| 1127 | |
| 1128 | def test_default_type_with_explicit_container_type(self): |
| 1129 | eq = self.assertEqual |
| 1130 | fp = openfile('msg_28.txt') |
| 1131 | try: |
| 1132 | msg = email.message_from_file(fp) |
| 1133 | finally: |
| 1134 | fp.close() |
| 1135 | container1 = msg.get_payload(0) |
| 1136 | eq(container1.get_default_type(), 'message/rfc822') |
| 1137 | eq(container1.get_type(), 'message/rfc822') |
| 1138 | container2 = msg.get_payload(1) |
| 1139 | eq(container2.get_default_type(), 'message/rfc822') |
| 1140 | eq(container2.get_type(), 'message/rfc822') |
| 1141 | container1a = container1.get_payload(0) |
| 1142 | eq(container1a.get_default_type(), 'text/plain') |
| 1143 | eq(container1a.get_type(), 'text/plain') |
| 1144 | container2a = container2.get_payload(0) |
| 1145 | eq(container2a.get_default_type(), 'text/plain') |
| 1146 | eq(container2a.get_type(), 'text/plain') |
| 1147 | |
| 1148 | def test_default_type_non_parsed(self): |
| 1149 | eq = self.assertEqual |
| 1150 | neq = self.ndiffAssertEqual |
| 1151 | # Set up container |
| 1152 | container = MIMEMultipart('digest', 'BOUNDARY') |
| 1153 | container.epilogue = '\n' |
| 1154 | # Set up subparts |
| 1155 | subpart1a = MIMEText('message 1\n') |
| 1156 | subpart2a = MIMEText('message 2\n') |
| 1157 | subpart1 = MIMEMessage(subpart1a) |
| 1158 | subpart2 = MIMEMessage(subpart2a) |
| 1159 | container.attach(subpart1) |
| 1160 | container.attach(subpart2) |
| 1161 | eq(subpart1.get_type(), 'message/rfc822') |
| 1162 | eq(subpart1.get_default_type(), 'message/rfc822') |
| 1163 | eq(subpart2.get_type(), 'message/rfc822') |
| 1164 | eq(subpart2.get_default_type(), 'message/rfc822') |
| 1165 | neq(container.as_string(0), '''\ |
| 1166 | Content-Type: multipart/digest; boundary="BOUNDARY" |
| 1167 | MIME-Version: 1.0 |
| 1168 | |
| 1169 | --BOUNDARY |
| 1170 | Content-Type: message/rfc822 |
| 1171 | MIME-Version: 1.0 |
| 1172 | |
| 1173 | Content-Type: text/plain; charset="us-ascii" |
| 1174 | MIME-Version: 1.0 |
| 1175 | Content-Transfer-Encoding: 7bit |
| 1176 | |
| 1177 | message 1 |
| 1178 | |
| 1179 | --BOUNDARY |
| 1180 | Content-Type: message/rfc822 |
| 1181 | MIME-Version: 1.0 |
| 1182 | |
| 1183 | Content-Type: text/plain; charset="us-ascii" |
| 1184 | MIME-Version: 1.0 |
| 1185 | Content-Transfer-Encoding: 7bit |
| 1186 | |
| 1187 | message 2 |
| 1188 | |
| 1189 | --BOUNDARY-- |
| 1190 | ''') |
| 1191 | del subpart1['content-type'] |
| 1192 | del subpart1['mime-version'] |
| 1193 | del subpart2['content-type'] |
| 1194 | del subpart2['mime-version'] |
| 1195 | eq(subpart1.get_type(), None) |
| 1196 | eq(subpart1.get_default_type(), 'message/rfc822') |
| 1197 | eq(subpart2.get_type(), None) |
| 1198 | eq(subpart2.get_default_type(), 'message/rfc822') |
| 1199 | neq(container.as_string(0), '''\ |
| 1200 | Content-Type: multipart/digest; boundary="BOUNDARY" |
| 1201 | MIME-Version: 1.0 |
| 1202 | |
| 1203 | --BOUNDARY |
| 1204 | |
| 1205 | Content-Type: text/plain; charset="us-ascii" |
| 1206 | MIME-Version: 1.0 |
| 1207 | Content-Transfer-Encoding: 7bit |
| 1208 | |
| 1209 | message 1 |
| 1210 | |
| 1211 | --BOUNDARY |
| 1212 | |
| 1213 | Content-Type: text/plain; charset="us-ascii" |
| 1214 | MIME-Version: 1.0 |
| 1215 | Content-Transfer-Encoding: 7bit |
| 1216 | |
| 1217 | message 2 |
| 1218 | |
| 1219 | --BOUNDARY-- |
| 1220 | ''') |
| 1221 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1222 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 1223 | |
| 1224 | # A general test of parser->model->generator idempotency. IOW, read a message |
| 1225 | # in, parse it into a message object tree, then without touching the tree, |
| 1226 | # regenerate the plain text. The original text and the transformed text |
| 1227 | # should be identical. Note: that we ignore the Unix-From since that may |
| 1228 | # contain a changed date. |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 1229 | class TestIdempotent(TestEmailBase): |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1230 | def _msgobj(self, filename): |
| 1231 | fp = openfile(filename) |
| 1232 | try: |
| 1233 | data = fp.read() |
| 1234 | finally: |
| 1235 | fp.close() |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1236 | msg = email.message_from_string(data) |
| 1237 | return msg, data |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1238 | |
| 1239 | def _idempotent(self, msg, text): |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 1240 | eq = self.ndiffAssertEqual |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1241 | s = StringIO() |
| 1242 | g = Generator(s, maxheaderlen=0) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1243 | g.flatten(msg) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1244 | eq(text, s.getvalue()) |
| 1245 | |
| 1246 | def test_parse_text_message(self): |
| 1247 | eq = self.assertEquals |
| 1248 | msg, text = self._msgobj('msg_01.txt') |
| 1249 | eq(msg.get_type(), 'text/plain') |
| 1250 | eq(msg.get_main_type(), 'text') |
| 1251 | eq(msg.get_subtype(), 'plain') |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1252 | eq(msg.get_params()[1], ('charset', 'us-ascii')) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1253 | eq(msg.get_param('charset'), 'us-ascii') |
| 1254 | eq(msg.preamble, None) |
| 1255 | eq(msg.epilogue, None) |
| 1256 | self._idempotent(msg, text) |
| 1257 | |
| 1258 | def test_parse_untyped_message(self): |
| 1259 | eq = self.assertEquals |
| 1260 | msg, text = self._msgobj('msg_03.txt') |
| 1261 | eq(msg.get_type(), None) |
| 1262 | eq(msg.get_params(), None) |
| 1263 | eq(msg.get_param('charset'), None) |
| 1264 | self._idempotent(msg, text) |
| 1265 | |
| 1266 | def test_simple_multipart(self): |
| 1267 | msg, text = self._msgobj('msg_04.txt') |
| 1268 | self._idempotent(msg, text) |
| 1269 | |
| 1270 | def test_MIME_digest(self): |
| 1271 | msg, text = self._msgobj('msg_02.txt') |
| 1272 | self._idempotent(msg, text) |
| 1273 | |
Barry Warsaw | 1969817 | 2002-06-29 15:23:39 +0000 | [diff] [blame] | 1274 | def test_long_header(self): |
| 1275 | msg, text = self._msgobj('msg_27.txt') |
| 1276 | self._idempotent(msg, text) |
| 1277 | |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 1278 | def test_MIME_digest_with_part_headers(self): |
| 1279 | msg, text = self._msgobj('msg_28.txt') |
| 1280 | self._idempotent(msg, text) |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 1281 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1282 | def test_mixed_with_image(self): |
| 1283 | msg, text = self._msgobj('msg_06.txt') |
| 1284 | self._idempotent(msg, text) |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 1285 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1286 | def test_multipart_report(self): |
| 1287 | msg, text = self._msgobj('msg_05.txt') |
| 1288 | self._idempotent(msg, text) |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1289 | |
| 1290 | def test_dsn(self): |
| 1291 | msg, text = self._msgobj('msg_16.txt') |
| 1292 | self._idempotent(msg, text) |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 1293 | |
Barry Warsaw | 1f0fa92 | 2001-10-19 04:08:59 +0000 | [diff] [blame] | 1294 | def test_preamble_epilogue(self): |
| 1295 | msg, text = self._msgobj('msg_21.txt') |
| 1296 | self._idempotent(msg, text) |
| 1297 | |
Barry Warsaw | 763af41 | 2002-01-27 06:48:47 +0000 | [diff] [blame] | 1298 | def test_multipart_one_part(self): |
| 1299 | msg, text = self._msgobj('msg_23.txt') |
| 1300 | self._idempotent(msg, text) |
| 1301 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1302 | def test_multipart_no_parts(self): |
| 1303 | msg, text = self._msgobj('msg_24.txt') |
| 1304 | self._idempotent(msg, text) |
| 1305 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1306 | def test_content_type(self): |
| 1307 | eq = self.assertEquals |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1308 | unless = self.failUnless |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1309 | # Get a message object and reset the seek pointer for other tests |
| 1310 | msg, text = self._msgobj('msg_05.txt') |
| 1311 | eq(msg.get_type(), 'multipart/report') |
| 1312 | # Test the Content-Type: parameters |
| 1313 | params = {} |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1314 | for pk, pv in msg.get_params(): |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1315 | params[pk] = pv |
| 1316 | eq(params['report-type'], 'delivery-status') |
Barry Warsaw | 65279d0 | 2001-09-26 05:47:08 +0000 | [diff] [blame] | 1317 | eq(params['boundary'], 'D1690A7AC1.996856090/mail.example.com') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1318 | eq(msg.preamble, 'This is a MIME-encapsulated message.\n\n') |
| 1319 | eq(msg.epilogue, '\n\n') |
| 1320 | eq(len(msg.get_payload()), 3) |
| 1321 | # Make sure the subparts are what we expect |
| 1322 | msg1 = msg.get_payload(0) |
| 1323 | eq(msg1.get_type(), 'text/plain') |
| 1324 | eq(msg1.get_payload(), 'Yadda yadda yadda\n') |
| 1325 | msg2 = msg.get_payload(1) |
| 1326 | eq(msg2.get_type(), None) |
| 1327 | eq(msg2.get_payload(), 'Yadda yadda yadda\n') |
| 1328 | msg3 = msg.get_payload(2) |
| 1329 | eq(msg3.get_type(), 'message/rfc822') |
| 1330 | self.failUnless(isinstance(msg3, Message)) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1331 | payload = msg3.get_payload() |
| 1332 | unless(isinstance(payload, ListType)) |
| 1333 | eq(len(payload), 1) |
| 1334 | msg4 = payload[0] |
| 1335 | unless(isinstance(msg4, Message)) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1336 | eq(msg4.get_payload(), 'Yadda yadda yadda\n') |
| 1337 | |
| 1338 | def test_parser(self): |
| 1339 | eq = self.assertEquals |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1340 | unless = self.failUnless |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1341 | msg, text = self._msgobj('msg_06.txt') |
| 1342 | # Check some of the outer headers |
| 1343 | eq(msg.get_type(), 'message/rfc822') |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1344 | # Make sure the payload is a list of exactly one sub-Message, and that |
| 1345 | # that submessage has a type of text/plain |
| 1346 | payload = msg.get_payload() |
| 1347 | unless(isinstance(payload, ListType)) |
| 1348 | eq(len(payload), 1) |
| 1349 | msg1 = payload[0] |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1350 | self.failUnless(isinstance(msg1, Message)) |
| 1351 | eq(msg1.get_type(), 'text/plain') |
| 1352 | self.failUnless(isinstance(msg1.get_payload(), StringType)) |
| 1353 | eq(msg1.get_payload(), '\n') |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1354 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 1355 | |
| 1356 | # Test various other bits of the package's functionality |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1357 | class TestMiscellaneous(unittest.TestCase): |
| 1358 | def test_message_from_string(self): |
| 1359 | fp = openfile('msg_01.txt') |
| 1360 | try: |
| 1361 | text = fp.read() |
| 1362 | finally: |
| 1363 | fp.close() |
| 1364 | msg = email.message_from_string(text) |
| 1365 | s = StringIO() |
| 1366 | # Don't wrap/continue long headers since we're trying to test |
| 1367 | # idempotency. |
| 1368 | g = Generator(s, maxheaderlen=0) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1369 | g.flatten(msg) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1370 | self.assertEqual(text, s.getvalue()) |
| 1371 | |
| 1372 | def test_message_from_file(self): |
| 1373 | fp = openfile('msg_01.txt') |
| 1374 | try: |
| 1375 | text = fp.read() |
| 1376 | fp.seek(0) |
| 1377 | msg = email.message_from_file(fp) |
| 1378 | s = StringIO() |
| 1379 | # Don't wrap/continue long headers since we're trying to test |
| 1380 | # idempotency. |
| 1381 | g = Generator(s, maxheaderlen=0) |
Barry Warsaw | 2c68506 | 2002-06-02 19:09:27 +0000 | [diff] [blame] | 1382 | g.flatten(msg) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1383 | self.assertEqual(text, s.getvalue()) |
| 1384 | finally: |
| 1385 | fp.close() |
| 1386 | |
| 1387 | def test_message_from_string_with_class(self): |
| 1388 | unless = self.failUnless |
| 1389 | fp = openfile('msg_01.txt') |
| 1390 | try: |
| 1391 | text = fp.read() |
| 1392 | finally: |
| 1393 | fp.close() |
| 1394 | # Create a subclass |
| 1395 | class MyMessage(Message): |
| 1396 | pass |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 1397 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1398 | msg = email.message_from_string(text, MyMessage) |
| 1399 | unless(isinstance(msg, MyMessage)) |
| 1400 | # Try something more complicated |
| 1401 | fp = openfile('msg_02.txt') |
| 1402 | try: |
| 1403 | text = fp.read() |
| 1404 | finally: |
| 1405 | fp.close() |
| 1406 | msg = email.message_from_string(text, MyMessage) |
| 1407 | for subpart in msg.walk(): |
| 1408 | unless(isinstance(subpart, MyMessage)) |
| 1409 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1410 | def test_message_from_file_with_class(self): |
| 1411 | unless = self.failUnless |
| 1412 | # Create a subclass |
| 1413 | class MyMessage(Message): |
| 1414 | pass |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 1415 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1416 | fp = openfile('msg_01.txt') |
| 1417 | try: |
| 1418 | msg = email.message_from_file(fp, MyMessage) |
| 1419 | finally: |
| 1420 | fp.close() |
| 1421 | unless(isinstance(msg, MyMessage)) |
| 1422 | # Try something more complicated |
| 1423 | fp = openfile('msg_02.txt') |
| 1424 | try: |
| 1425 | msg = email.message_from_file(fp, MyMessage) |
| 1426 | finally: |
| 1427 | fp.close() |
| 1428 | for subpart in msg.walk(): |
| 1429 | unless(isinstance(subpart, MyMessage)) |
| 1430 | |
Barry Warsaw | fee435a | 2001-10-09 19:23:57 +0000 | [diff] [blame] | 1431 | def test__all__(self): |
| 1432 | module = __import__('email') |
| 1433 | all = module.__all__ |
| 1434 | all.sort() |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 1435 | self.assertEqual(all, ['Charset', 'Encoders', 'Errors', 'Generator', |
| 1436 | 'Header', 'Iterators', 'MIMEAudio', |
| 1437 | 'MIMEBase', 'MIMEImage', 'MIMEMessage', |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1438 | 'MIMEText', 'Message', 'Parser', |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 1439 | 'Utils', 'base64MIME', |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1440 | 'message_from_file', 'message_from_string', |
| 1441 | 'quopriMIME']) |
Barry Warsaw | fee435a | 2001-10-09 19:23:57 +0000 | [diff] [blame] | 1442 | |
Barry Warsaw | 75edc6a | 2001-11-09 17:46:17 +0000 | [diff] [blame] | 1443 | def test_formatdate(self): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1444 | now = time.time() |
| 1445 | self.assertEqual(Utils.parsedate(Utils.formatdate(now))[:6], |
| 1446 | time.gmtime(now)[:6]) |
Barry Warsaw | 75edc6a | 2001-11-09 17:46:17 +0000 | [diff] [blame] | 1447 | |
Barry Warsaw | 4586d2c | 2001-11-19 18:38:42 +0000 | [diff] [blame] | 1448 | def test_formatdate_localtime(self): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1449 | now = time.time() |
| 1450 | self.assertEqual( |
| 1451 | Utils.parsedate(Utils.formatdate(now, localtime=1))[:6], |
| 1452 | time.localtime(now)[:6]) |
Barry Warsaw | 75a40fc | 2001-11-19 16:31:06 +0000 | [diff] [blame] | 1453 | |
| 1454 | def test_parsedate_none(self): |
Barry Warsaw | 19c10ca | 2001-11-13 18:01:37 +0000 | [diff] [blame] | 1455 | self.assertEqual(Utils.parsedate(''), None) |
| 1456 | |
Barry Warsaw | eae36ac | 2001-12-20 16:37:27 +0000 | [diff] [blame] | 1457 | def test_parseaddr_empty(self): |
| 1458 | self.assertEqual(Utils.parseaddr('<>'), ('', '')) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1459 | self.assertEqual(Utils.formataddr(Utils.parseaddr('<>')), '') |
| 1460 | |
| 1461 | def test_noquote_dump(self): |
| 1462 | self.assertEqual( |
| 1463 | Utils.formataddr(('A Silly Person', 'person@dom.ain')), |
| 1464 | 'A Silly Person <person@dom.ain>') |
| 1465 | |
| 1466 | def test_escape_dump(self): |
| 1467 | self.assertEqual( |
| 1468 | Utils.formataddr(('A (Very) Silly Person', 'person@dom.ain')), |
| 1469 | r'"A \(Very\) Silly Person" <person@dom.ain>') |
| 1470 | a = r'A \(Special\) Person' |
| 1471 | b = 'person@dom.ain' |
| 1472 | self.assertEqual(Utils.parseaddr(Utils.formataddr((a, b))), (a, b)) |
| 1473 | |
| 1474 | def test_quote_dump(self): |
| 1475 | self.assertEqual( |
| 1476 | Utils.formataddr(('A Silly; Person', 'person@dom.ain')), |
| 1477 | r'"A Silly; Person" <person@dom.ain>') |
| 1478 | |
| 1479 | def test_fix_eols(self): |
| 1480 | eq = self.assertEqual |
| 1481 | eq(Utils.fix_eols('hello'), 'hello') |
| 1482 | eq(Utils.fix_eols('hello\n'), 'hello\r\n') |
| 1483 | eq(Utils.fix_eols('hello\r'), 'hello\r\n') |
| 1484 | eq(Utils.fix_eols('hello\r\n'), 'hello\r\n') |
| 1485 | eq(Utils.fix_eols('hello\n\r'), 'hello\r\n\r\n') |
| 1486 | |
| 1487 | def test_charset_richcomparisons(self): |
| 1488 | eq = self.assertEqual |
| 1489 | ne = self.failIfEqual |
| 1490 | cset1 = Charset() |
| 1491 | cset2 = Charset() |
| 1492 | eq(cset1, 'us-ascii') |
| 1493 | eq(cset1, 'US-ASCII') |
| 1494 | eq(cset1, 'Us-AsCiI') |
| 1495 | eq('us-ascii', cset1) |
| 1496 | eq('US-ASCII', cset1) |
| 1497 | eq('Us-AsCiI', cset1) |
| 1498 | ne(cset1, 'usascii') |
| 1499 | ne(cset1, 'USASCII') |
| 1500 | ne(cset1, 'UsAsCiI') |
| 1501 | ne('usascii', cset1) |
| 1502 | ne('USASCII', cset1) |
| 1503 | ne('UsAsCiI', cset1) |
| 1504 | eq(cset1, cset2) |
| 1505 | eq(cset2, cset1) |
Barry Warsaw | eae36ac | 2001-12-20 16:37:27 +0000 | [diff] [blame] | 1506 | |
Barry Warsaw | 4be9ecc | 2002-05-22 01:52:10 +0000 | [diff] [blame] | 1507 | def test_getaddresses(self): |
| 1508 | eq = self.assertEqual |
| 1509 | eq(Utils.getaddresses(['aperson@dom.ain (Al Person)', |
| 1510 | 'Bud Person <bperson@dom.ain>']), |
| 1511 | [('Al Person', 'aperson@dom.ain'), |
| 1512 | ('Bud Person', 'bperson@dom.ain')]) |
| 1513 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1514 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 1515 | |
| 1516 | # Test the iterator/generators |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1517 | class TestIterators(TestEmailBase): |
| 1518 | def test_body_line_iterator(self): |
| 1519 | eq = self.assertEqual |
| 1520 | # First a simple non-multipart message |
| 1521 | msg = self._msgobj('msg_01.txt') |
| 1522 | it = Iterators.body_line_iterator(msg) |
Barry Warsaw | d1de6ea | 2001-10-04 18:18:37 +0000 | [diff] [blame] | 1523 | lines = list(it) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1524 | eq(len(lines), 6) |
| 1525 | eq(EMPTYSTRING.join(lines), msg.get_payload()) |
| 1526 | # Now a more complicated multipart |
| 1527 | msg = self._msgobj('msg_02.txt') |
| 1528 | it = Iterators.body_line_iterator(msg) |
Barry Warsaw | d1de6ea | 2001-10-04 18:18:37 +0000 | [diff] [blame] | 1529 | lines = list(it) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1530 | eq(len(lines), 43) |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 1531 | fp = openfile('msg_19.txt') |
| 1532 | try: |
| 1533 | eq(EMPTYSTRING.join(lines), fp.read()) |
| 1534 | finally: |
| 1535 | fp.close() |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1536 | |
| 1537 | def test_typed_subpart_iterator(self): |
| 1538 | eq = self.assertEqual |
| 1539 | msg = self._msgobj('msg_04.txt') |
| 1540 | it = Iterators.typed_subpart_iterator(msg, 'text') |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1541 | lines = [] |
| 1542 | subparts = 0 |
| 1543 | for subpart in it: |
| 1544 | subparts += 1 |
| 1545 | lines.append(subpart.get_payload()) |
| 1546 | eq(subparts, 2) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1547 | eq(EMPTYSTRING.join(lines), """\ |
| 1548 | a simple kind of mirror |
| 1549 | to reflect upon our own |
| 1550 | a simple kind of mirror |
| 1551 | to reflect upon our own |
| 1552 | """) |
| 1553 | |
Barry Warsaw | cdc632c | 2001-10-15 04:39:02 +0000 | [diff] [blame] | 1554 | def test_typed_subpart_iterator_default_type(self): |
| 1555 | eq = self.assertEqual |
| 1556 | msg = self._msgobj('msg_03.txt') |
| 1557 | it = Iterators.typed_subpart_iterator(msg, 'text', 'plain') |
| 1558 | lines = [] |
| 1559 | subparts = 0 |
| 1560 | for subpart in it: |
| 1561 | subparts += 1 |
| 1562 | lines.append(subpart.get_payload()) |
| 1563 | eq(subparts, 1) |
| 1564 | eq(EMPTYSTRING.join(lines), """\ |
| 1565 | |
| 1566 | Hi, |
| 1567 | |
| 1568 | Do you like this message? |
| 1569 | |
| 1570 | -Me |
| 1571 | """) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1572 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1573 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 1574 | |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 1575 | class TestParsers(TestEmailBase): |
Barry Warsaw | bf7a59d | 2001-10-11 15:44:50 +0000 | [diff] [blame] | 1576 | def test_header_parser(self): |
| 1577 | eq = self.assertEqual |
| 1578 | # Parse only the headers of a complex multipart MIME document |
Barry Warsaw | bf7a59d | 2001-10-11 15:44:50 +0000 | [diff] [blame] | 1579 | fp = openfile('msg_02.txt') |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 1580 | try: |
| 1581 | msg = HeaderParser().parse(fp) |
| 1582 | finally: |
| 1583 | fp.close() |
Barry Warsaw | bf7a59d | 2001-10-11 15:44:50 +0000 | [diff] [blame] | 1584 | eq(msg['from'], 'ppp-request@zzz.org') |
| 1585 | eq(msg['to'], 'ppp@zzz.org') |
| 1586 | eq(msg.get_type(), 'multipart/mixed') |
| 1587 | eq(msg.is_multipart(), 0) |
| 1588 | self.failUnless(isinstance(msg.get_payload(), StringType)) |
| 1589 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1590 | def test_whitespace_continuaton(self): |
| 1591 | eq = self.assertEqual |
| 1592 | # This message contains a line after the Subject: header that has only |
| 1593 | # whitespace, but it is not empty! |
| 1594 | msg = email.message_from_string("""\ |
| 1595 | From: aperson@dom.ain |
| 1596 | To: bperson@dom.ain |
| 1597 | Subject: the next line has a space on it |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 1598 | \x20 |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1599 | Date: Mon, 8 Apr 2002 15:09:19 -0400 |
| 1600 | Message-ID: spam |
| 1601 | |
| 1602 | Here's the message body |
| 1603 | """) |
| 1604 | eq(msg['subject'], 'the next line has a space on it\n ') |
| 1605 | eq(msg['message-id'], 'spam') |
| 1606 | eq(msg.get_payload(), "Here's the message body\n") |
| 1607 | |
Barry Warsaw | e0d85c8 | 2002-05-19 23:52:54 +0000 | [diff] [blame] | 1608 | def test_crlf_separation(self): |
| 1609 | eq = self.assertEqual |
| 1610 | fp = openfile('msg_26.txt') |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 1611 | try: |
| 1612 | msg = Parser().parse(fp) |
| 1613 | finally: |
| 1614 | fp.close() |
Barry Warsaw | e0d85c8 | 2002-05-19 23:52:54 +0000 | [diff] [blame] | 1615 | eq(len(msg.get_payload()), 2) |
| 1616 | part1 = msg.get_payload(0) |
| 1617 | eq(part1.get_type(), 'text/plain') |
| 1618 | eq(part1.get_payload(), 'Simple email with attachment.\r\n\r\n') |
| 1619 | part2 = msg.get_payload(1) |
| 1620 | eq(part2.get_type(), 'application/riscos') |
| 1621 | |
Barry Warsaw | 329d3af | 2002-07-09 02:38:24 +0000 | [diff] [blame] | 1622 | def test_multipart_digest_with_extra_mime_headers(self): |
| 1623 | eq = self.assertEqual |
| 1624 | neq = self.ndiffAssertEqual |
| 1625 | fp = openfile('msg_28.txt') |
| 1626 | try: |
| 1627 | msg = email.message_from_file(fp) |
| 1628 | finally: |
| 1629 | fp.close() |
| 1630 | # Structure is: |
| 1631 | # multipart/digest |
| 1632 | # message/rfc822 |
| 1633 | # text/plain |
| 1634 | # message/rfc822 |
| 1635 | # text/plain |
| 1636 | eq(msg.is_multipart(), 1) |
| 1637 | eq(len(msg.get_payload()), 2) |
| 1638 | part1 = msg.get_payload(0) |
| 1639 | eq(part1.get_type(), 'message/rfc822') |
| 1640 | eq(part1.is_multipart(), 1) |
| 1641 | eq(len(part1.get_payload()), 1) |
| 1642 | part1a = part1.get_payload(0) |
| 1643 | eq(part1a.is_multipart(), 0) |
| 1644 | eq(part1a.get_type(), 'text/plain') |
| 1645 | neq(part1a.get_payload(), 'message 1\n') |
| 1646 | # next message/rfc822 |
| 1647 | part2 = msg.get_payload(1) |
| 1648 | eq(part2.get_type(), 'message/rfc822') |
| 1649 | eq(part2.is_multipart(), 1) |
| 1650 | eq(len(part2.get_payload()), 1) |
| 1651 | part2a = part2.get_payload(0) |
| 1652 | eq(part2a.is_multipart(), 0) |
| 1653 | eq(part2a.get_type(), 'text/plain') |
| 1654 | neq(part2a.get_payload(), 'message 2\n') |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 1655 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1656 | |
| 1657 | |
| 1658 | class TestBase64(unittest.TestCase): |
| 1659 | def test_len(self): |
| 1660 | eq = self.assertEqual |
| 1661 | eq(base64MIME.base64_len('hello'), |
| 1662 | len(base64MIME.encode('hello', eol=''))) |
| 1663 | for size in range(15): |
| 1664 | if size == 0 : bsize = 0 |
| 1665 | elif size <= 3 : bsize = 4 |
| 1666 | elif size <= 6 : bsize = 8 |
| 1667 | elif size <= 9 : bsize = 12 |
| 1668 | elif size <= 12: bsize = 16 |
| 1669 | else : bsize = 20 |
| 1670 | eq(base64MIME.base64_len('x'*size), bsize) |
| 1671 | |
| 1672 | def test_decode(self): |
| 1673 | eq = self.assertEqual |
| 1674 | eq(base64MIME.decode(''), '') |
| 1675 | eq(base64MIME.decode('aGVsbG8='), 'hello') |
| 1676 | eq(base64MIME.decode('aGVsbG8=', 'X'), 'hello') |
| 1677 | eq(base64MIME.decode('aGVsbG8NCndvcmxk\n', 'X'), 'helloXworld') |
| 1678 | |
| 1679 | def test_encode(self): |
| 1680 | eq = self.assertEqual |
| 1681 | eq(base64MIME.encode(''), '') |
| 1682 | eq(base64MIME.encode('hello'), 'aGVsbG8=\n') |
| 1683 | # Test the binary flag |
| 1684 | eq(base64MIME.encode('hello\n'), 'aGVsbG8K\n') |
| 1685 | eq(base64MIME.encode('hello\n', 0), 'aGVsbG8NCg==\n') |
| 1686 | # Test the maxlinelen arg |
| 1687 | eq(base64MIME.encode('xxxx ' * 20, maxlinelen=40), """\ |
| 1688 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg |
| 1689 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg |
| 1690 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg |
| 1691 | eHh4eCB4eHh4IA== |
| 1692 | """) |
| 1693 | # Test the eol argument |
| 1694 | eq(base64MIME.encode('xxxx ' * 20, maxlinelen=40, eol='\r\n'), """\ |
| 1695 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r |
| 1696 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r |
| 1697 | eHh4eCB4eHh4IHh4eHggeHh4eCB4eHh4IHh4eHgg\r |
| 1698 | eHh4eCB4eHh4IA==\r |
| 1699 | """) |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 1700 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1701 | def test_header_encode(self): |
| 1702 | eq = self.assertEqual |
| 1703 | he = base64MIME.header_encode |
| 1704 | eq(he('hello'), '=?iso-8859-1?b?aGVsbG8=?=') |
| 1705 | eq(he('hello\nworld'), '=?iso-8859-1?b?aGVsbG8NCndvcmxk?=') |
| 1706 | # Test the charset option |
| 1707 | eq(he('hello', charset='iso-8859-2'), '=?iso-8859-2?b?aGVsbG8=?=') |
| 1708 | # Test the keep_eols flag |
| 1709 | eq(he('hello\nworld', keep_eols=1), |
| 1710 | '=?iso-8859-1?b?aGVsbG8Kd29ybGQ=?=') |
| 1711 | # Test the maxlinelen argument |
| 1712 | eq(he('xxxx ' * 20, maxlinelen=40), """\ |
| 1713 | =?iso-8859-1?b?eHh4eCB4eHh4IHh4eHggeHg=?= |
| 1714 | =?iso-8859-1?b?eHggeHh4eCB4eHh4IHh4eHg=?= |
| 1715 | =?iso-8859-1?b?IHh4eHggeHh4eCB4eHh4IHg=?= |
| 1716 | =?iso-8859-1?b?eHh4IHh4eHggeHh4eCB4eHg=?= |
| 1717 | =?iso-8859-1?b?eCB4eHh4IHh4eHggeHh4eCA=?= |
| 1718 | =?iso-8859-1?b?eHh4eCB4eHh4IHh4eHgg?=""") |
| 1719 | # Test the eol argument |
| 1720 | eq(he('xxxx ' * 20, maxlinelen=40, eol='\r\n'), """\ |
| 1721 | =?iso-8859-1?b?eHh4eCB4eHh4IHh4eHggeHg=?=\r |
| 1722 | =?iso-8859-1?b?eHggeHh4eCB4eHh4IHh4eHg=?=\r |
| 1723 | =?iso-8859-1?b?IHh4eHggeHh4eCB4eHh4IHg=?=\r |
| 1724 | =?iso-8859-1?b?eHh4IHh4eHggeHh4eCB4eHg=?=\r |
| 1725 | =?iso-8859-1?b?eCB4eHh4IHh4eHggeHh4eCA=?=\r |
| 1726 | =?iso-8859-1?b?eHh4eCB4eHh4IHh4eHgg?=""") |
| 1727 | |
| 1728 | |
| 1729 | |
| 1730 | class TestQuopri(unittest.TestCase): |
| 1731 | def setUp(self): |
| 1732 | self.hlit = [chr(x) for x in range(ord('a'), ord('z')+1)] + \ |
| 1733 | [chr(x) for x in range(ord('A'), ord('Z')+1)] + \ |
| 1734 | [chr(x) for x in range(ord('0'), ord('9')+1)] + \ |
| 1735 | ['!', '*', '+', '-', '/', ' '] |
| 1736 | self.hnon = [chr(x) for x in range(256) if chr(x) not in self.hlit] |
| 1737 | assert len(self.hlit) + len(self.hnon) == 256 |
| 1738 | self.blit = [chr(x) for x in range(ord(' '), ord('~')+1)] + ['\t'] |
| 1739 | self.blit.remove('=') |
| 1740 | self.bnon = [chr(x) for x in range(256) if chr(x) not in self.blit] |
| 1741 | assert len(self.blit) + len(self.bnon) == 256 |
| 1742 | |
| 1743 | def test_header_quopri_check(self): |
| 1744 | for c in self.hlit: |
| 1745 | self.failIf(quopriMIME.header_quopri_check(c)) |
| 1746 | for c in self.hnon: |
| 1747 | self.failUnless(quopriMIME.header_quopri_check(c)) |
| 1748 | |
| 1749 | def test_body_quopri_check(self): |
| 1750 | for c in self.blit: |
| 1751 | self.failIf(quopriMIME.body_quopri_check(c)) |
| 1752 | for c in self.bnon: |
| 1753 | self.failUnless(quopriMIME.body_quopri_check(c)) |
| 1754 | |
| 1755 | def test_header_quopri_len(self): |
| 1756 | eq = self.assertEqual |
| 1757 | hql = quopriMIME.header_quopri_len |
| 1758 | enc = quopriMIME.header_encode |
| 1759 | for s in ('hello', 'h@e@l@l@o@'): |
| 1760 | # Empty charset and no line-endings. 7 == RFC chrome |
| 1761 | eq(hql(s), len(enc(s, charset='', eol=''))-7) |
| 1762 | for c in self.hlit: |
| 1763 | eq(hql(c), 1) |
| 1764 | for c in self.hnon: |
| 1765 | eq(hql(c), 3) |
| 1766 | |
| 1767 | def test_body_quopri_len(self): |
| 1768 | eq = self.assertEqual |
| 1769 | bql = quopriMIME.body_quopri_len |
| 1770 | for c in self.blit: |
| 1771 | eq(bql(c), 1) |
| 1772 | for c in self.bnon: |
| 1773 | eq(bql(c), 3) |
| 1774 | |
| 1775 | def test_quote_unquote_idempotent(self): |
| 1776 | for x in range(256): |
| 1777 | c = chr(x) |
| 1778 | self.assertEqual(quopriMIME.unquote(quopriMIME.quote(c)), c) |
| 1779 | |
| 1780 | def test_header_encode(self): |
| 1781 | eq = self.assertEqual |
| 1782 | he = quopriMIME.header_encode |
| 1783 | eq(he('hello'), '=?iso-8859-1?q?hello?=') |
| 1784 | eq(he('hello\nworld'), '=?iso-8859-1?q?hello=0D=0Aworld?=') |
| 1785 | # Test the charset option |
| 1786 | eq(he('hello', charset='iso-8859-2'), '=?iso-8859-2?q?hello?=') |
| 1787 | # Test the keep_eols flag |
| 1788 | eq(he('hello\nworld', keep_eols=1), '=?iso-8859-1?q?hello=0Aworld?=') |
| 1789 | # Test a non-ASCII character |
Jack Jansen | 1476c27 | 2002-04-23 10:52:44 +0000 | [diff] [blame] | 1790 | eq(he('hello\xc7there'), '=?iso-8859-1?q?hello=C7there?=') |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1791 | # Test the maxlinelen argument |
| 1792 | eq(he('xxxx ' * 20, maxlinelen=40), """\ |
| 1793 | =?iso-8859-1?q?xxxx_xxxx_xxxx_xxxx_xx?= |
| 1794 | =?iso-8859-1?q?xx_xxxx_xxxx_xxxx_xxxx?= |
| 1795 | =?iso-8859-1?q?_xxxx_xxxx_xxxx_xxxx_x?= |
| 1796 | =?iso-8859-1?q?xxx_xxxx_xxxx_xxxx_xxx?= |
| 1797 | =?iso-8859-1?q?x_xxxx_xxxx_?=""") |
| 1798 | # Test the eol argument |
| 1799 | eq(he('xxxx ' * 20, maxlinelen=40, eol='\r\n'), """\ |
| 1800 | =?iso-8859-1?q?xxxx_xxxx_xxxx_xxxx_xx?=\r |
| 1801 | =?iso-8859-1?q?xx_xxxx_xxxx_xxxx_xxxx?=\r |
| 1802 | =?iso-8859-1?q?_xxxx_xxxx_xxxx_xxxx_x?=\r |
| 1803 | =?iso-8859-1?q?xxx_xxxx_xxxx_xxxx_xxx?=\r |
| 1804 | =?iso-8859-1?q?x_xxxx_xxxx_?=""") |
| 1805 | |
| 1806 | def test_decode(self): |
| 1807 | eq = self.assertEqual |
| 1808 | eq(quopriMIME.decode(''), '') |
| 1809 | eq(quopriMIME.decode('hello'), 'hello') |
| 1810 | eq(quopriMIME.decode('hello', 'X'), 'hello') |
| 1811 | eq(quopriMIME.decode('hello\nworld', 'X'), 'helloXworld') |
| 1812 | |
| 1813 | def test_encode(self): |
| 1814 | eq = self.assertEqual |
| 1815 | eq(quopriMIME.encode(''), '') |
| 1816 | eq(quopriMIME.encode('hello'), 'hello') |
| 1817 | # Test the binary flag |
| 1818 | eq(quopriMIME.encode('hello\r\nworld'), 'hello\nworld') |
| 1819 | eq(quopriMIME.encode('hello\r\nworld', 0), 'hello\nworld') |
| 1820 | # Test the maxlinelen arg |
| 1821 | eq(quopriMIME.encode('xxxx ' * 20, maxlinelen=40), """\ |
| 1822 | xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx= |
| 1823 | xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx= |
| 1824 | x xxxx xxxx xxxx xxxx=20""") |
| 1825 | # Test the eol argument |
| 1826 | eq(quopriMIME.encode('xxxx ' * 20, maxlinelen=40, eol='\r\n'), """\ |
| 1827 | xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx=\r |
| 1828 | xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxx=\r |
| 1829 | x xxxx xxxx xxxx xxxx=20""") |
| 1830 | eq(quopriMIME.encode("""\ |
| 1831 | one line |
| 1832 | |
| 1833 | two line"""), """\ |
| 1834 | one line |
| 1835 | |
| 1836 | two line""") |
Barry Warsaw | 16f9055 | 2002-04-16 05:06:42 +0000 | [diff] [blame] | 1837 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1838 | |
| 1839 | |
| 1840 | # Test the Charset class |
| 1841 | class TestCharset(unittest.TestCase): |
| 1842 | def test_idempotent(self): |
| 1843 | eq = self.assertEqual |
| 1844 | # Make sure us-ascii = no Unicode conversion |
| 1845 | c = Charset('us-ascii') |
| 1846 | s = 'Hello World!' |
| 1847 | sp = c.to_splittable(s) |
| 1848 | eq(s, c.from_splittable(sp)) |
| 1849 | # test 8-bit idempotency with us-ascii |
| 1850 | s = '\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa' |
| 1851 | sp = c.to_splittable(s) |
| 1852 | eq(s, c.from_splittable(sp)) |
| 1853 | |
| 1854 | |
| 1855 | |
| 1856 | # Test multilingual MIME headers. |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 1857 | class TestHeader(TestEmailBase): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1858 | def test_simple(self): |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 1859 | eq = self.ndiffAssertEqual |
| 1860 | h = Header('Hello World!') |
| 1861 | eq(h.encode(), 'Hello World!') |
| 1862 | h.append(' Goodbye World!') |
| 1863 | eq(h.encode(), 'Hello World! Goodbye World!') |
| 1864 | |
| 1865 | def test_simple_surprise(self): |
| 1866 | eq = self.ndiffAssertEqual |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1867 | h = Header('Hello World!') |
| 1868 | eq(h.encode(), 'Hello World!') |
| 1869 | h.append('Goodbye World!') |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 1870 | eq(h.encode(), 'Hello World!Goodbye World!') |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1871 | |
| 1872 | def test_header_needs_no_decoding(self): |
| 1873 | h = 'no decoding needed' |
| 1874 | self.assertEqual(decode_header(h), [(h, None)]) |
| 1875 | |
| 1876 | def test_long(self): |
| 1877 | 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.", |
| 1878 | maxlinelen=76) |
| 1879 | for l in h.encode().split('\n '): |
| 1880 | self.failUnless(len(l) <= 76) |
| 1881 | |
| 1882 | def test_multilingual(self): |
Barry Warsaw | c53b29e | 2002-07-09 16:36:36 +0000 | [diff] [blame] | 1883 | eq = self.ndiffAssertEqual |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1884 | g = Charset("iso-8859-1") |
| 1885 | cz = Charset("iso-8859-2") |
| 1886 | utf8 = Charset("utf-8") |
| 1887 | g_head = "Die Mieter treten hier ein werden mit einem Foerderband komfortabel den Korridor entlang, an s\xfcdl\xfcndischen Wandgem\xe4lden vorbei, gegen die rotierenden Klingen bef\xf6rdert. " |
| 1888 | cz_head = "Finan\xe8ni metropole se hroutily pod tlakem jejich d\xf9vtipu.. " |
| 1889 | utf8_head = u"\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das Nunstuck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput.\u300d\u3068\u8a00\u3063\u3066\u3044\u307e\u3059\u3002".encode("utf-8") |
| 1890 | h = Header(g_head, g) |
| 1891 | h.append(cz_head, cz) |
| 1892 | h.append(utf8_head, utf8) |
| 1893 | enc = h.encode() |
| 1894 | eq(enc, """=?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_eine?= |
| 1895 | =?iso-8859-1?q?m_Foerderband_komfortabel_den_Korridor_ent?= |
| 1896 | =?iso-8859-1?q?lang=2C_an_s=FCdl=FCndischen_Wandgem=E4lden_vorbei?= |
| 1897 | =?iso-8859-1?q?=2C_gegen_die_rotierenden_Klingen_bef=F6rdert=2E_?= |
| 1898 | =?iso-8859-2?q?Finan=E8ni_metropole_se_hroutil?= |
| 1899 | =?iso-8859-2?q?y_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= |
| 1900 | =?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv?= |
| 1901 | =?utf-8?b?44GV44KM44Gm44GE44G+44Gb44KT44CC5LiA?= |
| 1902 | =?utf-8?b?6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM?= |
| 1903 | =?utf-8?b?44CB44GC44Go44Gv44Gn44Gf44KJ44KB44Gn?= |
| 1904 | =?utf-8?b?44GZ44CC5a6f6Zqb44Gr44Gv44CMV2VubiBpc3QgZGE=?= |
| 1905 | =?utf-8?b?cyBOdW5zdHVjayBnaXQgdW5k?= |
| 1906 | =?utf-8?b?IFNsb3Rlcm1leWVyPyBKYSEgQmVpaGVyaHVuZCBkYXMgT2Rl?= |
| 1907 | =?utf-8?b?ciBkaWUgRmxpcHBlcndhbGR0?= |
| 1908 | =?utf-8?b?IGdlcnNwdXQu44CN44Go6KiA44Gj44Gm44GE44G+44GZ44CC?=""") |
| 1909 | eq(decode_header(enc), |
| 1910 | [(g_head, "iso-8859-1"), (cz_head, "iso-8859-2"), |
| 1911 | (utf8_head, "utf-8")]) |
Barry Warsaw | 3fdc889 | 2002-06-29 03:27:27 +0000 | [diff] [blame] | 1912 | # Test for conversion to unicode. BAW: Python 2.1 doesn't support the |
| 1913 | # __unicode__() protocol, so do things this way for compatibility. |
| 1914 | ustr = h.__unicode__() |
| 1915 | # For Python 2.2 and beyond |
| 1916 | #ustr = unicode(h) |
| 1917 | eq(ustr.encode('utf-8'), |
| 1918 | 'Die Mieter treten hier ein werden mit einem Foerderband ' |
| 1919 | 'komfortabel den Korridor entlang, an s\xc3\xbcdl\xc3\xbcndischen ' |
| 1920 | 'Wandgem\xc3\xa4lden vorbei, gegen die rotierenden Klingen ' |
| 1921 | 'bef\xc3\xb6rdert. Finan\xc4\x8dni metropole se hroutily pod ' |
| 1922 | 'tlakem jejich d\xc5\xafvtipu.. \xe6\xad\xa3\xe7\xa2\xba\xe3\x81' |
| 1923 | '\xab\xe8\xa8\x80\xe3\x81\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3' |
| 1924 | '\xe3\x81\xaf\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3' |
| 1925 | '\x81\xbe\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83' |
| 1926 | '\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8\xaa\x9e' |
| 1927 | '\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81\xe3\x81\x82\xe3' |
| 1928 | '\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81\x9f\xe3\x82\x89\xe3\x82' |
| 1929 | '\x81\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82\xe5\xae\x9f\xe9\x9a\x9b' |
| 1930 | '\xe3\x81\xab\xe3\x81\xaf\xe3\x80\x8cWenn ist das Nunstuck git ' |
| 1931 | 'und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt ' |
| 1932 | 'gersput.\xe3\x80\x8d\xe3\x81\xa8\xe8\xa8\x80\xe3\x81\xa3\xe3\x81' |
| 1933 | '\xa6\xe3\x81\x84\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82') |
Barry Warsaw | c53b29e | 2002-07-09 16:36:36 +0000 | [diff] [blame] | 1934 | # Test make_header() |
| 1935 | newh = make_header(decode_header(enc)) |
| 1936 | eq(newh, enc) |
| 1937 | |
| 1938 | def test_header_ctor_default_args(self): |
| 1939 | eq = self.ndiffAssertEqual |
| 1940 | h = Header() |
| 1941 | eq(h, '') |
| 1942 | h.append('foo', Charset('iso-8859-1')) |
| 1943 | eq(h, '=?iso-8859-1?q?foo?=') |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1944 | |
Barry Warsaw | e0d85c8 | 2002-05-19 23:52:54 +0000 | [diff] [blame] | 1945 | def test_explicit_maxlinelen(self): |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 1946 | eq = self.ndiffAssertEqual |
Barry Warsaw | e0d85c8 | 2002-05-19 23:52:54 +0000 | [diff] [blame] | 1947 | hstr = 'A very long line that must get split to something other than at the 76th character boundary to test the non-default behavior' |
| 1948 | h = Header(hstr) |
| 1949 | eq(h.encode(), '''\ |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 1950 | A very long line that must get split to something other than at the 76th |
| 1951 | character boundary to test the non-default behavior''') |
Barry Warsaw | e0d85c8 | 2002-05-19 23:52:54 +0000 | [diff] [blame] | 1952 | h = Header(hstr, header_name='Subject') |
| 1953 | eq(h.encode(), '''\ |
| 1954 | A very long line that must get split to something other than at the |
Barry Warsaw | b6a9213 | 2002-06-28 23:49:33 +0000 | [diff] [blame] | 1955 | 76th character boundary to test the non-default behavior''') |
Barry Warsaw | e0d85c8 | 2002-05-19 23:52:54 +0000 | [diff] [blame] | 1956 | h = Header(hstr, maxlinelen=1024, header_name='Subject') |
| 1957 | eq(h.encode(), hstr) |
| 1958 | |
Barry Warsaw | bf7a59d | 2001-10-11 15:44:50 +0000 | [diff] [blame] | 1959 | |
Barry Warsaw | 9546e79 | 2002-06-29 05:58:45 +0000 | [diff] [blame] | 1960 | # Test RFC 2231 header parameters decoding |
| 1961 | class TestRFC2231(TestEmailBase): |
| 1962 | def test_get_param(self): |
| 1963 | eq = self.assertEqual |
| 1964 | msg = self._msgobj('msg_29.txt') |
| 1965 | eq(msg.get_param('title'), |
| 1966 | ('us-ascii', 'en', 'This is even more ***fun*** isn\'t it!')) |
| 1967 | eq(msg.get_param('title', unquote=0), |
| 1968 | ('us-ascii', 'en', '"This is even more ***fun*** isn\'t it!"')) |
| 1969 | |
| 1970 | |
Barry Warsaw | bf7a59d | 2001-10-11 15:44:50 +0000 | [diff] [blame] | 1971 | |
Barry Warsaw | c9ad32c | 2002-04-15 22:14:06 +0000 | [diff] [blame] | 1972 | def _testclasses(): |
| 1973 | mod = sys.modules[__name__] |
| 1974 | return [getattr(mod, name) for name in dir(mod) if name.startswith('Test')] |
| 1975 | |
| 1976 | |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1977 | def suite(): |
| 1978 | suite = unittest.TestSuite() |
Barry Warsaw | c9ad32c | 2002-04-15 22:14:06 +0000 | [diff] [blame] | 1979 | for testclass in _testclasses(): |
| 1980 | suite.addTest(unittest.makeSuite(testclass)) |
Barry Warsaw | 4107585 | 2001-09-23 03:18:13 +0000 | [diff] [blame] | 1981 | return suite |
| 1982 | |
| 1983 | |
Barry Warsaw | c9ad32c | 2002-04-15 22:14:06 +0000 | [diff] [blame] | 1984 | def test_main(): |
| 1985 | for testclass in _testclasses(): |
| 1986 | test_support.run_unittest(testclass) |
| 1987 | |
| 1988 | |
Barry Warsaw | 08a534d | 2001-10-04 17:58:50 +0000 | [diff] [blame] | 1989 | |
Guido van Rossum | 78f0dd3 | 2001-12-07 21:07:08 +0000 | [diff] [blame] | 1990 | if __name__ == '__main__': |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1991 | unittest.main(defaultTest='suite') |