Home / Class/ FlaskClient Class — flask Architecture

FlaskClient Class — flask Architecture

Architecture documentation for the FlaskClient class in testing.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  bd3a5eb6_81cb_6392_c9b8_19dcbec7785f["FlaskClient"]
  9d1103a6_3809_8e43_8b99_b375b026b496["testing.py"]
  bd3a5eb6_81cb_6392_c9b8_19dcbec7785f -->|defined in| 9d1103a6_3809_8e43_8b99_b375b026b496
  daa23239_2494_91e6_d108_2ec497edf0d3["__init__()"]
  bd3a5eb6_81cb_6392_c9b8_19dcbec7785f -->|method| daa23239_2494_91e6_d108_2ec497edf0d3
  eb7c3517_582e_6dd2_c80b_a8be784317c7["session_transaction()"]
  bd3a5eb6_81cb_6392_c9b8_19dcbec7785f -->|method| eb7c3517_582e_6dd2_c80b_a8be784317c7
  1138f3ac_9053_e93b_3cc9_fa68bae97450["_copy_environ()"]
  bd3a5eb6_81cb_6392_c9b8_19dcbec7785f -->|method| 1138f3ac_9053_e93b_3cc9_fa68bae97450
  4e0af686_482d_2acf_be5d_79822774d7b0["_request_from_builder_args()"]
  bd3a5eb6_81cb_6392_c9b8_19dcbec7785f -->|method| 4e0af686_482d_2acf_be5d_79822774d7b0
  3dfbf89e_0efa_e73a_439f_b07e59e5d57d["open()"]
  bd3a5eb6_81cb_6392_c9b8_19dcbec7785f -->|method| 3dfbf89e_0efa_e73a_439f_b07e59e5d57d
  d675ffc7_27f0_eeeb_d2e1_37d2429047bb["__enter__()"]
  bd3a5eb6_81cb_6392_c9b8_19dcbec7785f -->|method| d675ffc7_27f0_eeeb_d2e1_37d2429047bb
  3ed0e94f_4a94_f3e2_010d_7b26571d5a85["__exit__()"]
  bd3a5eb6_81cb_6392_c9b8_19dcbec7785f -->|method| 3ed0e94f_4a94_f3e2_010d_7b26571d5a85

Relationship Graph

Source Code

src/flask/testing.py lines 109–262

class FlaskClient(Client):
    """Works like a regular Werkzeug test client, with additional behavior for
    Flask. Can defer the cleanup of the request context until the end of a
    ``with`` block. For general information about how to use this class refer to
    :class:`werkzeug.test.Client`.

    .. versionchanged:: 0.12
       `app.test_client()` includes preset default environment, which can be
       set after instantiation of the `app.test_client()` object in
       `client.environ_base`.

    Basic usage is outlined in the :doc:`/testing` chapter.
    """

    application: Flask

    def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
        super().__init__(*args, **kwargs)
        self.preserve_context = False
        self._new_contexts: list[t.ContextManager[t.Any]] = []
        self._context_stack = ExitStack()
        self.environ_base = {
            "REMOTE_ADDR": "127.0.0.1",
            "HTTP_USER_AGENT": f"Werkzeug/{_get_werkzeug_version()}",
        }

    @contextmanager
    def session_transaction(
        self, *args: t.Any, **kwargs: t.Any
    ) -> t.Iterator[SessionMixin]:
        """When used in combination with a ``with`` statement this opens a
        session transaction.  This can be used to modify the session that
        the test client uses.  Once the ``with`` block is left the session is
        stored back.

        ::

            with client.session_transaction() as session:
                session['value'] = 42

        Internally this is implemented by going through a temporary test
        request context and since session handling could depend on
        request variables this function accepts the same arguments as
        :meth:`~flask.Flask.test_request_context` which are directly
        passed through.
        """
        if self._cookies is None:
            raise TypeError(
                "Cookies are disabled. Create a client with 'use_cookies=True'."
            )

        app = self.application
        ctx = app.test_request_context(*args, **kwargs)
        self._add_cookies_to_wsgi(ctx.request.environ)

        with ctx:
            sess = app.session_interface.open_session(app, ctx.request)

        if sess is None:
            raise RuntimeError("Session backend did not open a session.")

        yield sess
        resp = app.response_class()

        if app.session_interface.is_null_session(sess):
            return

        with ctx:
            app.session_interface.save_session(app, sess, resp)

        self._update_cookies_from_response(
            ctx.request.host.partition(":")[0],
            ctx.request.path,
            resp.headers.getlist("Set-Cookie"),
        )

    def _copy_environ(self, other: WSGIEnvironment) -> WSGIEnvironment:
        out = {**self.environ_base, **other}

        if self.preserve_context:
            out["werkzeug.debug.preserve_context"] = self._new_contexts.append

Frequently Asked Questions

What is the FlaskClient class?
FlaskClient is a class in the flask codebase, defined in src/flask/testing.py.
Where is FlaskClient defined?
FlaskClient is defined in src/flask/testing.py at line 109.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free