MCPcopy
hub / github.com/pallets/flask / EnvironBuilder

Class EnvironBuilder

src/flask/testing.py:27–94  ·  view source on GitHub ↗

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 relativ

Source from the content-addressed store, hash-verified

25
26
27class EnvironBuilder(werkzeug.test.EnvironBuilder):
28 """An :class:`~werkzeug.test.EnvironBuilder`, that takes defaults from the
29 application.
30
31 :param app: The Flask application to configure the environment from.
32 :param path: URL path being requested.
33 :param base_url: Base URL where the app is being served, which
34 ``path`` is relative to. If not given, built from
35 :data:`PREFERRED_URL_SCHEME`, ``subdomain``,
36 :data:`SERVER_NAME`, and :data:`APPLICATION_ROOT`.
37 :param subdomain: Subdomain name to append to :data:`SERVER_NAME`.
38 :param url_scheme: Scheme to use instead of
39 :data:`PREFERRED_URL_SCHEME`.
40 :param json: If given, this is serialized as JSON and passed as
41 ``data``. Also defaults ``content_type`` to
42 ``application/json``.
43 :param args: other positional arguments passed to
44 :class:`~werkzeug.test.EnvironBuilder`.
45 :param kwargs: other keyword arguments passed to
46 :class:`~werkzeug.test.EnvironBuilder`.
47 """
48
49 def __init__(
50 self,
51 app: Flask,
52 path: str = "/",
53 base_url: str | None = None,
54 subdomain: str | None = None,
55 url_scheme: str | None = None,
56 *args: t.Any,
57 **kwargs: t.Any,
58 ) -> None:
59 assert not (base_url or subdomain or url_scheme) or (
60 base_url is not None
61 ) != bool(subdomain or url_scheme), (
62 'Cannot pass "subdomain" or "url_scheme" with "base_url".'
63 )
64
65 if base_url is None:
66 http_host = app.config.get("SERVER_NAME") or "localhost"
67 app_root = app.config["APPLICATION_ROOT"]
68
69 if subdomain:
70 http_host = f"{subdomain}.{http_host}"
71
72 if url_scheme is None:
73 url_scheme = app.config["PREFERRED_URL_SCHEME"]
74
75 url = urlsplit(path)
76 base_url = (
77 f"{url.scheme or url_scheme}://{url.netloc or http_host}"
78 f"/{app_root.lstrip('/')}"
79 )
80 path = url.path
81
82 if url.query:
83 path = f"{path}?{url.query}"
84

Calls

no outgoing calls