Skip to content

Commit

Permalink
Always show server location in ASGI (sanic-org#2522)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Hopkins <[email protected]>
Co-authored-by: Zhiwei Liang <[email protected]>
Co-authored-by: Néstor Pérez <[email protected]>
  • Loading branch information
3 people committed Aug 11, 2022
1 parent f4c8252 commit 3abe4f8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ def update_config(self, config: Union[bytes, str, dict, Any]):
self.config.update_config(config)

@property
def asgi(self):
def asgi(self) -> bool:
return self.state.asgi

@asgi.setter
Expand Down
4 changes: 2 additions & 2 deletions sanic/mixins/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def motd(
)
)
else:
server = "ASGI" if self.asgi else "unknown"
server = "ASGI" if self.asgi else "unknown" # type: ignore

display = {
"mode": " ".join(mode),
Expand Down Expand Up @@ -576,7 +576,7 @@ def serve_location(self) -> str:
server_settings = self.state.server_info[0].settings
return self.get_server_location(server_settings)
except IndexError:
location = "ASGI" if self.asgi else "unknown"
location = "ASGI" if self.asgi else "unknown" # type: ignore
return f"http://<{location}>"

@staticmethod
Expand Down
10 changes: 10 additions & 0 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,13 @@ def signal_handler(signal):
assert response.status_code == 200
assert response.text == "test_signals_triggered"
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>"
6 changes: 5 additions & 1 deletion tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from collections import namedtuple
from pathlib import Path
from sys import version_info

import pytest

Expand Down Expand Up @@ -74,7 +75,10 @@ def test_full_message(client):
"""
)
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


Expand Down

0 comments on commit 3abe4f8

Please sign in to comment.