diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-03-01 23:09:57 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-03-01 23:09:57 (GMT) |
commit | f0e293cbcdaabcf6b3a6a4c2470b3fab42f2e960 (patch) | |
tree | 79301ad9f0fb1401db1dbe489bc0278cc1ad1325 /Lib/test/test_cgi.py | |
parent | 2b574a2332a6c909de619f480d02ca144fd2e517 (diff) | |
download | cpython-f0e293cbcdaabcf6b3a6a4c2470b3fab42f2e960.zip cpython-f0e293cbcdaabcf6b3a6a4c2470b3fab42f2e960.tar.gz cpython-f0e293cbcdaabcf6b3a6a4c2470b3fab42f2e960.tar.bz2 |
Merged revisions 88700 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r88700 | victor.stinner | 2011-03-02 00:08:36 +0100 (mer., 02 mars 2011) | 3 lines
Issue #10911: Add tests on CGI with non-ASCII characters
Patch written by Pierre Quentel
........
Diffstat (limited to 'Lib/test/test_cgi.py')
-rw-r--r-- | Lib/test/test_cgi.py | 75 |
1 files changed, 51 insertions, 24 deletions
diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py index 985de11..d745199 100644 --- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -197,30 +197,12 @@ class CgiTests(unittest.TestCase): 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-- -""" - encoding = 'ascii' - fp = BytesIO(postdata.encode(encoding)) - fs = cgi.FieldStorage(fp, environ=env, encoding=encoding) + 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':''}, @@ -231,6 +213,21 @@ Content-Disposition: form-data; name="submit" 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'], @@ -346,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) |