r46917 - Update after review.

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

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

Log:
Update after review.

Modified: branches/http-session-id-type-8215/twisted/topfiles/8215.bugfix
==============================================================================
--- branches/http-session-id-type-8215/twisted/topfiles/8215.bugfix	(original)
+++ branches/http-session-id-type-8215/twisted/topfiles/8215.bugfix	Sat Mar  5 05:08:22 2016
@@ -1 +1 @@
-Ensure to generate Session.uid as 'bytes' type with both Python 2/3
+twisted.web.server.site.makeSession now generates an uid of type bytes on both Python 2 and 3.

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 05:08:22 2016
@@ -544,7 +544,9 @@
     This utility class contains no functionality, but is used to
     represent a session.
 
-    @ivar uid: A unique identifier for the session, C{bytes}.
+    @ivar uid: A unique identifier for the session.
+    @type uid: L{bytes}
+
     @ivar _reactor: An object providing L{IReactorTime} to use for scheduling
         expiration.
     @ivar sessionTimeout: timeout of a session, in seconds.
@@ -685,8 +687,12 @@
 
     def getSession(self, uid):
         """
-        Get a previously generated session, by its unique ID.
-        This raises a KeyError if the session is not found.
+        Get a previously generated session.
+
+        @param uid: Unique ID of the session.
+        @type uid: L{bytes}.
+
+        @raise: KeyError if the session is not found.
         """
         return self.sessions[uid]
 

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 05:08:22 2016
@@ -60,6 +60,22 @@
     Unit tests for L{server.Site}.
     """
 
+    def getAutoExpiringSession(self, site):
+        """
+        Create a new session which auto expires at cleanup.
+
+        @param site: The site on which the session is created.
+        @type site: L{server.Site}
+
+        @return: A newly created session.
+        @rtype: L{server.Session}
+        """
+        session = site.makeSession()
+        # Clean delayed calls from session expiration.
+        self.addCleanup(session.expire)
+        return session
+
+
     def test_simplestSite(self):
         """
         L{Site.getResourceFor} returns the C{b""} child of the root resource it
@@ -110,32 +126,34 @@
 
     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}
+        It generates a new C{Session} instance with an uid of type L{bytes}.
         """
         site = server.Site(resource.Resource())
+        session = self.getAutoExpiringSession(site)
 
-        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):
+    def test_getSessionExistent(self):
         """
-        Get a previously generated session, by its unique ID.
-        This raises a KeyError if the session is not found.
+        It gets a previously generated session, by its unique ID.
         """
         site = server.Site(resource.Resource())
-        session = site.makeSession()
+        createdSession = self.getAutoExpiringSession(site)
 
-        session = site.getSession(session.uid)
-        self.assertIsInstance(session, server.Session)
+        retrievedSession = site.getSession(createdSession.uid)
+
+        self.assertIs(createdSession, retrievedSession)
+
+
+    def test_getSessionNonExistent(self):
+        """
+        It raises a KeyError if the session is not found.
+        """
+        site = server.Site(resource.Resource())
 
-        session.expire()
-        self.assertRaises(KeyError, site.getSession, session.uid)
+        self.assertRaises(KeyError, site.getSession, b'no-such-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.