test_email.py
来自「mallet是自然语言处理、机器学习领域的一个开源项目。」· Python 代码 · 共 1,770 行 · 第 1/5 页
PY
1,770 行
('report-type', old_val)]) def test_set_type(self): eq = self.assertEqual msg = Message() self.assertRaises(ValueError, msg.set_type, 'text') msg.set_type('text/plain') eq(msg['content-type'], 'text/plain') msg.set_param('charset', 'us-ascii') eq(msg['content-type'], 'text/plain; charset="us-ascii"') msg.set_type('text/html') eq(msg['content-type'], 'text/html; charset="us-ascii"') def test_get_content_type_missing(self): msg = Message() self.assertEqual(msg.get_content_type(), 'text/plain') def test_get_content_type_missing_with_default_type(self): msg = Message() msg.set_default_type('message/rfc822') self.assertEqual(msg.get_content_type(), 'message/rfc822') def test_get_content_type_from_message_implicit(self): msg = self._msgobj('msg_30.txt') self.assertEqual(msg.get_payload(0).get_content_type(), 'message/rfc822') def test_get_content_type_from_message_explicit(self): msg = self._msgobj('msg_28.txt') self.assertEqual(msg.get_payload(0).get_content_type(), 'message/rfc822') def test_get_content_type_from_message_text_plain_implicit(self): msg = self._msgobj('msg_03.txt') self.assertEqual(msg.get_content_type(), 'text/plain') def test_get_content_type_from_message_text_plain_explicit(self): msg = self._msgobj('msg_01.txt') self.assertEqual(msg.get_content_type(), 'text/plain') def test_get_content_maintype_missing(self): msg = Message() self.assertEqual(msg.get_content_maintype(), 'text') def test_get_content_maintype_missing_with_default_type(self): msg = Message() msg.set_default_type('message/rfc822') self.assertEqual(msg.get_content_maintype(), 'message') def test_get_content_maintype_from_message_implicit(self): msg = self._msgobj('msg_30.txt') self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message') def test_get_content_maintype_from_message_explicit(self): msg = self._msgobj('msg_28.txt') self.assertEqual(msg.get_payload(0).get_content_maintype(), 'message') def test_get_content_maintype_from_message_text_plain_implicit(self): msg = self._msgobj('msg_03.txt') self.assertEqual(msg.get_content_maintype(), 'text') def test_get_content_maintype_from_message_text_plain_explicit(self): msg = self._msgobj('msg_01.txt') self.assertEqual(msg.get_content_maintype(), 'text') def test_get_content_subtype_missing(self): msg = Message() self.assertEqual(msg.get_content_subtype(), 'plain') def test_get_content_subtype_missing_with_default_type(self): msg = Message() msg.set_default_type('message/rfc822') self.assertEqual(msg.get_content_subtype(), 'rfc822') def test_get_content_subtype_from_message_implicit(self): msg = self._msgobj('msg_30.txt') self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822') def test_get_content_subtype_from_message_explicit(self): msg = self._msgobj('msg_28.txt') self.assertEqual(msg.get_payload(0).get_content_subtype(), 'rfc822') def test_get_content_subtype_from_message_text_plain_implicit(self): msg = self._msgobj('msg_03.txt') self.assertEqual(msg.get_content_subtype(), 'plain') def test_get_content_subtype_from_message_text_plain_explicit(self): msg = self._msgobj('msg_01.txt') self.assertEqual(msg.get_content_subtype(), 'plain') def test_get_content_maintype_error(self): msg = Message() msg['Content-Type'] = 'no-slash-in-this-string' self.assertEqual(msg.get_content_maintype(), 'text') def test_get_content_subtype_error(self): msg = Message() msg['Content-Type'] = 'no-slash-in-this-string' self.assertEqual(msg.get_content_subtype(), 'plain') def test_replace_header(self): eq = self.assertEqual msg = Message() msg.add_header('First', 'One') msg.add_header('Second', 'Two') msg.add_header('Third', 'Three') eq(msg.keys(), ['First', 'Second', 'Third']) eq(msg.values(), ['One', 'Two', 'Three']) msg.replace_header('Second', 'Twenty') eq(msg.keys(), ['First', 'Second', 'Third']) eq(msg.values(), ['One', 'Twenty', 'Three']) msg.add_header('First', 'Eleven') msg.replace_header('First', 'One Hundred') eq(msg.keys(), ['First', 'Second', 'Third', 'First']) eq(msg.values(), ['One Hundred', 'Twenty', 'Three', 'Eleven']) self.assertRaises(KeyError, msg.replace_header, 'Fourth', 'Missing') def test_broken_base64_payload(self): x = 'AwDp0P7//y6LwKEAcPa/6Q=9' msg = Message() msg['content-type'] = 'audio/x-midi' msg['content-transfer-encoding'] = 'base64' msg.set_payload(x) self.assertEqual(msg.get_payload(decode=True), x)# Test the email.Encoders moduleclass TestEncoders(unittest.TestCase): def test_encode_noop(self): eq = self.assertEqual msg = MIMEText('hello world', _encoder=Encoders.encode_noop) eq(msg.get_payload(), 'hello world') def test_encode_7bit(self): eq = self.assertEqual msg = MIMEText('hello world', _encoder=Encoders.encode_7or8bit) eq(msg.get_payload(), 'hello world') eq(msg['content-transfer-encoding'], '7bit') msg = MIMEText('hello \x7f world', _encoder=Encoders.encode_7or8bit) eq(msg.get_payload(), 'hello \x7f world') eq(msg['content-transfer-encoding'], '7bit') def test_encode_8bit(self): eq = self.assertEqual msg = MIMEText('hello \x80 world', _encoder=Encoders.encode_7or8bit) eq(msg.get_payload(), 'hello \x80 world') eq(msg['content-transfer-encoding'], '8bit') def test_encode_empty_payload(self): eq = self.assertEqual msg = Message() msg.set_charset('us-ascii') eq(msg['content-transfer-encoding'], '7bit') def test_encode_base64(self): eq = self.assertEqual msg = MIMEText('hello world', _encoder=Encoders.encode_base64) eq(msg.get_payload(), 'aGVsbG8gd29ybGQ=') eq(msg['content-transfer-encoding'], 'base64') def test_encode_quoted_printable(self): eq = self.assertEqual msg = MIMEText('hello world', _encoder=Encoders.encode_quopri) eq(msg.get_payload(), 'hello=20world') eq(msg['content-transfer-encoding'], 'quoted-printable') def test_default_cte(self): eq = self.assertEqual msg = MIMEText('hello world') eq(msg['content-transfer-encoding'], '7bit') def test_default_cte(self): eq = self.assertEqual # With no explicit _charset its us-ascii, and all are 7-bit msg = MIMEText('hello world') eq(msg['content-transfer-encoding'], '7bit') # Similar, but with 8-bit data msg = MIMEText('hello \xf8 world') eq(msg['content-transfer-encoding'], '8bit') # And now with a different charset msg = MIMEText('hello \xf8 world', _charset='iso-8859-1') eq(msg['content-transfer-encoding'], 'quoted-printable')# Test long header wrappingclass TestLongHeaders(TestEmailBase): def test_split_long_continuation(self): eq = self.ndiffAssertEqual msg = email.message_from_string("""\Subject: bug demonstration\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789\tmore texttest""") sfp = StringIO() g = Generator(sfp) g.flatten(msg) eq(sfp.getvalue(), """\Subject: bug demonstration\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789\tmore texttest""") def test_another_long_almost_unsplittable_header(self): eq = self.ndiffAssertEqual hstr = """\bug demonstration\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789\tmore text""" h = Header(hstr, continuation_ws='\t') eq(h.encode(), """\bug demonstration\t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789\tmore text""") h = Header(hstr) eq(h.encode(), """\bug demonstration 12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 more text""") def test_long_nonstring(self): eq = self.ndiffAssertEqual g = Charset("iso-8859-1") cz = Charset("iso-8859-2") utf8 = Charset("utf-8") 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. " cz_head = "Finan\xe8ni metropole se hroutily pod tlakem jejich d\xf9vtipu.. " 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") h = Header(g_head, g, header_name='Subject') h.append(cz_head, cz) h.append(utf8_head, utf8) msg = Message() msg['Subject'] = h sfp = StringIO() g = Generator(sfp) g.flatten(msg) eq(sfp.getvalue(), """\Subject: =?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerd?= =?iso-8859-1?q?erband_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndi?= =?iso-8859-1?q?schen_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Kling?= =?iso-8859-1?q?en_bef=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_met?= =?iso-8859-2?q?ropole_se_hroutily_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= =?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE?= =?utf-8?b?44G+44Gb44KT44CC5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB?= =?utf-8?b?44GC44Go44Gv44Gn44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CM?= =?utf-8?q?Wenn_ist_das_Nunstuck_git_und_Slotermeyer=3F_Ja!_Beiherhund_das?= =?utf-8?b?IE9kZXIgZGllIEZsaXBwZXJ3YWxkdCBnZXJzcHV0LuOAjeOBqOiogOOBow==?= =?utf-8?b?44Gm44GE44G+44GZ44CC?=""") eq(h.encode(), """\=?iso-8859-1?q?Die_Mieter_treten_hier_ein_werden_mit_einem_Foerd?= =?iso-8859-1?q?erband_komfortabel_den_Korridor_entlang=2C_an_s=FCdl=FCndi?= =?iso-8859-1?q?schen_Wandgem=E4lden_vorbei=2C_gegen_die_rotierenden_Kling?= =?iso-8859-1?q?en_bef=F6rdert=2E_?= =?iso-8859-2?q?Finan=E8ni_met?= =?iso-8859-2?q?ropole_se_hroutily_pod_tlakem_jejich_d=F9vtipu=2E=2E_?= =?utf-8?b?5q2j56K644Gr6KiA44GG44Go57+76Kiz44Gv44GV44KM44Gm44GE?= =?utf-8?b?44G+44Gb44KT44CC5LiA6YOo44Gv44OJ44Kk44OE6Kqe44Gn44GZ44GM44CB?= =?utf-8?b?44GC44Go44Gv44Gn44Gf44KJ44KB44Gn44GZ44CC5a6f6Zqb44Gr44Gv44CM?= =?utf-8?q?Wenn_ist_das_Nunstuck_git_und_Slotermeyer=3F_Ja!_Beiherhund_das?= =?utf-8?b?IE9kZXIgZGllIEZsaXBwZXJ3YWxkdCBnZXJzcHV0LuOAjeOBqOiogOOBow==?= =?utf-8?b?44Gm44GE44G+44GZ44CC?=""") def test_long_header_encode(self): eq = self.ndiffAssertEqual h = Header('wasnipoop; giraffes="very-long-necked-animals"; ' 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"', header_name='X-Foobar-Spoink-Defrobnit') eq(h.encode(), '''\wasnipoop; giraffes="very-long-necked-animals"; spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') def test_long_header_encode_with_tab_continuation(self): eq = self.ndiffAssertEqual h = Header('wasnipoop; giraffes="very-long-necked-animals"; ' 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"', header_name='X-Foobar-Spoink-Defrobnit', continuation_ws='\t') eq(h.encode(), '''\wasnipoop; giraffes="very-long-necked-animals";\tspooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') def test_header_splitter(self): eq = self.ndiffAssertEqual msg = MIMEText('') # It'd be great if we could use add_header() here, but that doesn't # guarantee an order of the parameters. msg['X-Foobar-Spoink-Defrobnit'] = ( 'wasnipoop; giraffes="very-long-necked-animals"; ' 'spooge="yummy"; hippos="gargantuan"; marshmallows="gooey"') sfp = StringIO() g = Generator(sfp) g.flatten(msg) eq(sfp.getvalue(), '''\Content-Type: text/plain; charset="us-ascii"MIME-Version: 1.0Content-Transfer-Encoding: 7bitX-Foobar-Spoink-Defrobnit: wasnipoop; giraffes="very-long-necked-animals";\tspooge="yummy"; hippos="gargantuan"; marshmallows="gooey"''') def test_no_semis_header_splitter(self): eq = self.ndiffAssertEqual msg = Message() msg['From'] = 'test@dom.ain' msg['References'] = SPACE.join(['<%d@dom.ain>' % i for i in range(10)]) msg.set_payload('Test') sfp = StringIO() g = Generator(sfp) g.flatten(msg) eq(sfp.getvalue(), """\From: test@dom.ainReferences: <0@dom.ain> <1@dom.ain> <2@dom.ain> <3@dom.ain> <4@dom.ain>\t<5@dom.ain> <6@dom.ain> <7@dom.ain> <8@dom.ain> <9@dom.ain>Test""") def test_no_split_long_header(self): eq = self.ndiffAssertEqual hstr = 'References: ' + 'x' * 80 h = Header(hstr, continuation_ws='\t') eq(h.encode(), """\References: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx""") def test_splitting_multiple_long_lines(self): eq = self.ndiffAssertEqual hstr = """\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)\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)\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)""" h = Header(hstr, continuation_ws='\t') eq(h.encode(), """\from babylon.socal-raves.org (localhost [127.0.0.1]);\tby babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;\tfor <mailman-admin@babylon.socal-raves.org>;\tSat, 2 Feb 2002 17:00:06 -0800 (PST)\tfrom babylon.socal-raves.org (localhost [127.0.0.1]);\tby babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;\tfor <mailman-admin@babylon.socal-raves.org>;\tSat, 2 Feb 2002 17:00:06 -0800 (PST)\tfrom babylon.socal-raves.org (localhost [127.0.0.1]);\tby babylon.socal-raves.org (Postfix) with ESMTP id B570E51B81;\tfor <mailman-admin@babylon.socal-raves.org>;\tSat, 2 Feb 2002 17:00:06 -0800 (PST)""") def test_splitting_first_line_only_is_long(self): eq = self.ndiffAssertEqual
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?