Skip to content

Commit dc11c08

Browse files
committed
fix: Logging middleware accepts TestClient without scope["client"]
Since PR encode/starlette#2377, we can no longer expect scope["client"] to return a tuple.
1 parent 1b8d688 commit dc11c08

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

troncos/contrib/asgi/logging/middleware.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ def __init__(
2323
) -> None:
2424
self._list: list[tuple[bytes, bytes]] = list(scope["headers"])
2525

26-
def add_client(self, client: tuple[str, int]) -> None:
26+
def add_client(self, client: tuple[str, int] | None) -> None:
2727
"""
2828
The client IP is not stored in the ASGI headers by default.
2929
Add the client ip to make sure we use it as a fallback if no
3030
proxy headers are set.
3131
"""
32+
host = client[0] if client else "<no-host>"
33+
port = client[1] if client else 0
3234
self._list.append(
3335
(
3436
"REMOTE_ADDR".encode("latin-1"),
35-
f"{client[0]}:{client[1]}".encode("latin-1"),
37+
f"{host}:{port}".encode("latin-1"),
3638
)
3739
)
3840

@@ -109,7 +111,7 @@ async def __call__(
109111
ipware = IpWare()
110112

111113
headers = Headers(scope=scope)
112-
headers.add_client(scope["client"])
114+
headers.add_client(scope.get("client"))
113115

114116
client_ip, _ = ipware.get_client_ip(cast(dict[str, str], headers))
115117

0 commit comments

Comments
 (0)