1010 - "__ init__ "
1111
1212The test client allows you to make requests against your ASGI application,
13- using the ` httpx ` library.
13+ using the ` httpx2 ` library.
14+
15+ !!! note
16+ The ` TestClient ` is built on ` httpx2 ` . Plain ` httpx ` is still supported, but
17+ deprecated - install ` httpx2 ` (included in ` starlette[full] ` ) instead.
1418
1519``` python
1620from starlette.responses import HTMLResponse
@@ -29,11 +33,11 @@ def test_app():
2933 assert response.status_code == 200
3034```
3135
32- The test client exposes the same interface as any other ` httpx ` session.
36+ The test client exposes the same interface as any other ` httpx2 ` session.
3337In particular, note that the calls to make a request are just standard
3438function calls, not awaitables.
3539
36- You can use any of ` httpx ` standard API, such as authentication, session
40+ You can use any of ` httpx2 ` standard API, such as authentication, session
3741cookies handling, or file uploads.
3842
3943For example, to set headers on the TestClient you can do:
@@ -65,7 +69,7 @@ with open("example.txt", "rb") as f1:
6569 response = client.post(" /form" , files = files)
6670```
6771
68- For more information you can check the ` httpx ` [ documentation] ( https://www.python-httpx.org/advanced/ ) .
72+ For more information you can check the ` httpx2 ` [ documentation] ( https://www.python-httpx.org/advanced/ ) .
6973
7074By default the ` TestClient ` will raise any exceptions that occur in the
7175application. Occasionally you might want to test the content of 500 error
@@ -117,7 +121,7 @@ def test_app():
117121
118122You can also test websocket sessions with the test client.
119123
120- The ` httpx ` library will be used to build the initial handshake, meaning you
124+ The ` httpx2 ` library will be used to build the initial handshake, meaning you
121125can use the same authentication options and other headers between both http and
122126websocket testing.
123127
@@ -150,7 +154,7 @@ always raised by the test client.
150154
151155#### Establishing a test session
152156
153- * ` .websocket_connect(url, subprotocols=None, **options) ` - Takes the same set of arguments as ` httpx .get()` .
157+ * ` .websocket_connect(url, subprotocols=None, **options) ` - Takes the same set of arguments as ` httpx2 .get()` .
154158
155159May raise ` starlette.websockets.WebSocketDisconnect ` if the application does not accept the websocket connection.
156160
@@ -191,12 +195,12 @@ using your existing async database client/infrastructure.
191195
192196For these situations, using ` TestClient ` is difficult because it creates its own event loop and async
193197resources (like a database connection) often cannot be shared across event loops.
194- The simplest way to work around this is to just make your entire test async and use an async client, like [ httpx .AsyncClient] .
198+ The simplest way to work around this is to just make your entire test async and use an async client, like [ httpx2 .AsyncClient] .
195199
196200Here is an example of such a test:
197201
198202``` python
199- from httpx import AsyncClient, ASGITransport
203+ from httpx2 import AsyncClient, ASGITransport
200204from starlette.applications import Starlette
201205from starlette.routing import Route
202206from starlette.requests import Request
@@ -222,4 +226,4 @@ async def test_app() -> None:
222226 assert r.text == " Hello World!"
223227```
224228
225- [ httpx .AsyncClient] : https://www.python-httpx.org/advanced/#calling-into-python-web-apps
229+ [ httpx2 .AsyncClient] : https://www.python-httpx.org/advanced/#calling-into-python-web-apps
0 commit comments