diff --git a/flask/testing.py b/flask/testing.py index c4d18ca2..6ce96163 100644 --- a/flask/testing.py +++ b/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: diff --git a/flask/testsuite/testing.py b/flask/testsuite/testing.py index f665e12c..b7a71b1a 100644 --- a/flask/testsuite/testing.py +++ b/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('/')