[Python-checkins] r88700 - in python/branches/py3k: Lib/test/test_cgi.py Misc/NEWS

victor.stinner python-checkins at python.org
Wed Mar 2 00:08:36 CET 2011


Author: victor.stinner
Date: Wed Mar  2 00:08:36 2011
New Revision: 88700

Log:
Issue #10911: Add tests on CGI with non-ASCII characters

Patch written by Pierre Quentel

Modified:
   python/branches/py3k/Lib/test/test_cgi.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/test/test_cgi.py
==============================================================================
--- python/branches/py3k/Lib/test/test_cgi.py	(original)
+++ python/branches/py3k/Lib/test/test_cgi.py	Wed Mar  2 00:08:36 2011
@@ -197,30 +197,12 @@
 
     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 @@
                 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 @@
             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)

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Wed Mar  2 00:08:36 2011
@@ -122,6 +122,9 @@
 Tests
 -----
 
+- Issue #10911: Add tests on CGI with non-ASCII characters. Patch written by
+  Pierre Quentel.
+
 - Issue #9931: Fix hangs in GUI tests under Windows in certain conditions.
   Patch by Hirokazu Yamamoto.
 


More information about the Python-checkins mailing list