我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用email.test()。
def test_split_long_continuation(self): eq = self.ndiffAssertEqual msg = email.message_from_string("""\ Subject: bug demonstration \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 \tmore text test """) sfp = StringIO() g = Generator(sfp) g.flatten(msg) eq(sfp.getvalue(), """\ Subject: bug demonstration \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 \tmore text test """)
def test_mime_attachments_in_constructor(self): eq = self.assertEqual text1 = MIMEText('') text2 = MIMEText('') msg = MIMEMultipart(_subparts=(text1, text2)) eq(len(msg.get_payload()), 2) eq(msg.get_payload(0), text1) eq(msg.get_payload(1), text2) # A general test of parser->model->generator idempotency. IOW, read a message # in, parse it into a message object tree, then without touching the tree, # regenerate the plain text. The original text and the transformed text # should be identical. Note: that we ignore the Unix-From since that may # contain a changed date.
def test_split_long_continuation(self): eq = self.ndiffAssertEqual msg = email.message_from_string("""\ Subject: bug demonstration \t12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 \tmore text test """) sfp = StringIO() g = Generator(sfp) g.flatten(msg) eq(sfp.getvalue(), """\ Subject: bug demonstration 12345678911234567892123456789312345678941234567895123456789612345678971234567898112345678911234567892123456789112345678911234567892123456789 more text test """)
def test_parseaddr_preserves_quoted_pairs_in_addresses(self): # issue 10005. Note that in the third test the second pair of # backslashes is not actually a quoted pair because it is not inside a # comment or quoted string: the address being parsed has a quoted # string containing a quoted backslash, followed by 'example' and two # backslashes, followed by another quoted string containing a space and # the word 'example'. parseaddr copies those two backslashes # literally. Per rfc5322 this is not technically correct since a \ may # not appear in an address outside of a quoted string. It is probably # a sensible Postel interpretation, though. eq = self.assertEqual eq(Utils.parseaddr('""example" example"@example.com'), ('', '""example" example"@example.com')) eq(Utils.parseaddr('"\\"example\\" example"@example.com'), ('', '"\\"example\\" example"@example.com')) eq(Utils.parseaddr('"\\\\"example\\\\" example"@example.com'), ('', '"\\\\"example\\\\" example"@example.com'))
def test_parseaddr_preserves_quoted_pairs_in_addresses(self): # issue 10005. Note that in the third test the second pair of # backslashes is not actually a quoted pair because it is not inside a # comment or quoted string: the address being parsed has a quoted # string containing a quoted backslash, followed by 'example' and two # backslashes, followed by another quoted string containing a space and # the word 'example'. parseaddr copies those two backslashes # literally. Per rfc5322 this is not technically correct since a \ may # not appear in an address outside of a quoted string. It is probably # a sensible Postel interpretation, though. eq = self.assertEqual eq(utils.parseaddr('""example" example"@example.com'), ('', '""example" example"@example.com')) eq(utils.parseaddr('"\\"example\\" example"@example.com'), ('', '"\\"example\\" example"@example.com')) eq(utils.parseaddr('"\\\\"example\\\\" example"@example.com'), ('', '"\\\\"example\\\\" example"@example.com'))
def test_8bit_in_quopri_body(self): # This is non-RFC compliant data...without 'decode' the library code # decodes the body using the charset from the headers, and because the # source byte really is utf-8 this works. This is likely to fail # against real dirty data (ie: produce mojibake), but the data is # invalid anyway so it is as good a guess as any. But this means that # this test just confirms the current behavior; that behavior is not # necessarily the best possible behavior. With 'decode' it is # returning the raw bytes, so that test should be of correct behavior, # or at least produce the same result that email4 did. m = self.bodytest_msg.format(charset='utf-8', cte='quoted-printable', bodyline='p=C3=B6stál').encode('utf-8') msg = email.message_from_bytes(m) self.assertEqual(msg.get_payload(), 'p=C3=B6stál\n') self.assertEqual(msg.get_payload(decode=True), 'pöstál\n'.encode('utf-8'))
def test_explicit_maxlinelen(self): eq = self.ndiffAssertEqual hstr = ('A very long line that must get split to something other ' 'than at the 76th character boundary to test the non-default ' 'behavior') h = Header(hstr) eq(h.encode(), '''\ A very long line that must get split to something other than at the 76th character boundary to test the non-default behavior''') eq(str(h), hstr) h = Header(hstr, header_name='Subject') eq(h.encode(), '''\ A very long line that must get split to something other than at the 76th character boundary to test the non-default behavior''') eq(str(h), hstr) h = Header(hstr, maxlinelen=1024, header_name='Subject') eq(h.encode(), hstr) eq(str(h), hstr)
def openfile(filename, mode='r'): path = os.path.join(os.path.dirname(landmark), 'data', filename) return open(path, mode) # Base test class
def setUp(self): # Make sure we pick up the audiotest.au that lives in email/test/data. # In Python, there's an audiotest.au living in Lib/test but that isn't # included in some binary distros that don't include the test # package. The trailing empty string on the .join() is significant # since findfile() will do a dirname(). datadir = os.path.join(os.path.dirname(landmark), 'data', '') fp = open(findfile('audiotest.au', datadir), 'rb') try: self._audiodata = fp.read() finally: fp.close() self._au = MIMEAudio(self._audiodata)
def test_message_from_string(self): fp = openfile('msg_01.txt') try: text = fp.read() finally: fp.close() msg = email.message_from_string(text) s = StringIO() # Don't wrap/continue long headers since we're trying to test # idempotency. g = Generator(s, maxheaderlen=0) g.flatten(msg) self.assertEqual(text, s.getvalue())
def test_message_from_file(self): fp = openfile('msg_01.txt') try: text = fp.read() fp.seek(0) msg = email.message_from_file(fp) s = StringIO() # Don't wrap/continue long headers since we're trying to test # idempotency. g = Generator(s, maxheaderlen=0) g.flatten(msg) self.assertEqual(text, s.getvalue()) finally: fp.close()
def test_idempotent(self): eq = self.assertEqual # Make sure us-ascii = no Unicode conversion c = Charset('us-ascii') s = 'Hello World!' sp = c.to_splittable(s) eq(s, c.from_splittable(sp)) # test 8-bit idempotency with us-ascii s = '\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa' sp = c.to_splittable(s) eq(s, c.from_splittable(sp))
def test_explicit_maxlinelen(self): eq = self.ndiffAssertEqual hstr = 'A very long line that must get split to something other than at the 76th character boundary to test the non-default behavior' h = Header(hstr) eq(h.encode(), '''\ A very long line that must get split to something other than at the 76th character boundary to test the non-default behavior''') h = Header(hstr, header_name='Subject') eq(h.encode(), '''\ A very long line that must get split to something other than at the 76th character boundary to test the non-default behavior''') h = Header(hstr, maxlinelen=1024, header_name='Subject') eq(h.encode(), hstr)