r46916 - Apply 8215.diff.

adiroiban-TA+aISz0psMTMxyoc4vAAJOcrHinNvQL0E9HWUfgJXw@public.gmane.org
Newsgroups gmane.comp.python.twisted.commits
Message-ID <[email protected]>
Author: adiroiban
Date: Sat Mar  5 04:08:12 2016
New Revision: 46916

Added:
   branches/http-session-id-type-8215/twisted/topfiles/8215.bugfix
Modified:
   branches/http-session-id-type-8215/twisted/web/server.py
   branches/http-session-id-type-8215/twisted/web/test/test_web.py

Log:
Apply 8215.diff.

Modified: branches/http-session-id-type-8215/twisted/web/server.py
==============================================================================
--- branches/http-session-id-type-8215/twisted/web/server.py	(original)
+++ branches/http-session-id-type-8215/twisted/web/server.py	Sat Mar  5 04:08:12 2016
@@ -664,12 +664,13 @@
         """
         (internal) Generate an opaque, unique ID for a user's session.
         """
+        from binascii import hexlify
         from hashlib import md5
         import random
         self.counter = self.counter + 1
-        return md5(networkString(
+        return hexlify(md5(networkString(
                 "%s_%s" % (str(random.random()), str(self.counter)))
-                   ).hexdigest()
+                   ).digest())
 
 
     def makeSession(self):

Modified: branches/http-session-id-type-8215/twisted/web/test/test_web.py
==============================================================================
--- branches/http-session-id-type-8215/twisted/web/test/test_web.py	(original)
+++ branches/http-session-id-type-8215/twisted/web/test/test_web.py	Sat Mar  5 04:08:12 2016
@@ -108,6 +108,35 @@
         self.assertIs(site.requestFactory, channel.requestFactory)
 
 
+    def test_makeSession(self):
+        """
+        C{Site} generate a new C{Session} instance.
+        The C{Session} uid type should be consistent with documentation, e.g.
+        ${bytes}
+        """
+        site = server.Site(resource.Resource())
+
+        session = site.makeSession()
+        self.assertIsInstance(session, server.Session)
+        self.assertIsInstance(session.uid, bytes)
+        
+        session.expire()  # avoid delayed calls lingering after test exit
+
+
+    def test_getSession(self):
+        """
+        Get a previously generated session, by its unique ID.
+        This raises a KeyError if the session is not found.
+        """
+        site = server.Site(resource.Resource())
+        session = site.makeSession()
+
+        session = site.getSession(session.uid)
+        self.assertIsInstance(session, server.Session)
+
+        session.expire()
+        self.assertRaises(KeyError, site.getSession, session.uid)
+
 
 class SessionTests(unittest.TestCase):
     """
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.