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

Method test_request_context

src/flask/app.py:1423–1477  ·  view source on GitHub ↗

Create a :class:`~flask.ctx.RequestContext` for a WSGI environment created from the given values. This is mostly useful during testing, where you may want to run a function that uses request data without dispatching a full request. See :doc:`/reqcontext`. Us

(self, *args: t.Any, **kwargs: t.Any)

Source from the content-addressed store, hash-verified

1421 return RequestContext(self, environ)
1422
1423 def test_request_context(self, *args: t.Any, **kwargs: t.Any) -> RequestContext:
1424 """Create a :class:`~flask.ctx.RequestContext` for a WSGI
1425 environment created from the given values. This is mostly useful
1426 during testing, where you may want to run a function that uses
1427 request data without dispatching a full request.
1428
1429 See :doc:`/reqcontext`.
1430
1431 Use a ``with`` block to push the context, which will make
1432 :data:`request` point at the request for the created
1433 environment. ::
1434
1435 with app.test_request_context(...):
1436 generate_report()
1437
1438 When using the shell, it may be easier to push and pop the
1439 context manually to avoid indentation. ::
1440
1441 ctx = app.test_request_context(...)
1442 ctx.push()
1443 ...
1444 ctx.pop()
1445
1446 Takes the same arguments as Werkzeug's
1447 :class:`~werkzeug.test.EnvironBuilder`, with some defaults from
1448 the application. See the linked Werkzeug docs for most of the
1449 available arguments. Flask-specific behavior is listed here.
1450
1451 :param path: URL path being requested.
1452 :param base_url: Base URL where the app is being served, which
1453 ``path`` is relative to. If not given, built from
1454 :data:`PREFERRED_URL_SCHEME`, ``subdomain``,
1455 :data:`SERVER_NAME`, and :data:`APPLICATION_ROOT`.
1456 :param subdomain: Subdomain name to append to
1457 :data:`SERVER_NAME`.
1458 :param url_scheme: Scheme to use instead of
1459 :data:`PREFERRED_URL_SCHEME`.
1460 :param data: The request body, either as a string or a dict of
1461 form keys and values.
1462 :param json: If given, this is serialized as JSON and passed as
1463 ``data``. Also defaults ``content_type`` to
1464 ``application/json``.
1465 :param args: other positional arguments passed to
1466 :class:`~werkzeug.test.EnvironBuilder`.
1467 :param kwargs: other keyword arguments passed to
1468 :class:`~werkzeug.test.EnvironBuilder`.
1469 """
1470 from .testing import EnvironBuilder
1471
1472 builder = EnvironBuilder(self, *args, **kwargs)
1473
1474 try:
1475 return self.request_context(builder.get_environ())
1476 finally:
1477 builder.close()
1478
1479 def wsgi_app(
1480 self, environ: WSGIEnvironment, start_response: StartResponse

Calls 3

request_contextMethod · 0.95
EnvironBuilderClass · 0.85
closeMethod · 0.80

Tested by

no test coverage detected