| 1 |
import datetime |
| 2 |
import textwrap |
| 3 |
import unittest |
| 4 |
from email import errors |
| 5 |
from email import policy |
| 6 |
from email.message import Message |
| 7 |
from test.test_email import TestEmailBase, parameterize |
| 8 |
from email import headerregistry |
| 9 |
from email.headerregistry import Address, Group |
| 10 |
from test.support import ALWAYS_EQ |
| 11 |
|
| 12 |
|
| 13 |
DITTO = object() |
| 14 |
|
| 15 |
|
| 16 |
class TestHeaderRegistry(TestEmailBase): |
| 17 |
|
| 18 |
def test_arbitrary_name_unstructured(self): |
| 19 |
factory = headerregistry.HeaderRegistry() |
| 20 |
h = factory('foobar', 'test') |
| 21 |
self.assertIsInstance(h, headerregistry.BaseHeader) |
| 22 |
self.assertIsInstance(h, headerregistry.UnstructuredHeader) |
| 23 |
|
| 24 |
def test_name_case_ignored(self): |
| 25 |
factory = headerregistry.HeaderRegistry() |
| 26 |
# Whitebox check that test is valid |
| 27 |
self.assertNotIn('Subject', factory.registry) |
| 28 |
h = factory('Subject', 'test') |
| 29 |
self.assertIsInstance(h, headerregistry.BaseHeader) |
| 30 |
self.assertIsInstance(h, headerregistry.UniqueUnstructuredHeader) |
| 31 |
|
| 32 |
class FooBase: |
| 33 |
def __init__(self, *args, **kw): |
| 34 |
pass |
| 35 |
|
| 36 |
def test_override_default_base_class(self): |
| 37 |
factory = headerregistry.HeaderRegistry(base_class=self.FooBase) |
| 38 |
h = factory('foobar', 'test') |
| 39 |
self.assertIsInstance(h, self.FooBase) |
| 40 |
self.assertIsInstance(h, headerregistry.UnstructuredHeader) |
| 41 |
|
| 42 |
class FooDefault: |
| 43 |
parse = headerregistry.UnstructuredHeader.parse |
| 44 |
|
| 45 |
def test_override_default_class(self): |
| 46 |
factory = headerregistry.HeaderRegistry(default_class=self.FooDefault) |
| 47 |
h = factory('foobar', 'test') |
| 48 |
self.assertIsInstance(h, headerregistry.BaseHeader) |
| 49 |
self.assertIsInstance(h, self.FooDefault) |
| 50 |
|
| 51 |
def test_override_default_class_only_overrides_default(self): |
| 52 |
factory = headerregistry.HeaderRegistry(default_class=self.FooDefault) |
| 53 |
h = factory('subject', 'test') |
| 54 |
self.assertIsInstance(h, headerregistry.BaseHeader) |
| 55 |
self.assertIsInstance(h, headerregistry.UniqueUnstructuredHeader) |
| 56 |
|
| 57 |
def test_dont_use_default_map(self): |
| 58 |
factory = headerregistry.HeaderRegistry(use_default_map=False) |
| 59 |
h = factory('subject', 'test') |
| 60 |
self.assertIsInstance(h, headerregistry.BaseHeader) |
| 61 |
self.assertIsInstance(h, headerregistry.UnstructuredHeader) |
| 62 |
|
| 63 |
def test_map_to_type(self): |
| 64 |
factory = headerregistry.HeaderRegistry() |
| 65 |
h1 = factory('foobar', 'test') |
| 66 |
factory.map_to_type('foobar', headerregistry.UniqueUnstructuredHeader) |
| 67 |
h2 = factory('foobar', 'test') |
| 68 |
self.assertIsInstance(h1, headerregistry.BaseHeader) |
| 69 |
self.assertIsInstance(h1, headerregistry.UnstructuredHeader) |
| 70 |
self.assertIsInstance(h2, headerregistry.BaseHeader) |
| 71 |
self.assertIsInstance(h2, headerregistry.UniqueUnstructuredHeader) |
| 72 |
|
| 73 |
|
| 74 |
class TestHeaderBase(TestEmailBase): |
| 75 |
|
| 76 |
factory = headerregistry.HeaderRegistry() |
| 77 |
|
| 78 |
def make_header(self, name, value): |
| 79 |
return self.factory(name, value) |
| 80 |
|
| 81 |
|
| 82 |
class TestBaseHeaderFeatures(TestHeaderBase): |
| 83 |
|
| 84 |
def test_str(self): |
| 85 |
h = self.make_header('subject', 'this is a test') |
| 86 |
self.assertIsInstance(h, str) |
| 87 |
self.assertEqual(h, 'this is a test') |
| 88 |
self.assertEqual(str(h), 'this is a test') |
| 89 |
|
| 90 |
def test_substr(self): |
| 91 |
h = self.make_header('subject', 'this is a test') |
| 92 |
self.assertEqual(h[5:7], 'is') |
| 93 |
|
| 94 |
def test_has_name(self): |
| 95 |
h = self.make_header('subject', 'this is a test') |
| 96 |
self.assertEqual(h.name, 'subject') |
| 97 |
|
| 98 |
def _test_attr_ro(self, attr): |
| 99 |
h = self.make_header('subject', 'this is a test') |
| 100 |
with self.assertRaises(AttributeError): |
| 101 |
setattr(h, attr, 'foo') |
| 102 |
|
| 103 |
def test_name_read_only(self): |
| 104 |
self._test_attr_ro('name') |
| 105 |
|
| 106 |
def test_defects_read_only(self): |
| 107 |
self._test_attr_ro('defects') |
| 108 |
|
| 109 |
def test_defects_is_tuple(self): |
| 110 |
h = self.make_header('subject', 'this is a test') |
| 111 |
self.assertEqual(len(h.defects), 0) |
| 112 |
self.assertIsInstance(h.defects, tuple) |
| 113 |
# Make sure it is still true when there are defects. |
| 114 |
h = self.make_header('date', '') |
| 115 |
self.assertEqual(len(h.defects), 1) |
| 116 |
self.assertIsInstance(h.defects, tuple) |
| 117 |
|
| 118 |
# XXX: FIXME |
| 119 |
#def test_CR_in_value(self): |
| 120 |
# # XXX: this also re-raises the issue of embedded headers, |
| 121 |
# # need test and solution for that. |
| 122 |
# value = '\r'.join(['this is', ' a test']) |
| 123 |
# h = self.make_header('subject', value) |
| 124 |
# self.assertEqual(h, value) |
| 125 |
# self.assertDefectsEqual(h.defects, [errors.ObsoleteHeaderDefect]) |
| 126 |
|
| 127 |
|
| 128 |
@parameterize |
| 129 |
class TestUnstructuredHeader(TestHeaderBase): |
| 130 |
|
| 131 |
def string_as_value(self, |
| 132 |
source, |
| 133 |
decoded, |
| 134 |
*args): |
| 135 |
l = len(args) |
| 136 |
defects = args[0] if l>0 else [] |
| 137 |
header = 'Subject:' + (' ' if source else '') |
| 138 |
folded = header + (args[1] if l>1 else source) + '\n' |
| 139 |
h = self.make_header('Subject', source) |
| 140 |
self.assertEqual(h, decoded) |
| 141 |
self.assertDefectsEqual(h.defects, defects) |
| 142 |
self.assertEqual(h.fold(policy=policy.default), folded) |
| 143 |
|
| 144 |
string_params = { |
| 145 |
|
| 146 |
'rfc2047_simple_quopri': ( |
| 147 |
'=?utf-8?q?this_is_a_test?=', |
| 148 |
'this is a test', |
| 149 |
[], |
| 150 |
'this is a test'), |
| 151 |
|
| 152 |
'rfc2047_gb2312_base64': ( |
| 153 |
'=?gb2312?b?1eLKx9bQzsSy4srUo6E=?=', |
| 154 |
'\u8fd9\u662f\u4e2d\u6587\u6d4b\u8bd5\uff01', |
| 155 |
[], |
| 156 |
'=?utf-8?b?6L+Z5piv5Lit5paH5rWL6K+V77yB?='), |
| 157 |
|
| 158 |
'rfc2047_simple_nonascii_quopri': ( |
| 159 |
'=?utf-8?q?=C3=89ric?=', |
| 160 |
'Éric'), |
| 161 |
|
| 162 |
'rfc2047_quopri_with_regular_text': ( |
| 163 |
'The =?utf-8?q?=C3=89ric=2C?= Himself', |
| 164 |
'The Éric, Himself'), |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
@parameterize |
| 170 |
class TestDateHeader(TestHeaderBase): |
| 171 |
|
| 172 |
datestring = 'Sun, 23 Sep 2001 20:10:55 -0700' |
| 173 |
utcoffset = datetime.timedelta(hours=-7) |
| 174 |
tz = datetime.timezone(utcoffset) |
| 175 |
dt = datetime.datetime(2001, 9, 23, 20, 10, 55, tzinfo=tz) |
| 176 |
|
| 177 |
def test_parse_date(self): |
| 178 |
h = self.make_header('date', self.datestring) |
| 179 |
self.assertEqual(h, self.datestring) |
| 180 |
self.assertEqual(h.datetime, self.dt) |
| 181 |
self.assertEqual(h.datetime.utcoffset(), self.utcoffset) |
| 182 |
self.assertEqual(h.defects, ()) |
| 183 |
|
| 184 |
def test_set_from_datetime(self): |
| 185 |
h = self.make_header('date', self.dt) |
| 186 |
self.assertEqual(h, self.datestring) |
| 187 |
self.assertEqual(h.datetime, self.dt) |
| 188 |
self.assertEqual(h.defects, ()) |
| 189 |
|
| 190 |
def test_date_header_properties(self): |
| 191 |
h = self.make_header('date', self.datestring) |
| 192 |
self.assertIsInstance(h, headerregistry.UniqueDateHeader) |
| 193 |
self.assertEqual(h.max_count, 1) |
| 194 |
self.assertEqual(h.defects, ()) |
| 195 |
|
| 196 |
def test_resent_date_header_properties(self): |
| 197 |
h = self.make_header('resent-date', self.datestring) |
| 198 |
self.assertIsInstance(h, headerregistry.DateHeader) |
| 199 |
self.assertEqual(h.max_count, None) |
| 200 |
self.assertEqual(h.defects, ()) |
| 201 |
|
| 202 |
def test_no_value_is_defect(self): |
| 203 |
h = self.make_header('date', '') |
| 204 |
self.assertEqual(len(h.defects), 1) |
| 205 |
self.assertIsInstance(h.defects[0], errors.HeaderMissingRequiredValue) |
| 206 |
|
| 207 |
def test_invalid_date_format(self): |
| 208 |
s = 'Not a date header' |
| 209 |
h = self.make_header('date', s) |
| 210 |
self.assertEqual(h, s) |
| 211 |
self.assertIsNone(h.datetime) |
| 212 |
self.assertEqual(len(h.defects), 1) |
| 213 |
self.assertIsInstance(h.defects[0], errors.InvalidDateDefect) |
| 214 |
|
| 215 |
def test_invalid_date_value(self): |
| 216 |
s = 'Tue, 06 Jun 2017 27:39:33 +0600' |
| 217 |
h = self.make_header('date', s) |
| 218 |
self.assertEqual(h, s) |
| 219 |
self.assertIsNone(h.datetime) |
| 220 |
self.assertEqual(len(h.defects), 1) |
| 221 |
self.assertIsInstance(h.defects[0], errors.InvalidDateDefect) |
| 222 |
|
| 223 |
def test_datetime_read_only(self): |
| 224 |
h = self.make_header('date', self.datestring) |
| 225 |
with self.assertRaises(AttributeError): |
| 226 |
h.datetime = 'foo' |
| 227 |
|
| 228 |
def test_set_date_header_from_datetime(self): |
| 229 |
m = Message(policy=policy.default) |
| 230 |
m['Date'] = self.dt |
| 231 |
self.assertEqual(m['Date'], self.datestring) |
| 232 |
self.assertEqual(m['Date'].datetime, self.dt) |
| 233 |
|
| 234 |
|
| 235 |
@parameterize |
| 236 |
class TestContentTypeHeader(TestHeaderBase): |
| 237 |
|
| 238 |
def content_type_as_value(self, |
| 239 |
source, |
| 240 |
content_type, |
| 241 |
maintype, |
| 242 |
subtype, |
| 243 |
*args): |
| 244 |
l = len(args) |
| 245 |
parmdict = args[0] if l>0 else {} |
| 246 |
defects = args[1] if l>1 else [] |
| 247 |
decoded = args[2] if l>2 and args[2] is not DITTO else source |
| 248 |
header = 'Content-Type:' + ' ' if source else '' |
| 249 |
folded = args[3] if l>3 else header + decoded + '\n' |
| 250 |
h = self.make_header('Content-Type', source) |
| 251 |
self.assertEqual(h.content_type, content_type) |
| 252 |
self.assertEqual(h.maintype, maintype) |
| 253 |
self.assertEqual(h.subtype, subtype) |
| 254 |
self.assertEqual(h.params, parmdict) |
| 255 |
with self.assertRaises(TypeError): |
| 256 |
h.params['abc'] = 'xyz' # make sure params is read-only. |
| 257 |
self.assertDefectsEqual(h.defects, defects) |
| 258 |
self.assertEqual(h, decoded) |
| 259 |
self.assertEqual(h.fold(policy=policy.default), folded) |
| 260 |
|
| 261 |
content_type_params = { |
| 262 |
|
| 263 |
# Examples from RFC 2045. |
| 264 |
|
| 265 |
'RFC_2045_1': ( |
| 266 |
'text/plain; charset=us-ascii (Plain text)', |
| 267 |
'text/plain', |
| 268 |
'text', |
| 269 |
'plain', |
| 270 |
{'charset': 'us-ascii'}, |
| 271 |
[], |
| 272 |
'text/plain; charset="us-ascii"'), |
| 273 |
|
| 274 |
'RFC_2045_2': ( |
| 275 |
'text/plain; charset=us-ascii', |
| 276 |
'text/plain', |
| 277 |
'text', |
| 278 |
'plain', |
| 279 |
{'charset': 'us-ascii'}, |
| 280 |
[], |
| 281 |
'text/plain; charset="us-ascii"'), |
| 282 |
|
| 283 |
'RFC_2045_3': ( |
| 284 |
'text/plain; charset="us-ascii"', |
| 285 |
'text/plain', |
| 286 |
'text', |
| 287 |
'plain', |
| 288 |
{'charset': 'us-ascii'}), |
| 289 |
|
| 290 |
# RFC 2045 5.2 says syntactically invalid values are to be treated as |
| 291 |
# text/plain. |
| 292 |
|
| 293 |
'no_subtype_in_content_type': ( |
| 294 |
'text/', |
| 295 |
'text/plain', |
| 296 |
'text', |
| 297 |
'plain', |
| 298 |
{}, |
| 299 |
[errors.InvalidHeaderDefect]), |
| 300 |
|
| 301 |
'no_slash_in_content_type': ( |
| 302 |
'foo', |
| 303 |
'text/plain', |
| 304 |
'text', |
| 305 |
'plain', |
| 306 |
{}, |
| 307 |
[errors.InvalidHeaderDefect]), |
| 308 |
|
| 309 |
'junk_text_in_content_type': ( |
| 310 |
'<crazy "stuff">', |
| 311 |
'text/plain', |
| 312 |
'text', |
| 313 |
'plain', |
| 314 |
{}, |
| 315 |
[errors.InvalidHeaderDefect]), |
| 316 |
|
| 317 |
'too_many_slashes_in_content_type': ( |
| 318 |
'image/jpeg/foo', |
| 319 |
'text/plain', |
| 320 |
'text', |
| 321 |
'plain', |
| 322 |
{}, |
| 323 |
[errors.InvalidHeaderDefect]), |
| 324 |
|
| 325 |
# But unknown names are OK. We could make non-IANA names a defect, but |
| 326 |
# by not doing so we make ourselves future proof. The fact that they |
| 327 |
# are unknown will be detectable by the fact that they don't appear in |
| 328 |
# the mime_registry...and the application is free to extend that list |
| 329 |
# to handle them even if the core library doesn't. |
| 330 |
|
| 331 |
'unknown_content_type': ( |
| 332 |
'bad/names', |
| 333 |
'bad/names', |
| 334 |
'bad', |
| 335 |
'names'), |
| 336 |
|
| 337 |
# The content type is case insensitive, and CFWS is ignored. |
| 338 |
|
| 339 |
'mixed_case_content_type': ( |
| 340 |
'ImAge/JPeg', |
| 341 |
'image/jpeg', |
| 342 |
'image', |
| 343 |
'jpeg'), |
| 344 |
|
| 345 |
'spaces_in_content_type': ( |
| 346 |
' text / plain ', |
| 347 |
'text/plain', |
| 348 |
'text', |
| 349 |
'plain'), |
| 350 |
|
| 351 |
'cfws_in_content_type': ( |
| 352 |
'(foo) text (bar)/(baz)plain(stuff)', |
| 353 |
'text/plain', |
| 354 |
'text', |
| 355 |
'plain'), |
| 356 |
|
| 357 |
# test some parameters (more tests could be added for parameters |
| 358 |
# associated with other content types, but since parameter parsing is |
| 359 |
# generic they would be redundant for the current implementation). |
| 360 |
|
| 361 |
'charset_param': ( |
| 362 |
'text/plain; charset="utf-8"', |
| 363 |
'text/plain', |
| 364 |
'text', |
| 365 |
'plain', |
| 366 |
{'charset': 'utf-8'}), |
| 367 |
|
| 368 |
'capitalized_charset': ( |
| 369 |
'text/plain; charset="US-ASCII"', |
| 370 |
'text/plain', |
| 371 |
'text', |
| 372 |
'plain', |
| 373 |
{'charset': 'US-ASCII'}), |
| 374 |
|
| 375 |
'unknown_charset': ( |
| 376 |
'text/plain; charset="fOo"', |
| 377 |
'text/plain', |
| 378 |
'text', |
| 379 |
'plain', |
| 380 |
{'charset': 'fOo'}), |
| 381 |
|
| 382 |
'capitalized_charset_param_name_and_comment': ( |
| 383 |
'text/plain; (interjection) Charset="utf-8"', |
| 384 |
'text/plain', |
| 385 |
'text', |
| 386 |
'plain', |
| 387 |
{'charset': 'utf-8'}, |
| 388 |
[], |
| 389 |
# Should the parameter name be lowercased here? |
| 390 |
'text/plain; Charset="utf-8"'), |
| 391 |
|
| 392 |
# Since this is pretty much the ur-mimeheader, we'll put all the tests |
| 393 |
# that exercise the parameter parsing and formatting here. Note that |
| 394 |
# when we refold we may canonicalize, so things like whitespace, |
| 395 |
# quoting, and rfc2231 encoding may change from what was in the input |
| 396 |
# header. |
| 397 |
|
| 398 |
'unquoted_param_value': ( |
| 399 |
'text/plain; title=foo', |
| 400 |
'text/plain', |
| 401 |
'text', |
| 402 |
'plain', |
| 403 |
{'title': 'foo'}, |
| 404 |
[], |
| 405 |
'text/plain; title="foo"', |
| 406 |
), |
| 407 |
|
| 408 |
'param_value_with_tspecials': ( |
| 409 |
'text/plain; title="(bar)foo blue"', |
| 410 |
'text/plain', |
| 411 |
'text', |
| 412 |
'plain', |
| 413 |
{'title': '(bar)foo blue'}), |
| 414 |
|
| 415 |
'param_with_extra_quoted_whitespace': ( |
| 416 |
'text/plain; title=" a loong way \t home "', |
| 417 |
'text/plain', |
| 418 |
'text', |
| 419 |
'plain', |
| 420 |
{'title': ' a loong way \t home '}), |
| 421 |
|
| 422 |
'bad_params': ( |
| 423 |
'blarg; baz; boo', |
| 424 |
'text/plain', |
| 425 |
'text', |
| 426 |
'plain', |
| 427 |
{'baz': '', 'boo': ''}, |
| 428 |
[errors.InvalidHeaderDefect]*3), |
| 429 |
|
| 430 |
'spaces_around_param_equals': ( |
| 431 |
'Multipart/mixed; boundary = "CPIMSSMTPC06p5f3tG"', |
| 432 |
'multipart/mixed', |
| 433 |
'multipart', |
| 434 |
'mixed', |
| 435 |
{'boundary': 'CPIMSSMTPC06p5f3tG'}, |
| 436 |
[], |
| 437 |
'Multipart/mixed; boundary="CPIMSSMTPC06p5f3tG"', |
| 438 |
), |
| 439 |
|
| 440 |
'spaces_around_semis': ( |
| 441 |
('image/jpeg; name="wibble.JPG" ; x-mac-type="4A504547" ; ' |
| 442 |
'x-mac-creator="474B4F4E"'), |
| 443 |
'image/jpeg', |
| 444 |
'image', |
| 445 |
'jpeg', |
| 446 |
{'name': 'wibble.JPG', |
| 447 |
'x-mac-type': '4A504547', |
| 448 |
'x-mac-creator': '474B4F4E'}, |
| 449 |
[], |
| 450 |
('image/jpeg; name="wibble.JPG"; x-mac-type="4A504547"; ' |
| 451 |
'x-mac-creator="474B4F4E"'), |
| 452 |
('Content-Type: image/jpeg; name="wibble.JPG";' |
| 453 |
' x-mac-type="4A504547";\n' |
| 454 |
' x-mac-creator="474B4F4E"\n'), |
| 455 |
), |
| 456 |
|
| 457 |
'lots_of_mime_params': ( |
| 458 |
('image/jpeg; name="wibble.JPG"; x-mac-type="4A504547"; ' |
| 459 |
'x-mac-creator="474B4F4E"; x-extrastuff="make it longer"'), |
| 460 |
'image/jpeg', |
| 461 |
'image', |
| 462 |
'jpeg', |
| 463 |
{'name': 'wibble.JPG', |
| 464 |
'x-mac-type': '4A504547', |
| 465 |
'x-mac-creator': '474B4F4E', |
| 466 |
'x-extrastuff': 'make it longer'}, |
| 467 |
[], |
| 468 |
('image/jpeg; name="wibble.JPG"; x-mac-type="4A504547"; ' |
| 469 |
'x-mac-creator="474B4F4E"; x-extrastuff="make it longer"'), |
| 470 |
# In this case the whole of the MimeParameters does *not* fit |
| 471 |
# one one line, so we break at a lower syntactic level. |
| 472 |
('Content-Type: image/jpeg; name="wibble.JPG";' |
| 473 |
' x-mac-type="4A504547";\n' |
| 474 |
' x-mac-creator="474B4F4E"; x-extrastuff="make it longer"\n'), |
| 475 |
), |
| 476 |
|
| 477 |
'semis_inside_quotes': ( |
| 478 |
'image/jpeg; name="Jim&&Jill"', |
| 479 |
'image/jpeg', |
| 480 |
'image', |
| 481 |
'jpeg', |
| 482 |
{'name': 'Jim&&Jill'}), |
| 483 |
|
| 484 |
'single_quotes_inside_quotes': ( |
| 485 |
'image/jpeg; name="Jim \'Bob\' Jill"', |
| 486 |
'image/jpeg', |
| 487 |
'image', |
| 488 |
'jpeg', |
| 489 |
{'name': "Jim 'Bob' Jill"}), |
| 490 |
|
| 491 |
'double_quotes_inside_quotes': ( |
| 492 |
r'image/jpeg; name="Jim \"Bob\" Jill"', |
| 493 |
'image/jpeg', |
| 494 |
'image', |
| 495 |
'jpeg', |
| 496 |
{'name': 'Jim "Bob" Jill'}, |
| 497 |
[], |
| 498 |
r'image/jpeg; name="Jim \"Bob\" Jill"'), |
| 499 |
|
| 500 |
'non_ascii_in_params': ( |
| 501 |
('foo\xa7/bar; b\xa7r=two; ' |
| 502 |
'baz=thr\xa7e'.encode('latin-1').decode('us-ascii', |
| 503 |
'surrogateescape')), |
| 504 |
'foo\uFFFD/bar', |
| 505 |
'foo\uFFFD', |
| 506 |
'bar', |
| 507 |
{'b\uFFFDr': 'two', 'baz': 'thr\uFFFDe'}, |
| 508 |
[errors.UndecodableBytesDefect]*3, |
| 509 |
'foo�/bar; b�r="two"; baz="thr�e"', |
| 510 |
# XXX Two bugs here: the mime type is not allowed to be an encoded |
| 511 |
# word, and we shouldn't be emitting surrogates in the parameter |
| 512 |
# names. But I don't know what the behavior should be here, so I'm |
| 513 |
# punting for now. In practice this is unlikely to be encountered |
| 514 |
# since headers with binary in them only come from a binary source |
| 515 |
# and are almost certain to be re-emitted without refolding. |
| 516 |
'Content-Type: =?unknown-8bit?q?foo=A7?=/bar; b\udca7r="two";\n' |
| 517 |
" baz*=unknown-8bit''thr%A7e\n", |
| 518 |
), |
| 519 |
|
| 520 |
# RFC 2231 parameter tests. |
| 521 |
|
| 522 |
'rfc2231_segmented_normal_values': ( |
| 523 |
'image/jpeg; name*0="abc"; name*1=".html"', |
| 524 |
'image/jpeg', |
| 525 |
'image', |
| 526 |
'jpeg', |
| 527 |
{'name': "abc.html"}, |
| 528 |
[], |
| 529 |
'image/jpeg; name="abc.html"'), |
| 530 |
|
| 531 |
'quotes_inside_rfc2231_value': ( |
| 532 |
r'image/jpeg; bar*0="baz\"foobar"; bar*1="\"baz"', |
| 533 |
'image/jpeg', |
| 534 |
'image', |
| 535 |
'jpeg', |
| 536 |
{'bar': 'baz"foobar"baz'}, |
| 537 |
[], |
| 538 |
r'image/jpeg; bar="baz\"foobar\"baz"'), |
| 539 |
|
| 540 |
'non_ascii_rfc2231_value': ( |
| 541 |
('text/plain; charset=us-ascii; ' |
| 542 |
"title*=us-ascii'en'This%20is%20" |
| 543 |
'not%20f\xa7n').encode('latin-1').decode('us-ascii', |
| 544 |
'surrogateescape'), |
| 545 |
'text/plain', |
| 546 |
'text', |
| 547 |
'plain', |
| 548 |
{'charset': 'us-ascii', 'title': 'This is not f\uFFFDn'}, |
| 549 |
[errors.UndecodableBytesDefect], |
| 550 |
'text/plain; charset="us-ascii"; title="This is not f�n"', |
| 551 |
'Content-Type: text/plain; charset="us-ascii";\n' |
| 552 |
" title*=unknown-8bit''This%20is%20not%20f%A7n\n", |
| 553 |
), |
| 554 |
|
| 555 |
'rfc2231_encoded_charset': ( |
| 556 |
'text/plain; charset*=ansi-x3.4-1968\'\'us-ascii', |
| 557 |
'text/plain', |
| 558 |
'text', |
| 559 |
'plain', |
| 560 |
{'charset': 'us-ascii'}, |
| 561 |
[], |
| 562 |
'text/plain; charset="us-ascii"'), |
| 563 |
|
| 564 |
# This follows the RFC: no double quotes around encoded values. |
| 565 |
'rfc2231_encoded_no_double_quotes': ( |
| 566 |
("text/plain;" |
| 567 |
"\tname*0*=''This%20is%20;" |
| 568 |
"\tname*1*=%2A%2A%2Afun%2A%2A%2A%20;" |
| 569 |
'\tname*2="is it not.pdf"'), |
| 570 |
'text/plain', |
| 571 |
'text', |
| 572 |
'plain', |
| 573 |
{'name': 'This is ***fun*** is it not.pdf'}, |
| 574 |
[], |
| 575 |
'text/plain; name="This is ***fun*** is it not.pdf"', |
| 576 |
), |
| 577 |
|
| 578 |
# Make sure we also handle it if there are spurious double quotes. |
| 579 |
'rfc2231_encoded_with_double_quotes': ( |
| 580 |
("text/plain;" |
| 581 |
'\tname*0*="us-ascii\'\'This%20is%20even%20more%20";' |
| 582 |
'\tname*1*="%2A%2A%2Afun%2A%2A%2A%20";' |
| 583 |
'\tname*2="is it not.pdf"'), |
| 584 |
'text/plain', |
| 585 |
'text', |
| 586 |
'plain', |
| 587 |
{'name': 'This is even more ***fun*** is it not.pdf'}, |
| 588 |
[errors.InvalidHeaderDefect]*2, |
| 589 |
'text/plain; name="This is even more ***fun*** is it not.pdf"', |
| 590 |
), |
| 591 |
|
| 592 |
'rfc2231_single_quote_inside_double_quotes': ( |
| 593 |
('text/plain; charset=us-ascii;' |
| 594 |
'\ttitle*0*="us-ascii\'en\'This%20is%20really%20";' |
| 595 |
'\ttitle*1*="%2A%2A%2Afun%2A%2A%2A%20";' |
| 596 |
'\ttitle*2="isn\'t it!"'), |
| 597 |
'text/plain', |
| 598 |
'text', |
| 599 |
'plain', |
| 600 |
{'charset': 'us-ascii', 'title': "This is really ***fun*** isn't it!"}, |
| 601 |
[errors.InvalidHeaderDefect]*2, |
| 602 |
('text/plain; charset="us-ascii"; ' |
| 603 |
'title="This is really ***fun*** isn\'t it!"'), |
| 604 |
('Content-Type: text/plain; charset="us-ascii";\n' |
| 605 |
' title="This is really ***fun*** isn\'t it!"\n'), |
| 606 |
), |
| 607 |
|
| 608 |
'rfc2231_single_quote_in_value_with_charset_and_lang': ( |
| 609 |
('application/x-foo;' |
| 610 |
"\tname*0*=\"us-ascii'en-us'Frank's\"; name*1*=\" Document\""), |
| 611 |
'application/x-foo', |
| 612 |
'application', |
| 613 |
'x-foo', |
| 614 |
{'name': "Frank's Document"}, |
| 615 |
[errors.InvalidHeaderDefect]*2, |
| 616 |
'application/x-foo; name="Frank\'s Document"', |
| 617 |
), |
| 618 |
|
| 619 |
'rfc2231_single_quote_in_non_encoded_value': ( |
| 620 |
('application/x-foo;' |
| 621 |
"\tname*0=\"us-ascii'en-us'Frank's\"; name*1=\" Document\""), |
| 622 |
'application/x-foo', |
| 623 |
'application', |
| 624 |
'x-foo', |
| 625 |
{'name': "us-ascii'en-us'Frank's Document"}, |
| 626 |
[], |
| 627 |
'application/x-foo; name="us-ascii\'en-us\'Frank\'s Document"', |
| 628 |
), |
| 629 |
|
| 630 |
'rfc2231_no_language_or_charset': ( |
| 631 |
'text/plain; NAME*0*=english_is_the_default.html', |
| 632 |
'text/plain', |
| 633 |
'text', |
| 634 |
'plain', |
| 635 |
{'name': 'english_is_the_default.html'}, |
| 636 |
[errors.InvalidHeaderDefect], |
| 637 |
'text/plain; NAME="english_is_the_default.html"'), |
| 638 |
|
| 639 |
'rfc2231_encoded_no_charset': ( |
| 640 |
("text/plain;" |
| 641 |
'\tname*0*="\'\'This%20is%20even%20more%20";' |
| 642 |
'\tname*1*="%2A%2A%2Afun%2A%2A%2A%20";' |
| 643 |
'\tname*2="is it.pdf"'), |
| 644 |
'text/plain', |
| 645 |
'text', |
| 646 |
'plain', |
| 647 |
{'name': 'This is even more ***fun*** is it.pdf'}, |
| 648 |
[errors.InvalidHeaderDefect]*2, |
| 649 |
'text/plain; name="This is even more ***fun*** is it.pdf"', |
| 650 |
), |
| 651 |
|
| 652 |
'rfc2231_partly_encoded': ( |
| 653 |
("text/plain;" |
| 654 |
'\tname*0*="\'\'This%20is%20even%20more%20";' |
| 655 |
'\tname*1*="%2A%2A%2Afun%2A%2A%2A%20";' |
| 656 |
'\tname*2="is it.pdf"'), |
| 657 |
'text/plain', |
| 658 |
'text', |
| 659 |
'plain', |
| 660 |
{'name': 'This is even more ***fun*** is it.pdf'}, |
| 661 |
[errors.InvalidHeaderDefect]*2, |
| 662 |
'text/plain; name="This is even more ***fun*** is it.pdf"', |
| 663 |
), |
| 664 |
|
| 665 |
'rfc2231_partly_encoded_2': ( |
| 666 |
("text/plain;" |
| 667 |
'\tname*0*="\'\'This%20is%20even%20more%20";' |
| 668 |
'\tname*1="%2A%2A%2Afun%2A%2A%2A%20";' |
| 669 |
'\tname*2="is it.pdf"'), |
| 670 |
'text/plain', |
| 671 |
'text', |
| 672 |
'plain', |
| 673 |
{'name': 'This is even more %2A%2A%2Afun%2A%2A%2A%20is it.pdf'}, |
| 674 |
[errors.InvalidHeaderDefect], |
| 675 |
('text/plain;' |
| 676 |
' name="This is even more %2A%2A%2Afun%2A%2A%2A%20is it.pdf"'), |
| 677 |
('Content-Type: text/plain;\n' |
| 678 |
' name="This is even more %2A%2A%2Afun%2A%2A%2A%20is' |
| 679 |
' it.pdf"\n'), |
| 680 |
), |
| 681 |
|
| 682 |
'rfc2231_unknown_charset_treated_as_ascii': ( |
| 683 |
"text/plain; name*0*=bogus'xx'ascii_is_the_default", |
| 684 |
'text/plain', |
| 685 |
'text', |
| 686 |
'plain', |
| 687 |
{'name': 'ascii_is_the_default'}, |
| 688 |
[], |
| 689 |
'text/plain; name="ascii_is_the_default"'), |
| 690 |
|
| 691 |
'rfc2231_bad_character_in_charset_parameter_value': ( |
| 692 |
"text/plain; charset*=ascii''utf-8%F1%F2%F3", |
| 693 |
'text/plain', |
| 694 |
'text', |
| 695 |
'plain', |
| 696 |
{'charset': 'utf-8\uFFFD\uFFFD\uFFFD'}, |
| 697 |
[errors.UndecodableBytesDefect], |
| 698 |
'text/plain; charset="utf-8\uFFFD\uFFFD\uFFFD"', |
| 699 |
"Content-Type: text/plain;" |
| 700 |
" charset*=unknown-8bit''utf-8%F1%F2%F3\n", |
| 701 |
), |
| 702 |
|
| 703 |
'rfc2231_utf8_in_supposedly_ascii_charset_parameter_value': ( |
| 704 |
"text/plain; charset*=ascii''utf-8%E2%80%9D", |
| 705 |
'text/plain', |
| 706 |
'text', |
| 707 |
'plain', |
| 708 |
{'charset': 'utf-8”'}, |
| 709 |
[errors.UndecodableBytesDefect], |
| 710 |
'text/plain; charset="utf-8”"', |
| 711 |
# XXX Should folding change the charset to utf8? Currently it just |
| 712 |
# reproduces the original, which is arguably fine. |
| 713 |
"Content-Type: text/plain;" |
| 714 |
" charset*=unknown-8bit''utf-8%E2%80%9D\n", |
| 715 |
), |
| 716 |
|
| 717 |
'rfc2231_nonascii_in_charset_of_charset_parameter_value': ( |
| 718 |
"text/plain; charset*=utf-8”''utf-8%E2%80%9D", |
| 719 |
'text/plain', |
| 720 |
'text', |
| 721 |
'plain', |
| 722 |
{'charset': 'utf-8”'}, |
| 723 |
[], |
| 724 |
'text/plain; charset="utf-8”"', |
| 725 |
"Content-Type: text/plain;" |
| 726 |
" charset*=utf-8''utf-8%E2%80%9D\n", |
| 727 |
), |
| 728 |
|
| 729 |
'rfc2231_encoded_then_unencoded_segments': ( |
| 730 |
('application/x-foo;' |
| 731 |
'\tname*0*="us-ascii\'en-us\'My";' |
| 732 |
'\tname*1=" Document";' |
| 733 |
'\tname*2=" For You"'), |
| 734 |
'application/x-foo', |
| 735 |
'application', |
| 736 |
'x-foo', |
| 737 |
{'name': 'My Document For You'}, |
| 738 |
[errors.InvalidHeaderDefect], |
| 739 |
'application/x-foo; name="My Document For You"', |
| 740 |
), |
| 741 |
|
| 742 |
# My reading of the RFC is that this is an invalid header. The RFC |
| 743 |
# says that if charset and language information is given, the first |
| 744 |
# segment *must* be encoded. |
| 745 |
'rfc2231_unencoded_then_encoded_segments': ( |
| 746 |
('application/x-foo;' |
| 747 |
'\tname*0=us-ascii\'en-us\'My;' |
| 748 |
'\tname*1*=" Document";' |
| 749 |
'\tname*2*=" For You"'), |
| 750 |
'application/x-foo', |
| 751 |
'application', |
| 752 |
'x-foo', |
| 753 |
{'name': 'My Document For You'}, |
| 754 |
[errors.InvalidHeaderDefect]*3, |
| 755 |
'application/x-foo; name="My Document For You"', |
| 756 |
), |
| 757 |
|
| 758 |
# XXX: I would say this one should default to ascii/en for the |
| 759 |
# "encoded" segment, since the first segment is not encoded and is |
| 760 |
# in double quotes, making the value a valid non-encoded string. The |
| 761 |
# old parser decodes this just like the previous case, which may be the |
| 762 |
# better Postel rule, but could equally result in borking headers that |
| 763 |
# intentionally have quoted quotes in them. We could get this 98% |
| 764 |
# right if we treat it as a quoted string *unless* it matches the |
| 765 |
# charset'lang'value pattern exactly *and* there is at least one |
| 766 |
# encoded segment. Implementing that algorithm will require some |
| 767 |
# refactoring, so I haven't done it (yet). |
| 768 |
'rfc2231_quoted_unencoded_then_encoded_segments': ( |
| 769 |
('application/x-foo;' |
| 770 |
'\tname*0="us-ascii\'en-us\'My";' |
| 771 |
'\tname*1*=" Document";' |
| 772 |
'\tname*2*=" For You"'), |
| 773 |
'application/x-foo', |
| 774 |
'application', |
| 775 |
'x-foo', |
| 776 |
{'name': "us-ascii'en-us'My Document For You"}, |
| 777 |
[errors.InvalidHeaderDefect]*2, |
| 778 |
'application/x-foo; name="us-ascii\'en-us\'My Document For You"', |
| 779 |
), |
| 780 |
|
| 781 |
# Make sure our folding algorithm produces multiple sections correctly. |
| 782 |
# We could mix encoded and non-encoded segments, but we don't, we just |
| 783 |
# make them all encoded. It might be worth fixing that, since the |
| 784 |
# sections can get used for wrapping ascii text. |
| 785 |
'rfc2231_folded_segments_correctly_formatted': ( |
| 786 |
('application/x-foo;' |
| 787 |
'\tname="' + "with spaces"*8 + '"'), |
| 788 |
'application/x-foo', |
| 789 |
'application', |
| 790 |
'x-foo', |
| 791 |
{'name': "with spaces"*8}, |
| 792 |
[], |
| 793 |
'application/x-foo; name="' + "with spaces"*8 + '"', |
| 794 |
"Content-Type: application/x-foo;\n" |
| 795 |
" name*0*=us-ascii''with%20spaceswith%20spaceswith%20spaceswith" |
| 796 |
"%20spaceswith;\n" |
| 797 |
" name*1*=%20spaceswith%20spaceswith%20spaceswith%20spaces\n" |
| 798 |
), |
| 799 |
|
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
@parameterize |
| 804 |
class TestContentTransferEncoding(TestHeaderBase): |
| 805 |
|
| 806 |
def cte_as_value(self, |
| 807 |
source, |
| 808 |
cte, |
| 809 |
*args): |
| 810 |
l = len(args) |
| 811 |
defects = args[0] if l>0 else [] |
| 812 |
decoded = args[1] if l>1 and args[1] is not DITTO else source |
| 813 |
header = 'Content-Transfer-Encoding:' + ' ' if source else '' |
| 814 |
folded = args[2] if l>2 else header + source + '\n' |
| 815 |
h = self.make_header('Content-Transfer-Encoding', source) |
| 816 |
self.assertEqual(h.cte, cte) |
| 817 |
self.assertDefectsEqual(h.defects, defects) |
| 818 |
self.assertEqual(h, decoded) |
| 819 |
self.assertEqual(h.fold(policy=policy.default), folded) |
| 820 |
|
| 821 |
cte_params = { |
| 822 |
|
| 823 |
'RFC_2183_1': ( |
| 824 |
'base64', |
| 825 |
'base64',), |
| 826 |
|
| 827 |
'no_value': ( |
| 828 |
'', |
| 829 |
'7bit', |
| 830 |
[errors.HeaderMissingRequiredValue], |
| 831 |
'', |
| 832 |
'Content-Transfer-Encoding:\n', |
| 833 |
), |
| 834 |
|
| 835 |
'junk_after_cte': ( |
| 836 |
'7bit and a bunch more', |
| 837 |
'7bit', |
| 838 |
[errors.InvalidHeaderDefect]), |
| 839 |
|
| 840 |
'extra_space_after_cte': ( |
| 841 |
'base64 ', |
| 842 |
'base64', |
| 843 |
[]), |
| 844 |
|
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
@parameterize |
| 849 |
class TestContentDisposition(TestHeaderBase): |
| 850 |
|
| 851 |
def content_disp_as_value(self, |
| 852 |
source, |
| 853 |
content_disposition, |
| 854 |
*args): |
| 855 |
l = len(args) |
| 856 |
parmdict = args[0] if l>0 else {} |
| 857 |
defects = args[1] if l>1 else [] |
| 858 |
decoded = args[2] if l>2 and args[2] is not DITTO else source |
| 859 |
header = 'Content-Disposition:' + ' ' if source else '' |
| 860 |
folded = args[3] if l>3 else header + source + '\n' |
| 861 |
h = self.make_header('Content-Disposition', source) |
| 862 |
self.assertEqual(h.content_disposition, content_disposition) |
| 863 |
self.assertEqual(h.params, parmdict) |
| 864 |
self.assertDefectsEqual(h.defects, defects) |
| 865 |
self.assertEqual(h, decoded) |
| 866 |
self.assertEqual(h.fold(policy=policy.default), folded) |
| 867 |
|
| 868 |
content_disp_params = { |
| 869 |
|
| 870 |
# Examples from RFC 2183. |
| 871 |
|
| 872 |
'RFC_2183_1': ( |
| 873 |
'inline', |
| 874 |
'inline',), |
| 875 |
|
| 876 |
'RFC_2183_2': ( |
| 877 |
('attachment; filename=genome.jpeg;' |
| 878 |
' modification-date="Wed, 12 Feb 1997 16:29:51 -0500";'), |
| 879 |
'attachment', |
| 880 |
{'filename': 'genome.jpeg', |
| 881 |
'modification-date': 'Wed, 12 Feb 1997 16:29:51 -0500'}, |
| 882 |
[], |
| 883 |
('attachment; filename="genome.jpeg"; ' |
| 884 |
'modification-date="Wed, 12 Feb 1997 16:29:51 -0500"'), |
| 885 |
('Content-Disposition: attachment; filename="genome.jpeg";\n' |
| 886 |
' modification-date="Wed, 12 Feb 1997 16:29:51 -0500"\n'), |
| 887 |
), |
| 888 |
|
| 889 |
'no_value': ( |
| 890 |
'', |
| 891 |
None, |
| 892 |
{}, |
| 893 |
[errors.HeaderMissingRequiredValue], |
| 894 |
'', |
| 895 |
'Content-Disposition:\n'), |
| 896 |
|
| 897 |
'invalid_value': ( |
| 898 |
'ab./k', |
| 899 |
'ab.', |
| 900 |
{}, |
| 901 |
[errors.InvalidHeaderDefect]), |
| 902 |
|
| 903 |
'invalid_value_with_params': ( |
| 904 |
'ab./k; filename="foo"', |
| 905 |
'ab.', |
| 906 |
{'filename': 'foo'}, |
| 907 |
[errors.InvalidHeaderDefect]), |
| 908 |
|
| 909 |
'invalid_parameter_value_with_fws_between_ew': ( |
| 910 |
'attachment; filename="=?UTF-8?Q?Schulbesuchsbest=C3=A4ttigung=2E?=' |
| 911 |
' =?UTF-8?Q?pdf?="', |
| 912 |
'attachment', |
| 913 |
{'filename': 'Schulbesuchsbestättigung.pdf'}, |
| 914 |
[errors.InvalidHeaderDefect]*3, |
| 915 |
('attachment; filename="Schulbesuchsbestättigung.pdf"'), |
| 916 |
('Content-Disposition: attachment;\n' |
| 917 |
' filename*=utf-8\'\'Schulbesuchsbest%C3%A4ttigung.pdf\n'), |
| 918 |
), |
| 919 |
|
| 920 |
'parameter_value_with_fws_between_tokens': ( |
| 921 |
'attachment; filename="File =?utf-8?q?Name?= With Spaces.pdf"', |
| 922 |
'attachment', |
| 923 |
{'filename': 'File Name With Spaces.pdf'}, |
| 924 |
[errors.InvalidHeaderDefect], |
| 925 |
'attachment; filename="File Name With Spaces.pdf"', |
| 926 |
('Content-Disposition: attachment; filename="File Name With Spaces.pdf"\n'), |
| 927 |
) |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
@parameterize |
| 932 |
class TestMIMEVersionHeader(TestHeaderBase): |
| 933 |
|
| 934 |
def version_string_as_MIME_Version(self, |
| 935 |
source, |
| 936 |
decoded, |
| 937 |
version, |
| 938 |
major, |
| 939 |
minor, |
| 940 |
defects): |
| 941 |
h = self.make_header('MIME-Version', source) |
| 942 |
self.assertEqual(h, decoded) |
| 943 |
self.assertEqual(h.version, version) |
| 944 |
self.assertEqual(h.major, major) |
| 945 |
self.assertEqual(h.minor, minor) |
| 946 |
self.assertDefectsEqual(h.defects, defects) |
| 947 |
if source: |
| 948 |
source = ' ' + source |
| 949 |
self.assertEqual(h.fold(policy=policy.default), |
| 950 |
'MIME-Version:' + source + '\n') |
| 951 |
|
| 952 |
version_string_params = { |
| 953 |
|
| 954 |
# Examples from the RFC. |
| 955 |
|
| 956 |
'RFC_2045_1': ( |
| 957 |
'1.0', |
| 958 |
'1.0', |
| 959 |
'1.0', |
| 960 |
1, |
| 961 |
0, |
| 962 |
[]), |
| 963 |
|
| 964 |
'RFC_2045_2': ( |
| 965 |
'1.0 (produced by MetaSend Vx.x)', |
| 966 |
'1.0 (produced by MetaSend Vx.x)', |
| 967 |
'1.0', |
| 968 |
1, |
| 969 |
0, |
| 970 |
[]), |
| 971 |
|
| 972 |
'RFC_2045_3': ( |
| 973 |
'(produced by MetaSend Vx.x) 1.0', |
| 974 |
'(produced by MetaSend Vx.x) 1.0', |
| 975 |
'1.0', |
| 976 |
1, |
| 977 |
0, |
| 978 |
[]), |
| 979 |
|
| 980 |
'RFC_2045_4': ( |
| 981 |
'1.(produced by MetaSend Vx.x)0', |
| 982 |
'1.(produced by MetaSend Vx.x)0', |
| 983 |
'1.0', |
| 984 |
1, |
| 985 |
0, |
| 986 |
[]), |
| 987 |
|
| 988 |
# Other valid values. |
| 989 |
|
| 990 |
'1_1': ( |
| 991 |
'1.1', |
| 992 |
'1.1', |
| 993 |
'1.1', |
| 994 |
1, |
| 995 |
1, |
| 996 |
[]), |
| 997 |
|
| 998 |
'2_1': ( |
| 999 |
'2.1', |
| 1000 |
'2.1', |
| 1001 |
'2.1', |
| 1002 |
2, |
| 1003 |
1, |
| 1004 |
[]), |
| 1005 |
|
| 1006 |
'whitespace': ( |
| 1007 |
'1 .0', |
| 1008 |
'1 .0', |
| 1009 |
'1.0', |
| 1010 |
1, |
| 1011 |
0, |
| 1012 |
[]), |
| 1013 |
|
| 1014 |
'leading_trailing_whitespace_ignored': ( |
| 1015 |
' 1.0 ', |
| 1016 |
' 1.0 ', |
| 1017 |
'1.0', |
| 1018 |
1, |
| 1019 |
0, |
| 1020 |
[]), |
| 1021 |
|
| 1022 |
# Recoverable invalid values. We can recover here only because we |
| 1023 |
# already have a valid value by the time we encounter the garbage. |
| 1024 |
# Anywhere else, and we don't know where the garbage ends. |
| 1025 |
|
| 1026 |
'non_comment_garbage_after': ( |
| 1027 |
'1.0 <abc>', |
| 1028 |
'1.0 <abc>', |
| 1029 |
'1.0', |
| 1030 |
1, |
| 1031 |
0, |
| 1032 |
[errors.InvalidHeaderDefect]), |
| 1033 |
|
| 1034 |
# Unrecoverable invalid values. We *could* apply more heuristics to |
| 1035 |
# get something out of the first two, but doing so is not worth the |
| 1036 |
# effort. |
| 1037 |
|
| 1038 |
'non_comment_garbage_before': ( |
| 1039 |
'<abc> 1.0', |
| 1040 |
'<abc> 1.0', |
| 1041 |
None, |
| 1042 |
None, |
| 1043 |
None, |
| 1044 |
[errors.InvalidHeaderDefect]), |
| 1045 |
|
| 1046 |
'non_comment_garbage_inside': ( |
| 1047 |
'1.<abc>0', |
| 1048 |
'1.<abc>0', |
| 1049 |
None, |
| 1050 |
None, |
| 1051 |
None, |
| 1052 |
[errors.InvalidHeaderDefect]), |
| 1053 |
|
| 1054 |
'two_periods': ( |
| 1055 |
'1..0', |
| 1056 |
'1..0', |
| 1057 |
None, |
| 1058 |
None, |
| 1059 |
None, |
| 1060 |
[errors.InvalidHeaderDefect]), |
| 1061 |
|
| 1062 |
'2_x': ( |
| 1063 |
'2.x', |
| 1064 |
'2.x', |
| 1065 |
None, # This could be 2, but it seems safer to make it None. |
| 1066 |
None, |
| 1067 |
None, |
| 1068 |
[errors.InvalidHeaderDefect]), |
| 1069 |
|
| 1070 |
'foo': ( |
| 1071 |
'foo', |
| 1072 |
'foo', |
| 1073 |
None, |
| 1074 |
None, |
| 1075 |
None, |
| 1076 |
[errors.InvalidHeaderDefect]), |
| 1077 |
|
| 1078 |
'missing': ( |
| 1079 |
'', |
| 1080 |
'', |
| 1081 |
None, |
| 1082 |
None, |
| 1083 |
None, |
| 1084 |
[errors.HeaderMissingRequiredValue]), |
| 1085 |
|
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
@parameterize |
| 1090 |
class TestAddressHeader(TestHeaderBase): |
| 1091 |
|
| 1092 |
example_params = { |
| 1093 |
|
| 1094 |
'empty': |
| 1095 |
('<>', |
| 1096 |
[errors.InvalidHeaderDefect], |
| 1097 |
'<>', |
| 1098 |
'', |
| 1099 |
'<>', |
| 1100 |
'', |
| 1101 |
'', |
| 1102 |
None), |
| 1103 |
|
| 1104 |
'address_only': |
| 1105 |
('[email protected]', |
| 1106 |
[], |
| 1107 |
'[email protected]', |
| 1108 |
'', |
| 1109 |
'[email protected]', |
| 1110 |
'zippy', |
| 1111 |
'pinhead.com', |
| 1112 |
None), |
| 1113 |
|
| 1114 |
'name_and_address': |
| 1115 |
('Zaphrod Beblebrux <[email protected]>', |
| 1116 |
[], |
| 1117 |
'Zaphrod Beblebrux <[email protected]>', |
| 1118 |
'Zaphrod Beblebrux', |
| 1119 |
'[email protected]', |
| 1120 |
'zippy', |
| 1121 |
'pinhead.com', |
| 1122 |
None), |
| 1123 |
|
| 1124 |
'quoted_local_part': |
| 1125 |
('Zaphrod Beblebrux <"foo bar"@pinhead.com>', |
| 1126 |
[], |
| 1127 |
'Zaphrod Beblebrux <"foo bar"@pinhead.com>', |
| 1128 |
'Zaphrod Beblebrux', |
| 1129 |
'"foo bar"@pinhead.com', |
| 1130 |
'foo bar', |
| 1131 |
'pinhead.com', |
| 1132 |
None), |
| 1133 |
|
| 1134 |
'quoted_parens_in_name': |
| 1135 |
(r'"A \(Special\) Person" <[email protected]>', |
| 1136 |
[], |
| 1137 |
'"A (Special) Person" <[email protected]>', |
| 1138 |
'A (Special) Person', |
| 1139 |
'[email protected]', |
| 1140 |
'person', |
| 1141 |
'dom.ain', |
| 1142 |
None), |
| 1143 |
|
| 1144 |
'quoted_backslashes_in_name': |
| 1145 |
(r'"Arthur \\Backslash\\ Foobar" <[email protected]>', |
| 1146 |
[], |
| 1147 |
r'"Arthur \\Backslash\\ Foobar" <[email protected]>', |
| 1148 |
r'Arthur \Backslash\ Foobar', |
| 1149 |
'[email protected]', |
| 1150 |
'person', |
| 1151 |
'dom.ain', |
| 1152 |
None), |
| 1153 |
|
| 1154 |
'name_with_dot': |
| 1155 |
('John X. Doe <[email protected]>', |
| 1156 |
[errors.ObsoleteHeaderDefect], |
| 1157 |
'"John X. Doe" <[email protected]>', |
| 1158 |
'John X. Doe', |
| 1159 |
'[email protected]', |
| 1160 |
'jxd', |
| 1161 |
'example.com', |
| 1162 |
None), |
| 1163 |
|
| 1164 |
'quoted_strings_in_local_part': |
| 1165 |
('""example" example"@example.com', |
| 1166 |
[errors.InvalidHeaderDefect]*3, |
| 1167 |
'"example example"@example.com', |
| 1168 |
'', |
| 1169 |
'"example example"@example.com', |
| 1170 |
'example example', |
| 1171 |
'example.com', |
| 1172 |
None), |
| 1173 |
|
| 1174 |
'escaped_quoted_strings_in_local_part': |
| 1175 |
(r'"\"example\" example"@example.com', |
| 1176 |
[], |
| 1177 |
r'"\"example\" example"@example.com', |
| 1178 |
'', |
| 1179 |
r'"\"example\" example"@example.com', |
| 1180 |
r'"example" example', |
| 1181 |
'example.com', |
| 1182 |
None), |
| 1183 |
|
| 1184 |
'escaped_escapes_in_local_part': |
| 1185 |
(r'"\\"example\\" example"@example.com', |
| 1186 |
[errors.InvalidHeaderDefect]*5, |
| 1187 |
r'"\\example\\\\ example"@example.com', |
| 1188 |
'', |
| 1189 |
r'"\\example\\\\ example"@example.com', |
| 1190 |
r'\example\\ example', |
| 1191 |
'example.com', |
| 1192 |
None), |
| 1193 |
|
| 1194 |
'spaces_in_unquoted_local_part_collapsed': |
| 1195 |
('merwok wok @example.com', |
| 1196 |
[errors.InvalidHeaderDefect]*2, |
| 1197 |
'"merwok wok"@example.com', |
| 1198 |
'', |
| 1199 |
'"merwok wok"@example.com', |
| 1200 |
'merwok wok', |
| 1201 |
'example.com', |
| 1202 |
None), |
| 1203 |
|
| 1204 |
'spaces_around_dots_in_local_part_removed': |
| 1205 |
('merwok. wok . [email protected]', |
| 1206 |
[errors.ObsoleteHeaderDefect], |
| 1207 |
'[email protected]', |
| 1208 |
'', |
| 1209 |
'[email protected]', |
| 1210 |
'merwok.wok.wok', |
| 1211 |
'example.com', |
| 1212 |
None), |
| 1213 |
|
| 1214 |
'rfc2047_atom_is_decoded': |
| 1215 |
('=?utf-8?q?=C3=89ric?= <[email protected]>', |
| 1216 |
[], |
| 1217 |
'Éric <[email protected]>', |
| 1218 |
'Éric', |
| 1219 |
'[email protected]', |
| 1220 |
'foo', |
| 1221 |
'example.com', |
| 1222 |
None), |
| 1223 |
|
| 1224 |
'rfc2047_atom_in_phrase_is_decoded': |
| 1225 |
('The =?utf-8?q?=C3=89ric=2C?= Himself <[email protected]>', |
| 1226 |
[], |
| 1227 |
'"The Éric, Himself" <[email protected]>', |
| 1228 |
'The Éric, Himself', |
| 1229 |
'[email protected]', |
| 1230 |
'foo', |
| 1231 |
'example.com', |
| 1232 |
None), |
| 1233 |
|
| 1234 |
'rfc2047_atom_in_quoted_string_is_decoded': |
| 1235 |
('"=?utf-8?q?=C3=89ric?=" <[email protected]>', |
| 1236 |
[errors.InvalidHeaderDefect, |
| 1237 |
errors.InvalidHeaderDefect], |
| 1238 |
'Éric <[email protected]>', |
| 1239 |
'Éric', |
| 1240 |
'[email protected]', |
| 1241 |
'foo', |
| 1242 |
'example.com', |
| 1243 |
None), |
| 1244 |
|
| 1245 |
'name_ending_with_dot_without_space': |
| 1246 |
('John X.<[email protected]>', |
| 1247 |
[errors.ObsoleteHeaderDefect], |
| 1248 |
'"John X." <[email protected]>', |
| 1249 |
'John X.', |
| 1250 |
'[email protected]', |
| 1251 |
'jxd', |
| 1252 |
'example.com', |
| 1253 |
None), |
| 1254 |
|
| 1255 |
'name_starting_with_dot': |
| 1256 |
('. Doe <[email protected]>', |
| 1257 |
[errors.InvalidHeaderDefect, errors.ObsoleteHeaderDefect], |
| 1258 |
'". Doe" <[email protected]>', |
| 1259 |
'. Doe', |
| 1260 |
'[email protected]', |
| 1261 |
'jxd', |
| 1262 |
'example.com', |
| 1263 |
None), |
| 1264 |
|
| 1265 |
# XXX: Need many more examples, and in particular some with names in |
| 1266 |
# trailing comments, which aren't currently handled. comments in |
| 1267 |
# general are not handled yet. |
| 1268 |
|
| 1269 |
} |
| 1270 |
|
| 1271 |
def example_as_address(self, source, defects, decoded, display_name, |
| 1272 |
addr_spec, username, domain, comment): |
| 1273 |
h = self.make_header('sender', source) |
| 1274 |
self.assertEqual(h, decoded) |
| 1275 |
self.assertDefectsEqual(h.defects, defects) |
| 1276 |
a = h.address |
| 1277 |
self.assertEqual(str(a), decoded) |
| 1278 |
self.assertEqual(len(h.groups), 1) |
| 1279 |
self.assertEqual([a], list(h.groups[0].addresses)) |
| 1280 |
self.assertEqual([a], list(h.addresses)) |
| 1281 |
self.assertEqual(a.display_name, display_name) |
| 1282 |
self.assertEqual(a.addr_spec, addr_spec) |
| 1283 |
self.assertEqual(a.username, username) |
| 1284 |
self.assertEqual(a.domain, domain) |
| 1285 |
# XXX: we have no comment support yet. |
| 1286 |
#self.assertEqual(a.comment, comment) |
| 1287 |
|
| 1288 |
example_broken_header_params = { |
| 1289 |
|
| 1290 |
'just_dquote': |
| 1291 |
('"', |
| 1292 |
[errors.InvalidHeaderDefect]*2, |
| 1293 |
'<>', |
| 1294 |
'', |
| 1295 |
'<>', |
| 1296 |
'', |
| 1297 |
'', |
| 1298 |
), |
| 1299 |
|
| 1300 |
} |
| 1301 |
|
| 1302 |
def example_broken_header_as_address( |
| 1303 |
self, |
| 1304 |
source, |
| 1305 |
defects, |
| 1306 |
decoded, |
| 1307 |
display_name, |
| 1308 |
addr_spec, |
| 1309 |
username, |
| 1310 |
domain, |
| 1311 |
): |
| 1312 |
h = self.make_header('sender', source) |
| 1313 |
self.assertEqual(h, decoded) |
| 1314 |
self.assertDefectsEqual(h.defects, defects) |
| 1315 |
a = h.address |
| 1316 |
self.assertEqual(str(a), decoded) |
| 1317 |
self.assertEqual(len(h.groups), 1) |
| 1318 |
self.assertEqual([a], list(h.groups[0].addresses)) |
| 1319 |
self.assertEqual([a], list(h.addresses)) |
| 1320 |
self.assertEqual(a.display_name, display_name) |
| 1321 |
self.assertEqual(a.addr_spec, addr_spec) |
| 1322 |
self.assertEqual(a.username, username) |
| 1323 |
self.assertEqual(a.domain, domain) |
| 1324 |
|
| 1325 |
def example_as_group(self, source, defects, decoded, display_name, |
| 1326 |
addr_spec, username, domain, comment): |
| 1327 |
source = 'foo: {};'.format(source) |
| 1328 |
gdecoded = 'foo: {};'.format(decoded) if decoded else 'foo:;' |
| 1329 |
h = self.make_header('to', source) |
| 1330 |
self.assertEqual(h, gdecoded) |
| 1331 |
self.assertDefectsEqual(h.defects, defects) |
| 1332 |
self.assertEqual(h.groups[0].addresses, h.addresses) |
| 1333 |
self.assertEqual(len(h.groups), 1) |
| 1334 |
self.assertEqual(len(h.addresses), 1) |
| 1335 |
a = h.addresses[0] |
| 1336 |
self.assertEqual(str(a), decoded) |
| 1337 |
self.assertEqual(a.display_name, display_name) |
| 1338 |
self.assertEqual(a.addr_spec, addr_spec) |
| 1339 |
self.assertEqual(a.username, username) |
| 1340 |
self.assertEqual(a.domain, domain) |
| 1341 |
|
| 1342 |
def test_simple_address_list(self): |
| 1343 |
value = ('Fred <[email protected]>, [email protected], ' |
| 1344 |
'"Harry W. Hastings" <[email protected]>') |
| 1345 |
h = self.make_header('to', value) |
| 1346 |
self.assertEqual(h, value) |
| 1347 |
self.assertEqual(len(h.groups), 3) |
| 1348 |
self.assertEqual(len(h.addresses), 3) |
| 1349 |
for i in range(3): |
| 1350 |
self.assertEqual(h.groups[i].addresses[0], h.addresses[i]) |
| 1351 |
self.assertEqual(str(h.addresses[0]), 'Fred <[email protected]>') |
| 1352 |
self.assertEqual(str(h.addresses[1]), '[email protected]') |
| 1353 |
self.assertEqual(str(h.addresses[2]), |
| 1354 |
'"Harry W. Hastings" <[email protected]>') |
| 1355 |
self.assertEqual(h.addresses[2].display_name, |
| 1356 |
'Harry W. Hastings') |
| 1357 |
|
| 1358 |
def test_complex_address_list(self): |
| 1359 |
examples = list(self.example_params.values()) |
| 1360 |
source = ('dummy list:;, another: (empty);,' + |
| 1361 |
', '.join([x[0] for x in examples[:4]]) + ', ' + |
| 1362 |
r'"A \"list\"": ' + |
| 1363 |
', '.join([x[0] for x in examples[4:6]]) + ';,' + |
| 1364 |
', '.join([x[0] for x in examples[6:]]) |
| 1365 |
) |
| 1366 |
# XXX: the fact that (empty) disappears here is a potential API design |
| 1367 |
# bug. We don't currently have a way to preserve comments. |
| 1368 |
expected = ('dummy list:;, another:;, ' + |
| 1369 |
', '.join([x[2] for x in examples[:4]]) + ', ' + |
| 1370 |
r'"A \"list\"": ' + |
| 1371 |
', '.join([x[2] for x in examples[4:6]]) + ';, ' + |
| 1372 |
', '.join([x[2] for x in examples[6:]]) |
| 1373 |
) |
| 1374 |
|
| 1375 |
h = self.make_header('to', source) |
| 1376 |
self.assertEqual(h.split(','), expected.split(',')) |
| 1377 |
self.assertEqual(h, expected) |
| 1378 |
self.assertEqual(len(h.groups), 7 + len(examples) - 6) |
| 1379 |
self.assertEqual(h.groups[0].display_name, 'dummy list') |
| 1380 |
self.assertEqual(h.groups[1].display_name, 'another') |
| 1381 |
self.assertEqual(h.groups[6].display_name, 'A "list"') |
| 1382 |
self.assertEqual(len(h.addresses), len(examples)) |
| 1383 |
for i in range(4): |
| 1384 |
self.assertIsNone(h.groups[i+2].display_name) |
| 1385 |
self.assertEqual(str(h.groups[i+2].addresses[0]), examples[i][2]) |
| 1386 |
for i in range(7, 7 + len(examples) - 6): |
| 1387 |
self.assertIsNone(h.groups[i].display_name) |
| 1388 |
self.assertEqual(str(h.groups[i].addresses[0]), examples[i-1][2]) |
| 1389 |
for i in range(len(examples)): |
| 1390 |
self.assertEqual(str(h.addresses[i]), examples[i][2]) |
| 1391 |
self.assertEqual(h.addresses[i].addr_spec, examples[i][4]) |
| 1392 |
|
| 1393 |
def test_address_read_only(self): |
| 1394 |
h = self.make_header('sender', '[email protected]') |
| 1395 |
with self.assertRaises(AttributeError): |
| 1396 |
h.address = 'foo' |
| 1397 |
|
| 1398 |
def test_addresses_read_only(self): |
| 1399 |
h = self.make_header('sender', '[email protected]') |
| 1400 |
with self.assertRaises(AttributeError): |
| 1401 |
h.addresses = 'foo' |
| 1402 |
|
| 1403 |
def test_groups_read_only(self): |
| 1404 |
h = self.make_header('sender', '[email protected]') |
| 1405 |
with self.assertRaises(AttributeError): |
| 1406 |
h.groups = 'foo' |
| 1407 |
|
| 1408 |
def test_addresses_types(self): |
| 1409 |
source = 'me <[email protected]>' |
| 1410 |
h = self.make_header('to', source) |
| 1411 |
self.assertIsInstance(h.addresses, tuple) |
| 1412 |
self.assertIsInstance(h.addresses[0], Address) |
| 1413 |
|
| 1414 |
def test_groups_types(self): |
| 1415 |
source = 'me <[email protected]>' |
| 1416 |
h = self.make_header('to', source) |
| 1417 |
self.assertIsInstance(h.groups, tuple) |
| 1418 |
self.assertIsInstance(h.groups[0], Group) |
| 1419 |
|
| 1420 |
def test_set_from_Address(self): |
| 1421 |
h = self.make_header('to', Address('me', 'foo', 'example.com')) |
| 1422 |
self.assertEqual(h, 'me <[email protected]>') |
| 1423 |
|
| 1424 |
def test_set_from_Address_list(self): |
| 1425 |
h = self.make_header('to', [Address('me', 'foo', 'example.com'), |
| 1426 |
Address('you', 'bar', 'example.com')]) |
| 1427 |
self.assertEqual(h, 'me <[email protected]>, you <[email protected]>') |
| 1428 |
|
| 1429 |
def test_set_from_Address_and_Group_list(self): |
| 1430 |
h = self.make_header('to', [Address('me', 'foo', 'example.com'), |
| 1431 |
Group('bing', [Address('fiz', 'z', 'b.com'), |
| 1432 |
Address('zif', 'f', 'c.com')]), |
| 1433 |
Address('you', 'bar', 'example.com')]) |
| 1434 |
self.assertEqual(h, 'me <[email protected]>, bing: fiz <[email protected]>, ' |
| 1435 |
'zif <[email protected]>;, you <[email protected]>') |
| 1436 |
self.assertEqual(h.fold(policy=policy.default.clone(max_line_length=40)), |
| 1437 |
'to: me <[email protected]>,\n' |
| 1438 |
' bing: fiz <[email protected]>, zif <[email protected]>;,\n' |
| 1439 |
' you <[email protected]>\n') |
| 1440 |
|
| 1441 |
def test_set_from_Group_list(self): |
| 1442 |
h = self.make_header('to', [Group('bing', [Address('fiz', 'z', 'b.com'), |
| 1443 |
Address('zif', 'f', 'c.com')])]) |
| 1444 |
self.assertEqual(h, 'bing: fiz <[email protected]>, zif <[email protected]>;') |
| 1445 |
|
| 1446 |
|
| 1447 |
class TestAddressAndGroup(TestEmailBase): |
| 1448 |
|
| 1449 |
def _test_attr_ro(self, obj, attr): |
| 1450 |
with self.assertRaises(AttributeError): |
| 1451 |
setattr(obj, attr, 'foo') |
| 1452 |
|
| 1453 |
def test_address_display_name_ro(self): |
| 1454 |
self._test_attr_ro(Address('foo', 'bar', 'baz'), 'display_name') |
| 1455 |
|
| 1456 |
def test_address_username_ro(self): |
| 1457 |
self._test_attr_ro(Address('foo', 'bar', 'baz'), 'username') |
| 1458 |
|
| 1459 |
def test_address_domain_ro(self): |
| 1460 |
self._test_attr_ro(Address('foo', 'bar', 'baz'), 'domain') |
| 1461 |
|
| 1462 |
def test_group_display_name_ro(self): |
| 1463 |
self._test_attr_ro(Group('foo'), 'display_name') |
| 1464 |
|
| 1465 |
def test_group_addresses_ro(self): |
| 1466 |
self._test_attr_ro(Group('foo'), 'addresses') |
| 1467 |
|
| 1468 |
def test_address_from_username_domain(self): |
| 1469 |
a = Address('foo', 'bar', 'baz') |
| 1470 |
self.assertEqual(a.display_name, 'foo') |
| 1471 |
self.assertEqual(a.username, 'bar') |
| 1472 |
self.assertEqual(a.domain, 'baz') |
| 1473 |
self.assertEqual(a.addr_spec, 'bar@baz') |
| 1474 |
self.assertEqual(str(a), 'foo <bar@baz>') |
| 1475 |
|
| 1476 |
def test_address_from_addr_spec(self): |
| 1477 |
a = Address('foo', addr_spec='bar@baz') |
| 1478 |
self.assertEqual(a.display_name, 'foo') |
| 1479 |
self.assertEqual(a.username, 'bar') |
| 1480 |
self.assertEqual(a.domain, 'baz') |
| 1481 |
self.assertEqual(a.addr_spec, 'bar@baz') |
| 1482 |
self.assertEqual(str(a), 'foo <bar@baz>') |
| 1483 |
|
| 1484 |
def test_address_with_no_display_name(self): |
| 1485 |
a = Address(addr_spec='bar@baz') |
| 1486 |
self.assertEqual(a.display_name, '') |
| 1487 |
self.assertEqual(a.username, 'bar') |
| 1488 |
self.assertEqual(a.domain, 'baz') |
| 1489 |
self.assertEqual(a.addr_spec, 'bar@baz') |
| 1490 |
self.assertEqual(str(a), 'bar@baz') |
| 1491 |
|
| 1492 |
def test_null_address(self): |
| 1493 |
a = Address() |
| 1494 |
self.assertEqual(a.display_name, '') |
| 1495 |
self.assertEqual(a.username, '') |
| 1496 |
self.assertEqual(a.domain, '') |
| 1497 |
self.assertEqual(a.addr_spec, '<>') |
| 1498 |
self.assertEqual(str(a), '<>') |
| 1499 |
|
| 1500 |
def test_domain_only(self): |
| 1501 |
# This isn't really a valid address. |
| 1502 |
a = Address(domain='buzz') |
| 1503 |
self.assertEqual(a.display_name, '') |
| 1504 |
self.assertEqual(a.username, '') |
| 1505 |
self.assertEqual(a.domain, 'buzz') |
| 1506 |
self.assertEqual(a.addr_spec, '@buzz') |
| 1507 |
self.assertEqual(str(a), '@buzz') |
| 1508 |
|
| 1509 |
def test_username_only(self): |
| 1510 |
# This isn't really a valid address. |
| 1511 |
a = Address(username='buzz') |
| 1512 |
self.assertEqual(a.display_name, '') |
| 1513 |
self.assertEqual(a.username, 'buzz') |
| 1514 |
self.assertEqual(a.domain, '') |
| 1515 |
self.assertEqual(a.addr_spec, 'buzz') |
| 1516 |
self.assertEqual(str(a), 'buzz') |
| 1517 |
|
| 1518 |
def test_display_name_only(self): |
| 1519 |
a = Address('buzz') |
| 1520 |
self.assertEqual(a.display_name, 'buzz') |
| 1521 |
self.assertEqual(a.username, '') |
| 1522 |
self.assertEqual(a.domain, '') |
| 1523 |
self.assertEqual(a.addr_spec, '<>') |
| 1524 |
self.assertEqual(str(a), 'buzz <>') |
| 1525 |
|
| 1526 |
def test_quoting(self): |
| 1527 |
# Ideally we'd check every special individually, but I'm not up for |
| 1528 |
# writing that many tests. |
| 1529 |
a = Address('Sara J.', 'bad name', 'example.com') |
| 1530 |
self.assertEqual(a.display_name, 'Sara J.') |
| 1531 |
self.assertEqual(a.username, 'bad name') |
| 1532 |
self.assertEqual(a.domain, 'example.com') |
| 1533 |
self.assertEqual(a.addr_spec, '"bad name"@example.com') |
| 1534 |
self.assertEqual(str(a), '"Sara J." <"bad name"@example.com>') |
| 1535 |
|
| 1536 |
def test_il8n(self): |
| 1537 |
a = Address('Éric', 'wok', 'exàmple.com') |
| 1538 |
self.assertEqual(a.display_name, 'Éric') |
| 1539 |
self.assertEqual(a.username, 'wok') |
| 1540 |
self.assertEqual(a.domain, 'exàmple.com') |
| 1541 |
self.assertEqual(a.addr_spec, 'wok@exàmple.com') |
| 1542 |
self.assertEqual(str(a), 'Éric <wok@exàmple.com>') |
| 1543 |
|
| 1544 |
# XXX: there is an API design issue that needs to be solved here. |
| 1545 |
#def test_non_ascii_username_raises(self): |
| 1546 |
# with self.assertRaises(ValueError): |
| 1547 |
# Address('foo', 'wők', 'example.com') |
| 1548 |
|
| 1549 |
def test_crlf_in_constructor_args_raises(self): |
| 1550 |
cases = ( |
| 1551 |
dict(display_name='foo\r'), |
| 1552 |
dict(display_name='foo\n'), |
| 1553 |
dict(display_name='foo\r\n'), |
| 1554 |
dict(domain='example.com\r'), |
| 1555 |
dict(domain='example.com\n'), |
| 1556 |
dict(domain='example.com\r\n'), |
| 1557 |
dict(username='wok\r'), |
| 1558 |
dict(username='wok\n'), |
| 1559 |
dict(username='wok\r\n'), |
| 1560 |
dict(addr_spec='[email protected]\r'), |
| 1561 |
dict(addr_spec='[email protected]\n'), |
| 1562 |
dict(addr_spec='[email protected]\r\n') |
| 1563 |
) |
| 1564 |
for kwargs in cases: |
| 1565 |
with self.subTest(kwargs=kwargs), self.assertRaisesRegex(ValueError, "invalid arguments"): |
| 1566 |
Address(**kwargs) |
| 1567 |
|
| 1568 |
def test_non_ascii_username_in_addr_spec_raises(self): |
| 1569 |
with self.assertRaises(ValueError): |
| 1570 |
Address('foo', addr_spec='wő[email protected]') |
| 1571 |
|
| 1572 |
def test_address_addr_spec_and_username_raises(self): |
| 1573 |
with self.assertRaises(TypeError): |
| 1574 |
Address('foo', username='bing', addr_spec='bar@baz') |
| 1575 |
|
| 1576 |
def test_address_addr_spec_and_domain_raises(self): |
| 1577 |
with self.assertRaises(TypeError): |
| 1578 |
Address('foo', domain='bing', addr_spec='bar@baz') |
| 1579 |
|
| 1580 |
def test_address_addr_spec_and_username_and_domain_raises(self): |
| 1581 |
with self.assertRaises(TypeError): |
| 1582 |
Address('foo', username='bong', domain='bing', addr_spec='bar@baz') |
| 1583 |
|
| 1584 |
def test_space_in_addr_spec_username_raises(self): |
| 1585 |
with self.assertRaises(ValueError): |
| 1586 |
Address('foo', addr_spec="bad [email protected]") |
| 1587 |
|
| 1588 |
def test_bad_addr_sepc_raises(self): |
| 1589 |
with self.assertRaises(ValueError): |
| 1590 |
Address('foo', addr_spec="name@ex[]ample.com") |
| 1591 |
|
| 1592 |
def test_empty_group(self): |
| 1593 |
g = Group('foo') |
| 1594 |
self.assertEqual(g.display_name, 'foo') |
| 1595 |
self.assertEqual(g.addresses, tuple()) |
| 1596 |
self.assertEqual(str(g), 'foo:;') |
| 1597 |
|
| 1598 |
def test_empty_group_list(self): |
| 1599 |
g = Group('foo', addresses=[]) |
| 1600 |
self.assertEqual(g.display_name, 'foo') |
| 1601 |
self.assertEqual(g.addresses, tuple()) |
| 1602 |
self.assertEqual(str(g), 'foo:;') |
| 1603 |
|
| 1604 |
def test_null_group(self): |
| 1605 |
g = Group() |
| 1606 |
self.assertIsNone(g.display_name) |
| 1607 |
self.assertEqual(g.addresses, tuple()) |
| 1608 |
self.assertEqual(str(g), 'None:;') |
| 1609 |
|
| 1610 |
def test_group_with_addresses(self): |
| 1611 |
addrs = [Address('b', 'b', 'c'), Address('a', 'b','c')] |
| 1612 |
g = Group('foo', addrs) |
| 1613 |
self.assertEqual(g.display_name, 'foo') |
| 1614 |
self.assertEqual(g.addresses, tuple(addrs)) |
| 1615 |
self.assertEqual(str(g), 'foo: b <b@c>, a <b@c>;') |
| 1616 |
|
| 1617 |
def test_group_with_addresses_no_display_name(self): |
| 1618 |
addrs = [Address('b', 'b', 'c'), Address('a', 'b','c')] |
| 1619 |
g = Group(addresses=addrs) |
| 1620 |
self.assertIsNone(g.display_name) |
| 1621 |
self.assertEqual(g.addresses, tuple(addrs)) |
| 1622 |
self.assertEqual(str(g), 'None: b <b@c>, a <b@c>;') |
| 1623 |
|
| 1624 |
def test_group_with_one_address_no_display_name(self): |
| 1625 |
addrs = [Address('b', 'b', 'c')] |
| 1626 |
g = Group(addresses=addrs) |
| 1627 |
self.assertIsNone(g.display_name) |
| 1628 |
self.assertEqual(g.addresses, tuple(addrs)) |
| 1629 |
self.assertEqual(str(g), 'b <b@c>') |
| 1630 |
|
| 1631 |
def test_display_name_quoting(self): |
| 1632 |
g = Group('foo.bar') |
| 1633 |
self.assertEqual(g.display_name, 'foo.bar') |
| 1634 |
self.assertEqual(g.addresses, tuple()) |
| 1635 |
self.assertEqual(str(g), '"foo.bar":;') |
| 1636 |
|
| 1637 |
def test_display_name_blanks_not_quoted(self): |
| 1638 |
g = Group('foo bar') |
| 1639 |
self.assertEqual(g.display_name, 'foo bar') |
| 1640 |
self.assertEqual(g.addresses, tuple()) |
| 1641 |
self.assertEqual(str(g), 'foo bar:;') |
| 1642 |
|
| 1643 |
def test_set_message_header_from_address(self): |
| 1644 |
a = Address('foo', 'bar', 'example.com') |
| 1645 |
m = Message(policy=policy.default) |
| 1646 |
m['To'] = a |
| 1647 |
self.assertEqual(m['to'], 'foo <[email protected]>') |
| 1648 |
self.assertEqual(m['to'].addresses, (a,)) |
| 1649 |
|
| 1650 |
def test_set_message_header_from_group(self): |
| 1651 |
g = Group('foo bar') |
| 1652 |
m = Message(policy=policy.default) |
| 1653 |
m['To'] = g |
| 1654 |
self.assertEqual(m['to'], 'foo bar:;') |
| 1655 |
self.assertEqual(m['to'].addresses, g.addresses) |
| 1656 |
|
| 1657 |
def test_address_comparison(self): |
| 1658 |
a = Address('foo', 'bar', 'example.com') |
| 1659 |
self.assertEqual(Address('foo', 'bar', 'example.com'), a) |
| 1660 |
self.assertNotEqual(Address('baz', 'bar', 'example.com'), a) |
| 1661 |
self.assertNotEqual(Address('foo', 'baz', 'example.com'), a) |
| 1662 |
self.assertNotEqual(Address('foo', 'bar', 'baz'), a) |
| 1663 |
self.assertFalse(a == object()) |
| 1664 |
self.assertTrue(a == ALWAYS_EQ) |
| 1665 |
|
| 1666 |
def test_group_comparison(self): |
| 1667 |
a = Address('foo', 'bar', 'example.com') |
| 1668 |
g = Group('foo bar', [a]) |
| 1669 |
self.assertEqual(Group('foo bar', (a,)), g) |
| 1670 |
self.assertNotEqual(Group('baz', [a]), g) |
| 1671 |
self.assertNotEqual(Group('foo bar', []), g) |
| 1672 |
self.assertFalse(g == object()) |
| 1673 |
self.assertTrue(g == ALWAYS_EQ) |
| 1674 |
|
| 1675 |
|
| 1676 |
class TestFolding(TestHeaderBase): |
| 1677 |
|
| 1678 |
def test_address_display_names(self): |
| 1679 |
"""Test the folding and encoding of address headers.""" |
| 1680 |
for name, result in ( |
| 1681 |
('Foo Bar, France', '"Foo Bar, France"'), |
| 1682 |
('Foo Bar (France)', '"Foo Bar (France)"'), |
| 1683 |
('Foo Bar, España', 'Foo =?utf-8?q?Bar=2C_Espa=C3=B1a?='), |
| 1684 |
('Foo Bar (España)', 'Foo Bar =?utf-8?b?KEVzcGHDsWEp?='), |
| 1685 |
('Foo, Bar España', '=?utf-8?q?Foo=2C_Bar_Espa=C3=B1a?='), |
| 1686 |
('Foo, Bar [España]', '=?utf-8?q?Foo=2C_Bar_=5BEspa=C3=B1a=5D?='), |
| 1687 |
('Foo Bär, France', 'Foo =?utf-8?q?B=C3=A4r=2C?= France'), |
| 1688 |
('Foo Bär <France>', 'Foo =?utf-8?q?B=C3=A4r_=3CFrance=3E?='), |
| 1689 |
( |
| 1690 |
'Lôrem ipsum dôlôr sit amet, cônsectetuer adipiscing. ' |
| 1691 |
'Suspendisse pôtenti. Aliquam nibh. Suspendisse pôtenti.', |
| 1692 |
'=?utf-8?q?L=C3=B4rem_ipsum_d=C3=B4l=C3=B4r_sit_amet=2C_c' |
| 1693 |
'=C3=B4nsectetuer?=\n =?utf-8?q?_adipiscing=2E_Suspendisse' |
| 1694 |
'_p=C3=B4tenti=2E_Aliquam_nibh=2E?=\n Suspendisse =?utf-8' |
| 1695 |
'?q?p=C3=B4tenti=2E?=', |
| 1696 |
), |
| 1697 |
): |
| 1698 |
h = self.make_header('To', Address(name, addr_spec='[email protected]')) |
| 1699 |
self.assertEqual(h.fold(policy=policy.default), |
| 1700 |
'To: %s <[email protected]>\n' % result) |
| 1701 |
|
| 1702 |
def test_short_unstructured(self): |
| 1703 |
h = self.make_header('subject', 'this is a test') |
| 1704 |
self.assertEqual(h.fold(policy=policy.default), |
| 1705 |
'subject: this is a test\n') |
| 1706 |
|
| 1707 |
def test_long_unstructured(self): |
| 1708 |
h = self.make_header('Subject', 'This is a long header ' |
| 1709 |
'line that will need to be folded into two lines ' |
| 1710 |
'and will demonstrate basic folding') |
| 1711 |
self.assertEqual(h.fold(policy=policy.default), |
| 1712 |
'Subject: This is a long header line that will ' |
| 1713 |
'need to be folded into two lines\n' |
| 1714 |
' and will demonstrate basic folding\n') |
| 1715 |
|
| 1716 |
def test_unstructured_short_max_line_length(self): |
| 1717 |
h = self.make_header('Subject', 'this is a short header ' |
| 1718 |
'that will be folded anyway') |
| 1719 |
self.assertEqual( |
| 1720 |
h.fold(policy=policy.default.clone(max_line_length=20)), |
| 1721 |
textwrap.dedent("""\ |
| 1722 |
Subject: this is a |
| 1723 |
short header that |
| 1724 |
will be folded |
| 1725 |
anyway |
| 1726 |
""")) |
| 1727 |
|
| 1728 |
def test_fold_unstructured_single_word(self): |
| 1729 |
h = self.make_header('Subject', 'test') |
| 1730 |
self.assertEqual(h.fold(policy=policy.default), 'Subject: test\n') |
| 1731 |
|
| 1732 |
def test_fold_unstructured_short(self): |
| 1733 |
h = self.make_header('Subject', 'test test test') |
| 1734 |
self.assertEqual(h.fold(policy=policy.default), |
| 1735 |
'Subject: test test test\n') |
| 1736 |
|
| 1737 |
def test_fold_unstructured_with_overlong_word(self): |
| 1738 |
h = self.make_header('Subject', 'thisisaverylonglineconsistingofa' |
| 1739 |
'singlewordthatwontfit') |
| 1740 |
self.assertEqual( |
| 1741 |
h.fold(policy=policy.default.clone(max_line_length=20)), |
| 1742 |
'Subject:\n' |
| 1743 |
' =?utf-8?q?thisisa?=\n' |
| 1744 |
' =?utf-8?q?verylon?=\n' |
| 1745 |
' =?utf-8?q?glineco?=\n' |
| 1746 |
' =?utf-8?q?nsistin?=\n' |
| 1747 |
' =?utf-8?q?gofasin?=\n' |
| 1748 |
' =?utf-8?q?gleword?=\n' |
| 1749 |
' =?utf-8?q?thatwon?=\n' |
| 1750 |
' =?utf-8?q?tfit?=\n' |
| 1751 |
) |
| 1752 |
|
| 1753 |
def test_fold_unstructured_with_two_overlong_words(self): |
| 1754 |
h = self.make_header('Subject', 'thisisaverylonglineconsistingofa' |
| 1755 |
'singlewordthatwontfit plusanotherverylongwordthatwontfit') |
| 1756 |
self.assertEqual( |
| 1757 |
h.fold(policy=policy.default.clone(max_line_length=20)), |
| 1758 |
'Subject:\n' |
| 1759 |
' =?utf-8?q?thisisa?=\n' |
| 1760 |
' =?utf-8?q?verylon?=\n' |
| 1761 |
' =?utf-8?q?glineco?=\n' |
| 1762 |
' =?utf-8?q?nsistin?=\n' |
| 1763 |
' =?utf-8?q?gofasin?=\n' |
| 1764 |
' =?utf-8?q?gleword?=\n' |
| 1765 |
' =?utf-8?q?thatwon?=\n' |
| 1766 |
' =?utf-8?q?tfit_pl?=\n' |
| 1767 |
' =?utf-8?q?usanoth?=\n' |
| 1768 |
' =?utf-8?q?erveryl?=\n' |
| 1769 |
' =?utf-8?q?ongword?=\n' |
| 1770 |
' =?utf-8?q?thatwon?=\n' |
| 1771 |
' =?utf-8?q?tfit?=\n' |
| 1772 |
) |
| 1773 |
|
| 1774 |
# XXX Need test for when max_line_length is less than the chrome size. |
| 1775 |
|
| 1776 |
def test_fold_unstructured_with_slightly_long_word(self): |
| 1777 |
h = self.make_header('Subject', 'thislongwordislessthanmaxlinelen') |
| 1778 |
self.assertEqual( |
| 1779 |
h.fold(policy=policy.default.clone(max_line_length=35)), |
| 1780 |
'Subject:\n thislongwordislessthanmaxlinelen\n') |
| 1781 |
|
| 1782 |
def test_fold_unstructured_with_commas(self): |
| 1783 |
# The old wrapper would fold this at the commas. |
| 1784 |
h = self.make_header('Subject', "This header is intended to " |
| 1785 |
"demonstrate, in a fairly succinct way, that we now do " |
| 1786 |
"not give a , special treatment in unstructured headers.") |
| 1787 |
self.assertEqual( |
| 1788 |
h.fold(policy=policy.default.clone(max_line_length=60)), |
| 1789 |
textwrap.dedent("""\ |
| 1790 |
Subject: This header is intended to demonstrate, in a fairly |
| 1791 |
succinct way, that we now do not give a , special treatment |
| 1792 |
in unstructured headers. |
| 1793 |
""")) |
| 1794 |
|
| 1795 |
def test_fold_address_list(self): |
| 1796 |
h = self.make_header('To', '"Theodore H. Perfect" <[email protected]>, ' |
| 1797 |
'"My address is very long because my name is long" <[email protected]>, ' |
| 1798 |
'"Only A. Friend" <[email protected]>') |
| 1799 |
self.assertEqual(h.fold(policy=policy.default), textwrap.dedent("""\ |
| 1800 |
To: "Theodore H. Perfect" <[email protected]>, |
| 1801 |
"My address is very long because my name is long" <[email protected]>, |
| 1802 |
"Only A. Friend" <[email protected]> |
| 1803 |
""")) |
| 1804 |
|
| 1805 |
def test_fold_date_header(self): |
| 1806 |
h = self.make_header('Date', 'Sat, 2 Feb 2002 17:00:06 -0800') |
| 1807 |
self.assertEqual(h.fold(policy=policy.default), |
| 1808 |
'Date: Sat, 02 Feb 2002 17:00:06 -0800\n') |
| 1809 |
|
| 1810 |
def test_fold_overlong_words_using_RFC2047(self): |
| 1811 |
h = self.make_header( |
| 1812 |
'X-Report-Abuse', |
| 1813 |
'<https://www.mailitapp.com/report_abuse.php?' |
| 1814 |
'mid=xxx-xxx-xxxxxxxxxxxxxxxxxxxxxxxx==-xxx-xx-xx>') |
| 1815 |
self.assertEqual( |
| 1816 |
h.fold(policy=policy.default), |
| 1817 |
'X-Report-Abuse: =?utf-8?q?=3Chttps=3A//www=2Emailitapp=2E' |
| 1818 |
'com/report=5Fabuse?=\n' |
| 1819 |
' =?utf-8?q?=2Ephp=3Fmid=3Dxxx-xxx-xxxx' |
| 1820 |
'xxxxxxxxxxxxxxxxxxxx=3D=3D-xxx-xx-xx?=\n' |
| 1821 |
' =?utf-8?q?=3E?=\n') |
| 1822 |
|
| 1823 |
def test_message_id_header_is_not_folded(self): |
| 1824 |
h = self.make_header( |
| 1825 |
'Message-ID', |
| 1826 |
'<[email protected]>') |
| 1827 |
self.assertEqual( |
| 1828 |
h.fold(policy=policy.default.clone(max_line_length=20)), |
| 1829 |
'Message-ID: <[email protected]>\n') |
| 1830 |
|
| 1831 |
# Test message-id isn't folded when id-right is no-fold-literal. |
| 1832 |
h = self.make_header( |
| 1833 |
'Message-ID', |
| 1834 |
'<somemessageidlongerthan@[127.0.0.0.0.0.0.0.0.1]>') |
| 1835 |
self.assertEqual( |
| 1836 |
h.fold(policy=policy.default.clone(max_line_length=20)), |
| 1837 |
'Message-ID: <somemessageidlongerthan@[127.0.0.0.0.0.0.0.0.1]>\n') |
| 1838 |
|
| 1839 |
# Test message-id isn't folded when id-right is non-ascii characters. |
| 1840 |
h = self.make_header('Message-ID', '<ईमेल@wők.com>') |
| 1841 |
self.assertEqual( |
| 1842 |
h.fold(policy=policy.default.clone(max_line_length=30)), |
| 1843 |
'Message-ID: <ईमेल@wők.com>\n') |
| 1844 |
|
| 1845 |
# Test message-id is folded without breaking the msg-id token into |
| 1846 |
# encoded words, *even* if they don't fit into max_line_length. |
| 1847 |
h = self.make_header('Message-ID', '<ईमेलfromMessage@wők.com>') |
| 1848 |
self.assertEqual( |
| 1849 |
h.fold(policy=policy.default.clone(max_line_length=20)), |
| 1850 |
'Message-ID:\n <ईमेलfromMessage@wők.com>\n') |
| 1851 |
|
| 1852 |
def test_fold_references(self): |
| 1853 |
h = self.make_header( |
| 1854 |
'References', |
| 1855 |
'<[email protected]> ' |
| 1856 |
'<[email protected]>' |
| 1857 |
) |
| 1858 |
self.assertEqual( |
| 1859 |
h.fold(policy=policy.default.clone(max_line_length=20)), |
| 1860 |
'References: ' |
| 1861 |
'<[email protected]>\n' |
| 1862 |
' <[email protected]>\n') |
| 1863 |
|
| 1864 |
|
| 1865 |
if __name__ == '__main__': |
| 1866 |
unittest.main() |