Skip to content

Commit

Permalink
Replace HTTP client on TestClient from requests to httpx (#1376)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Sep 6, 2022
1 parent 243d03e commit 6765502
Show file tree
Hide file tree
Showing 13 changed files with 485 additions and 280 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -30,7 +30,7 @@ It is production-ready, and gives you the following:
* WebSocket support.
* In-process background tasks.
* Startup and shutdown events.
* Test client built on `requests`.
* Test client built on `httpx`.
* CORS, GZip, Static Files, Streaming responses.
* Session and Cookie support.
* 100% test coverage.
Expand Down Expand Up @@ -87,7 +87,7 @@ For a more complete example, see [encode/starlette-example](https://github.com/e

Starlette only requires `anyio`, and the following are optional:

* [`requests`][requests] - Required if you want to use the `TestClient`.
* [`httpx`][httpx] - Required if you want to use the `TestClient`.
* [`jinja2`][jinja2] - Required if you want to use `Jinja2Templates`.
* [`python-multipart`][python-multipart] - Required if you want to support form parsing, with `request.form()`.
* [`itsdangerous`][itsdangerous] - Required for `SessionMiddleware` support.
Expand Down Expand Up @@ -134,7 +134,7 @@ in isolation.
<p align="center"><i>Starlette is <a href="https://github.com/encode/starlette/blob/master/LICENSE.md">BSD licensed</a> code.<br/>Designed & crafted with care.</i></br>&mdash; ⭐️ &mdash;</p>

[asgi]: https://asgi.readthedocs.io/en/latest/
[requests]: http://docs.python-requests.org/en/master/
[httpx]: https://www.python-httpx.org/
[jinja2]: http://jinja.pocoo.org/
[python-multipart]: https://andrew-d.github.io/python-multipart/
[itsdangerous]: https://pythonhosted.org/itsdangerous/
Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Expand Up @@ -27,7 +27,7 @@ It is production-ready, and gives you the following:
* WebSocket support.
* In-process background tasks.
* Startup and shutdown events.
* Test client built on `requests`.
* Test client built on `httpx`.
* CORS, GZip, Static Files, Streaming responses.
* Session and Cookie support.
* 100% test coverage.
Expand Down Expand Up @@ -83,7 +83,7 @@ For a more complete example, [see here](https://github.com/encode/starlette-exam

Starlette only requires `anyio`, and the following dependencies are optional:

* [`requests`][requests] - Required if you want to use the `TestClient`.
* [`httpx`][httpx] - Required if you want to use the `TestClient`.
* [`jinja2`][jinja2] - Required if you want to use `Jinja2Templates`.
* [`python-multipart`][python-multipart] - Required if you want to support form parsing, with `request.form()`.
* [`itsdangerous`][itsdangerous] - Required for `SessionMiddleware` support.
Expand Down Expand Up @@ -130,7 +130,7 @@ in isolation.
<p align="center"><i>Starlette is <a href="https://github.com/encode/starlette/blob/master/LICENSE.md">BSD licensed</a> code.<br/>Designed & crafted with care.</i></br>&mdash; ⭐️ &mdash;</p>

[asgi]: https://asgi.readthedocs.io/en/latest/
[requests]: http://docs.python-requests.org/en/master/
[httpx]: https://www.python-httpx.org/
[jinja2]: http://jinja.pocoo.org/
[python-multipart]: https://andrew-d.github.io/python-multipart/
[itsdangerous]: https://pythonhosted.org/itsdangerous/
Expand Down
10 changes: 5 additions & 5 deletions docs/testclient.md
@@ -1,6 +1,6 @@

The test client allows you to make requests against your ASGI application,
using the `requests` library.
using the `httpx` library.

```python
from starlette.responses import HTMLResponse
Expand All @@ -19,11 +19,11 @@ def test_app():
assert response.status_code == 200
```

The test client exposes the same interface as any other `requests` session.
The test client exposes the same interface as any other `httpx` session.
In particular, note that the calls to make a request are just standard
function calls, not awaitables.

You can use any of `requests` standard API, such as authentication, session
You can use any of `httpx` standard API, such as authentication, session
cookies handling, or file uploads.

For example, to set headers on the TestClient you can do:
Expand Down Expand Up @@ -96,7 +96,7 @@ def test_app()

You can also test websocket sessions with the test client.

The `requests` library will be used to build the initial handshake, meaning you
The `httpx` library will be used to build the initial handshake, meaning you
can use the same authentication options and other headers between both http and
websocket testing.

Expand Down Expand Up @@ -129,7 +129,7 @@ always raised by the test client.

#### Establishing a test session

* `.websocket_connect(url, subprotocols=None, **options)` - Takes the same set of arguments as `requests.get()`.
* `.websocket_connect(url, subprotocols=None, **options)` - Takes the same set of arguments as `httpx.get()`.

May raise `starlette.websockets.WebSocketDisconnect` if the application does not accept the websocket connection.

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -37,7 +37,7 @@ full = [
"jinja2",
"python-multipart",
"pyyaml",
"requests",
"httpx>=0.22.0",
]

[project.urls]
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Expand Up @@ -10,7 +10,6 @@ flake8==3.9.2
isort==5.10.1
mypy==0.971
typing_extensions==4.3.0
types-requests==2.26.3
types-contextvars==2.4.7
types-PyYAML==6.0.11
types-dataclasses==0.6.6
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Expand Up @@ -32,6 +32,9 @@ filterwarnings=
ignore: starlette\.middleware\.wsgi is deprecated and will be removed in a future release\.*:DeprecationWarning
ignore: Async generator 'starlette\.requests\.Request\.stream' was garbage collected before it had been exhausted.*:ResourceWarning
ignore: path is deprecated.*:DeprecationWarning:certifi
ignore: Use 'content=<...>' to upload raw bytes/text content.:DeprecationWarning
ignore: The `allow_redirects` argument is deprecated. Use `follow_redirects` instead.:DeprecationWarning
ignore: 'cgi' is deprecated and slated for removal in Python 3\.13:DeprecationWarning

[coverage:run]
source_pkgs = starlette, tests
Expand Down

0 comments on commit 6765502

Please sign in to comment.