diff options
Diffstat (limited to 'Lib/test/test_cgi.py')
-rw-r--r-- | Lib/test/test_cgi.py | 136 |
1 files changed, 79 insertions, 57 deletions
diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py index 16c62041255..d745199464a 100644 --- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -1,11 +1,10 @@ -from test.support import run_unittest +from test.support import run_unittest, check_warnings import cgi import os import sys import tempfile import unittest -from io import StringIO -from warnings import catch_warnings, filterwarnings +from io import StringIO, BytesIO class HackedSysModule: # The regression test will have real values in sys.argv, which @@ -15,11 +14,6 @@ class HackedSysModule: cgi.sys = HackedSysModule() -try: - from io import StringIO -except ImportError: - from io import StringIO - class ComparableException: def __init__(self, err): self.err = err @@ -43,7 +37,7 @@ def do_test(buf, method): env['REQUEST_METHOD'] = 'GET' env['QUERY_STRING'] = buf elif method == "POST": - fp = StringIO(buf) + fp = BytesIO(buf.encode('latin-1')) # FieldStorage expects bytes env['REQUEST_METHOD'] = 'POST' env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded' env['CONTENT_LENGTH'] = str(len(buf)) @@ -111,13 +105,14 @@ def first_second_elts(list): return [(p[0], p[1][0]) for p in list] def gen_result(data, environ): - fake_stdin = StringIO(data) + encoding = 'latin-1' + fake_stdin = BytesIO(data.encode(encoding)) fake_stdin.seek(0) - form = cgi.FieldStorage(fp=fake_stdin, environ=environ) + form = cgi.FieldStorage(fp=fake_stdin, environ=environ, encoding=encoding) result = {} for k, v in dict(form).items(): - result[k] = type(v) is list and form.getlist(k) or v.value + result[k] = isinstance(v, list) and form.getlist(k) or v.value return result @@ -127,23 +122,23 @@ class CgiTests(unittest.TestCase): for orig, expect in parse_strict_test_cases: # Test basic parsing d = do_test(orig, "GET") - self.assertEqual(d, expect, "Error parsing %s" % repr(orig)) + self.assertEqual(d, expect, "Error parsing %s method GET" % repr(orig)) d = do_test(orig, "POST") - self.assertEqual(d, expect, "Error parsing %s" % repr(orig)) + self.assertEqual(d, expect, "Error parsing %s method POST" % repr(orig)) env = {'QUERY_STRING': orig} fs = cgi.FieldStorage(environ=env) - if type(expect) == type({}): + if isinstance(expect, dict): # test dict interface self.assertEqual(len(expect), len(fs)) - self.assertEqual(norm(expect.keys()), norm(fs.keys())) + self.assertCountEqual(expect.keys(), fs.keys()) ##self.assertEqual(norm(expect.values()), norm(fs.values())) ##self.assertEqual(norm(expect.items()), norm(fs.items())) self.assertEqual(fs.getvalue("nonexistent field", "default"), "default") # test individual fields for key in expect.keys(): expect_val = expect[key] - self.assertTrue(key in fs) + self.assertIn(key, fs) if len(expect_val) > 1: self.assertEqual(fs.getvalue(key), expect_val) else: @@ -160,6 +155,7 @@ class CgiTests(unittest.TestCase): cgi.logfp = None cgi.logfile = "/dev/null" cgi.initlog("%s", "Testing log 3") + self.addCleanup(cgi.logfp.close) cgi.log("Testing log 4") def test_fieldstorage_readline(self): @@ -186,50 +182,52 @@ class CgiTests(unittest.TestCase): setattr(self, name, a) return a - f = TestReadlineFile(tempfile.TemporaryFile("w+")) - f.write('x' * 256 * 1024) + f = TestReadlineFile(tempfile.TemporaryFile("wb+")) + self.addCleanup(f.close) + f.write(b'x' * 256 * 1024) f.seek(0) env = {'REQUEST_METHOD':'PUT'} fs = cgi.FieldStorage(fp=f, environ=env) + self.addCleanup(fs.file.close) # if we're not chunking properly, readline is only called twice # (by read_binary); if we are chunking properly, it will be called 5 times # as long as the chunksize is 1 << 16. self.assertTrue(f.numcalls > 2) + f.close() def test_fieldstorage_multipart(self): #Test basic FieldStorage multipart parsing - env = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE':'multipart/form-data; boundary=---------------------------721837373350705526688164684', 'CONTENT_LENGTH':'558'} - postdata = """-----------------------------721837373350705526688164684 -Content-Disposition: form-data; name="id" - -1234 ------------------------------721837373350705526688164684 -Content-Disposition: form-data; name="title" - - ------------------------------721837373350705526688164684 -Content-Disposition: form-data; name="file"; filename="test.txt" -Content-Type: text/plain - -Testing 123. - ------------------------------721837373350705526688164684 -Content-Disposition: form-data; name="submit" - - Add\x20 ------------------------------721837373350705526688164684-- -""" - fs = cgi.FieldStorage(fp=StringIO(postdata), environ=env) + env = { + 'REQUEST_METHOD': 'POST', + 'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY), + 'CONTENT_LENGTH': '558'} + fp = BytesIO(POSTDATA.encode('latin-1')) + fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1") self.assertEqual(len(fs.list), 4) expect = [{'name':'id', 'filename':None, 'value':'1234'}, {'name':'title', 'filename':None, 'value':''}, - {'name':'file', 'filename':'test.txt', 'value':'Testing 123.'}, + {'name':'file', 'filename':'test.txt', 'value':b'Testing 123.\n'}, {'name':'submit', 'filename':None, 'value':' Add '}] for x in range(len(fs.list)): for k, exp in expect[x].items(): got = getattr(fs.list[x], k) self.assertEqual(got, exp) + def test_fieldstorage_multipart_non_ascii(self): + #Test basic FieldStorage multipart parsing + env = {'REQUEST_METHOD':'POST', + 'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY), + 'CONTENT_LENGTH':'558'} + for encoding in ['iso-8859-1','utf-8']: + fp = BytesIO(POSTDATA_NON_ASCII.encode(encoding)) + fs = cgi.FieldStorage(fp, environ=env,encoding=encoding) + self.assertEqual(len(fs.list), 1) + expect = [{'name':'id', 'filename':None, 'value':'\xe7\xf1\x80'}] + for x in range(len(fs.list)): + for k, exp in expect[x].items(): + got = getattr(fs.list[x], k) + self.assertEqual(got, exp) + _qs_result = { 'key1': 'value1', 'key2': ['value2x', 'value2y'], @@ -248,8 +246,7 @@ Content-Disposition: form-data; name="submit" self.assertEqual(self._qs_result, v) def testQSAndFormData(self): - data = """ ----123 + data = """---123 Content-Disposition: form-data; name="key2" value2y @@ -273,8 +270,7 @@ value4 self.assertEqual(self._qs_result, v) def testQSAndFormDataFile(self): - data = """ ----123 + data = """---123 Content-Disposition: form-data; name="key2" value2y @@ -302,26 +298,22 @@ this is the content of the fake file } result = self._qs_result.copy() result.update({ - 'upload': 'this is the content of the fake file' + 'upload': b'this is the content of the fake file\n' }) v = gen_result(data, environ) self.assertEqual(result, v) def test_deprecated_parse_qs(self): - # this func is moved to urlparse, this is just a sanity check - with catch_warnings(): - filterwarnings('ignore', - 'cgi.parse_qs is deprecated, use urllib.parse.parse_qs instead', - DeprecationWarning) + # this func is moved to urllib.parse, this is just a sanity check + with check_warnings(('cgi.parse_qs is deprecated, use urllib.parse.' + 'parse_qs instead', DeprecationWarning)): self.assertEqual({'a': ['A1'], 'B': ['B3'], 'b': ['B2']}, cgi.parse_qs('a=A1&b=B2&B=B3')) def test_deprecated_parse_qsl(self): - # this func is moved to urlparse, this is just a sanity check - with catch_warnings(): - filterwarnings('ignore', - 'cgi.parse_qsl is deprecated, use urllib.parse.parse_qsl instead', - DeprecationWarning) + # this func is moved to urllib.parse, this is just a sanity check + with check_warnings(('cgi.parse_qsl is deprecated, use urllib.parse.' + 'parse_qsl instead', DeprecationWarning)): self.assertEqual([('a', 'A1'), ('b', 'B2'), ('B', 'B3')], cgi.parse_qsl('a=A1&b=B2&B=B3')) @@ -351,6 +343,36 @@ this is the content of the fake file cgi.parse_header('attachment; filename="strange;name";size=123;'), ("attachment", {"filename": "strange;name", "size": "123"})) +BOUNDARY = "---------------------------721837373350705526688164684" + +POSTDATA = """-----------------------------721837373350705526688164684 +Content-Disposition: form-data; name="id" + +1234 +-----------------------------721837373350705526688164684 +Content-Disposition: form-data; name="title" + + +-----------------------------721837373350705526688164684 +Content-Disposition: form-data; name="file"; filename="test.txt" +Content-Type: text/plain + +Testing 123. + +-----------------------------721837373350705526688164684 +Content-Disposition: form-data; name="submit" + + Add\x20 +-----------------------------721837373350705526688164684-- +""" + +POSTDATA_NON_ASCII = """-----------------------------721837373350705526688164684 +Content-Disposition: form-data; name="id" + +\xe7\xf1\x80 +-----------------------------721837373350705526688164684 +""" + def test_main(): run_unittest(CgiTests) |