Ticket #8215: 8215.diff

File 8215.diff, 2.2 KB (added by SamyCookie, 3 years ago)
  • new file twisted/topfiles/8215.bugfix

    diff --git a/twisted/topfiles/8215.bugfix b/twisted/topfiles/8215.bugfix
    new file mode 100644
    index 0000000..0632b96
    - +  
     1Ensure to generate Session.uid as 'bytes' type with both Python 2/3
  • twisted/web/server.py

    diff --git a/twisted/web/server.py b/twisted/web/server.py
    index 2ec4d58..ece7d05 100644
    a b def _mkuid(self): 
    664664        """
    665665        (internal) Generate an opaque, unique ID for a user's session.
    666666        """
     667        from binascii import hexlify
    667668        from hashlib import md5
    668669        import random
    669670        self.counter = self.counter + 1
    670         return md5(networkString(
     671        return hexlify(md5(networkString(
    671672                "%s_%s" % (str(random.random()), str(self.counter)))
    672                    ).hexdigest()
     673                   ).digest())
    673674
    674675
    675676    def makeSession(self):
  • twisted/web/test/test_web.py

    diff --git a/twisted/web/test/test_web.py b/twisted/web/test/test_web.py
    index 0048d55..4fdf73d 100644
    a b def test_buildProtocol(self): 
    108108        self.assertIs(site.requestFactory, channel.requestFactory)
    109109
    110110
     111    def test_makeSession(self):
     112        """
     113        C{Site} generate a new C{Session} instance.
     114        The C{Session} uid type should be consistent with documentation, e.g.
     115        ${bytes}
     116        """
     117        site = server.Site(resource.Resource())
     118
     119        session = site.makeSession()
     120        self.assertIsInstance(session, server.Session)
     121        self.assertIsInstance(session.uid, bytes)
     122       
     123        session.expire()  # avoid delayed calls lingering after test exit
     124
     125
     126    def test_getSession(self):
     127        """
     128        Get a previously generated session, by its unique ID.
     129        This raises a KeyError if the session is not found.
     130        """
     131        site = server.Site(resource.Resource())
     132        session = site.makeSession()
     133
     134        session = site.getSession(session.uid)
     135        self.assertIsInstance(session, server.Session)
     136
     137        session.expire()
     138        self.assertRaises(KeyError, site.getSession, session.uid)
     139
    111140
    112141class SessionTests(unittest.TestCase):
    113142    """