blob: ef7dfa5361f3f84a0559cf1faf1218788a3cb2ef [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test.support import run_unittest
Jeremy Hyltond9827c42000-08-03 22:11:43 +00002import cgi
3import os
4import sys
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00005import tempfile
Thomas Wouters89f507f2006-12-13 04:49:30 +00006import unittest
Guido van Rossum34d19282007-08-09 01:03:29 +00007from io import StringIO
Facundo Batistaa27244b2008-09-09 02:43:19 +00008from warnings import catch_warnings, filterwarnings
Jeremy Hyltond9827c42000-08-03 22:11:43 +00009
10class HackedSysModule:
11 # The regression test will have real values in sys.argv, which
Fred Drake004d5e62000-10-23 17:22:08 +000012 # will completely confuse the test of the cgi module
Jeremy Hyltond9827c42000-08-03 22:11:43 +000013 argv = []
14 stdin = sys.stdin
15
16cgi.sys = HackedSysModule()
17
18try:
Guido van Rossum34d19282007-08-09 01:03:29 +000019 from io import StringIO
Jeremy Hyltond9827c42000-08-03 22:11:43 +000020except ImportError:
Guido van Rossum34d19282007-08-09 01:03:29 +000021 from io import StringIO
Jeremy Hyltond9827c42000-08-03 22:11:43 +000022
23class ComparableException:
24 def __init__(self, err):
25 self.err = err
26
27 def __str__(self):
28 return str(self.err)
29
Guido van Rossum47b9ff62006-08-24 00:41:19 +000030 def __eq__(self, anExc):
Jeremy Hyltond9827c42000-08-03 22:11:43 +000031 if not isinstance(anExc, Exception):
Guido van Rossum47b9ff62006-08-24 00:41:19 +000032 return NotImplemented
33 return (self.err.__class__ == anExc.__class__ and
34 self.err.args == anExc.args)
Jeremy Hyltond9827c42000-08-03 22:11:43 +000035
36 def __getattr__(self, attr):
Guido van Rossum846d6db2001-01-17 15:08:37 +000037 return getattr(self.err, attr)
Jeremy Hyltond9827c42000-08-03 22:11:43 +000038
39def do_test(buf, method):
40 env = {}
41 if method == "GET":
42 fp = None
43 env['REQUEST_METHOD'] = 'GET'
44 env['QUERY_STRING'] = buf
45 elif method == "POST":
46 fp = StringIO(buf)
47 env['REQUEST_METHOD'] = 'POST'
48 env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
49 env['CONTENT_LENGTH'] = str(len(buf))
50 else:
Collin Winter3add4d72007-08-29 23:37:32 +000051 raise ValueError("unknown method: %s" % method)
Jeremy Hyltond9827c42000-08-03 22:11:43 +000052 try:
53 return cgi.parse(fp, env, strict_parsing=1)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000054 except Exception as err:
Jeremy Hyltond9827c42000-08-03 22:11:43 +000055 return ComparableException(err)
56
Neil Schemenauer66edb622004-07-19 15:38:11 +000057parse_strict_test_cases = [
Jeremy Hyltond9827c42000-08-03 22:11:43 +000058 ("", ValueError("bad query field: ''")),
59 ("&", ValueError("bad query field: ''")),
60 ("&&", ValueError("bad query field: ''")),
Jeremy Hyltonafde7e22000-09-15 20:06:57 +000061 (";", ValueError("bad query field: ''")),
62 (";&;", ValueError("bad query field: ''")),
Jeremy Hyltond9827c42000-08-03 22:11:43 +000063 # Should the next few really be valid?
64 ("=", {}),
65 ("=&=", {}),
Jeremy Hyltonafde7e22000-09-15 20:06:57 +000066 ("=;=", {}),
Jeremy Hyltond9827c42000-08-03 22:11:43 +000067 # This rest seem to make sense
68 ("=a", {'': ['a']}),
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']}),
Jeremy Hyltonafde7e22000-09-15 20:06:57 +000081 ("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']}),
Jeremy Hyltond9827c42000-08-03 22:11:43 +000083 ("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'],
85 'cuyer': ['r'],
86 'expire': ['964546263'],
87 'kid': ['130003.300038'],
88 'lobale': ['en-US'],
89 'order_id': ['0bb2e248638833d48cb7fed300000f1b'],
90 'ss': ['env'],
91 'view': ['bustomer'],
92 }),
Fred Drake004d5e62000-10-23 17:22:08 +000093
Jeremy Hyltond9827c42000-08-03 22:11:43 +000094 ("group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse",
95 {'SUBMIT': ['Browse'],
96 '_assigned_to': ['31392'],
97 '_category': ['100'],
98 '_status': ['1'],
99 'group_id': ['5470'],
100 'set': ['custom'],
101 })
102 ]
103
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000104def norm(seq):
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000105 return sorted(seq, key=repr)
Jeremy Hyltond9827c42000-08-03 22:11:43 +0000106
107def first_elts(list):
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000108 return [p[0] for p in list]
Jeremy Hyltond9827c42000-08-03 22:11:43 +0000109
110def first_second_elts(list):
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000111 return [(p[0], p[1][0]) for p in list]
112
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000113def gen_result(data, environ):
114 fake_stdin = StringIO(data)
115 fake_stdin.seek(0)
116 form = cgi.FieldStorage(fp=fake_stdin, environ=environ)
117
118 result = {}
119 for k, v in dict(form).items():
120 result[k] = type(v) is list and form.getlist(k) or v.value
121
122 return result
Jeremy Hyltond9827c42000-08-03 22:11:43 +0000123
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124class CgiTests(unittest.TestCase):
Neil Schemenauer66edb622004-07-19 15:38:11 +0000125
Thomas Wouters89f507f2006-12-13 04:49:30 +0000126 def test_strict(self):
127 for orig, expect in parse_strict_test_cases:
128 # Test basic parsing
129 d = do_test(orig, "GET")
130 self.assertEqual(d, expect, "Error parsing %s" % repr(orig))
131 d = do_test(orig, "POST")
132 self.assertEqual(d, expect, "Error parsing %s" % repr(orig))
133
134 env = {'QUERY_STRING': orig}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000135 fs = cgi.FieldStorage(environ=env)
136 if type(expect) == type({}):
137 # test dict interface
Georg Brandl49d1b4f2008-05-11 21:42:51 +0000138 self.assertEqual(len(expect), len(fs))
139 self.assertEqual(norm(expect.keys()), norm(fs.keys()))
140 ##self.assertEqual(norm(expect.values()), norm(fs.values()))
141 ##self.assertEqual(norm(expect.items()), norm(fs.items()))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000142 self.assertEqual(fs.getvalue("nonexistent field", "default"), "default")
143 # test individual fields
144 for key in expect.keys():
145 expect_val = expect[key]
Georg Brandlab91fde2009-08-13 08:51:18 +0000146 self.assertTrue(key in fs)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000147 if len(expect_val) > 1:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000148 self.assertEqual(fs.getvalue(key), expect_val)
149 else:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150 self.assertEqual(fs.getvalue(key), expect_val[0])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000151
152 def test_log(self):
153 cgi.log("Testing")
154
155 cgi.logfp = StringIO()
156 cgi.initlog("%s", "Testing initlog 1")
157 cgi.log("%s", "Testing log 2")
158 self.assertEqual(cgi.logfp.getvalue(), "Testing initlog 1\nTesting log 2\n")
159 if os.path.exists("/dev/null"):
160 cgi.logfp = None
161 cgi.logfile = "/dev/null"
162 cgi.initlog("%s", "Testing log 3")
163 cgi.log("Testing log 4")
164
165 def test_fieldstorage_readline(self):
166 # FieldStorage uses readline, which has the capacity to read all
167 # contents of the input file into memory; we use readline's size argument
168 # to prevent that for files that do not contain any newlines in
169 # non-GET/HEAD requests
170 class TestReadlineFile:
171 def __init__(self, file):
172 self.file = file
173 self.numcalls = 0
174
175 def readline(self, size=None):
176 self.numcalls += 1
177 if size:
178 return self.file.readline(size)
Jeremy Hyltond9827c42000-08-03 22:11:43 +0000179 else:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000180 return self.file.readline()
Jeremy Hyltond9827c42000-08-03 22:11:43 +0000181
Thomas Wouters89f507f2006-12-13 04:49:30 +0000182 def __getattr__(self, name):
183 file = self.__dict__['file']
184 a = getattr(file, name)
185 if not isinstance(a, int):
186 setattr(self, name, a)
187 return a
Jeremy Hyltond9827c42000-08-03 22:11:43 +0000188
Guido van Rossuma1a68522007-08-28 03:11:34 +0000189 f = TestReadlineFile(tempfile.TemporaryFile("w+"))
190 f.write('x' * 256 * 1024)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000191 f.seek(0)
192 env = {'REQUEST_METHOD':'PUT'}
193 fs = cgi.FieldStorage(fp=f, environ=env)
194 # if we're not chunking properly, readline is only called twice
195 # (by read_binary); if we are chunking properly, it will be called 5 times
196 # as long as the chunksize is 1 << 16.
Georg Brandlab91fde2009-08-13 08:51:18 +0000197 self.assertTrue(f.numcalls > 2)
Jeremy Hyltond9827c42000-08-03 22:11:43 +0000198
Thomas Wouters89f507f2006-12-13 04:49:30 +0000199 def test_fieldstorage_multipart(self):
200 #Test basic FieldStorage multipart parsing
201 env = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE':'multipart/form-data; boundary=---------------------------721837373350705526688164684', 'CONTENT_LENGTH':'558'}
202 postdata = """-----------------------------721837373350705526688164684
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000203Content-Disposition: form-data; name="id"
204
2051234
206-----------------------------721837373350705526688164684
207Content-Disposition: form-data; name="title"
208
209
210-----------------------------721837373350705526688164684
211Content-Disposition: form-data; name="file"; filename="test.txt"
212Content-Type: text/plain
213
214Testing 123.
215
216-----------------------------721837373350705526688164684
217Content-Disposition: form-data; name="submit"
218
219 Add\x20
220-----------------------------721837373350705526688164684--
221"""
Thomas Wouters89f507f2006-12-13 04:49:30 +0000222 fs = cgi.FieldStorage(fp=StringIO(postdata), environ=env)
223 self.assertEquals(len(fs.list), 4)
224 expect = [{'name':'id', 'filename':None, 'value':'1234'},
225 {'name':'title', 'filename':None, 'value':''},
Barry Warsaw596097e2008-06-12 02:38:51 +0000226 {'name':'file', 'filename':'test.txt', 'value':'Testing 123.'},
Thomas Wouters89f507f2006-12-13 04:49:30 +0000227 {'name':'submit', 'filename':None, 'value':' Add '}]
228 for x in range(len(fs.list)):
229 for k, exp in expect[x].items():
230 got = getattr(fs.list[x], k)
231 self.assertEquals(got, exp)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000232
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000233 _qs_result = {
234 'key1': 'value1',
235 'key2': ['value2x', 'value2y'],
236 'key3': 'value3',
237 'key4': 'value4'
238 }
239 def testQSAndUrlEncode(self):
240 data = "key2=value2x&key3=value3&key4=value4"
241 environ = {
242 'CONTENT_LENGTH': str(len(data)),
243 'CONTENT_TYPE': 'application/x-www-form-urlencoded',
244 'QUERY_STRING': 'key1=value1&key2=value2y',
245 'REQUEST_METHOD': 'POST',
246 }
247 v = gen_result(data, environ)
248 self.assertEqual(self._qs_result, v)
249
250 def testQSAndFormData(self):
251 data = """
252---123
253Content-Disposition: form-data; name="key2"
254
255value2y
256---123
257Content-Disposition: form-data; name="key3"
258
259value3
260---123
261Content-Disposition: form-data; name="key4"
262
263value4
264---123--
265"""
266 environ = {
267 'CONTENT_LENGTH': str(len(data)),
268 'CONTENT_TYPE': 'multipart/form-data; boundary=-123',
269 'QUERY_STRING': 'key1=value1&key2=value2x',
270 'REQUEST_METHOD': 'POST',
271 }
272 v = gen_result(data, environ)
273 self.assertEqual(self._qs_result, v)
274
275 def testQSAndFormDataFile(self):
276 data = """
277---123
278Content-Disposition: form-data; name="key2"
279
280value2y
281---123
282Content-Disposition: form-data; name="key3"
283
284value3
285---123
286Content-Disposition: form-data; name="key4"
287
288value4
289---123
290Content-Disposition: form-data; name="upload"; filename="fake.txt"
291Content-Type: text/plain
292
293this is the content of the fake file
294
295---123--
296"""
297 environ = {
298 'CONTENT_LENGTH': str(len(data)),
299 'CONTENT_TYPE': 'multipart/form-data; boundary=-123',
300 'QUERY_STRING': 'key1=value1&key2=value2x',
301 'REQUEST_METHOD': 'POST',
302 }
303 result = self._qs_result.copy()
304 result.update({
305 'upload': 'this is the content of the fake file'
306 })
307 v = gen_result(data, environ)
308 self.assertEqual(result, v)
309
Facundo Batistabd18fd62008-09-08 00:23:29 +0000310 def test_deprecated_parse_qs(self):
311 # this func is moved to urlparse, this is just a sanity check
Facundo Batistaa27244b2008-09-09 02:43:19 +0000312 with catch_warnings():
313 filterwarnings('ignore',
314 'cgi.parse_qs is deprecated, use urllib.parse.parse_qs instead',
315 DeprecationWarning)
316 self.assertEqual({'a': ['A1'], 'B': ['B3'], 'b': ['B2']},
317 cgi.parse_qs('a=A1&b=B2&B=B3'))
Facundo Batistabd18fd62008-09-08 00:23:29 +0000318
319 def test_deprecated_parse_qsl(self):
320 # this func is moved to urlparse, this is just a sanity check
Facundo Batistaa27244b2008-09-09 02:43:19 +0000321 with catch_warnings():
322 filterwarnings('ignore',
323 'cgi.parse_qsl is deprecated, use urllib.parse.parse_qsl instead',
324 DeprecationWarning)
325 self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')],
326 cgi.parse_qsl('a=A1&b=B2&B=B3'))
Facundo Batistabd18fd62008-09-08 00:23:29 +0000327
Fred Drake9a0a65b2008-12-04 19:24:50 +0000328 def test_parse_header(self):
329 self.assertEqual(
330 cgi.parse_header("text/plain"),
331 ("text/plain", {}))
332 self.assertEqual(
333 cgi.parse_header("text/vnd.just.made.this.up ; "),
334 ("text/vnd.just.made.this.up", {}))
335 self.assertEqual(
336 cgi.parse_header("text/plain;charset=us-ascii"),
337 ("text/plain", {"charset": "us-ascii"}))
338 self.assertEqual(
339 cgi.parse_header('text/plain ; charset="us-ascii"'),
340 ("text/plain", {"charset": "us-ascii"}))
341 self.assertEqual(
342 cgi.parse_header('text/plain ; charset="us-ascii"; another=opt'),
343 ("text/plain", {"charset": "us-ascii", "another": "opt"}))
344 self.assertEqual(
345 cgi.parse_header('attachment; filename="silly.txt"'),
346 ("attachment", {"filename": "silly.txt"}))
347 self.assertEqual(
348 cgi.parse_header('attachment; filename="strange;name"'),
349 ("attachment", {"filename": "strange;name"}))
350 self.assertEqual(
351 cgi.parse_header('attachment; filename="strange;name";size=123;'),
352 ("attachment", {"filename": "strange;name", "size": "123"}))
353
354
Thomas Wouters89f507f2006-12-13 04:49:30 +0000355def test_main():
356 run_unittest(CgiTests)
357
358if __name__ == '__main__':
359 test_main()