Home / Class/ EnvironBuilder Class — flask Architecture

EnvironBuilder Class — flask Architecture

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

Entity Profile

Dependency Diagram

graph TD
  7460417c_4950_0f33_33b8_5c4d83805bac["EnvironBuilder"]
  9d1103a6_3809_8e43_8b99_b375b026b496["testing.py"]
  7460417c_4950_0f33_33b8_5c4d83805bac -->|defined in| 9d1103a6_3809_8e43_8b99_b375b026b496
  33dc35c6_266a_5b22_e680_684511859331["__init__()"]
  7460417c_4950_0f33_33b8_5c4d83805bac -->|method| 33dc35c6_266a_5b22_e680_684511859331
  98430786_d5f7_3acc_6fbd_90855f0bc14d["json_dumps()"]
  7460417c_4950_0f33_33b8_5c4d83805bac -->|method| 98430786_d5f7_3acc_6fbd_90855f0bc14d

Relationship Graph

Source Code

src/flask/testing.py lines 27–94

class EnvironBuilder(werkzeug.test.EnvironBuilder):
    """An :class:`~werkzeug.test.EnvironBuilder`, that takes defaults from the
    application.

    :param app: The Flask application to configure the environment from.
    :param path: URL path being requested.
    :param base_url: Base URL where the app is being served, which
        ``path`` is relative to. If not given, built from
        :data:`PREFERRED_URL_SCHEME`, ``subdomain``,
        :data:`SERVER_NAME`, and :data:`APPLICATION_ROOT`.
    :param subdomain: Subdomain name to append to :data:`SERVER_NAME`.
    :param url_scheme: Scheme to use instead of
        :data:`PREFERRED_URL_SCHEME`.
    :param json: If given, this is serialized as JSON and passed as
        ``data``. Also defaults ``content_type`` to
        ``application/json``.
    :param args: other positional arguments passed to
        :class:`~werkzeug.test.EnvironBuilder`.
    :param kwargs: other keyword arguments passed to
        :class:`~werkzeug.test.EnvironBuilder`.
    """

    def __init__(
        self,
        app: Flask,
        path: str = "/",
        base_url: str | None = None,
        subdomain: str | None = None,
        url_scheme: str | None = None,
        *args: t.Any,
        **kwargs: t.Any,
    ) -> None:
        assert not (base_url or subdomain or url_scheme) or (
            base_url is not None
        ) != bool(subdomain or url_scheme), (
            'Cannot pass "subdomain" or "url_scheme" with "base_url".'
        )

        if base_url is None:
            http_host = app.config.get("SERVER_NAME") or "localhost"
            app_root = app.config["APPLICATION_ROOT"]

            if subdomain:
                http_host = f"{subdomain}.{http_host}"

            if url_scheme is None:
                url_scheme = app.config["PREFERRED_URL_SCHEME"]

            url = urlsplit(path)
            base_url = (
                f"{url.scheme or url_scheme}://{url.netloc or http_host}"
                f"/{app_root.lstrip('/')}"
            )
            path = url.path

            if url.query:
                path = f"{path}?{url.query}"

        self.app = app
        super().__init__(path, base_url, *args, **kwargs)

    def json_dumps(self, obj: t.Any, **kwargs: t.Any) -> str:
        """Serialize ``obj`` to a JSON-formatted string.

        The serialization will be configured according to the config associated
        with this EnvironBuilder's ``app``.
        """
        return self.app.json.dumps(obj, **kwargs)

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free