473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

brain cramp: emulating cgi.FieldStorage

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
3 2191
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
figured it out...

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

Sep 15 '05 #3
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Fred Murray | last post by:
Hi all, When uploading a file ("input type=file"), FieldStorage usually behaves correctly, but every once in a while (every couple days?), it will suddenly start causing internal server errors. ...
1
by: Ames Andreas (MPA/DF) | last post by:
Hi, sorry for following-up to myself. Some remarks: 1) Please excuse the bogus original message. I wrote it a minute before I knocked off work and I promise to never do so again :-) 2)...
1
by: Ames Andreas (MPA/DF) | last post by:
Hi, I'm currently using Zope 2.7.2-0 which in turn uses cgi.FieldStorage from Python 2.3.4. FieldStorage can either build a 'list' or a 'file' to represent a request. Unfortunately it...
0
by: Neil Benn | last post by:
Hello, I'm writing a simple cgi script and want to be able to access the filename in a FieldStorage file instance. I have successfully manmaged to access the file as a 'file-like object' by...
4
by: Derek Basch | last post by:
Given this FieldStorage object: FieldStorage(None, None, ) I am trying to cgi.urlencode the FieldStorage object to POST to another cgi script. The documentation for urlencode,...
7
by: robert | last post by:
running 8.1.7 server, 8.1.6 client. i *thought* inner join should not return nulls, but not only that, but i get way more rows than i'm expecting. assume: order table: order_number
0
by: John Salerno | last post by:
I'm trying to use a for loop with a FieldStorage object and I get the following error. Can you not treat it like a dictionary, or am I writing the for loop incorrectly? for item in form: print...
7
by: John Salerno | last post by:
If I want to get all the values that are entered into an HTML form and write them to a file, is there some way to handle them all at the same time, or must FieldStorage be indexed by each specific...
4
by: arorap | last post by:
I've mod_php installed with Apache 2.2. In one of my folders, I'm using the cgihandler as the PythonHandler as my target host runs python only as CGI. Here cgi.FieldStorage() doesn't seem to work....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.