forked from orbit-oss/flask
Require that cookies are enabled in the test client for session transactions
This commit is contained in:
parent
5235c3e37e
commit
c8ec453d86
2 changed files with 18 additions and 5 deletions
|
|
@ -53,10 +53,12 @@ class FlaskClient(Client):
|
|||
:meth:`~flask.Flask.test_request_context` which are directly
|
||||
passed through.
|
||||
"""
|
||||
if self.cookie_jar is None:
|
||||
raise RuntimeError('Session transactions only make sense '
|
||||
'with cookies enabled.')
|
||||
app = self.application
|
||||
environ_overrides = kwargs.pop('environ_overrides', {})
|
||||
if self.cookie_jar is not None:
|
||||
self.cookie_jar.inject_wsgi(environ_overrides)
|
||||
self.cookie_jar.inject_wsgi(environ_overrides)
|
||||
outer_reqctx = _request_ctx_stack.top
|
||||
with app.test_request_context(*args, **kwargs) as c:
|
||||
sess = app.open_session(c.request)
|
||||
|
|
@ -80,9 +82,8 @@ class FlaskClient(Client):
|
|||
resp = app.response_class()
|
||||
if not app.session_interface.is_null_session(sess):
|
||||
app.save_session(sess, resp)
|
||||
if self.cookie_jar is not None:
|
||||
headers = resp.get_wsgi_headers(c.request.environ)
|
||||
self.cookie_jar.extract_wsgi(c.request.environ, headers)
|
||||
headers = resp.get_wsgi_headers(c.request.environ)
|
||||
self.cookie_jar.extract_wsgi(c.request.environ, headers)
|
||||
|
||||
def open(self, *args, **kwargs):
|
||||
if self.context_preserved:
|
||||
|
|
|
|||
|
|
@ -84,6 +84,18 @@ class TestToolsTestCase(FlaskTestCase):
|
|||
with c.session_transaction():
|
||||
self.assert_(req is flask.request._get_current_object())
|
||||
|
||||
def test_session_transaction_needs_cookies(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
c = app.test_client(use_cookies=False)
|
||||
try:
|
||||
with c.session_transaction() as s:
|
||||
pass
|
||||
except RuntimeError, e:
|
||||
self.assert_('cookies' in str(e))
|
||||
else:
|
||||
self.fail('Expected runtime error')
|
||||
|
||||
def test_test_client_context_binding(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue