1 from test_support
import verify
, verbose
7 # The regression test will have real values in sys.argv, which
8 # will completely confuse the test of the cgi module
12 cgi
.sys
= HackedSysModule()
15 from cStringIO
import StringIO
17 from StringIO
import StringIO
19 class ComparableException
:
20 def __init__(self
, err
):
26 def __cmp__(self
, anExc
):
27 if not isinstance(anExc
, Exception):
29 x
= cmp(self
.err
.__class
__, anExc
.__class
__)
32 return cmp(self
.err
.args
, anExc
.args
)
34 def __getattr__(self
, attr
):
35 return getattr(self
.err
, attr
)
37 def do_test(buf
, method
):
41 env
['REQUEST_METHOD'] = 'GET'
42 env
['QUERY_STRING'] = buf
43 elif method
== "POST":
45 env
['REQUEST_METHOD'] = 'POST'
46 env
['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
47 env
['CONTENT_LENGTH'] = str(len(buf
))
49 raise ValueError, "unknown method: %s" % method
51 return cgi
.parse(fp
, env
, strict_parsing
=1)
52 except StandardError, err
:
53 return ComparableException(err
)
55 # A list of test cases. Each test case is a a two-tuple that contains
56 # a string with the query and a dictionary with the expected result.
59 ("", ValueError("bad query field: ''")),
60 ("&", ValueError("bad query field: ''")),
61 ("&&", ValueError("bad query field: ''")),
62 (";", ValueError("bad query field: ''")),
63 (";&;", ValueError("bad query field: ''")),
64 # Should the next few really be valid?
68 # This rest seem to make sense
70 ("&=a", ValueError("bad query field: ''")),
71 ("=a&", ValueError("bad query field: ''")),
72 ("=&a", ValueError("bad query field: 'a'")),
73 ("b=a", {'b': ['a']}),
74 ("b+=a", {'b ': ['a']}),
75 ("a=b=a", {'a': ['b=a']}),
76 ("a=+b=a", {'a': [' b=a']}),
77 ("&b=a", ValueError("bad query field: ''")),
78 ("b&=a", ValueError("bad query field: 'b'")),
79 ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
80 ("a=a+b&a=b+a", {'a': ['a b', 'b a']}),
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 ("x=1;y=2.0;z=2-3.%2b0", {'x': ['1'], 'y': ['2.0'], 'z': ['2-3.+0']}),
84 ("Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env",
85 {'Hbc5161168c542333633315dee1182227:key_store_seqid': ['400006'],
87 'expire': ['964546263'],
88 'kid': ['130003.300038'],
90 'order_id': ['0bb2e248638833d48cb7fed300000f1b'],
95 ("group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse",
96 {'SUBMIT': ['Browse'],
97 '_assigned_to': ['31392'],
100 'group_id': ['5470'],
106 if type(list) == type([]):
110 def first_elts(list):
111 return map(lambda x
:x
[0], list)
113 def first_second_elts(list):
114 return map(lambda p
:(p
[0], p
[1][0]), list)
117 for orig
, expect
in parse_test_cases
:
120 d
= do_test(orig
, "GET")
121 verify(d
== expect
, "Error parsing %s" % repr(orig
))
122 d
= do_test(orig
, "POST")
123 verify(d
== expect
, "Error parsing %s" % repr(orig
))
125 env
= {'QUERY_STRING': orig
}
126 fcd
= cgi
.FormContentDict(env
)
127 sd
= cgi
.SvFormContentDict(env
)
128 fs
= cgi
.FieldStorage(environ
=env
)
129 if type(expect
) == type({}):
130 # test dict interface
131 verify(len(expect
) == len(fcd
))
132 verify(norm(expect
.keys()) == norm(fcd
.keys()))
133 verify(norm(expect
.values()) == norm(fcd
.values()))
134 verify(norm(expect
.items()) == norm(fcd
.items()))
135 verify(fcd
.get("nonexistent field", "default") == "default")
136 verify(len(sd
) == len(fs
))
137 verify(norm(sd
.keys()) == norm(fs
.keys()))
138 verify(fs
.getvalue("nonexistent field", "default") == "default")
139 # test individual fields
140 for key
in expect
.keys():
141 expect_val
= expect
[key
]
142 verify(fcd
.has_key(key
))
143 verify(norm(fcd
[key
]) == norm(expect
[key
]))
144 verify(fcd
.get(key
, "default") == fcd
[key
])
145 verify(fs
.has_key(key
))
146 if len(expect_val
) > 1:
153 verify(not single_value
)
154 verify(fs
.getvalue(key
) == expect_val
)
157 verify(val
== expect_val
[0])
158 verify(fs
.getvalue(key
) == expect_val
[0])
159 verify(norm(sd
.getlist(key
)) == norm(expect_val
))
161 verify(norm(sd
.values()) == \
162 first_elts(norm(expect
.values())))
163 verify(norm(sd
.items()) == \
164 first_second_elts(norm(expect
.items())))
166 # Test the weird FormContentDict classes
167 env
= {'QUERY_STRING': "x=1&y=2.0&z=2-3.%2b0&1=1abc"}
168 expect
= {'x': 1, 'y': 2.0, 'z': '2-3.+0', '1': '1abc'}
169 d
= cgi
.InterpFormContentDict(env
)
170 for k
, v
in expect
.items():
172 for k
, v
in d
.items():
173 verify(expect
[k
] == v
)
174 verify(norm(expect
.values()) == norm(d
.values()))
179 cgi
.logfp
= sys
.stdout
180 cgi
.initlog("%s", "Testing initlog 1")
181 cgi
.log("%s", "Testing log 2")
182 if os
.path
.exists("/dev/null"):
184 cgi
.logfile
= "/dev/null"
185 cgi
.initlog("%s", "Testing log 3")
186 cgi
.log("Testing log 4")