By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

brain cramp: emulating cgi.FieldStorage

P: n/a
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?

Sep 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Sep 15 '05 #2

P: n/a
figured it out...

os.environ["QUERY_STRING"] = "foo=bar"
fs = cgi.FieldStorage()
functionToBeTested(fs)

Sep 15 '05 #3

P: n/a
"Chris Curvey" <cc*****@gmail.com> writes:
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?


QUERY_STRING="var1=val1&var2=val2&var3=val3" REQUEST_METHOD="GET" python mycgiprogram.py

Similar hacks work for POST if you want to store a test query in a
file.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Sep 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.