Always show server location in ASGI (#2522)

Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Co-authored-by: Zhiwei Liang <zhi.wei.liang@outlook.com>
Co-authored-by: Néstor Pérez <25409753+prryplatypus@users.noreply.github.com>
This commit is contained in:
Adam Hopkins 2022-08-11 10:00:35 +03:00 committed by GitHub
parent 7827b1b41d
commit 0392d1dcfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 5 deletions

View File

@ -1315,7 +1315,7 @@ class Sanic(BaseSanic, RunnerMixin, metaclass=TouchUpMeta):
self.config.update_config(config) self.config.update_config(config)
@property @property
def asgi(self): def asgi(self) -> bool:
return self.state.asgi return self.state.asgi
@asgi.setter @asgi.setter

View File

@ -525,7 +525,7 @@ class RunnerMixin(metaclass=SanicMeta):
) )
) )
else: else:
server = "" server = "ASGI" if self.asgi else "unknown" # type: ignore
display = { display = {
"mode": " ".join(mode), "mode": " ".join(mode),
@ -571,8 +571,12 @@ class RunnerMixin(metaclass=SanicMeta):
@property @property
def serve_location(self) -> str: def serve_location(self) -> str:
try:
server_settings = self.state.server_info[0].settings server_settings = self.state.server_info[0].settings
return self.get_server_location(server_settings) return self.get_server_location(server_settings)
except IndexError:
location = "ASGI" if self.asgi else "unknown" # type: ignore
return f"http://<{location}>"
@staticmethod @staticmethod
def get_server_location( def get_server_location(

View File

@ -546,3 +546,13 @@ async def test_signals_triggered(app):
assert response.status_code == 200 assert response.status_code == 200
assert response.text == "test_signals_triggered" assert response.text == "test_signals_triggered"
assert signals_triggered == signals_expected assert signals_triggered == signals_expected
@pytest.mark.asyncio
async def test_asgi_serve_location(app):
@app.get("/")
def _request(request: Request):
return text(request.app.serve_location)
_, response = await app.asgi_client.get("/")
assert response.text == "http://<ASGI>"