async def test_trio_http_json_server_GET(): method = "GET" some_param = 33 path = "/info" api_handlers = {path: {method: get_info_handler}} context = TestContext() server = JSONHTTPServer(api_handlers, context) with trio.move_on_after(TEST_TIMEOUT): async with trio.open_nursery() as nursery: some_free_port = 0 listeners = await nursery.start( trio.serve_tcp, server.handler, some_free_port ) client_stream = await open_stream_to_socket_listener(listeners[0]) request = ( f"{method} {path}?some-param={some_param} HTTP/1.0\r\n\r\n" ).encode() await client_stream.send_all(request) response = bytes() async for chunk in client_stream: response += chunk response_body = response.decode("utf-8").split("\r\n\r\n")[-1] result = json.loads(response_body) assert result == SOME_INFO + str(some_param) nursery.cancel_scope.cancel()
async def test_trio_http_json_server(): method = "POST" path = "/increment" api_handlers = {path: {method: post_increment_handler}} server = JSONHTTPServer(api_handlers) with trio.move_on_after(TEST_TIMEOUT): async with trio.open_nursery() as nursery: some_free_port = 0 listeners = await nursery.start( trio.serve_tcp, server.handler, some_free_port ) client_stream = await open_stream_to_socket_listener(listeners[0]) body = '{"x": 1}' request = ( f"{method} {path} HTTP/1.0\r\nContent-Length: {len(body)}\r\n\r\n{body}" ).encode() await client_stream.send_all(request) response = bytes() async for chunk in client_stream: response += chunk response_body = response.decode("utf-8").split("\r\n\r\n")[-1] result = json.loads(response_body) assert result["result"] == 2 nursery.cancel_scope.cancel()
def _mk_validator_api_server( validator_api_port: int, context: Context ) -> JSONHTTPServer[Context]: # NOTE: `mypy` claims the handlers are not typed correctly although it does determine # the async callable to be a subtype of the declared type so it seems like a bug # and we will ignore for now... # See https://mypy.readthedocs.io/en/stable/more_types.html#typing-async-await return JSONHTTPServer( ValidatorAPIHandlers, context, validator_api_port # type: ignore )