class documentation

class EnvironBuilder: (source)

View In Hierarchy

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_environ 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.setter Undocumented
Method base_url.setter 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_length.setter Undocumented
Method content_type.setter Undocumented
Method files.setter Undocumented
Method form.setter Undocumented
Method get_environ Return the built environ.
Method get_request Returns a request with the data. If the request class is not specified :attr:`request_class` is used.
Method input_stream.setter Undocumented
Method mimetype.setter Undocumented
Method query_string.setter Undocumented
Class Variable json_dumps Undocumented
Class Variable server_protocol Undocumented
Class Variable wsgi_version Undocumented
Instance Variable charset Undocumented
Instance Variable closed Undocumented
Instance Variable content_type 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_base Undocumented
Instance Variable environ_overrides Undocumented
Instance Variable errors_stream 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_uri Undocumented
Instance Variable run_once Undocumented
Instance Variable script_root Undocumented
Instance Variable url_scheme Undocumented
Property args The URL arguments as :class:`MultiDict`.
Property base_url The base URL is used to extract the URL scheme, host name, port, and root path.
Property content_length 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_stream 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_params 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_string The query string. If you set this to a string :attr:`args` will no longer be available.
Property server_name The server name (read-only, use :attr:`host` to set)
Property server_port The server port as integer (read-only, use :attr:`host` to set)
Static Method _make_base_url Undocumented
Method _add_file_from_data Called in the EnvironBuilder to add files from the data dict.
Method _get_form Common behavior for getting the :attr:`form` and :attr:`files` properties.
Method _set_form 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_stream Undocumented
Instance Variable _query_string Undocumented
@classmethod
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

def __del__(self): (source)

Undocumented

def __init__(self, path: 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

Undocumented

@base_url.setter
def base_url(self, value: t.Optional[str]): (source)

Undocumented

def close(self): (source)

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.

@content_length.setter
def content_length(self, value: t.Optional[int]): (source)

Undocumented

@content_type.setter
def content_type(self, value: t.Optional[str]): (source)

Undocumented

@files.setter
def files(self, value: FileMultiDict): (source)

Undocumented

@form.setter
def form(self, value: MultiDict): (source)

Undocumented

def get_environ(self) -> WSGIEnvironment: (source)

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.

def get_request(self, cls: t.Optional[t.Type[Request]] = None) -> Request: (source)

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.

@input_stream.setter
def input_stream(self, value: t.Optional[t.IO[bytes]]): (source)

Undocumented

@mimetype.setter
def mimetype(self, value: str): (source)

Undocumented

@query_string.setter
def query_string(self, value: t.Optional[str]): (source)

Undocumented

json_dumps = (source)

Undocumented

server_protocol: str = (source)

Undocumented

wsgi_version: tuple[int, ...] = (source)

Undocumented

Undocumented

Undocumented

@property
content_type = (source)

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.

environ_base = (source)

Undocumented

environ_overrides = (source)

Undocumented

errors_stream = (source)

Undocumented

Undocumented

Undocumented

Undocumented

multiprocess = (source)

Undocumented

multithread = (source)

Undocumented

Undocumented

request_uri = (source)

Undocumented

run_once = (source)

Undocumented

script_root = (source)

Undocumented

url_scheme = (source)

Undocumented

The URL arguments as :class:`MultiDict`.

The base URL is used to extract the URL scheme, host name, port, and root path.

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.

A :class:`FileMultiDict` of uploaded files. Use :meth:`~FileMultiDict.add_file` to add new files.

A :class:`MultiDict` of form values.

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 (content type without charset etc.) .. versionadded:: 0.14

@property
mimetype_params: t.Mapping[str, str] = (source)

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

@property
query_string: str = (source)

The query string. If you set this to a string :attr:`args` will no longer be available.

@property
server_name: str = (source)

The server name (read-only, use :attr:`host` to set)

@property
server_port: int = (source)

The server port as integer (read-only, use :attr:`host` to set)

@staticmethod
def _make_base_url(scheme: str, host: str, script_root: str) -> str: (source)

Undocumented

def _add_file_from_data(self, key: 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.

def _get_form(self, name: str, storage: t.Type[_TAnyMultiDict]) -> _TAnyMultiDict: (source)

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.

def _set_form(self, name: str, value: MultiDict): (source)

Common behavior for setting the :attr:`form` and :attr:`files` properties. :param name: Name of the internal cached attribute. :param value: Value to assign to the attribute.

Undocumented

Undocumented

Undocumented

_input_stream = (source)

Undocumented

_query_string = (source)

Undocumented