Commits

Gael Pasgrimaud committed e95ac10

fix #25. params are now encoded if a charset is provided

  • Participants
  • Parent commits 27037ab

Comments (0)

Files changed (2)

File tests/test_testing.py

         self.assertTrue(res.content_length > 0)
         self.assertEqual(res.body, to_bytes(''))
 
+    def test_post_unicode(self):
+        res = self.app.post('/', params=dict(a=u('é')),
+               content_type='application/x-www-form-urlencoded;charset=utf8')
+        res.mustcontain('a=%C3%A9')
+
     def test_get_params(self):
         res = self.app.post('/', params=dict(a=1))
         res.mustcontain('a=1')

File webtest/app.py

         """
         environ = self._make_environ(extra_environ)
         # @@: Should this be all non-strings?
-        if isinstance(params, (list, tuple, dict)):
-            params = urlencode(params, doseq=True)
-        if hasattr(params, 'items'):
-            params = urlencode(params.items(), doseq=True)
+        params = encode_params(params, content_type)
         if upload_files or \
             (content_type and to_string(content_type).startswith('multipart')):
             params = cgi.parse_qsl(params, keep_blank_values=True)
                       ('&', '&')]:
         v = v.replace(ent, repl)
     return v
+
+def encode_params(params, content_type):
+    if isinstance(params, dict) or hasattr(params, 'items'):
+        params = list(params.items())
+    if isinstance(params, (list, tuple)):
+        if content_type:
+            content_type = content_type.lower()
+            if 'charset=' in content_type:
+                charset = content_type.split('charset=')[1]
+                charset = charset.strip('; ').lower()
+                encoded_params = []
+                for k, v in params:
+                    if isinstance(v, text_type):
+                        v = v.encode(charset)
+                    encoded_params.append((k, v))
+                params = encoded_params
+        params = urlencode(params, doseq=True)
+    return params