small fix to start_server docstring & added a test

This commit is contained in:
Abubakar Abid 2022-02-27 16:44:59 -05:00
parent 51e57675da
commit ea96e1f753
2 changed files with 21 additions and 2 deletions

View File

@ -77,7 +77,7 @@ def start_server(
ssl_keyfile: Optional[str] = None,
ssl_certfile: Optional[str] = None,
ssl_keyfile_password: Optional[str] = None,
) -> Tuple[int, str, fastapi.FastAPI, threading.Thread, None]:
) -> Tuple[int, str, fastapi.FastAPI, Server]:
"""Launches a local server running the provided Interface
Parameters:
interface: The interface object to run on the server
@ -87,6 +87,11 @@ def start_server(
ssl_keyfile: If a path to a file is provided, will use this as the private key file to create a local server running on https.
ssl_certfile: If a path to a file is provided, will use this as the signed certificate for https. Needs to be provided if ssl_keyfile is provided.
ssl_keyfile_password (str): If a password is provided, will use this with the ssl certificate for https.
Returns:
port: the port number the server is running on
path_to_local_server: the complete address that the local server can be accessed at
app: the FastAPI app object
server: the server object that is a subclass of uvicorn.Server (used to close the server)
"""
server_name = server_name or LOCALHOST_NAME
# if port is not specified, search for first available port

View File

@ -3,7 +3,7 @@
import os
import unittest
import unittest.mock as mock
import urllib.request
import urllib
import warnings
import aiohttp
@ -44,6 +44,20 @@ class TestInterfaceCustomParameters(unittest.TestCase):
self.assertEqual(response.status_code, 500)
self.assertTrue("error" in response.json())
io.close()
class TestStartServer(unittest.TestCase):
def test_start_server(self):
io = Interface(lambda x: x, "number", "number")
port = networking.get_first_available_port(
networking.INITIAL_PORT_VALUE,
networking.INITIAL_PORT_VALUE+networking.TRY_NUM_PORTS
)
_, local_path, _, server = networking.start_server(io, server_port=port)
url = urllib.parse.urlparse(local_path)
self.assertEquals(url.scheme, "http")
self.assertEquals(url.port, port)
server.close()
class TestFlagging(unittest.TestCase):