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

Tres Seaver tseaver@zope.com
Wed, 13 Mar 2002 19:57:24 -0500


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

Modified Files:
	Framework.py 
Log Message:


  - Add support for capturing cookies issued by the server and resending
    them in subsequent requests (makes testing session-based apps possible).


=== Packages/FunctionalTests/Framework.py 1.10 => 1.11 ===
-import httplib, urllib, urlparse, base64
+import httplib, urllib, urlparse, base64, Cookie
 import MimeWriter, StringIO
 
 import ConfigParserExt
@@ -34,6 +34,7 @@
         self._errors = []
         self._fatal_errors = []
         self._children = []
+        self._cookies = []
 
     #
     #   Queries
@@ -58,6 +59,19 @@
         result.update( self._defaults )
         return result
 
+    def getCookies( self ):
+        """
+            Return cookies we have accumulated during the request.
+        """
+        #import pdb; pdb.set_trace()
+        return self._cookies
+
+    def setCookies( self, cookies ):
+        """
+            Store our cookies.
+        """
+        self._cookies = cookies
+
     def setStateValue( self, key, value ):
         """
             Remember a value (e.g., across invocations)
@@ -220,6 +234,9 @@
     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
 
@@ -252,6 +269,22 @@
     def getReplyStatus( self ):
         return '%d %s' % ( self._reply_code, self._reply_message )
 
+    def getCookies( self ):
+        """
+            Return a sequence (maybe empty) of Cookie objects describing
+            the cookies set by the response.
+        """
+        result = []
+
+        for h in filter( lambda x: x.lower().startswith( 'set-cookie:' )
+                       , self._reply_headers.headers ):
+
+            cookie = Cookie.SmartCookie()
+            cookie.load( h )
+            result.append( cookie )
+
+        return result
+
 class ZEORequestInvocation( InvocationBase ):
 
     _result = None
@@ -628,6 +661,10 @@
         for key, value in self.getCookies():
             connection.putheader( 'Cookie', '%s=%s' % ( key, value ) )
 
+        for cookie in result.getCookies():
+            connection.putheader( 'Cookie'
+                                , cookie.output( header='', attrs=[] ) )
+
         data = self.getData()
         if data:
             connection.putheader( 'Content-length', str( len( data ) ) )
@@ -929,6 +966,9 @@
             request, requests = requests[0], requests[1:]
             invocation = request( result )
             result.addInvocation( invocation, request )
+            cookies = invocation.getCookies()
+            if cookies:
+                result.setCookies( cookies )
 
         if result():
             self._checkPostcondition( result )