Replace mutable "allowed_hosts" argument of TrustedHostMiddleware with None (#448)

* Replace mutable "allowed_hosts" argument of starlette.middleware.trustedhost.TrustedHostMiddleware with None.

* Add unit test to cover case when TrustedHostMiddleware.allowed_hosts is None

* Fix style issues

* Code style fixes

* Fix unit tests for TrustedHostMiddleware
This commit is contained in:
Alex Oleshkevich 2019-03-27 12:23:07 +03:00 committed by Tom Christie
parent 9ba2ba624b
commit 1d42dd8b59
2 changed files with 10 additions and 1 deletions

View File

@ -11,9 +11,12 @@ class TrustedHostMiddleware:
def __init__(
self,
app: ASGIApp,
allowed_hosts: typing.Sequence[str] = ["*"],
allowed_hosts: typing.Sequence[str] = None,
www_redirect: bool = True,
) -> None:
if allowed_hosts is None:
allowed_hosts = ["*"]
for pattern in allowed_hosts:
assert "*" not in pattern[1:], ENFORCE_DOMAIN_WILDCARD
if pattern.startswith("*") and pattern != "*":

View File

@ -28,6 +28,12 @@ def test_trusted_host_middleware():
assert response.status_code == 400
def test_default_allowed_hosts():
app = Starlette()
middleware = TrustedHostMiddleware(app)
assert middleware.allowed_hosts == ["*"]
def test_www_redirect():
app = Starlette()