module documentation

Test web server.

This server runs in a dedicated docker container during testing and acts as a mock for our http-related functionality.

This server does almost nothing by default, but the client can "extend" it by providing "snippets". A snippet is a Python code fragment, which is injected directly into the request handler. It views the request handler object as self and must end with a call to self.out(text, status), which returns an HTTP response to the client. When a request arrives, all snippets added so far are executed until one of them returns.

The server understands two POST requests:

  • /__add reads a snippet from the request body and adds it to the request handler
  • /__del removes all snippets so far.

IT IS AN EXTREMELY BAD IDEA TO RUN THIS SERVER OUTSIDE OF A TEST ENVIRONMENT.

Example of use:

# set up a snippet

requests.post('http://mock-server/__add', data=r'''
    if self.path == '/say-hello':
        return self.out('HELLO')
''')

# invoke it

text = requests.get('http://mock-server/say-hello')
assert text == 'HELLO'

Useful properties of the handler object (self) to use in snippets

client_address - tuple[host, port]
path    - url path
method  - GET/POST
body    - raw bytes body>
text    - body decoded as utf8
json    - body decoded as json
query   - query string dict, e.g. {'a': '1', 'b': '2'}
query2  - query string array dict, e.g. {'a': ['1'], 'b': ['2']}
Class HTTPRequestHandler Undocumented
Function dedent Undocumented
Function indent Undocumented
Function main Undocumented
Constant _SNIPPETS Undocumented
def dedent(s): (source)

Undocumented

def indent(s): (source)

Undocumented

def main(): (source)

Undocumented

_SNIPPETS: list = (source)

Undocumented

Value
[]