diff --git a/starlette/testclient.py b/starlette/testclient.py index d076331c1..f17d4e892 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -298,7 +298,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response: "scheme": scheme, "query_string": query.encode(), "headers": headers, - "client": None, + "client": ["testclient", 50000], "server": [host, port], "subprotocols": subprotocols, "state": self.app_state.copy(), @@ -317,7 +317,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response: "scheme": scheme, "query_string": query.encode(), "headers": headers, - "client": None, + "client": ["testclient", 50000], "server": [host, port], "extensions": {"http.response.debug": {}}, "state": self.app_state.copy(), diff --git a/tests/test_testclient.py b/tests/test_testclient.py index e8956cd30..4ed1ced9a 100644 --- a/tests/test_testclient.py +++ b/tests/test_testclient.py @@ -274,6 +274,19 @@ async def asgi(receive: Receive, send: Send) -> None: assert websocket.should_close.is_set() +def test_client(test_client_factory: TestClientFactory) -> None: + async def app(scope: Scope, receive: Receive, send: Send) -> None: + client = scope.get("client") + assert client is not None + host, port = client + response = JSONResponse({"host": host, "port": port}) + await response(scope, receive, send) + + client = test_client_factory(app) + response = client.get("/") + assert response.json() == {"host": "testclient", "port": 50000} + + @pytest.mark.parametrize("param", ("2020-07-14T00:00:00+00:00", "España", "voilà")) def test_query_params(test_client_factory: TestClientFactory, param: str) -> None: def homepage(request: Request) -> Response: