]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Revert "Turn `scope["client"]` to `None` on `TestClient` (#2377)" (#2525)
authorAmin Alaee <mohammadamin.alaee@gmail.com>
Thu, 29 Feb 2024 12:55:04 +0000 (13:55 +0100)
committerGitHub <noreply@github.com>
Thu, 29 Feb 2024 12:55:04 +0000 (05:55 -0700)
* Revert "Turn `scope["client"]` to `None` on `TestClient` (#2377)"

This reverts commit 483849a466a2bfc121f5a367339e1aa3ed20344b.

* format

* Add type hints

---------

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
starlette/testclient.py
tests/test_testclient.py

index d076331c188c97c5075f4e42c4e8204d7f0692f9..f17d4e8923658a00220044805f344fc8584d07e9 100644 (file)
@@ -298,7 +298,7 @@ class _TestClientTransport(httpx.BaseTransport):
                 "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 @@ class _TestClientTransport(httpx.BaseTransport):
             "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(),
index e8956cd30a407ca22f4a52616a56a01d6883ef1f..4ed1ced9a37c16c135e669a89efcfd01b3612cff 100644 (file)
@@ -274,6 +274,19 @@ def test_websocket_not_block_on_close(test_client_factory: TestClientFactory) ->
     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: