[Zope-CVS] CVS: Packages/FunctionalTests/FunctionalTests - ScenarioGenerator.py:1.8

Tres Seaver tseaver at zope.com
Thu Mar 4 08:51:31 EST 2004


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

Modified Files:
	ScenarioGenerator.py 
Log Message:


  - Parse content type, etc., using mimetools.Message instead of rfc822.

  - Reshuffle handling of the payload, to leverage cgi.FieldStorage's
    ability to parse 'multipart/form-encoded'.

    o XXX: Still need to figure out what to do with file uploads.


=== Packages/FunctionalTests/FunctionalTests/ScenarioGenerator.py 1.7 => 1.8 ===
--- Packages/FunctionalTests/FunctionalTests/ScenarioGenerator.py:1.7	Wed Mar  3 14:01:43 2004
+++ Packages/FunctionalTests/FunctionalTests/ScenarioGenerator.py	Thu Mar  4 08:51:30 2004
@@ -9,7 +9,8 @@
 import re
 import getopt
 import glob
-import rfc822
+import cgi
+import mimetools
 import urllib
 import urlparse
 
@@ -264,44 +265,11 @@
         parms[ 'saw_authentication' ] = 1
         self._print( _AUTHENTICATION_SKELETON )
 
-    def _handleContentType( self, value, parms ):
-        """
-            Record the content type for later processing.
-        """
-        parms[ 'content_type' ] = value
-
     HEADER_HANDLERS =\
     { 'cookie'          : _handleCookie
     , 'authentication'  : _handleAuthentication
-    , 'content-type'    : _handleContentType
     }
 
-    def _handleURLEncoded( self, data ):
-        """
-            Emit Field_## values for data, which is encoded using
-            'application/x-www-form-urlencoded'.
-        """
-        count = 0
-        for key_val in data.split( '&' ):
-            k, v = map( urllib.unquote_plus, key_val.split( '=' ) )
-            count = count + 1
-            self._print( _FIELD_SKELETON
-                       , FIELD_NUM=count
-                       , FIELD_NAME=k
-                       , FIELD_VALUE=v
-                       )
-
-    def _handleFormdata( self, data ):
-        """
-            Emit Field_## values for data, which is encoded using
-            'multipart/formdata'.
-        """
-        raise NotImplemented    # TODO:  Implement me!
-
-    CONTENT_HANDLERS =\
-    { 'application/x-www-form-urlencoded'   : _handleURLEncoded
-    , 'multipart/formdata'                  : _handleFormdata
-    }
 
     def _addExcludePattern( self, pattern ):
         """
@@ -349,6 +317,7 @@
 
         f = open( filename )
         all_text = f.read()
+        body_end = f.tell()
         f.seek( 0 )
 
         exclude = self._getExcludeRegex()
@@ -400,26 +369,58 @@
                    , REQUEST_URI=uri
                    )
 
-        headers = rfc822.Message( f )
-        payload = f.read()
-        f.close()
+        headers = mimetools.Message( f )
+        main_type = headers.getmaintype()
+        sub_type = headers.getsubtype()
+
+        content_type = parms[ 'content_type' ] = headers.gettype()
+        parms[ 'encoding' ] = headers.getencoding()
+
+        for param_name in headers.getparamnames():
+            parms[ param_name ] = headers.getparam( param_name )
 
         for k, v in headers.items():
             self._log( 'Header %s: %s' % ( k, v ), 3 )
             handler = self.HEADER_HANDLERS.get( k, None )
             if handler:
                 handler( self, v, parms )
+        
+        body_start = f.tell()
+        content_length = body_end - body_start
+
+        cgi_environ = { 'REQUEST_METHOD' : http_verb
+                      , 'QUERY_STRING'   : query
+                      , 'CONTENT_TYPE'   : headers.typeheader
+                      , 'CONTENT_LENGTH' : content_length
+                      }
+
+        if http_verb is 'GET': # body not used
+            fp = None
+        else:
+            fp = f
+
+        form_data = cgi.FieldStorage( fp=fp
+                                    , environ=cgi_environ
+                                    , keep_blank_values=1
+                                  # , headers=headers.dict  XXX
+                                    )
+
+        index = 0
+        for k in form_data.keys():
 
-        content_type = parms[ 'content_type' ]
+            index += 1
+            v = form_data.getvalue( k )
 
-        if content_type is not None:
-            handler = self.CONTENT_HANDLERS.get( content_type, None )
+            # TODO: handle uploaded files.
 
-            if handler is None:
-                self._log( "Can't handle content type: %s"
-                            % content_type, 0 )
-            else:
-                handler( self, payload )
+            self._print( _FIELD_SKELETON
+                       , FIELD_NUM=index
+                       , FIELD_NAME=k
+                       , FIELD_VALUE=v
+                       )
+
+        payload = f.read()
+        f.close()
 
         self._print( _RESULT_SKELETON
                    , RESULT_CODE=200    # TODO:  Read from response?




More information about the Zope-CVS mailing list