diff --git a/py2http/examples/object_dispatch.py b/py2http/examples/object_dispatch.py index 77230da..d7eba36 100644 --- a/py2http/examples/object_dispatch.py +++ b/py2http/examples/object_dispatch.py @@ -11,7 +11,7 @@ from typing import MutableMapping -from py2http.service import shared_objects +from py2http.service import run_app, shared_objects class BackendStore(MutableMapping): @@ -60,7 +60,7 @@ def __delitem__(self, key: str): app = mk_app(handlers, publish_openapi=True, publish_swagger=True) -def test_service(rooturl="http://localhost:8080"): +def test_service(rooturl="http://localhost:3030"): import requests url = f"{rooturl}/backend_store" @@ -75,7 +75,9 @@ def test_service(rooturl="http://localhost:8080"): if __name__ == "__main__": - app.run() + # Not `app.run()`: that is bottle's own runner (port 8080), while the OpenAPI spec (and + # so Swagger's "Try it out") points at the port `run_app` serves on, 3030 by default. + run_app(app) # old code (with errors) diff --git a/py2http/openapi_utils.py b/py2http/openapi_utils.py index ea6127d..0ddc9de 100644 --- a/py2http/openapi_utils.py +++ b/py2http/openapi_utils.py @@ -34,8 +34,12 @@ def openapi_type_mapping(obj_type): return oatype_for_pytype.get(obj_type, None) +from py2http.default_configs import default_configs + BINARY = "binary" -DFLT_SERVER_URL = "http://localhost:3030" +# Where `run_app` serves unless told otherwise: the spec's server URL must match it, or +# Swagger's "Try it out" calls a port nothing listens on (i2mint/py2http#13). +DFLT_SERVER_URL = f"http://{default_configs['host']}:{default_configs['port']}" class OpenApiExtractor: diff --git a/py2http/tests/test_example_ports.py b/py2http/tests/test_example_ports.py new file mode 100644 index 0000000..e0d2df6 --- /dev/null +++ b/py2http/tests/test_example_ports.py @@ -0,0 +1,15 @@ +"""The example services must listen where their OpenAPI spec says (i2mint/py2http#13).""" + +from py2http.default_configs import default_configs +from py2http.openapi_utils import DFLT_SERVER_URL, mk_openapi_template + + +def test_openapi_server_url_is_where_run_app_serves(): + assert DFLT_SERVER_URL == f"http://{default_configs['host']}:{default_configs['port']}" + assert mk_openapi_template()["servers"] == [{"url": DFLT_SERVER_URL}] + + +def test_object_dispatch_example_checks_the_port_the_spec_names(): + from py2http.examples import object_dispatch as ex + + assert ex.test_service.__defaults__ == (DFLT_SERVER_URL,)