werkzeug.EnvironBuilder

class werkzeug.EnvironBuilder(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')[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 (create_environ(), BaseResponse.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: If it’s a string it is converted into a input_stream, the content_length is set and you have to provide a content_type.
  • a dict: If it’s a dict the keys have to be strings and the values any of the following objects:
    • a file-like object. These are converted into FileStorage objects automatically.
    • a tuple. The add_file() method is called with the tuple items as positional arguments.

New in version 0.6: path and base_url can now be unicode strings that are encoded using the iri_to_uri() function.

Parameters:
  • 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.
  • 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).
  • query_string – an optional string or dict with URL parameters.
  • method – the HTTP method to use, defaults to GET.
  • input_stream – an 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_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.
  • content_length – The content length for the request. You don’t have to specify this when providing data via data.
  • errors_stream – an optional error stream that is used for wsgi.errors. Defaults to stderr.
  • multithread – controls wsgi.multithread. Defaults to False.
  • multiprocess – controls wsgi.multiprocess. Defaults to False.
  • run_once – controls wsgi.run_once. Defaults to False.
  • headers – an optional list or Headers object of headers.
  • data – a string or dict of form data. See explanation above.
  • environ_base – an optional dict of environment defaults.
  • environ_overrides – an optional dict of environment overrides.
  • charset – the charset used to encode unicode data.

Methods

__init__([path, base_url, query_string, ...])
close() Closes all files.
get_environ() Return the built environ.
get_request([cls]) Returns a request with the data.

Attributes

args The URL arguments as MultiDict.
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).
content_length The content length as integer.
content_type The content type for the request.
files
form
input_stream An optional input stream.
query_string The query string.
server_name The server name (read-only, use host to set)
server_port The server port as integer (read-only, use host to set)
server_protocol the server protocol to use. defaults to HTTP/1.1
wsgi_version the wsgi version to use. defaults to (1, 0)