Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace HTTP client on TestClient from requests to httpx #1376

Merged
merged 25 commits into from Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 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,6 +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/
[httpx]: https://www.python-httpx.org/
[requests]: http://docs.python-requests.org/en/master/
Kludex marked this conversation as resolved.
Show resolved Hide resolved
[jinja2]: http://jinja.pocoo.org/
[python-multipart]: https://andrew-d.github.io/python-multipart/
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.

By default the `TestClient` will raise any exceptions that occur in the
Expand Down Expand Up @@ -58,7 +58,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 @@ -91,7 +91,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
1 change: 0 additions & 1 deletion requirements.txt
Expand Up @@ -9,7 +9,6 @@ databases[sqlite]==0.5.5
flake8==4.0.1
isort==5.10.1
mypy==0.931
types-requests==2.26.3
types-contextvars==2.4.2
types-PyYAML==6.0.4
types-dataclasses==0.6.2
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Expand Up @@ -29,6 +29,8 @@ filterwarnings=
ignore: The 'variables' alias has been deprecated. Please use 'variable_values' instead\.:DeprecationWarning
# Workaround for Python 3.9.7 (see https://bugs.python.org/issue45097)
ignore:The loop argument is deprecated since Python 3\.8, and scheduled for removal in Python 3\.10\.:DeprecationWarning:asyncio
ignore: The `allow_redirects` argument is deprecated. Use `follow_redirects` instead.:DeprecationWarning
ignore: Use 'content=<...>' to upload raw bytes/text content.:DeprecationWarning
ignore: run_until_first_complete is deprecated and will be removed in a future version.:DeprecationWarning
ignore: starlette\.middleware\.wsgi is deprecated and will be removed in a future release\.*:DeprecationWarning

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -48,7 +48,8 @@ def get_long_description():
"jinja2",
"python-multipart",
"pyyaml",
"requests",
# "httpx>=0.20.0"
"httpx @ git+https://github.com/encode/httpx.git@master",
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until next httpx release. This is a blocker.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could httpx cut a release? This is going to cause issues for anyone that depends on httpx right? I realize you can probably just not install the "full" extra, but this still breaks things for those that are already doing it / it's a bit unergonomic and confusing.

]
},
classifiers=[
Expand Down