from test
.test_support
import verify
, verbose
# The regression test will have real values in sys.argv, which
# will completely confuse the test of the cgi module
cgi
.sys
= HackedSysModule()
from cStringIO
import StringIO
from StringIO
import StringIO
class ComparableException
:
def __cmp__(self
, anExc
):
if not isinstance(anExc
, Exception):
x
= cmp(self
.err
.__class
__, anExc
.__class
__)
return cmp(self
.err
.args
, anExc
.args
)
def __getattr__(self
, attr
):
return getattr(self
.err
, attr
)
def do_test(buf
, method
):
env
['REQUEST_METHOD'] = 'GET'
env
['QUERY_STRING'] = buf
env
['REQUEST_METHOD'] = 'POST'
env
['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
env
['CONTENT_LENGTH'] = str(len(buf
))
raise ValueError, "unknown method: %s" % method
return cgi
.parse(fp
, env
, strict_parsing
=1)
except StandardError, err
:
return ComparableException(err
)
# A list of test cases. Each test case is a a two-tuple that contains
# a string with the query and a dictionary with the expected result.
("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
("a=1&a=2", [('a', '1'), ('a', '2')]),
parse_strict_test_cases
= [
("", ValueError("bad query field: ''")),
("&", ValueError("bad query field: ''")),
("&&", ValueError("bad query field: ''")),
(";", ValueError("bad query field: ''")),
(";&;", ValueError("bad query field: ''")),
# Should the next few really be valid?
# This rest seem to make sense
("&=a", ValueError("bad query field: ''")),
("=a&", ValueError("bad query field: ''")),
("=&a", ValueError("bad query field: 'a'")),
("a=b=a", {'a': ['b=a']}),
("a=+b=a", {'a': [' b=a']}),
("&b=a", ValueError("bad query field: ''")),
("b&=a", ValueError("bad query field: 'b'")),
("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
("a=a+b&a=b+a", {'a': ['a b', 'b a']}),
("x=1&y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
("x=1;y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
("x=1;y=2.0;z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env",
{'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'],
'kid': ['130003.300038'],
'order_id': ['0bb2e248638833d48cb7fed300000f1b'],
("group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse",
'_assigned_to': ['31392'],
if type(list) == type([]):
return map(lambda x
:x
[0], list)
def first_second_elts(list):
return map(lambda p
:(p
[0], p
[1][0]), list)
for orig
, expect
in parse_qsl_test_cases
:
result
= cgi
.parse_qsl(orig
, keep_blank_values
=True)
print repr(orig
), '=>', result
verify(result
== expect
, "Error parsing %s" % repr(orig
))
for orig
, expect
in parse_strict_test_cases
:
verify(d
== expect
, "Error parsing %s" % repr(orig
))
d
= do_test(orig
, "POST")
verify(d
== expect
, "Error parsing %s" % repr(orig
))
env
= {'QUERY_STRING': orig
}
fcd
= cgi
.FormContentDict(env
)
sd
= cgi
.SvFormContentDict(env
)
fs
= cgi
.FieldStorage(environ
=env
)
if type(expect
) == type({}):
verify(len(expect
) == len(fcd
))
verify(norm(expect
.keys()) == norm(fcd
.keys()))
verify(norm(expect
.values()) == norm(fcd
.values()))
verify(norm(expect
.items()) == norm(fcd
.items()))
verify(fcd
.get("nonexistent field", "default") == "default")
verify(len(sd
) == len(fs
))
verify(norm(sd
.keys()) == norm(fs
.keys()))
verify(fs
.getvalue("nonexistent field", "default") == "default")
for key
in expect
.keys():
verify(norm(fcd
[key
]) == norm(expect
[key
]))
verify(fcd
.get(key
, "default") == fcd
[key
])
verify(fs
.getvalue(key
) == expect_val
)
verify(val
== expect_val
[0])
verify(fs
.getvalue(key
) == expect_val
[0])
verify(norm(sd
.getlist(key
)) == norm(expect_val
))
verify(norm(sd
.values()) == \
first_elts(norm(expect
.values())))
verify(norm(sd
.items()) == \
first_second_elts(norm(expect
.items())))
# Test the weird FormContentDict classes
env
= {'QUERY_STRING': "x=1&y=2.0&z=2-3.%2b0&1=1abc"}
expect
= {'x': 1, 'y': 2.0, 'z': '2-3.+0', '1': '1abc'}
d
= cgi
.InterpFormContentDict(env
)
for k
, v
in expect
.items():
verify(norm(expect
.values()) == norm(d
.values()))
cgi
.initlog("%s", "Testing initlog 1")
cgi
.log("%s", "Testing log 2")
if os
.path
.exists("/dev/null"):
cgi
.logfile
= "/dev/null"
cgi
.initlog("%s", "Testing log 3")