Method: flask.app.Flask.test_request_context
Calls: 66, Exceptions: 0, Paths: 1Back
Path 1: 66 calls (1.0)
() (56) ('/',) (6) ('/?name=World',) (2) ('/somepage',) (1) ('/meh',) (1)
{} (60) dict (4) {'url_scheme': 'https'} (1) {'subdomain': 'xxx'} (1)
1def test_request_context(self, *args: t.Any, **kwargs: t.Any) -> RequestContext:
2 """Create a :class:`~flask.ctx.RequestContext` for a WSGI
3 environment created from the given values. This is mostly useful
4 during testing, where you may want to run a function that uses
5 request data without dispatching a full request.
6
7 See :doc:`/reqcontext`.
8
9 Use a ``with`` block to push the context, which will make
10 :data:`request` point at the request for the created
11 environment. ::
12
13 with app.test_request_context(...):
14 generate_report()
15
16 When using the shell, it may be easier to push and pop the
17 context manually to avoid indentation. ::
18
19 ctx = app.test_request_context(...)
20 ctx.push()
21 ...
22 ctx.pop()
23
24 Takes the same arguments as Werkzeug's
25 :class:`~werkzeug.test.EnvironBuilder`, with some defaults from
26 the application. See the linked Werkzeug docs for most of the
27 available arguments. Flask-specific behavior is listed here.
28
29 :param path: URL path being requested.
30 :param base_url: Base URL where the app is being served, which
31 ``path`` is relative to. If not given, built from
32 :data:`PREFERRED_URL_SCHEME`, ``subdomain``,
33 :data:`SERVER_NAME`, and :data:`APPLICATION_ROOT`.
34 :param subdomain: Subdomain name to append to
35 :data:`SERVER_NAME`.
36 :param url_scheme: Scheme to use instead of
37 :data:`PREFERRED_URL_SCHEME`.
38 :param data: The request body, either as a string or a dict of
39 form keys and values.
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 from .testing import EnvironBuilder
49
50 builder = EnvironBuilder(self, *args, **kwargs)
51
52 try:
53 return self.request_context(builder.get_environ())
54 finally:
55 builder.close()