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
parent f4c8252185
commit 3abe4f885e
No known key found for this signature in database
GPG Key ID: 9F85EE6C807303FB
4 changed files with 18 additions and 4 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

@ -526,7 +526,7 @@ class RunnerMixin(metaclass=SanicMeta):
) )
) )
else: else:
server = "ASGI" if self.asgi else "unknown" server = "ASGI" if self.asgi else "unknown" # type: ignore
display = { display = {
"mode": " ".join(mode), "mode": " ".join(mode),
@ -576,7 +576,7 @@ class RunnerMixin(metaclass=SanicMeta):
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: except IndexError:
location = "ASGI" if self.asgi else "unknown" location = "ASGI" if self.asgi else "unknown" # type: ignore
return f"http://<{location}>" return f"http://<{location}>"
@staticmethod @staticmethod

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>"

View File

@ -2,6 +2,7 @@ import json as stdjson
from collections import namedtuple from collections import namedtuple
from pathlib import Path from pathlib import Path
from sys import version_info
import pytest import pytest
@ -74,7 +75,10 @@ def test_full_message(client):
""" """
) )
response = client.recv() response = client.recv()
assert len(response) == 151
# AltSvcCheck touchup removes the Alt-Svc header from the
# response in the Python 3.9+ in this case
assert len(response) == (151 if version_info < (3, 9) else 140)
assert b"200 OK" in response assert b"200 OK" in response