:tocdepth: 3 :py:mod:`gws.test.mockserver` ============================= .. py:module:: gws.test.mockserver .. autoapi-nested-parse:: 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. The properties of the request handler (like ``path``) are available as variables in snippets. With ``return end(content, status, **headers)`` the snippet can return 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 the following POST requests: - ``/__add`` reads a snippet from the request body and adds it to the request handler - ``/__del`` removes all snippets so far - ``/__set`` removes all and add this one 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 path == '/say-hello' and query.get('x') == 'y': return end('HELLO') ''') # invoke it res = requests.get('http://mock-server/say-hello?x=y') assert res.text == 'HELLO' The mockserver runs in a GWS container, so all gws modules are available for import. **Source code:** :source:`gws.test.mockserver` Module Contents --------------- .. py:class:: HTTPRequestHandler(*args, directory=None, **kwargs) Bases: :py:obj:`http.server.SimpleHTTPRequestHandler` Simple HTTP request handler with GET and HEAD commands. This serves files from the current directory and any of its subdirectories. The MIME type for files is determined by calling the .guess_type() method. The GET and HEAD requests are identical except that the HEAD request omits the actual contents of the file. .. py:attribute:: body :type: bytes Raw request body. .. py:attribute:: json :type: dict Request body decoded as json. .. py:attribute:: method :type: str GET, POST etc. .. py:attribute:: path :type: str Url path part. .. py:attribute:: protocol_version :value: 'HTTP/1.1' Protocol version. .. py:attribute:: query :type: dict Query string as a key => value dict, e.g. ``{'a': '1', 'b': '2', ...etc}`` .. py:attribute:: query2 :type: dict Query string as a key => [values] dict, e.g. ``{'a': ['1', '2'], ...etc}`` .. py:attribute:: remote_host :type: str Remote host. .. py:attribute:: remote_port :type: int Remote post. .. py:attribute:: text :type: str Request body decoded as utf8. .. py:method:: do_GET() Serve a GET request. .. py:method:: do_POST() .. py:method:: end(content, status=200, **headers) .. py:method:: handle_one_request() Handle a single HTTP request. You normally don't need to override this method; see the class __doc__ string for information on how to handle specific HTTP commands such as GET and POST. .. py:method:: prepare(body: bytes) .. py:method:: run_snippets() .. py:function:: main()