brain cramp: emulating cgi.FieldStorage

Max M maxm at mxm.dk
Thu Sep 15 11:07:40 EDT 2005


Chris Curvey wrote:
> I can't be the first person to want to do this, but I also can't seem
> to find a solution.  (Perhaps my Google skills are poor today.)  How
> can I emulate cgi.FieldStorage() for my unit tests so that I don't have
> to put a web server in the way?
> 
> what I'd like to do is something like
> 
> fs = cgi.FieldStorage()
> fs["foo"] = "bar"
> functionToBeTested(fs)
> 
> Any hints/leads/suggestions?


Can't you just pass the FieldStorage some dummy data to parse?

It looks pretty simple.

class FieldStorage:

     def __init__(self, fp=None, headers=None, outerboundary="",
                  environ=os.environ, keep_blank_values=0, 
strict_parsing=0):
         """Constructor.  Read multipart/* until last part.

         Arguments, all optional:

         fp              : file pointer; default: sys.stdin
             (not used when the request method is GET)

         headers         : header dictionary-like object; default:
             taken from environ as per CGI spec

         outerboundary   : terminating multipart boundary
             (for internal use only)

         environ         : environment dictionary; default: os.environ

         keep_blank_values: flag indicating whether blank values in
             URL encoded forms should be treated as blank strings.
             A true value indicates that blanks should be retained as
             blank strings.  The default false value indicates that
             blank values are to be ignored and treated as if they were
             not included.

         strict_parsing: flag indicating what to do with parsing errors.
             If false (the default), errors are silently ignored.
             If true, errors raise a ValueError exception.

         """


-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list