def test_header_from_mime_encoding(self): assert not encoding.header_from_mime_encoding(None) self.assertEqual(len(BAD_HEADERS), len(encoding.header_from_mime_encoding(BAD_HEADERS))) for i, header in enumerate(BAD_HEADERS): self.assertEqual(DECODED_HEADERS[i], encoding.header_from_mime_encoding(header))
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_header_from_mime_encoding(): assert not encoding.header_from_mime_encoding(None) assert_equal(len(BAD_HEADERS), len(encoding.header_from_mime_encoding(BAD_HEADERS))) for i, header in enumerate(BAD_HEADERS): assert_equal(DECODED_HEADERS[i], encoding.header_from_mime_encoding(header))
def test_dumb_shit(): # this is a sample of possibly the worst case Mutt can produce idiot = '=?iso-8859-1?B?SOlhdnkgTel05WwgVW7uY/hk?=\n\t=?iso-8859-1?Q?=E9?=' should_be = u'H\xe9avy M\xe9t\xe5l Un\xeec\xf8d\xe9' assert_equal(encoding.header_from_mime_encoding(idiot), should_be)
BAD_HEADERS = [ u'"\u8003\u53d6\u5206\u4eab" <*****@*****.**>'.encode('utf-8'), '"=?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__
def test_mutt_badness(self): # this is a sample of possibly the worst case Mutt can produce idiot = '=?iso-8859-1?B?SOlhdnkgTel05WwgVW7uY/hk?=\n\t=?iso-8859-1?Q?=E9?=' should_be = 'H\xe9avy M\xe9t\xe5l Un\xeec\xf8d\xe9' self.assertEqual(encoding.header_from_mime_encoding(idiot), should_be)
BAD_HEADERS = [ '"\u8003\u53d6\u5206\u4eab" <*****@*****.**>', '"=?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'