def test_headers_round_trip(): # round trip the headers to make sure they convert reliably back and forth for header in BAD_HEADERS: original = encoding.header_from_mime_encoding(header) assert original assert "=?" not in original and "?=" not in original, "Didn't decode: %r" % (encoding.SCANNER.scan(header),) encoded = encoding.header_to_mime_encoding(original) assert encoded return_original = encoding.header_from_mime_encoding(encoded) assert_equal(original, return_original) return_encoded = encoding.header_to_mime_encoding(return_original) assert_equal(encoded, return_encoded)
def test_headers_round_trip(self): # round trip the headers to make sure they convert reliably back and forth for header in BAD_HEADERS: original = encoding.header_from_mime_encoding(header) assert original assert "=?" not in original and "?=" not in original, "Didn't decode: %r" % header encoded = encoding.header_to_mime_encoding(original) assert encoded return_original = encoding.header_from_mime_encoding(encoded) self.assertEqual(original, return_original) return_encoded = encoding.header_to_mime_encoding(return_original) self.assertEqual(encoded, return_encoded)
def test_odd_roundtrip_bug(): decoded_addrs=[u'"\u0414\u0435\u043b\u043e\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u043e" <*****@*****.**>', u'"\u8003\u53d6\u5206\u4eab" <*****@*****.**>', u'"Exquisite Replica"\n\t<*****@*****.**>',] for decoded in decoded_addrs: encoded = encoding.header_to_mime_encoding(decoded) assert '<' in encoded and '"' in encoded, "Address wasn't encoded correctly:\n%s" % encoded
def test_odd_roundtrip_bug(self): decoded_addrs = [ '"\u0414\u0435\u043b\u043e\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u043e" <*****@*****.**>', # noqa: E501 '"\u8003\u53d6\u5206\u4eab" <*****@*****.**>', '"Exquisite Replica"\n\t<*****@*****.**>', ] for decoded in decoded_addrs: encoded = encoding.header_to_mime_encoding(decoded) assert '<' in encoded and '"' in encoded, "Address wasn't encoded correctly:\n%s" % encoded
def to_message(mail): """ Given a MailBase message, this will construct a MIMEPart that is canonicalized for use with the Python email API. """ ctype, params = mail.content_encoding['Content-Type'] if not ctype: if mail.parts: ctype = 'multipart/mixed' else: ctype = 'text/plain' else: if mail.parts: assert ctype.startswith("multipart") or ctype.startswith( "message" ), "Content type should be multipart or message, not %r" % ctype # adjust the content type according to what it should be now mail.content_encoding['Content-Type'] = (ctype, params) try: out = MurmurMIMEPart(ctype, **params) except TypeError as exc: raise EncodingError( "Content-Type malformed, not allowed: %r; %r (Python ERROR: %s" % (ctype, params, exc.message)) for k in mail.keys(): if k in ADDRESS_HEADERS_WHITELIST: out[k.encode('ascii')] = header_to_mime_encoding(mail[k]) else: out[k.encode('ascii')] = header_to_mime_encoding(mail[k], not_email=True) out.extract_payload(mail) # go through the children for part in mail.parts: out.attach(to_message(part)) return out
def test_header_to_mime_encoding(): for i, header in enumerate(DECODED_HEADERS): assert_equal(NORMALIZED_HEADERS[i], encoding.header_to_mime_encoding(header))
'"=?windows-1251?B?RXhxdWlzaXRlIFJlcGxpY2E=?="\n\t<*****@*****.**>', '=?iso-2022-jp?B?Zmlicm91c19mYXZvcmF0ZUB5YWhvby5jby5qcA==?=<*****@*****.**>', '=?windows-1252?Q?Global_Leadership_in_HandCare_-_Consumer,\n\t_Professional_and_Industrial_Products_OTC_:_FLKI?=', '=?windows-1252?q?Global_Leadership_in_Handcare_-_Consumer, _Auto,\n\t_Professional_&_Industrial_Products_-_OTC_:_FLKI?=', 'I am just normal.', '=?koi8-r?B?WW91ciBtYW6ScyBzdGFtaW5hIHdpbGwgY29tZSBiYWNrIHRvIHlvdSBs?=\n\t=?koi8-r?B?aWtlIGEgYm9vbWVyYW5nLg==?=', '=?koi8-r?B?WW91IGNhbiBiZSBvbiB0b3AgaW4gYmVkcm9vbSBhZ2FpbiCWIGp1c3Qg?=\n\t=?koi8-r?B?YXNrIHVzIGZvciBhZHZpY2Uu?=', '"=?koi8-r?B?5MXMz9DSz8na18/E09TXzw==?=" <*****@*****.**>', '=?utf-8?b?IumrlOiCsuWckuWNgOermSDihpIg6ZW35bqa6Yar6Zmi56uZIOKGkiDmlofljJbk?=\n =?utf-8?b?uInot6/nq5kiIDx2Z3hkcmp5Y2lAZG5zLmh0Lm5ldC50dz4=?=', '=?iso-8859-1?B?SOlhdnkgTel05WwgVW7uY/hk?=\n\t=?iso-8859-1?Q?=E9?=', ] DECODED_HEADERS = encoding.header_from_mime_encoding(BAD_HEADERS) NORMALIZED_HEADERS = [encoding.header_to_mime_encoding(x) for x in DECODED_HEADERS] def test_HeaderDict(): items = [("Subject","Test"), ("Subject","Second subject!"), ("From","moggers@localhost"), ("To","tsyesika@remotehost")] #__init__ headers = encoding.HeaderDict(items) #__getitem__ assert_equal(headers["From"], "moggers@localhost") assert_is_none(headers["Cheese"]) assert_equal(len(headers.get_all("Subject")), 2) #__contains__ contains = "Subject" in headers not_contains = "Cheese" in headers
def test_header_to_mime_encoding(self): for i, header in enumerate(DECODED_HEADERS): self.assertEqual(NORMALIZED_HEADERS[i], encoding.header_to_mime_encoding(header))
'"=?windows-1251?B?RXhxdWlzaXRlIFJlcGxpY2E=?="\n\t<*****@*****.**>', '=?iso-2022-jp?B?Zmlicm91c19mYXZvcmF0ZUB5YWhvby5jby5qcA==?=<*****@*****.**>', '=?windows-1252?Q?Global_Leadership_in_HandCare_-_Consumer,\n\t_Professional_and_Industrial_Products_OTC_:_FLKI?=', '=?windows-1252?q?Global_Leadership_in_Handcare_-_Consumer, _Auto,\n\t_Professional_&_Industrial_Products_-_OTC_:_FLKI?=', # noqa 'I am just normal.', '=?koi8-r?B?WW91ciBtYW6ScyBzdGFtaW5hIHdpbGwgY29tZSBiYWNrIHRvIHlvdSBs?=\n\t=?koi8-r?B?aWtlIGEgYm9vbWVyYW5nLg==?=', '=?koi8-r?B?WW91IGNhbiBiZSBvbiB0b3AgaW4gYmVkcm9vbSBhZ2FpbiCWIGp1c3Qg?=\n\t=?koi8-r?B?YXNrIHVzIGZvciBhZHZpY2Uu?=', '"=?koi8-r?B?5MXMz9DSz8na18/E09TXzw==?=" <*****@*****.**>', '=?utf-8?b?IumrlOiCsuWckuWNgOermSDihpIg6ZW35bqa6Yar6Zmi56uZIOKGkiDmlofljJbk?=\n =?utf-8?b?uInot6/nq5kiIDx2Z3hkcmp5Y2lAZG5zLmh0Lm5ldC50dz4=?=', # noqa: E501 '=?iso-8859-1?B?SOlhdnkgTel05WwgVW7uY/hk?=\n\t=?iso-8859-1?Q?=E9?=', ] DECODED_HEADERS = encoding.header_from_mime_encoding(BAD_HEADERS) NORMALIZED_HEADERS = [ encoding.header_to_mime_encoding(x) for x in DECODED_HEADERS ] class EncodingTestCase(SalmonTestCase): def test_MailBase(self): the_subject = 'p\xf6stal' m = encoding.MailBase() m['To'] = "testing@localhost" m['Subject'] = the_subject m['Content-Type'] = 'text/plain; charset=iso-8859-1' m['MIME-Version'] = '1.0' self.assertEqual(m['To'], "testing@localhost") self.assertEqual(m['TO'], m['To'])
'"=?windows-1251?B?RXhxdWlzaXRlIFJlcGxpY2E=?="\n\t<*****@*****.**>', '=?iso-2022-jp?B?Zmlicm91c19mYXZvcmF0ZUB5YWhvby5jby5qcA==?=<*****@*****.**>', '=?windows-1252?Q?Global_Leadership_in_HandCare_-_Consumer,\n\t_Professional_and_Industrial_Products_OTC_:_FLKI?=', '=?windows-1252?q?Global_Leadership_in_Handcare_-_Consumer, _Auto,\n\t_Professional_&_Industrial_Products_-_OTC_:_FLKI?=', 'I am just normal.', '=?koi8-r?B?WW91ciBtYW6ScyBzdGFtaW5hIHdpbGwgY29tZSBiYWNrIHRvIHlvdSBs?=\n\t=?koi8-r?B?aWtlIGEgYm9vbWVyYW5nLg==?=', '=?koi8-r?B?WW91IGNhbiBiZSBvbiB0b3AgaW4gYmVkcm9vbSBhZ2FpbiCWIGp1c3Qg?=\n\t=?koi8-r?B?YXNrIHVzIGZvciBhZHZpY2Uu?=', '"=?koi8-r?B?5MXMz9DSz8na18/E09TXzw==?=" <*****@*****.**>', '=?utf-8?b?IumrlOiCsuWckuWNgOermSDihpIg6ZW35bqa6Yar6Zmi56uZIOKGkiDmlofljJbk?=\n =?utf-8?b?uInot6/nq5kiIDx2Z3hkcmp5Y2lAZG5zLmh0Lm5ldC50dz4=?=', '=?iso-8859-1?B?SOlhdnkgTel05WwgVW7uY/hk?=\n\t=?iso-8859-1?Q?=E9?=', ] DECODED_HEADERS = encoding.header_from_mime_encoding(BAD_HEADERS) NORMALIZED_HEADERS = [encoding.header_to_mime_encoding(x) for x in DECODED_HEADERS] def test_MailBase(): the_subject = u'p\xf6stal' m = encoding.MailBase() m['To'] = "testing@localhost" m['Subject'] = the_subject m['Content-Type'] = 'text/plain; charset=iso-8859-1' m['MIME-Version'] = '1.0' assert m['To'] == "testing@localhost" assert m['TO'] == m['To'] assert m['to'] == m['To']