niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
brain cramp: emulating cgi.FieldStorage
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
Mike Meyer
*nix forums Guru


Joined: 21 Feb 2005
Posts: 1044

PostPosted: Thu Sep 15, 2005 10:23 pm    Post subject: Re: brain cramp: emulating cgi.FieldStorage Reply with quote

"Chris Curvey" <ccurvey@gmail.com> writes:

Quote:
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 <mwm@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Back to top
Max M
*nix forums addict


Joined: 22 Feb 2005
Posts: 82

PostPosted: Thu Sep 15, 2005 1:07 pm    Post subject: Re: brain cramp: emulating cgi.FieldStorage Reply with quote

Chris Curvey wrote:
Quote:
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
Back to top
Chris Curvey
*nix forums addict


Joined: 02 May 2005
Posts: 50

PostPosted: Thu Sep 15, 2005 1:04 pm    Post subject: Re: brain cramp: emulating cgi.FieldStorage Reply with quote

figured it out...

os.environ["QUERY_STRING"] = "foo=bar"
fs = cgi.FieldStorage()
functionToBeTested(fs)
Back to top
Chris Curvey
*nix forums addict


Joined: 02 May 2005
Posts: 50

PostPosted: Thu Sep 15, 2005 12:11 pm    Post subject: brain cramp: emulating cgi.FieldStorage Reply with quote

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?
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 5:57 am | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts follow-up to FieldStorage John Salerno python 7 Mon Jun 05, 2006 3:23 pm
No new posts can you iterate over a FieldStorage object? John Salerno python 0 Fri Jun 02, 2006 7:15 pm
No new posts emulating action_replace_with_url() feature of MIMEDefang Kurt Lieber Postfix 2 Wed May 17, 2006 5:28 pm
No new posts removing cgi fieldstorage keys Kevin F python 2 Wed Apr 19, 2006 2:53 am
No new posts Brain Teaser - how would you do this? Marc Perkel Exim 13 Mon Apr 03, 2006 7:29 pm

Personal Car Finance | Satellite TV | Cheap Magazines | Bankruptcy | Magazine Subscriptions
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1503s ][ Queries: 20 (0.0695s) ][ GZIP on - Debug on ]