Skip to content

Commit 39dccd9

Browse files
aminalaeeKludex
andauthored
Revert "Turn scope["client"] to None on TestClient (#2377)" (#2525)
* Revert "Turn `scope["client"]` to `None` on `TestClient` (#2377)" This reverts commit 483849a. * format * Add type hints --------- Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent bd77d7d commit 39dccd9

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

starlette/testclient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response:
298298
"scheme": scheme,
299299
"query_string": query.encode(),
300300
"headers": headers,
301-
"client": None,
301+
"client": ["testclient", 50000],
302302
"server": [host, port],
303303
"subprotocols": subprotocols,
304304
"state": self.app_state.copy(),
@@ -317,7 +317,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response:
317317
"scheme": scheme,
318318
"query_string": query.encode(),
319319
"headers": headers,
320-
"client": None,
320+
"client": ["testclient", 50000],
321321
"server": [host, port],
322322
"extensions": {"http.response.debug": {}},
323323
"state": self.app_state.copy(),

tests/test_testclient.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,19 @@ async def asgi(receive: Receive, send: Send) -> None:
274274
assert websocket.should_close.is_set()
275275

276276

277+
def test_client(test_client_factory: TestClientFactory) -> None:
278+
async def app(scope: Scope, receive: Receive, send: Send) -> None:
279+
client = scope.get("client")
280+
assert client is not None
281+
host, port = client
282+
response = JSONResponse({"host": host, "port": port})
283+
await response(scope, receive, send)
284+
285+
client = test_client_factory(app)
286+
response = client.get("/")
287+
assert response.json() == {"host": "testclient", "port": 50000}
288+
289+
277290
@pytest.mark.parametrize("param", ("2020-07-14T00:00:00+00:00", "España", "voilà"))
278291
def test_query_params(test_client_factory: TestClientFactory, param: str) -> None:
279292
def homepage(request: Request) -> Response:

0 commit comments

Comments
 (0)