All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lib-python.2.7.test.test_rfc822.py Maven / Gradle / Ivy

Go to download

Jython is an implementation of the high-level, dynamic, object-oriented language Python written in 100% Pure Java, and seamlessly integrated with the Java platform. It thus allows you to run Python on any Java platform.

There is a newer version: 2.7.4
Show newest version
import unittest
from test import test_support

rfc822 = test_support.import_module("rfc822", deprecated=True)

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO


class MessageTestCase(unittest.TestCase):
    def create_message(self, msg):
        return rfc822.Message(StringIO(msg))

    def test_get(self):
        msg = self.create_message(
            'To: "last, first" \n\ntest\n')
        self.assertTrue(msg.get("to") == '"last, first" ')
        self.assertTrue(msg.get("TO") == '"last, first" ')
        self.assertTrue(msg.get("No-Such-Header") is None)
        self.assertTrue(msg.get("No-Such-Header", "No-Such-Value")
                     == "No-Such-Value")

    def test_setdefault(self):
        msg = self.create_message(
            'To: "last, first" \n\ntest\n')
        self.assertTrue(not msg.has_key("New-Header"))
        self.assertTrue(msg.setdefault("New-Header", "New-Value") == "New-Value")
        self.assertTrue(msg.setdefault("New-Header", "Different-Value")
                     == "New-Value")
        self.assertTrue(msg["new-header"] == "New-Value")

        self.assertTrue(msg.setdefault("Another-Header") == "")
        self.assertTrue(msg["another-header"] == "")

    def check(self, msg, results):
        """Check addresses and the date."""
        m = self.create_message(msg)
        i = 0
        for n, a in m.getaddrlist('to') + m.getaddrlist('cc'):
            try:
                mn, ma = results[i][0], results[i][1]
            except IndexError:
                print 'extra parsed address:', repr(n), repr(a)
                continue
            i = i + 1
            self.assertEqual(mn, n,
                             "Un-expected name: %r != %r" % (mn, n))
            self.assertEqual(ma, a,
                             "Un-expected address: %r != %r" % (ma, a))
            if mn == n and ma == a:
                pass
            else:
                print 'not found:', repr(n), repr(a)

        out = m.getdate('date')
        if out:
            self.assertEqual(out,
                             (1999, 1, 13, 23, 57, 35, 0, 1, 0),
                             "date conversion failed")


    # Note: all test cases must have the same date (in various formats),
    # or no date!

    def test_basic(self):
        self.check(
            'Date:    Wed, 13 Jan 1999 23:57:35 -0500\n'
            'From:    Guido van Rossum \n'
            'To:      "Guido van\n'
            '\t : Rossum" \n'
            'Subject: test2\n'
            '\n'
            'test2\n',
            [('Guido van\n\t : Rossum', '[email protected]')])

        self.check(
            'From: Barry \n'
            'Date:  13-Jan-1999 23:57:35 EST\n'
            '\n'
            'test',
            [('Guido: the Barbarian', '[email protected]'),
             ('Guido: the Madman', '[email protected]')
             ])

        self.check(
            'To: "The monster with\n'
            '     the very long name: Guido" \n'
            'Date:    Wed, 13 Jan 1999 23:57:35 -0500\n'
            '\n'
            'test',
            [('The monster with\n     the very long name: Guido',
              '[email protected]')])

        self.check(
            'To: "Amit J. Patel" \n'
            'CC: Mike Fletcher ,\n'
            '        "\'[email protected]\'" \n'
            'Cc: [email protected], [email protected]\n'
            'Cc: [email protected]\n'
            'Date:    Wed, 13 Jan 1999 23:57:35 -0500\n'
            '\n'
            'test',
            [('Amit J. Patel', '[email protected]'),
             ('Mike Fletcher', '[email protected]'),
             ("'[email protected]'", '[email protected]'),
             ('', '[email protected]'),
             ('', '[email protected]'),
             ('', '[email protected]'),
             ])

        self.check(
            'To: Some One \n'
            'From: Anudder Persin \n'
            'Date:\n'
            '\n'
            'test',
            [('Some One', '[email protected]')])

        self.check(
            'To: [email protected] (User J. Person)\n\n',
            [('User J. Person', '[email protected]')])

    def test_doublecomment(self):
        # The RFC allows comments within comments in an email addr
        self.check(
            'To: [email protected] ((User J. Person)), John Doe \n\n',
            [('User J. Person', '[email protected]'), ('John Doe', '[email protected]')])

    def test_twisted(self):
        # This one is just twisted.  I don't know what the proper
        # result should be, but it shouldn't be to infloop, which is
        # what used to happen!
        self.check(
            'To: <[smtp:[email protected]][email protected]>\n'
            'Date:    Wed, 13 Jan 1999 23:57:35 -0500\n'
            '\n'
            'test',
            [('', ''),
             ('', '[email protected]'),
             ('', '[email protected]'),
             ])

    def test_commas_in_full_name(self):
        # This exercises the old commas-in-a-full-name bug, which
        # should be doing the right thing in recent versions of the
        # module.
        self.check(
            'To: "last, first" \n'
            '\n'
            'test',
            [('last, first', '[email protected]')])

    def test_quoted_name(self):
        self.check(
            'To: (Comment stuff) "Quoted name"@somewhere.com\n'
            '\n'
            'test',
            [('Comment stuff', '"Quoted name"@somewhere.com')])

    def test_bogus_to_header(self):
        self.check(
            'To: :\n'
            'Cc: [email protected]\n'
            'Date:    Wed, 13 Jan 1999 23:57:35 -0500\n'
            '\n'
            'test',
            [('', '[email protected]')])

    def test_addr_ipquad(self):
        self.check(
            'To: guido@[132.151.1.21]\n'
            '\n'
            'foo',
            [('', 'guido@[132.151.1.21]')])

    def test_iter(self):
        m = rfc822.Message(StringIO(
            'Date:    Wed, 13 Jan 1999 23:57:35 -0500\n'
            'From:    Guido van Rossum \n'
            'To:      "Guido van\n'
            '\t : Rossum" \n'
            'Subject: test2\n'
            '\n'
            'test2\n' ))
        self.assertEqual(sorted(m), ['date', 'from', 'subject', 'to'])

    def test_rfc2822_phrases(self):
        # RFC 2822 (the update to RFC 822) specifies that dots in phrases are
        # obsolete syntax, which conforming programs MUST recognize but NEVER
        # generate (see $4.1 Miscellaneous obsolete tokens).  This is a
        # departure from RFC 822 which did not allow dots in non-quoted
        # phrases.
        self.check('To: User J. Person \n\n',
                   [('User J. Person', '[email protected]')])

    # This takes too long to add to the test suite
##    def test_an_excrutiatingly_long_address_field(self):
##        OBSCENELY_LONG_HEADER_MULTIPLIER = 10000
##        oneaddr = ('Person' * 10) + '@' + ('.'.join(['dom']*10)) + '.com'
##        addr = ', '.join([oneaddr] * OBSCENELY_LONG_HEADER_MULTIPLIER)
##        lst = rfc822.AddrlistClass(addr).getaddrlist()
##        self.assertEqual(len(lst), OBSCENELY_LONG_HEADER_MULTIPLIER)

    def test_2getaddrlist(self):
        eq = self.assertEqual
        msg = self.create_message("""\
To: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

A test message.
""")
        ccs = [('', a) for a in
               ['[email protected]', '[email protected]', '[email protected]']]
        addrs = msg.getaddrlist('cc')
        addrs.sort()
        eq(addrs, ccs)
        # Try again, this one used to fail
        addrs = msg.getaddrlist('cc')
        addrs.sort()
        eq(addrs, ccs)

    def test_parseaddr(self):
        eq = self.assertEqual
        eq(rfc822.parseaddr('<>'), ('', ''))
        eq(rfc822.parseaddr('[email protected]'), ('', '[email protected]'))
        eq(rfc822.parseaddr('[email protected] (Bea A. Person)'),
           ('Bea A. Person', '[email protected]'))
        eq(rfc822.parseaddr('Cynthia Person '),
           ('Cynthia Person', '[email protected]'))

    def test_quote_unquote(self):
        eq = self.assertEqual
        eq(rfc822.quote('foo\\wacky"name'), 'foo\\\\wacky\\"name')
        eq(rfc822.unquote('"foo\\\\wacky\\"name"'), 'foo\\wacky"name')


def test_main():
    test_support.run_unittest(MessageTestCase)


if __name__ == "__main__":
    test_main()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy