From 1b65b0d119140ddbc7fa54064392334415c80d59 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Thu, 22 Feb 2024 12:44:49 +0000 Subject: [PATCH 1/3] Revert "Turn `scope["client"]` to `None` on `TestClient` (#2377)" This reverts commit 483849a466a2bfc121f5a367339e1aa3ed20344b. --- starlette/testclient.py | 4 ++-- tests/test_testclient.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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..8f3a518c1 100644 --- a/tests/test_testclient.py +++ b/tests/test_testclient.py @@ -274,6 +274,20 @@ async def asgi(receive: Receive, send: Send) -> None: assert websocket.should_close.is_set() + +def test_client(test_client_factory): + async def app(scope, receive, send): + 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: From 5e4c0d83cd3dc39544fbef1b5ec3d09e9874b52c Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Thu, 22 Feb 2024 12:48:34 +0000 Subject: [PATCH 2/3] format --- tests/test_testclient.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_testclient.py b/tests/test_testclient.py index 8f3a518c1..258123b52 100644 --- a/tests/test_testclient.py +++ b/tests/test_testclient.py @@ -274,7 +274,6 @@ async def asgi(receive: Receive, send: Send) -> None: assert websocket.should_close.is_set() - def test_client(test_client_factory): async def app(scope, receive, send): client = scope.get("client") From d6eabb0973370b9b15d7778548f230ff4fc35f91 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Mon, 26 Feb 2024 09:45:25 +0100 Subject: [PATCH 3/3] Add type hints --- tests/test_testclient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_testclient.py b/tests/test_testclient.py index 258123b52..4ed1ced9a 100644 --- a/tests/test_testclient.py +++ b/tests/test_testclient.py @@ -274,8 +274,8 @@ async def asgi(receive: Receive, send: Send) -> None: assert websocket.should_close.is_set() -def test_client(test_client_factory): - async def app(scope, receive, send): +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