From 3abe4f885e0c74682238afc2543ab6c4b2db06cb Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 11 Aug 2022 10:00:35 +0300 Subject: [PATCH] Always show server location in ASGI (#2522) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adam Hopkins Co-authored-by: Zhiwei Liang Co-authored-by: Néstor Pérez <25409753+prryplatypus@users.noreply.github.com> --- sanic/app.py | 2 +- sanic/mixins/runner.py | 4 ++-- tests/test_asgi.py | 10 ++++++++++ tests/test_http.py | 6 +++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 47a600cb..78327b46 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1315,7 +1315,7 @@ class Sanic(BaseSanic, RunnerMixin, metaclass=TouchUpMeta): self.config.update_config(config) @property - def asgi(self): + def asgi(self) -> bool: return self.state.asgi @asgi.setter diff --git a/sanic/mixins/runner.py b/sanic/mixins/runner.py index 57387401..a94cf574 100644 --- a/sanic/mixins/runner.py +++ b/sanic/mixins/runner.py @@ -526,7 +526,7 @@ class RunnerMixin(metaclass=SanicMeta): ) ) else: - server = "ASGI" if self.asgi else "unknown" + server = "ASGI" if self.asgi else "unknown" # type: ignore display = { "mode": " ".join(mode), @@ -576,7 +576,7 @@ class RunnerMixin(metaclass=SanicMeta): 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 diff --git a/tests/test_asgi.py b/tests/test_asgi.py index 4c473047..129bbe0b 100644 --- a/tests/test_asgi.py +++ b/tests/test_asgi.py @@ -546,3 +546,13 @@ async def test_signals_triggered(app): 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://" diff --git a/tests/test_http.py b/tests/test_http.py index 1e385449..edcbfed8 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -2,6 +2,7 @@ import json as stdjson from collections import namedtuple from pathlib import Path +from sys import version_info import pytest @@ -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