[Zope-CVS] CVS: Packages/FunctionalTests - Framework.py:1.12

Tres Seaver tseaver@zope.com
Thu, 14 Mar 2002 15:12:06 -0500


Update of /cvs-repository/Packages/FunctionalTests
In directory cvs.zope.org:/tmp/cvs-serv23333

Modified Files:
	Framework.py 
Log Message:


  - Added ability to capture and return server-originated cookies
    (e.g., to allow testing of session-based applications).


=== Packages/FunctionalTests/Framework.py 1.11 => 1.12 ===
             Return cookies we have accumulated during the request.
         """
-        #import pdb; pdb.set_trace()
         return self._cookies
 
     def setCookies( self, cookies ):
@@ -234,9 +233,6 @@
     def getResult( self ):
         raise NotImplementedError
 
-    def getCookies( self ):
-        return ()
-
     def getInterval( self ):
         return self._stop_time and self._stop_time - self._start_time or 0.0
 
@@ -429,6 +425,7 @@
         self._has_file_field  = 0
         self._expected_result = 200
         self._expected_redirect = None
+        self._expected_cookies = ()
     
     def _initURLParts( self, URL ):
 
@@ -555,6 +552,27 @@
             Return the URL expected for a redirect.
         """
         return self._expected_redirect
+
+    def getExpectedCookies( self ):
+        """
+            Return the cookie IDs expected for this request
+        """
+        return self._expected_cookies
+
+    def matchExpectedCookies( self, cookies ):
+        """
+            Do 'cookies' match what we expect?
+        """
+        for cookie in cookies:
+            for key in cookie.keys():
+                if key not in self._expected_cookies:
+                    return 0
+        return 1
+
+    def retainCookies( self ):
+        """
+        """
+        return self._expected_cookies is not None
     
     def getContentType( self ):
         """
@@ -632,6 +650,15 @@
         """
         self._expected_time = response_time
 
+    def setExpectedCookies( self, cookies ):
+        """
+            Override the default expected response time, in seconds.
+        """
+        if type( cookies ) == type( '' ):
+            cookies = cookies.split( ';' )
+
+        self._expected_cookies = cookies
+
     #
     #   Request invocation
     #
@@ -721,6 +748,12 @@
 
     if 'expected_time' in options:
         r.setExpectedTime( cp.getfloat( section, 'expected_time' ) )
+
+    if 'expected_cookies' in options:
+        r.setExpectedCookies( cp.get( section, 'expected_cookies' ) )
+
+    if 'flush_cookies' in options:
+        r.setExpectedCookies( None )
     
     return r
 
@@ -963,12 +996,17 @@
         requests = self.listRequests()
 
         while result() and requests:
+
             request, requests = requests[0], requests[1:]
             invocation = request( result )
             result.addInvocation( invocation, request )
-            cookies = invocation.getCookies()
-            if cookies:
-                result.setCookies( cookies )
+
+            if hasattr( invocation, 'getCookies' ):
+                cookies = invocation.getCookies()
+                if cookies and request.matchExpectedCookies( cookies ):
+                    result.setCookies( cookies )
+                elif not request.retainCookies():
+                    result.setCookies( () )
 
         if result():
             self._checkPostcondition( result )