mirror of https://github.com/encode/starlette.git
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:
parent
9ba2ba624b
commit
1d42dd8b59
|
@ -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 != "*":
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue