From 540b4144908362ec1e22866d67cac33a69e8411e Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Sun, 6 Jul 2014 10:27:17 -1000 Subject: [PATCH] Try to decode bytestrings from UTF8 in json_safe Closes #148 --- httpbin/helpers.py | 2 +- test_httpbin.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/httpbin/helpers.py b/httpbin/helpers.py index bce823d..b2264d3 100644 --- a/httpbin/helpers.py +++ b/httpbin/helpers.py @@ -77,6 +77,7 @@ def json_safe(string, content_type='application/octet-stream'): URL scheme was chosen for its simplicity. """ try: + string = string.decode('utf-8') _encoded = json.dumps(string) return string except (ValueError, TypeError): @@ -152,7 +153,6 @@ def get_dict(*keys, **extras): _keys = ('url', 'args', 'form', 'data', 'origin', 'headers', 'files', 'json') assert all(map(_keys.__contains__, keys)) - data = request.data form = request.form form = semiflatten(request.form) diff --git a/test_httpbin.py b/test_httpbin.py index 248ce02..ab3e9bb 100755 --- a/test_httpbin.py +++ b/test_httpbin.py @@ -47,6 +47,10 @@ class HttpbinTestCase(unittest.TestCase): response = self.app.post('/post', data={"file": f.read()}) self.assertEqual(response.status_code, 200) + def test_post_body_unicode(self): + response = self.app.post('/post', data=u'оживлённым'.encode('utf-8')) + self.assertEqual(json.loads(response.data.decode('utf-8'))['data'], u'оживлённым') + def test_post_file_with_missing_content_type_header(self): # I built up the form data manually here because I couldn't find a way # to convince the werkzeug test client to send files without the