6 # The regression test will have real values in sys.argv, which
7 # will completely confuse the test of the cgi module
11 cgi
.sys
= HackedSysModule()
14 from cStringIO
import StringIO
16 from StringIO
import StringIO
18 class ComparableException
:
19 def __init__(self
, err
):
25 def __cmp__(self
, anExc
):
26 if not isinstance(anExc
, Exception):
28 x
= cmp(self
.err
.__class
__, anExc
.__class
__)
31 return cmp(self
.err
.args
, anExc
.args
)
33 def __getattr__(self
, attr
):
34 return getattr(self
, self
.err
)
36 def do_test(buf
, method
):
40 env
['REQUEST_METHOD'] = 'GET'
41 env
['QUERY_STRING'] = buf
42 elif method
== "POST":
44 env
['REQUEST_METHOD'] = 'POST'
45 env
['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
46 env
['CONTENT_LENGTH'] = str(len(buf
))
48 raise ValueError, "unknown method: %s" % method
50 return cgi
.parse(fp
, env
, strict_parsing
=1)
51 except StandardError, err
:
52 return ComparableException(err
)
54 # A list of test cases. Each test case is a a two-tuple that contains
55 # a string with the query and a dictionary with the expected result.
58 ("", ValueError("bad query field: ''")),
59 ("&", ValueError("bad query field: ''")),
60 ("&&", ValueError("bad query field: ''")),
61 (";", ValueError("bad query field: ''")),
62 (";&;", ValueError("bad query field: ''")),
63 # Should the next few really be valid?
67 # This rest seem to make sense
69 ("&=a", ValueError("bad query field: ''")),
70 ("=a&", ValueError("bad query field: ''")),
71 ("=&a", ValueError("bad query field: 'a'")),
72 ("b=a", {'b': ['a']}),
73 ("b+=a", {'b ': ['a']}),
74 ("a=b=a", {'a': ['b=a']}),
75 ("a=+b=a", {'a': [' b=a']}),
76 ("&b=a", ValueError("bad query field: ''")),
77 ("b&=a", ValueError("bad query field: 'b'")),
78 ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
79 ("a=a+b&a=b+a", {'a': ['a b', 'b a']}),
80 ("x=1&y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
81 ("x=1;y=2.0&z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
82 ("x=1;y=2.0;z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
83 ("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env",
84 {'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'],
86 'expire': ['964546263'],
87 'kid': ['130003.300038'],
89 'order_id': ['0bb2e248638833d48cb7fed300000f1b'],
94 ("group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse",
95 {'SUBMIT': ['Browse'],
96 '_assigned_to': ['31392'],
105 if type(list) == type([]):
109 def first_elts(list):
110 return map(lambda x
:x
[0], list)
112 def first_second_elts(list):
113 return map(lambda p
:(p
[0], p
[1][0]), list)
116 for orig
, expect
in parse_test_cases
:
119 d
= do_test(orig
, "GET")
120 assert d
== expect
, "Error parsing %s" % repr(orig
)
121 d
= do_test(orig
, "POST")
122 assert d
== expect
, "Error parsing %s" % repr(orig
)
124 env
= {'QUERY_STRING': orig
}
125 fcd
= cgi
.FormContentDict(env
)
126 sd
= cgi
.SvFormContentDict(env
)
127 fs
= cgi
.FieldStorage(environ
=env
)
128 if type(expect
) == type({}):
129 # test dict interface
130 assert len(expect
) == len(fcd
)
131 assert norm(expect
.keys()) == norm(fcd
.keys())
132 assert norm(expect
.values()) == norm(fcd
.values())
133 assert norm(expect
.items()) == norm(fcd
.items())
134 assert fcd
.get("nonexistent field", "default") == "default"
135 assert len(sd
) == len(fs
)
136 assert norm(sd
.keys()) == norm(fs
.keys())
137 assert fs
.getvalue("nonexistent field", "default") == "default"
138 # test individual fields
139 for key
in expect
.keys():
140 expect_val
= expect
[key
]
141 assert fcd
.has_key(key
)
142 assert norm(fcd
[key
]) == norm(expect
[key
])
143 assert fcd
.get(key
, "default") == fcd
[key
]
144 assert fs
.has_key(key
)
145 if len(expect_val
) > 1:
152 assert not single_value
153 assert fs
.getvalue(key
) == expect_val
156 assert val
== expect_val
[0]
157 assert fs
.getvalue(key
) == expect_val
[0]
158 assert norm(sd
.getlist(key
)) == norm(expect_val
)
160 assert norm(sd
.values()) == \
161 first_elts(norm(expect
.values()))
162 assert norm(sd
.items()) == \
163 first_second_elts(norm(expect
.items()))
165 # Test the weird FormContentDict classes
166 env
= {'QUERY_STRING': "x=1&y=2.0&z=2-3.%2b0&1=1abc"}
167 expect
= {'x': 1, 'y': 2.0, 'z': '2-3.+0', '1': '1abc'}
168 d
= cgi
.InterpFormContentDict(env
)
169 for k
, v
in expect
.items():
171 for k
, v
in d
.items():
172 assert expect
[k
] == v
173 assert norm(expect
.values()) == norm(d
.values())
178 cgi
.logfp
= sys
.stdout
179 cgi
.initlog("%s", "Testing initlog 1")
180 cgi
.log("%s", "Testing log 2")
181 if os
.path
.exists("/dev/null"):
183 cgi
.logfile
= "/dev/null"
184 cgi
.initlog("%s", "Testing log 3")
185 cgi
.log("Testing log 4")