Browse Source

Require that cookies are enabled in the test client for session transactions

pull/309/head
Armin Ronacher 14 years ago
parent
commit
c8ec453d86
  1. 11
      flask/testing.py
  2. 12
      flask/testsuite/testing.py

11
flask/testing.py

@ -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:

12
flask/testsuite/testing.py

@ -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…
Cancel
Save