[web/views] port JSON views to py3k
authorRémi Cardona <remi.cardona@logilab.fr>
Wed, 16 Sep 2015 18:43:19 +0200
changeset 10700 a6d9d27f4253
parent 10699 9bad9e061932
child 10701 ca3efd72e854
[web/views] port JSON views to py3k
devtools/testlib.py
web/test/unittest_views_json.py
web/views/json.py
--- a/devtools/testlib.py	Wed Sep 16 18:43:02 2015 +0200
+++ b/devtools/testlib.py	Wed Sep 16 18:43:19 2015 +0200
@@ -88,7 +88,7 @@
 
 class JsonValidator(object):
     def parse_string(self, data):
-        return json.loads(data)
+        return json.loads(data.decode('ascii'))
 
 @contextmanager
 def real_error_handling(app):
--- a/web/test/unittest_views_json.py	Wed Sep 16 18:43:02 2015 +0200
+++ b/web/test/unittest_views_json.py	Wed Sep 16 18:43:19 2015 +0200
@@ -16,12 +16,14 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+from six import binary_type
+
 from cubicweb.devtools.testlib import CubicWebTC
 
 
 class JsonViewsTC(CubicWebTC):
     anonymize = True
-    res_jsonp_data = '[["guests", 1]]'
+    res_jsonp_data = b'[["guests", 1]]'
 
     def setUp(self):
         super(JsonViewsTC, self).setUp()
@@ -47,10 +49,10 @@
                              'rql': u'Any GN,COUNT(X) GROUPBY GN ORDERBY GN '
                              'WHERE X in_group G, G name GN'})
             data = self.ctrl_publish(req, ctrl='jsonp')
-            self.assertIsInstance(data, str)
+            self.assertIsInstance(data, binary_type)
             self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript'])
             # because jsonp anonymizes data, only 'guests' group should be found
-            self.assertEqual(data, 'foo(%s)' % self.res_jsonp_data)
+            self.assertEqual(data, b'foo(' + self.res_jsonp_data + b')')
 
     def test_json_rsetexport_with_jsonp_and_bad_vid(self):
         with self.admin_access.web_request() as req:
@@ -61,7 +63,7 @@
             data = self.ctrl_publish(req, ctrl='jsonp')
             self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript'])
             # result should be plain json, not the table view
-            self.assertEqual(data, 'foo(%s)' % self.res_jsonp_data)
+            self.assertEqual(data, b'foo(' + self.res_jsonp_data + b')')
 
     def test_json_ersetexport(self):
         with self.admin_access.web_request() as req:
@@ -79,7 +81,7 @@
 
 class NotAnonymousJsonViewsTC(JsonViewsTC):
     anonymize = False
-    res_jsonp_data = '[["guests", 1], ["managers", 1]]'
+    res_jsonp_data = b'[["guests", 1], ["managers", 1]]'
 
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
--- a/web/views/json.py	Wed Sep 16 18:43:02 2015 +0200
+++ b/web/views/json.py	Wed Sep 16 18:43:19 2015 +0200
@@ -64,7 +64,7 @@
             # use ``application/javascript`` if ``callback`` parameter is
             # provided, keep ``application/json`` otherwise
             self._cw.set_content_type('application/javascript')
-            json_data = b'%s(%s)' % (json_padding, json_data)
+            json_data = json_padding + b'(' + json_data + b')'
         return json_data
 
 
@@ -85,7 +85,8 @@
             indent = int(self._cw.form['_indent'])
         else:
             indent = None
-        self.w(json_dumps(data, indent=indent))
+        # python's json.dumps escapes non-ascii characters
+        self.w(json_dumps(data, indent=indent).encode('ascii'))
 
 
 class JsonRsetView(JsonMixIn, AnyRsetView):