Document dev proxy setup (#933)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yeray Diaz Diaz · committed by GitHub · parent d34c89a · Browse files · 1 file·+57 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+57 added−0 removed
- other:+57 added−0 removed
Provenance
- Commit
afbd4c8- Tree
ae6d27c- Author
Yeray Diaz Diaz <yeraydiazdiaz@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
d34c89a
1 file changed+57 added−0 removed
modified:docs/contributing.md+57 added
154 unmodified linesExpand all155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215154 unmodified linesExpand all
For an example, see [#362](https://github.com/encode/httpx/pull/362).
Once the release PR is merged, run `$ scripts/publish` to publish the new release to PyPI.
## Development proxy setup
To test and debug requests via a proxy it's best to run a proxy server locally.Any server should do but HTTPCore's test suite uses[`mitmproxy`](https://mitmproxy.org/) which is written in Python, it's fullyfeatured and has excellent UI and tools for introspection of requests.
You can install `mitmproxy` using `pip install mitmproxy` or [severalother ways](https://docs.mitmproxy.org/stable/overview-installation/).
`mitmproxy` does require setting up local TLS certificates for HTTPS requests,as its main purpose is to allow developers to inspect requests that pass throughit. We can set them up follows:
1. [`pip install trustme-cli`](https://github.com/sethmlarson/trustme-cli/).2. `trustme-cli -i example.org www.example.org`, assuming you want to testconnecting to that domain, this will create three files: `server.pem`,`server.key` and `client.pem`.3. `mitmproxy` requires a PEM file that includes the private key and thecertificate so we need to concatenate them:`cat server.key server.pem > server.withkey.pem`.4. Start the proxy server `mitmproxy --certs server.withkey.pem`, or use the[other mitmproxy commands](https://docs.mitmproxy.org/stable/) with differentUI options.
At this point the server is ready to start serving requests, you'll need toconfigure HTTPX as described in the[proxy section](https://www.python-httpx.org/advanced/#http-proxying) andthe [SSL certificates section](https://www.python-httpx.org/advanced/#ssl-certificates),this is where our previously generated `client.pem` comes in:
```import httpx
proxies = {"all": "http://127.0.0.1:8080/"}
with httpx.Client(proxies=proxies, verify="/path/to/client.pem") as client: response = client.get("https://example.org") print(response.status_code) # should print 200```
Note, however, that HTTPS requests will only succeed to the host specifiedin the SSL/TLS certificate we generated, HTTPS requests to other hosts willraise an error like:
```ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificateverify failed: Hostname mismatch, certificate is not valid for'duckduckgo.com'. (_ssl.c:1108)```
If you want to make requests to more hosts you'll need to regenerate thecertificates and include all the hosts you intend to connect to in theseconds step, i.e.
`trustme-cli -i example.org www.example.org duckduckgo.com www.duckduckgo.com`