Skip to content

Commit

Permalink
Revert "Turn scope["client"] to None on TestClient (#2377)" (#2525
Browse files Browse the repository at this point in the history
)

* Revert "Turn `scope["client"]` to `None` on `TestClient` (#2377)"

This reverts commit 483849a.

* format

* Add type hints

---------

Co-authored-by: Marcelo Trylesinski <[email protected]>
  • Loading branch information
aminalaee and Kludex authored Feb 29, 2024
1 parent bd77d7d commit 39dccd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions starlette/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
13 changes: 13 additions & 0 deletions tests/test_testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 39dccd9

Please sign in to comment.