class documentation

class EnvironBuilder:

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 (create_environ, Response.from_values, 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 form and 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 input_stream, the content_length is set and you have to provide a content_type.
  • a dict or 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 file-like object: These are converted into FileStorage objects automatically.
    • a tuple: The ~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.
Changed in version 2.0: REQUEST_URI and RAW_URI is the full raw URI including the query string, not only the path.
Changed in version 2.0: The default request_class is Request instead of BaseRequest.
New in version 2.0: Added the auth parameter.
New in version 0.15: The json param and json_dumps method.
New in version 0.15: The environ has keys REQUEST_URI and RAW_URI containing the path before perecent-decoding. This is not part of the WSGI PEP, but many WSGI servers include it.
Changed in version 0.6: path and base_url can now be unicode strings that are encoded with iri_to_uri.
Parameters
paththe 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.
base​_urlthe 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).
query​_stringan optional string or dict with URL parameters.
methodthe HTTP method to use, defaults to GET.
input​_streaman optional input stream. Do not specify this and data. As soon as an input stream is set you can't modify args and files unless you set the input_stream to None again.
content​_typeThe content type for the request. As of 0.5 you don't have to provide this when specifying files and form data via data.
content​_lengthThe content length for the request. You don't have to specify this when providing data via data.
errors​_streaman optional error stream that is used for wsgi.errors. Defaults to stderr.
multithreadcontrols wsgi.multithread. Defaults to False.
multiprocesscontrols wsgi.multiprocess. Defaults to False.
run​_oncecontrols wsgi.run_once. Defaults to False.
headersan optional list or Headers object of headers.
dataa string or dict of form data or a file-object. See explanation above.
jsonAn object to be serialized and assigned to data. Defaults the content type to "application/json". Serialized with the function assigned to json_dumps.
environ​_basean optional dict of environment defaults.
environ​_overridesan optional dict of environment overrides.
charsetthe charset used to encode string data.
authAn authorization object to use for the Authorization header value. A (username, password) tuple is a shortcut for Basic authorization.
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 file objects into the 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 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 headers. Do not set if you set files or 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 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 headers. Do not set if you set files or form for auto detection.
Property files A FileMultiDict of uploaded files. Use ~FileMultiDict.add_file to add new files.
Property form A MultiDict of form values.
Property input​_stream No summary
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 args will no longer be available.
Property server​_name The server name (read-only, use host to set)
Property server​_port The server port as integer (read-only, use 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 form and files properties.
Method ​_set​_form Common behavior for setting the form and 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, **kwargs):

Turn an environ dict back into a builder. Any extra kwargs override the args extracted from the environ.

Changed in version 2.0: Path and query values are passed through the WSGI decoding dance to avoid double encoding.
New in version 0.15.
Parameters
environ:WSGIEnvironmentUndocumented
**kwargs:t.AnyUndocumented
Returns
EnvironBuilderUndocumented
def __del__(self):

Undocumented

def __init__(self, path='/', base_url=None, query_string=None, method='GET', input_stream=None, content_type=None, content_length=None, errors_stream=None, multithread=False, multiprocess=False, run_once=False, headers=None, data=None, environ_base=None, environ_overrides=None, charset='utf-8', mimetype=None, json=None, auth=None):

Undocumented

Parameters
path:strUndocumented
base​_url:t.Optional[str]Undocumented
query​_string:t.Optional[t.Union[t.Mapping[str, str], str]]Undocumented
method:strUndocumented
input​_stream:t.Optional[t.IO[bytes]]Undocumented
content​_type:t.Optional[str]Undocumented
content​_length:t.Optional[int]Undocumented
errors​_stream:t.Optional[t.IO[str]]Undocumented
multithread:boolUndocumented
multiprocess:boolUndocumented
run​_once:boolUndocumented
headers:t.Optional[t.Union[Headers, t.Iterable[t.Tuple[str, str]]]]Undocumented
data:t.Optional[t.Union[t.IO[bytes], str, bytes, t.Mapping[str, t.Any]]]Undocumented
environ​_base:t.Optional[t.Mapping[str, t.Any]]Undocumented
environ​_overrides:t.Optional[t.Mapping[str, t.Any]]Undocumented
charset:strUndocumented
mimetype:t.Optional[str]Undocumented
json:t.Optional[t.Mapping[str, t.Any]]Undocumented
auth:t.Optional[t.Union[Authorization, t.Tuple[str, str]]]Undocumented
@args.setter
def args(self, value):

Undocumented

Parameters
value:t.Optional[MultiDict]Undocumented
@base_url.setter
def base_url(self, value):

Undocumented

Parameters
value:t.Optional[str]Undocumented
def close(self):
Closes all files. If you put real file objects into the files dict you can call this method to automatically close them all in one go.
@content_length.setter
def content_length(self, value):

Undocumented

Parameters
value:t.Optional[int]Undocumented
@content_type.setter
def content_type(self, value):

Undocumented

Parameters
value:t.Optional[str]Undocumented
@files.setter
def files(self, value):

Undocumented

Parameters
value:FileMultiDictUndocumented
@form.setter
def form(self, value):

Undocumented

Parameters
value:MultiDictUndocumented
def get_environ(self):

Return the built environ.

Changed in version 0.15: The content type and length headers are set based on input stream detection. Previously this only set the WSGI keys.
Returns
WSGIEnvironmentUndocumented
def get_request(self, cls=None):
Returns a request with the data. If the request class is not specified request_class is used.
Parameters
cls:t.Optional[t.Type[Request]]The request wrapper to use.
Returns
RequestUndocumented
@input_stream.setter
def input_stream(self, value):

Undocumented

Parameters
value:t.Optional[t.IO[bytes]]Undocumented
@mimetype.setter
def mimetype(self, value):

Undocumented

Parameters
value:strUndocumented
@query_string.setter
def query_string(self, value):

Undocumented

Parameters
value:t.Optional[str]Undocumented
json_dumps =

Undocumented

server_protocol: str =

Undocumented

wsgi_version: tuple[int, ...] =

Undocumented

charset =

Undocumented

closed: bool =

Undocumented

@property
content_type =
The content type for the request. Reflected from and to the headers. Do not set if you set files or form for auto detection.
environ_base =

Undocumented

environ_overrides =

Undocumented

errors_stream =

Undocumented

headers =

Undocumented

host =

Undocumented

method =

Undocumented

multiprocess =

Undocumented

multithread =

Undocumented

path =

Undocumented

request_uri =

Undocumented

run_once =

Undocumented

script_root =

Undocumented

url_scheme =

Undocumented

@property
args: MultiDict =
The URL arguments as MultiDict.
@property
base_url: str =
The base URL is used to extract the URL scheme, host name, port, and root path.
@property
content_length: t.Optional[int] =
The content length as integer. Reflected from and to the headers. Do not set if you set files or form for auto detection.
@property
files: FileMultiDict =
A FileMultiDict of uploaded files. Use ~FileMultiDict.add_file to add new files.
@property
form: MultiDict =
A MultiDict of form values.
@property
input_stream: t.Optional[t.IO[bytes]] =
An optional input stream. This is mutually exclusive with setting form and 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: t.Optional[str] =

The mimetype (content type without charset etc.)

New in version 0.14.
@property
mimetype_params: t.Mapping[str, str] =

The mimetype parameters as dict. For example if the content type is text/html; charset=utf-8 the params would be {'charset': 'utf-8'}.

New in version 0.14.
@property
query_string: str =
The query string. If you set this to a string args will no longer be available.
@property
server_name: str =
The server name (read-only, use host to set)
@property
server_port: int =
The server port as integer (read-only, use host to set)
@staticmethod
def _make_base_url(scheme, host, script_root):

Undocumented

Parameters
scheme:strUndocumented
host:strUndocumented
script​_root:strUndocumented
Returns
strUndocumented
def _add_file_from_data(self, key, value):
Called in the EnvironBuilder to add files from the data dict.
Parameters
key:strUndocumented
value:t.Union[t.IO[bytes], t.Tuple[t.IO[bytes], str], t.Tuple[t.IO[bytes], str, str]]Undocumented
def _get_form(self, name, storage):
Common behavior for getting the form and files properties.
Parameters
name:strName of the internal cached attribute.
storage:t.Type[_TAnyMultiDict]Storage class used for the data.
Returns
_TAnyMultiDictUndocumented
def _set_form(self, name, value):
Common behavior for setting the form and files properties.
Parameters
name:strName of the internal cached attribute.
value:MultiDictValue to assign to the attribute.
_args =

Undocumented

_files =

Undocumented

_form =

Undocumented

_input_stream =

Undocumented

_query_string =

Undocumented