https://github.com/kludex/bump-testclient
Replace Starlette's TestClient from `requests` to `httpx`! 🎉
https://github.com/kludex/bump-testclient
Last synced: 6 months ago
JSON representation
Replace Starlette's TestClient from `requests` to `httpx`! 🎉
- Host: GitHub
- URL: https://github.com/kludex/bump-testclient
- Owner: Kludex
- License: mit
- Created: 2022-09-07T05:51:38.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-17T17:29:15.000Z (almost 3 years ago)
- Last Synced: 2025-03-29T11:41:28.986Z (7 months ago)
- Language: Python
- Size: 18.6 KB
- Stars: 42
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
bump-testclient
Starlette 0.21.0 changed the `TestClient` implementation: it replaced the `requests` library with `httpx`.
As those libraries have different APIs, this change break tests for Starlette's users.This [codemod](https://libcst.readthedocs.io/en/stable/codemods_tutorial.html) makes the transition to Starlette 0.21.0 easier.
It makes the changes needed to make the tests work again.## Why?
Make your life easier. Suggested by [Sebastián RamÃrez](https://twitter.com/tiangolo) as a joke, but well... I did it.
## Transformations
1. Replace `client.(...)` by `client.request("", ...)`
The methods ("delete", "get", "head", "options") doesn't accept the `content`, `data`, `json` and `files` parameters.
Conditions for this transformation:
- Using `client.` and `` in ("delete", "get", "head", "options").
- Using `content`, `data`, `json` or `files` parameters.2. Replace `client.(..., allow_redirects=...)` by `client.(..., follow_redirects=...)`
HTTPX uses `follow_redirects` instead of `allow_redirects`.
3. Replace `client.(..., data=...)` by `client.(..., content=...)`
If the argument passed to `data` is either text or bytes, `content` should be used instead.
Conditions for this to happen:
- `data` parameter receives a bytes/text argument.## Installation
```bash
pip install bump-testclient
```## Usage
Run the following on the repository you want to format:
```bash
python -m bump_testclient
```You can also use the pre-commit. Add the following to your `.pre-commit-config.yaml` file:
```yaml
- repo: https://github.com/Kludex/bump-testclient
rev: 0.3.0
hooks:
- id: bump_testclient
```## License
This project is licensed under the terms of the MIT license.