diff options
author | Ryan Petrello <lists@ryanpetrello.com> | 2013-09-30 17:36:08 -0400 |
---|---|---|
committer | Ryan Petrello <lists@ryanpetrello.com> | 2013-09-30 18:00:39 -0400 |
commit | ffefc00dc2832ac7f0f054a37dd391a154f0d863 (patch) | |
tree | 504a858ada92a2d3adafa567d45b539d60aca988 | |
parent | 55075fb8cf58b0c4ba029cd097232118b079185c (diff) | |
download | pecan-ffefc00dc2832ac7f0f054a37dd391a154f0d863.tar.gz |
Persist `pecan.request.context` across internal redirects.
Fixes-bug: 1233367
Change-Id: Iae7926003cd672b874b62418c53e5af227878151
-rw-r--r-- | pecan/core.py | 3 | ||||
-rw-r--r-- | pecan/tests/test_base.py | 34 |
2 files changed, 36 insertions, 1 deletions
diff --git a/pecan/core.py b/pecan/core.py index 6fdf024..668a42a 100644 --- a/pecan/core.py +++ b/pecan/core.py @@ -119,6 +119,7 @@ def redirect(location=None, internal=False, code=None, headers={}, if internal: if code is not None: raise ValueError('Cannot specify a code for internal redirects') + request.environ['pecan.recursive.context'] = request.context raise ForwardRequestException(location) if code is None: code = 302 @@ -561,7 +562,7 @@ class Pecan(object): # handle the request try: # add context and environment to the request - req.context = {} + req.context = environ.get('pecan.recursive.context', {}) req.pecan = dict(content_type=None) self.handle_request(req, resp) diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py index d9808a0..95c09d1 100644 --- a/pecan/tests/test_base.py +++ b/pecan/tests/test_base.py @@ -1,5 +1,6 @@ import sys import os +import json import warnings if sys.version_info < (2, 7): import unittest2 as unittest # pragma: nocover @@ -873,6 +874,39 @@ class TestRedirect(PecanTestCase): assert res.request.environ['HTTP_X_FORWARDED_PROTO'] == 'https' +class TestInternalRedirectContext(PecanTestCase): + + @property + def app_(self): + class RootController(object): + + @expose() + def redirect_with_context(self): + request.context['foo'] = 'bar' + redirect('/testing') + + @expose() + def internal_with_context(self): + request.context['foo'] = 'bar' + redirect('/testing', internal=True) + + @expose('json') + def testing(self): + return request.context + + return TestApp(make_app(RootController(), debug=False)) + + def test_internal_with_request_context(self): + r = self.app_.get('/internal_with_context') + assert r.status_int == 200 + assert json.loads(r.body.decode()) == {'foo': 'bar'} + + def test_context_does_not_bleed(self): + r = self.app_.get('/redirect_with_context').follow() + assert r.status_int == 200 + assert json.loads(r.body.decode()) == {} + + class TestStreamedResponse(PecanTestCase): def test_streaming_response(self): |