class EnvironBuilder: (source)
This class can be used to conveniently create a WSGI environment for testing purposes. It can be used to quickly create WSGI environments or request objects from arbitrary data. The signature of this class is also used in some other places as of Werkzeug 0.5 (:func:`create_environ`, :meth:`Response.from_values`, :meth:`Client.open`). Because of this most of the functionality is available through the constructor alone. Files and regular form data can be manipulated independently of each other with the :attr:`form` and :attr:`files` attributes, but are passed with the same argument to the constructor: `data`. `data` can be any of these values: - a `str` or `bytes` object: The object is converted into an :attr:`input_stream`, the :attr:`content_length` is set and you have to provide a :attr:`content_type`. - a `dict` or :class:`MultiDict`: The keys have to be strings. The values have to be either any of the following objects, or a list of any of the following objects: - a :class:`file`-like object: These are converted into :class:`FileStorage` objects automatically. - a `tuple`: The :meth:`~FileMultiDict.add_file` method is called with the key and the unpacked `tuple` items as positional arguments. - a `str`: The string is set as form data for the associated key. - a file-like object: The object content is loaded in memory and then handled like a regular `str` or a `bytes`. :param path: the path of the request. In the WSGI environment this will end up as `PATH_INFO`. If the `query_string` is not defined and there is a question mark in the `path` everything after it is used as query string. :param base_url: the base URL is a URL that is used to extract the WSGI URL scheme, host (server name + server port) and the script root (`SCRIPT_NAME`). :param query_string: an optional string or dict with URL parameters. :param method: the HTTP method to use, defaults to `GET`. :param input_stream: an optional input stream. Do not specify this and `data`. As soon as an input stream is set you can't modify :attr:`args` and :attr:`files` unless you set the :attr:`input_stream` to `None` again. :param content_type: The content type for the request. As of 0.5 you don't have to provide this when specifying files and form data via `data`. :param content_length: The content length for the request. You don't have to specify this when providing data via `data`. :param errors_stream: an optional error stream that is used for `wsgi.errors`. Defaults to :data:`stderr`. :param multithread: controls `wsgi.multithread`. Defaults to `False`. :param multiprocess: controls `wsgi.multiprocess`. Defaults to `False`. :param run_once: controls `wsgi.run_once`. Defaults to `False`. :param headers: an optional list or :class:`Headers` object of headers. :param data: a string or dict of form data or a file-object. See explanation above. :param json: An object to be serialized and assigned to ``data``. Defaults the content type to ``"application/json"``. Serialized with the function assigned to :attr:`json_dumps`. :param environ_base: an optional dict of environment defaults. :param environ_overrides: an optional dict of environment overrides. :param charset: the charset used to encode string data. :param auth: An authorization object to use for the ``Authorization`` header value. A ``(username, password)`` tuple is a shortcut for ``Basic`` authorization. .. versionchanged:: 2.1 ``CONTENT_TYPE`` and ``CONTENT_LENGTH`` are not duplicated as header keys in the environ. .. versionchanged:: 2.0 ``REQUEST_URI`` and ``RAW_URI`` is the full raw URI including the query string, not only the path. .. versionchanged:: 2.0 The default :attr:`request_class` is ``Request`` instead of ``BaseRequest``. .. versionadded:: 2.0 Added the ``auth`` parameter. .. versionadded:: 0.15 The ``json`` param and :meth:`json_dumps` method. .. versionadded:: 0.15 The environ has keys ``REQUEST_URI`` and ``RAW_URI`` containing the path before percent-decoding. This is not part of the WSGI PEP, but many WSGI servers include it. .. versionchanged:: 0.6 ``path`` and ``base_url`` can now be unicode strings that are encoded with :func:`iri_to_uri`.
Class Method | from |
Turn an environ dict back into a builder. Any extra kwargs override the args extracted from the environ. |
Method | __del__ |
Undocumented |
Method | __init__ |
Undocumented |
Method | args |
Undocumented |
Method | base |
Undocumented |
Method | close |
Closes all files. If you put real :class:`file` objects into the :attr:`files` dict you can call this method to automatically close them all in one go. |
Method | content |
Undocumented |
Method | content |
Undocumented |
Method | files |
Undocumented |
Method | form |
Undocumented |
Method | get |
Return the built environ. |
Method | get |
Returns a request with the data. If the request class is not specified :attr:`request_class` is used. |
Method | input |
Undocumented |
Method | mimetype |
Undocumented |
Method | query |
Undocumented |
Class Variable | json |
Undocumented |
Class Variable | server |
Undocumented |
Class Variable | wsgi |
Undocumented |
Instance Variable | charset |
Undocumented |
Instance Variable | closed |
Undocumented |
Instance Variable | content |
The content type for the request. Reflected from and to the :attr:`headers`. Do not set if you set :attr:`files` or :attr:`form` for auto detection. |
Instance Variable | environ |
Undocumented |
Instance Variable | environ |
Undocumented |
Instance Variable | errors |
Undocumented |
Instance Variable | headers |
Undocumented |
Instance Variable | host |
Undocumented |
Instance Variable | method |
Undocumented |
Instance Variable | multiprocess |
Undocumented |
Instance Variable | multithread |
Undocumented |
Instance Variable | path |
Undocumented |
Instance Variable | request |
Undocumented |
Instance Variable | run |
Undocumented |
Instance Variable | script |
Undocumented |
Instance Variable | url |
Undocumented |
Property | args |
The URL arguments as :class:`MultiDict`. |
Property | base |
The base URL is used to extract the URL scheme, host name, port, and root path. |
Property | content |
The content length as integer. Reflected from and to the :attr:`headers`. Do not set if you set :attr:`files` or :attr:`form` for auto detection. |
Property | files |
A :class:`FileMultiDict` of uploaded files. Use :meth:`~FileMultiDict.add_file` to add new files. |
Property | form |
A :class:`MultiDict` of form values. |
Property | input |
An optional input stream. This is mutually exclusive with setting :attr:`form` and :attr:`files`, setting it will clear those. Do not provide this if the method is not ``POST`` or another method that has a body. |
Property | mimetype |
The mimetype (content type without charset etc.) |
Property | mimetype |
The mimetype parameters as dict. For example if the content type is ``text/html; charset=utf-8`` the params would be ``{'charset': 'utf-8'}``. |
Property | query |
The query string. If you set this to a string :attr:`args` will no longer be available. |
Property | server |
The server name (read-only, use :attr:`host` to set) |
Property | server |
The server port as integer (read-only, use :attr:`host` to set) |
Static Method | _make |
Undocumented |
Method | _add |
Called in the EnvironBuilder to add files from the data dict. |
Method | _get |
Common behavior for getting the :attr:`form` and :attr:`files` properties. |
Method | _set |
Common behavior for setting the :attr:`form` and :attr:`files` properties. |
Instance Variable | _args |
Undocumented |
Instance Variable | _files |
Undocumented |
Instance Variable | _form |
Undocumented |
Instance Variable | _input |
Undocumented |
Instance Variable | _query |
Undocumented |
def from_environ(cls, environ:
WSGIEnvironment
, **kwargs: t.Any
) -> EnvironBuilder
:
(source)
¶
Turn an environ dict back into a builder. Any extra kwargs override the args extracted from the environ. .. versionchanged:: 2.0 Path and query values are passed through the WSGI decoding dance to avoid double encoding. .. versionadded:: 0.15
str
= '/', base_url: t.Optional[ str]
= None, query_string: t.Optional[ t.Union[ t.Mapping[ str, str], str]]
= None, method: str
= 'GET', input_stream: t.Optional[ t.IO[ bytes]]
= None, content_type: t.Optional[ str]
= None, content_length: t.Optional[ int]
= None, errors_stream: t.Optional[ t.IO[ str]]
= None, multithread: bool
= False, multiprocess: bool
= False, run_once: bool
= False, headers: t.Optional[ t.Union[ Headers, t.Iterable[ t.Tuple[ str, str]]]]
= None, data: t.Optional[ t.Union[ t.IO[ bytes], str, bytes, t.Mapping[ str, t.Any]]]
= None, environ_base: t.Optional[ t.Mapping[ str, t.Any]]
= None, environ_overrides: t.Optional[ t.Mapping[ str, t.Any]]
= None, charset: str
= 'utf-8', mimetype: t.Optional[ str]
= None, json: t.Optional[ t.Mapping[ str, t.Any]]
= None, auth: t.Optional[ t.Union[ Authorization, t.Tuple[ str, str]]]
= None):
(source)
¶
Undocumented
Closes all files. If you put real :class:`file` objects into the :attr:`files` dict you can call this method to automatically close them all in one go.
Return the built environ. .. versionchanged:: 0.15 The content type and length headers are set based on input stream detection. Previously this only set the WSGI keys.
Returns a request with the data. If the request class is not specified :attr:`request_class` is used. :param cls: The request wrapper to use.
The content type for the request. Reflected from and to the :attr:`headers`. Do not set if you set :attr:`files` or :attr:`form` for auto detection.
The content length as integer. Reflected from and to the :attr:`headers`. Do not set if you set :attr:`files` or :attr:`form` for auto detection.
An optional input stream. This is mutually exclusive with setting :attr:`form` and :attr:`files`, setting it will clear those. Do not provide this if the method is not ``POST`` or another method that has a body.
The mimetype parameters as dict. For example if the content type is ``text/html; charset=utf-8`` the params would be ``{'charset': 'utf-8'}``. .. versionadded:: 0.14
str
, value: t.Union[ t.IO[ bytes], t.Tuple[ t.IO[ bytes], str], t.Tuple[ t.IO[ bytes], str, str]]
):
(source)
¶
Called in the EnvironBuilder to add files from the data dict.
Common behavior for getting the :attr:`form` and :attr:`files` properties. :param name: Name of the internal cached attribute. :param storage: Storage class used for the data.