[web/json] Controller.publish must return encoded data
We were mixing binary from super() and text from our params, thus
wrongly returning text. Closes #5507573.
--- a/web/test/unittest_views_json.py Wed Apr 15 22:48:20 2015 +0200
+++ b/web/test/unittest_views_json.py Thu Jun 18 18:22:55 2015 +0200
@@ -43,10 +43,11 @@
def test_json_rsetexport_with_jsonp(self):
with self.admin_access.web_request() as req:
- req.form.update({'callback': 'foo',
- 'rql': 'Any GN,COUNT(X) GROUPBY GN ORDERBY GN '
+ req.form.update({'callback': u'foo',
+ '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.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)
--- a/web/views/json.py Wed Apr 15 22:48:20 2015 +0200
+++ b/web/views/json.py Thu Jun 18 18:22:55 2015 +0200
@@ -60,11 +60,11 @@
def _get_json_data(self, rset):
json_data = super(JsonpController, self).publish(rset)
if 'callback' in self._cw.form: # jsonp
- json_padding = self._cw.form['callback']
- # use ``application/javascript`` is ``callback`` parameter is
- # provided, let ``application/json`` otherwise
+ json_padding = self._cw.form['callback'].encode('ascii')
+ # use ``application/javascript`` if ``callback`` parameter is
+ # provided, keep ``application/json`` otherwise
self._cw.set_content_type('application/javascript')
- json_data = '%s(%s)' % (json_padding, json_data)
+ json_data = b'%s(%s)' % (json_padding, json_data)
return json_data