On exceptions from CW, copy headers
authorChristophe de Vienne <christophe@unlish.com>
Tue, 10 Feb 2015 16:35:06 +0100
changeset 11556 1eeba41a2e95
parent 11555 03db03f5c303
child 11557 e6bfa34183d3
On exceptions from CW, copy headers Closes #4939219
pyramid_cubicweb/core.py
pyramid_cubicweb/tests/test_core.py
--- a/pyramid_cubicweb/core.py	Tue Feb 10 10:23:20 2015 +0100
+++ b/pyramid_cubicweb/core.py	Tue Feb 10 16:35:06 2015 +0100
@@ -1,3 +1,5 @@
+import itertools
+
 from contextlib import contextmanager
 from warnings import warn
 from cgi import FieldStorage
@@ -70,6 +72,12 @@
             self._protect_data_access = False
 
 
+def cw_headers(request):
+    return itertools.chain(
+        *[[(k, item) for item in v]
+          for k, v in request.cw_request.headers_out.getAllRawHeaders()])
+
+
 @contextmanager
 def cw_to_pyramid(request):
     """ Context manager to wrap a call to the cubicweb API.
@@ -80,7 +88,8 @@
         yield
     except cubicweb.web.Redirect as ex:
         assert 300 <= ex.status < 400
-        raise httpexceptions.status_map[ex.status](ex.location)
+        raise httpexceptions.status_map[ex.status](
+            ex.location, headers=cw_headers(request))
     except cubicweb.web.StatusResponse as ex:
         warn('[3.16] StatusResponse is deprecated use req.status_out',
              DeprecationWarning, stacklevel=2)
@@ -91,13 +100,15 @@
             request.cw_request._(
                 'You\'re not authorized to access this page. '
                 'If you think you should, please contact the site '
-                'administrator.'))
+                'administrator.'),
+            headers=cw_headers(request))
     except cubicweb.web.Forbidden:
         raise httpexceptions.HTTPForbidden(
             request.cw_request._(
                 'This action is forbidden. '
                 'If you think it should be allowed, please contact the site '
-                'administrator.'))
+                'administrator.'),
+            headers=cw_headers(request))
     except (rql.BadRQLQuery, cubicweb.web.RequestError) as ex:
         raise
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyramid_cubicweb/tests/test_core.py	Tue Feb 10 16:35:06 2015 +0100
@@ -0,0 +1,25 @@
+from pyramid_cubicweb.tests import PyramidCWTest
+
+from cubicweb.view import View
+from cubicweb.web import Redirect
+
+
+class Redirector(View):
+    __regid__ = 'redirector'
+
+    def call(self, rset=None):
+        self._cw.set_header('Cache-Control', 'no-cache')
+        raise Redirect('http://example.org')
+
+
+class CoreTest(PyramidCWTest):
+    anonymous_allowed = True
+
+    def test_cw_to_pyramid_copy_headers_on_redirect(self):
+        self.vreg.register(Redirector)
+        try:
+            res = self.webapp.get('/?vid=redirector', expect_errors=True)
+            self.assertEqual(res.status_int, 303)
+            self.assertEqual(res.headers['Cache-Control'], 'no-cache')
+        finally:
+            self.vreg.unregister(Redirector)