[Zope-CVS] CVS: Packages/FunctionalTests/FunctionalTests - Request.py:1.7.2.5

Karl Anderson cvs-admin at zope.org
Fri Nov 14 19:52:00 EST 2003


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

Modified Files:
      Tag: kra-misc-branch
	Request.py 
Log Message:
Cache content-type and data for multipart type requests.
We need to do this because both these must give the same boundary.

HTTPRequest:

added _contentTypeCache, _dataCache, _setMultipartDataAndType()

getContentType(), getData(): use caches

addField(): check data cache


=== Packages/FunctionalTests/FunctionalTests/Request.py 1.7.2.4 => 1.7.2.5 ===
--- Packages/FunctionalTests/FunctionalTests/Request.py:1.7.2.4	Fri Nov 14 17:29:23 2003
+++ Packages/FunctionalTests/FunctionalTests/Request.py	Fri Nov 14 19:52:00 2003
@@ -1,13 +1,16 @@
 """ Classes:  RequestError, HTTPRequest, ZEORequest
     Functions: buildRequest
 
-$Id
+$Id $
 """
 import re
 import httplib
 import urllib
 import urlparse
 import base64
+import MimeWriter
+import StringIO
+import rfc822
 
 from interfaces import IPluginFunction
 from interfaces import IRequest
@@ -183,6 +186,8 @@
         self._expected_redirect = None
         self._expected_cookies = ()
         self._payload_checker = None
+        self._contentTypeCache = None
+        self._dataCache = None
     
     def _initURLParts( self, URL ):
 
@@ -327,7 +332,12 @@
 
         """ See IHTTPRequest.
         """
-        return self._has_file_field and _MULTIPART or _URLENCODED
+        if self._has_file_field:
+            self._setMultipartDataAndType()
+            return self._contentTypeCache
+
+        return _URLENCODED
+
 
     def getData( self ):
 
@@ -335,7 +345,8 @@
         """
         if self._has_file_field:
 
-            return _mime_encode( self._fields )
+            self._setMultipartDataAndType()
+            return self._dataCache
 
         else:
 
@@ -374,6 +385,9 @@
 
         """ See IHTTPRequest.
         """
+        if self._dataCache:
+            # adding could change boundary which may have been gotten
+            raise RequestError, "cannot add field after getting data or type"
         field = _buildField( field_desc )
         if field[1] == 'file':
             self._has_file_field = 1
@@ -420,6 +434,21 @@
         """
         self._payload_checker = PluginFunction( filename, function )
 
+    # we need to cache because content-type and data must both state
+    # the same boundary
+    def _setMultipartDataAndType( self ):
+        """ See IHTTPRequest.
+        """
+        if not self._has_file_field:
+            raise RuntimeError, \
+                "attempt to cache data and type of non-multipart type request"
+        if not self._contentTypeCache:
+            mess = _mime_encode( self._fields )
+            type = mess['content-type']
+            type = type.replace('\n ', ' ')
+            self._dataCache = mess.fp.read()[1:]
+            self._contentTypeCache = type
+
     #
     #   Request invocation
     #
@@ -588,16 +617,13 @@
     raise RequestError, 'Invalid field_desc: %s' % field_desc
 
 _URLENCODED = 'application/x-www-form-urlencoded'
-_MULTIPART  = 'multipart/form-data'
+#_MULTIPART  = 'multipart/form-data'
 
 def _mime_encode( fields ):
 
     """ Build a 'multipart/form-data' representation of the
         (name,type,value) items in fields.
     """
-    import MimeWriter
-    import StringIO
-    import rfc822
     # MimeWriter has subtle differences from the spec, will ususally be OK
     # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306    
     fp = StringIO.StringIO()
@@ -613,8 +639,7 @@
         f.write( field[2] )
     writer.lastpart()
     fp.seek(0)
-    mess = rfc822.Message( fp )
-    return mess['content-type'] + mess.fp.read()
+    return rfc822.Message( fp )
 
 def _buildSleepRequest( cp, section ):
 




More information about the Zope-CVS mailing list