Method: flask.testing.FlaskClient.session_transaction
Calls: 6, Exceptions: 2, Paths: 3Back
Path 1: 4 calls (0.67)
() (4)
{} (4)
SecureCookieSession (4)
1@contextmanager
2 def session_transaction(
3 self, *args: t.Any, **kwargs: t.Any
4 ) -> t.Generator[SessionMixin, None, None]:
5 """When used in combination with a ``with`` statement this opens a
6 session transaction. This can be used to modify the session that
7 the test client uses. Once the ``with`` block is left the session is
8 stored back.
9
10 ::
11
12 with client.session_transaction() as session:
13 session['value'] = 42
14
15 Internally this is implemented by going through a temporary test
16 request context and since session handling could depend on
17 request variables this function accepts the same arguments as
18 :meth:`~flask.Flask.test_request_context` which are directly
19 passed through.
20 """
21 if self._cookies is None:
22 raise TypeError(
23 "Cookies are disabled. Create a client with 'use_cookies=True'."
24 )
25
26 app = self.application
27 ctx = app.test_request_context(*args, **kwargs)
28 self._add_cookies_to_wsgi(ctx.request.environ)
29
30 with ctx:
31 sess = app.session_interface.open_session(app, ctx.request)
32
33 if sess is None:
34 raise RuntimeError("Session backend did not open a session.")
35
36 yield sess
37 resp = app.response_class()
38
39 if app.session_interface.is_null_session(sess):
40 return
41
42 with ctx:
43 app.session_interface.save_session(app, sess, resp)
44
45 self._update_cookies_from_response(
46 ctx.request.host.partition(":")[0],
47 ctx.request.path,
48 resp.headers.getlist("Set-Cookie"),
49 )
Path 2: 1 calls (0.17)
() (1)
{} (1)
RuntimeError (1)
1@contextmanager
2 def session_transaction(
3 self, *args: t.Any, **kwargs: t.Any
4 ) -> t.Generator[SessionMixin, None, None]:
5 """When used in combination with a ``with`` statement this opens a
6 session transaction. This can be used to modify the session that
7 the test client uses. Once the ``with`` block is left the session is
8 stored back.
9
10 ::
11
12 with client.session_transaction() as session:
13 session['value'] = 42
14
15 Internally this is implemented by going through a temporary test
16 request context and since session handling could depend on
17 request variables this function accepts the same arguments as
18 :meth:`~flask.Flask.test_request_context` which are directly
19 passed through.
20 """
21 if self._cookies is None:
22 raise TypeError(
23 "Cookies are disabled. Create a client with 'use_cookies=True'."
24 )
25
26 app = self.application
27 ctx = app.test_request_context(*args, **kwargs)
28 self._add_cookies_to_wsgi(ctx.request.environ)
29
30 with ctx:
31 sess = app.session_interface.open_session(app, ctx.request)
32
33 if sess is None:
34 raise RuntimeError("Session backend did not open a session.")
35
36 yield sess
37 resp = app.response_class()
38
39 if app.session_interface.is_null_session(sess):
40 return
41
42 with ctx:
43 app.session_interface.save_session(app, sess, resp)
44
45 self._update_cookies_from_response(
46 ctx.request.host.partition(":")[0],
47 ctx.request.path,
48 resp.headers.getlist("Set-Cookie"),
49 )
Path 3: 1 calls (0.17)
() (1)
{} (1)
TypeError (1)
1@contextmanager
2 def session_transaction(
3 self, *args: t.Any, **kwargs: t.Any
4 ) -> t.Generator[SessionMixin, None, None]:
5 """When used in combination with a ``with`` statement this opens a
6 session transaction. This can be used to modify the session that
7 the test client uses. Once the ``with`` block is left the session is
8 stored back.
9
10 ::
11
12 with client.session_transaction() as session:
13 session['value'] = 42
14
15 Internally this is implemented by going through a temporary test
16 request context and since session handling could depend on
17 request variables this function accepts the same arguments as
18 :meth:`~flask.Flask.test_request_context` which are directly
19 passed through.
20 """
21 if self._cookies is None:
22 raise TypeError(
23 "Cookies are disabled. Create a client with 'use_cookies=True'."
24 )
25
26 app = self.application
27 ctx = app.test_request_context(*args, **kwargs)
28 self._add_cookies_to_wsgi(ctx.request.environ)
29
30 with ctx:
31 sess = app.session_interface.open_session(app, ctx.request)
32
33 if sess is None:
34 raise RuntimeError("Session backend did not open a session.")
35
36 yield sess
37 resp = app.response_class()
38
39 if app.session_interface.is_null_session(sess):
40 return
41
42 with ctx:
43 app.session_interface.save_session(app, sess, resp)
44
45 self._update_cookies_from_response(
46 ctx.request.host.partition(":")[0],
47 ctx.request.path,
48 resp.headers.getlist("Set-Cookie"),
49 )