Test Client

class microdot.test_client.TestClient(app, cookies=None, scheme=None, host=None)

A test client for Microdot.

Parameters:
  • app – The Microdot application instance.

  • cookies – A dictionary of cookies to use when sending requests to the application.

  • scheme – The scheme to use for requests, either ‘http’ or ‘https’.

  • host – The host to use for requests.

The following example shows how to create a test client for an application and send a test request:

from microdot import Microdot

app = Microdot()

@app.get('/')
async def index():
    return 'Hello, World!'

async def test_hello_world(self):
    client = TestClient(app)
    res = await client.get('/')
    assert res.status_code == 200
    assert res.text == 'Hello, World!'
async get(path, headers=None)

Send a GET request to the application.

Parameters:
  • path – The request URL.

  • headers – A dictionary of headers to send with the request.

This method returns a TestResponse object.

async post(path, headers=None, body=None)

Send a POST request to the application.

Parameters:
  • path – The request URL.

  • headers – A dictionary of headers to send with the request.

  • body – The request body. If a dictionary or list is provided, a JSON-encoded body will be sent. A string body is encoded to bytes as UTF-8. A bytes body is sent as-is.

This method returns a TestResponse object.

async put(path, headers=None, body=None)

Send a PUT request to the application.

Parameters:
  • path – The request URL.

  • headers – A dictionary of headers to send with the request.

  • body – The request body. If a dictionary or list is provided, a JSON-encoded body will be sent. A string body is encoded to bytes as UTF-8. A bytes body is sent as-is.

This method returns a TestResponse object.

async patch(path, headers=None, body=None)

Send a PATCH request to the application.

Parameters:
  • path – The request URL.

  • headers – A dictionary of headers to send with the request.

  • body – The request body. If a dictionary or list is provided, a JSON-encoded body will be sent. A string body is encoded to bytes as UTF-8. A bytes body is sent as-is.

This method returns a TestResponse object.

async delete(path, headers=None)

Send a DELETE request to the application.

Parameters:
  • path – The request URL.

  • headers – A dictionary of headers to send with the request.

This method returns a TestResponse object.

async websocket(path, client, headers=None)

Send a websocket connection request to the application.

Parameters:
  • path – The request URL.

  • client – A generator function that yields client messages.

  • headers – A dictionary of headers to send with the request.

class microdot.test_client.TestResponse

A response object issued by the Microdot test client.

status_code

The numeric status code returned by the server.

reason

The text reason associated with the status response, such as 'OK' or 'NOT FOUND'. Set to None unless the application explicitly sets it on the response object.

headers

A dictionary with the response headers.

body

The body of the response, as a bytes object.

text

The body of the response, decoded to a UTF-8 string. Set to None if the response cannot be represented as UTF-8 text.

json

The body of the JSON response, decoded to a dictionary or list. Set Note if the response does not have a JSON payload.

events

The body of the SSE response, decoded to a list of events, each given as a dictionary with a data key and optionally also event and id keys. Set to None if the response does not have an SSE payload.