[web/views] Controllers should always return bytes
authorRémi Cardona <remi.cardona@logilab.fr>
Tue, 08 Dec 2015 16:27:05 +0100
changeset 10968 9c6c3e68422e
parent 10967 3f620fd1ed18
child 10969 b4de8b1cc135
[web/views] Controllers should always return bytes Previous code breaks only on py3k.
web/test/unittest_views_basecontrollers.py
web/views/basecontrollers.py
--- a/web/test/unittest_views_basecontrollers.py	Wed Dec 09 15:51:20 2015 +0100
+++ b/web/test/unittest_views_basecontrollers.py	Tue Dec 08 16:27:05 2015 +0100
@@ -23,12 +23,12 @@
 import lxml
 
 from logilab.common.testlib import unittest_main
-
 from logilab.common.decorators import monkeypatch
 
 from cubicweb import Binary, NoSelectableObject, ValidationError
 from cubicweb.schema import RRQLExpression
 from cubicweb.devtools.testlib import CubicWebTC
+from cubicweb.devtools.webtest import CubicWebTestTC
 from cubicweb.utils import json_dumps
 from cubicweb.uilib import rql_for_eid
 from cubicweb.web import Redirect, RemoteCallFailed
@@ -41,6 +41,19 @@
 from cubicweb.server.hook import Hook, Operation
 from cubicweb.predicates import is_instance
 
+
+class ViewControllerTC(CubicWebTestTC):
+    def test_view_ctrl_with_valid_cache_headers(self):
+        resp = self.webapp.get('/manage')
+        self.assertEqual(resp.etag, 'manage/guests')
+        self.assertEqual(resp.status_code, 200)
+        cache_headers = {'if-modified-since': resp.headers['Last-Modified'],
+                         'if-none-match': resp.etag}
+        resp = self.webapp.get('/manage', headers=cache_headers)
+        self.assertEqual(resp.status_code, 304)
+        self.assertEqual(len(resp.body), 0)
+
+
 def req_form(user):
     return {'eid': [str(user.eid)],
             '_cw_entity_fields:%s' % user.eid: '_cw_generic_field',
--- a/web/views/basecontrollers.py	Wed Dec 09 15:51:20 2015 +0100
+++ b/web/views/basecontrollers.py	Tue Dec 08 16:27:05 2015 +0100
@@ -128,7 +128,7 @@
         view, rset = self._select_view_and_rset(rset)
         view.set_http_cache_headers()
         if self._cw.is_client_cache_valid():
-            return ''
+            return b''
         template = self.appli.main_template_id(self._cw)
         return self._cw.vreg['views'].main_template(self._cw, template,
                                                     rset=rset, view=view)