testing.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. from __future__ import annotations
  2. import importlib.metadata
  3. import typing as t
  4. from contextlib import contextmanager
  5. from contextlib import ExitStack
  6. from copy import copy
  7. from types import TracebackType
  8. from urllib.parse import urlsplit
  9. import werkzeug.test
  10. from click.testing import CliRunner
  11. from werkzeug.test import Client
  12. from werkzeug.wrappers import Request as BaseRequest
  13. from .cli import ScriptInfo
  14. from .sessions import SessionMixin
  15. if t.TYPE_CHECKING: # pragma: no cover
  16. from werkzeug.test import TestResponse
  17. from .app import Flask
  18. class EnvironBuilder(werkzeug.test.EnvironBuilder):
  19. """An :class:`~werkzeug.test.EnvironBuilder`, that takes defaults from the
  20. application.
  21. :param app: The Flask application to configure the environment from.
  22. :param path: URL path being requested.
  23. :param base_url: Base URL where the app is being served, which
  24. ``path`` is relative to. If not given, built from
  25. :data:`PREFERRED_URL_SCHEME`, ``subdomain``,
  26. :data:`SERVER_NAME`, and :data:`APPLICATION_ROOT`.
  27. :param subdomain: Subdomain name to append to :data:`SERVER_NAME`.
  28. :param url_scheme: Scheme to use instead of
  29. :data:`PREFERRED_URL_SCHEME`.
  30. :param json: If given, this is serialized as JSON and passed as
  31. ``data``. Also defaults ``content_type`` to
  32. ``application/json``.
  33. :param args: other positional arguments passed to
  34. :class:`~werkzeug.test.EnvironBuilder`.
  35. :param kwargs: other keyword arguments passed to
  36. :class:`~werkzeug.test.EnvironBuilder`.
  37. """
  38. def __init__(
  39. self,
  40. app: Flask,
  41. path: str = "/",
  42. base_url: str | None = None,
  43. subdomain: str | None = None,
  44. url_scheme: str | None = None,
  45. *args: t.Any,
  46. **kwargs: t.Any,
  47. ) -> None:
  48. assert not (base_url or subdomain or url_scheme) or (
  49. base_url is not None
  50. ) != bool(
  51. subdomain or url_scheme
  52. ), 'Cannot pass "subdomain" or "url_scheme" with "base_url".'
  53. if base_url is None:
  54. http_host = app.config.get("SERVER_NAME") or "localhost"
  55. app_root = app.config["APPLICATION_ROOT"]
  56. if subdomain:
  57. http_host = f"{subdomain}.{http_host}"
  58. if url_scheme is None:
  59. url_scheme = app.config["PREFERRED_URL_SCHEME"]
  60. url = urlsplit(path)
  61. base_url = (
  62. f"{url.scheme or url_scheme}://{url.netloc or http_host}"
  63. f"/{app_root.lstrip('/')}"
  64. )
  65. path = url.path
  66. if url.query:
  67. sep = b"?" if isinstance(url.query, bytes) else "?"
  68. path += sep + url.query
  69. self.app = app
  70. super().__init__(path, base_url, *args, **kwargs)
  71. def json_dumps(self, obj: t.Any, **kwargs: t.Any) -> str: # type: ignore
  72. """Serialize ``obj`` to a JSON-formatted string.
  73. The serialization will be configured according to the config associated
  74. with this EnvironBuilder's ``app``.
  75. """
  76. return self.app.json.dumps(obj, **kwargs)
  77. _werkzeug_version = ""
  78. def _get_werkzeug_version() -> str:
  79. global _werkzeug_version
  80. if not _werkzeug_version:
  81. _werkzeug_version = importlib.metadata.version("werkzeug")
  82. return _werkzeug_version
  83. class FlaskClient(Client):
  84. """Works like a regular Werkzeug test client but has knowledge about
  85. Flask's contexts to defer the cleanup of the request context until
  86. the end of a ``with`` block. For general information about how to
  87. use this class refer to :class:`werkzeug.test.Client`.
  88. .. versionchanged:: 0.12
  89. `app.test_client()` includes preset default environment, which can be
  90. set after instantiation of the `app.test_client()` object in
  91. `client.environ_base`.
  92. Basic usage is outlined in the :doc:`/testing` chapter.
  93. """
  94. application: Flask
  95. def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
  96. super().__init__(*args, **kwargs)
  97. self.preserve_context = False
  98. self._new_contexts: list[t.ContextManager[t.Any]] = []
  99. self._context_stack = ExitStack()
  100. self.environ_base = {
  101. "REMOTE_ADDR": "127.0.0.1",
  102. "HTTP_USER_AGENT": f"Werkzeug/{_get_werkzeug_version()}",
  103. }
  104. @contextmanager
  105. def session_transaction(
  106. self, *args: t.Any, **kwargs: t.Any
  107. ) -> t.Generator[SessionMixin, None, None]:
  108. """When used in combination with a ``with`` statement this opens a
  109. session transaction. This can be used to modify the session that
  110. the test client uses. Once the ``with`` block is left the session is
  111. stored back.
  112. ::
  113. with client.session_transaction() as session:
  114. session['value'] = 42
  115. Internally this is implemented by going through a temporary test
  116. request context and since session handling could depend on
  117. request variables this function accepts the same arguments as
  118. :meth:`~flask.Flask.test_request_context` which are directly
  119. passed through.
  120. """
  121. if self._cookies is None:
  122. raise TypeError(
  123. "Cookies are disabled. Create a client with 'use_cookies=True'."
  124. )
  125. app = self.application
  126. ctx = app.test_request_context(*args, **kwargs)
  127. self._add_cookies_to_wsgi(ctx.request.environ)
  128. with ctx:
  129. sess = app.session_interface.open_session(app, ctx.request)
  130. if sess is None:
  131. raise RuntimeError("Session backend did not open a session.")
  132. yield sess
  133. resp = app.response_class()
  134. if app.session_interface.is_null_session(sess):
  135. return
  136. with ctx:
  137. app.session_interface.save_session(app, sess, resp)
  138. self._update_cookies_from_response(
  139. ctx.request.host.partition(":")[0],
  140. ctx.request.path,
  141. resp.headers.getlist("Set-Cookie"),
  142. )
  143. def _copy_environ(self, other):
  144. out = {**self.environ_base, **other}
  145. if self.preserve_context:
  146. out["werkzeug.debug.preserve_context"] = self._new_contexts.append
  147. return out
  148. def _request_from_builder_args(self, args, kwargs):
  149. kwargs["environ_base"] = self._copy_environ(kwargs.get("environ_base", {}))
  150. builder = EnvironBuilder(self.application, *args, **kwargs)
  151. try:
  152. return builder.get_request()
  153. finally:
  154. builder.close()
  155. def open(
  156. self,
  157. *args: t.Any,
  158. buffered: bool = False,
  159. follow_redirects: bool = False,
  160. **kwargs: t.Any,
  161. ) -> TestResponse:
  162. if args and isinstance(
  163. args[0], (werkzeug.test.EnvironBuilder, dict, BaseRequest)
  164. ):
  165. if isinstance(args[0], werkzeug.test.EnvironBuilder):
  166. builder = copy(args[0])
  167. builder.environ_base = self._copy_environ(builder.environ_base or {})
  168. request = builder.get_request()
  169. elif isinstance(args[0], dict):
  170. request = EnvironBuilder.from_environ(
  171. args[0], app=self.application, environ_base=self._copy_environ({})
  172. ).get_request()
  173. else:
  174. # isinstance(args[0], BaseRequest)
  175. request = copy(args[0])
  176. request.environ = self._copy_environ(request.environ)
  177. else:
  178. # request is None
  179. request = self._request_from_builder_args(args, kwargs)
  180. # Pop any previously preserved contexts. This prevents contexts
  181. # from being preserved across redirects or multiple requests
  182. # within a single block.
  183. self._context_stack.close()
  184. response = super().open(
  185. request,
  186. buffered=buffered,
  187. follow_redirects=follow_redirects,
  188. )
  189. response.json_module = self.application.json # type: ignore[assignment]
  190. # Re-push contexts that were preserved during the request.
  191. while self._new_contexts:
  192. cm = self._new_contexts.pop()
  193. self._context_stack.enter_context(cm)
  194. return response
  195. def __enter__(self) -> FlaskClient:
  196. if self.preserve_context:
  197. raise RuntimeError("Cannot nest client invocations")
  198. self.preserve_context = True
  199. return self
  200. def __exit__(
  201. self,
  202. exc_type: type | None,
  203. exc_value: BaseException | None,
  204. tb: TracebackType | None,
  205. ) -> None:
  206. self.preserve_context = False
  207. self._context_stack.close()
  208. class FlaskCliRunner(CliRunner):
  209. """A :class:`~click.testing.CliRunner` for testing a Flask app's
  210. CLI commands. Typically created using
  211. :meth:`~flask.Flask.test_cli_runner`. See :ref:`testing-cli`.
  212. """
  213. def __init__(self, app: Flask, **kwargs: t.Any) -> None:
  214. self.app = app
  215. super().__init__(**kwargs)
  216. def invoke( # type: ignore
  217. self, cli: t.Any = None, args: t.Any = None, **kwargs: t.Any
  218. ) -> t.Any:
  219. """Invokes a CLI command in an isolated environment. See
  220. :meth:`CliRunner.invoke <click.testing.CliRunner.invoke>` for
  221. full method documentation. See :ref:`testing-cli` for examples.
  222. If the ``obj`` argument is not given, passes an instance of
  223. :class:`~flask.cli.ScriptInfo` that knows how to load the Flask
  224. app being tested.
  225. :param cli: Command object to invoke. Default is the app's
  226. :attr:`~flask.app.Flask.cli` group.
  227. :param args: List of strings to invoke the command with.
  228. :return: a :class:`~click.testing.Result` object.
  229. """
  230. if cli is None:
  231. cli = self.app.cli # type: ignore
  232. if "obj" not in kwargs:
  233. kwargs["obj"] = ScriptInfo(create_app=lambda: self.app)
  234. return super().invoke(cli, args, **kwargs)