drakken.mock module

Mock objects for testing.

class drakken.mock.Client(prefix, app)

Bases: object

Mock web browser, sends requests to app and receives responses.

For simplicity it talks directly to the WSGI app instead of through a web server like gunicorn.

Example:

app = Drakken()
client = Client(prefix="http://testserver", app=app)
s = "Calm, steamy morning."

@app.route("/home")
def home(request, response):
    response.text = s

url = "http://testserver/home"
r = client.get(url)
print(r.text)
print(r.status_code))
__init__(prefix, app)

Initialize mock client.

Parameters:
  • prefix (str) – URL prefix. Example: ‘http:/testserver

  • app – Drakken app instance.

delete(url, **kwargs)
get(url, **kwargs)
get_environ(url, method, json, data)

Return environ dict for this URL.

handle_request(method, url, allow_redirects=True, headers={}, json={}, data={})

Handle request.

Parameters:
  • method (str) – ‘DELETE’, ‘GET’, ‘PATCH’, ‘POST’, ‘PUT’.

  • url (str) – URL.

  • allow_redirects – (bool). Default: True.

  • headers (dict) – client headers. Default: {}

  • json (dict) – JSON data. Default: {}

  • data (dict) – form data. Default: {}

Returns:

mock.Response object.

patch(url, **kwargs)
post(url, **kwargs)
put(url, **kwargs)
xform(name)

Convert header name to uppercase.

class drakken.mock.Response(url)

Bases: object

Wrapper of all info returned by server.

history = []
load(content)
start_response(status, response_headers, exc_info=None)

Callback function to send HTTP status and headers to server.

drakken.mock.cookie2environ(cookies)

Convert to cookie (dict) to environ (dict).

Returns:

‘name1=val1; name2=val2…’}

Return type:

{‘HTTP_COOKIE’

drakken.mock.header2cookie(header)

Convert header (str) to cookie (dict)