Fix pass_through with HTTP proxy - #303
Conversation
|
First, thank you for making respx, very neat! This PR is a compromise, a small implementation which I think could solve a real issue for users, namely using this library behind a proxy. I ran into this myself when using Anthropic web based Claude Code offering, which is behind a proxy. However this also means mocking the proxy CONNECT request will not work properly, which is not great. |
|
Thanks for raising the issue @tboerstad, and sorry for the late reply 😅 .. .. It feels like there should be a better place to address the issue, e.g. where the URL parsing error happens, and let the connect request successfully flow through the respx router and properly be passed through 🤔 i.e. ending up with this being possible .. respx.route(method="CONNECT").pass_through() |
|
Thank you @lundberg. I agree, that's a better solution. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #303 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 22 22
Lines 2941 2996 +55
Branches 200 204 +4
=========================================
+ Hits 2941 2996 +55 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
lundberg
left a comment
There was a problem hiding this comment.
Looks good, fix failing workflow jobs and we're good to go!
Btw, you can locally run lint fix, test matrix etc with task command (no args)
Oops, sorry for the back and forth. It's working locally now, except for a formatter issue with |
My bad 😉 .. I've pushed a fix to master, please rebase. |
Using pass_through fails when behind a proxy (e.g. corporate network
with http_proxy/https_proxy environment variables):
$ https_proxy=http://proxy:8080 python
>>> with respx.mock:
... respx.route().pass_through()
... httpx.get("https://example.com")
httpx.InvalidURL: Invalid port: 8080example.com:443
Bypass CONNECT requests in HTTPCoreMocker to allow proxy tunnel
establishment.
CONNECT requests were silently forwarded to the real transport, so there was no way to mock proxy tunnel behavior. This parses them into proper httpx.Request objects so they go through normal route matching like everything else.
Thanks, have rebased, everything is passing locally now. |
This PR addresses an issue where
pass_throughin a proxy environment causes requests to fail:When a proxy is configured, httpcore sends a CONNECT request to establish a tunnel. This request uses authority-form URLs (e.g.
example.com:443) with no scheme. When respx intercepts this and tries to parse it into anhttpx.URL, the parser crashes.This PR fixes the URL construction in
to_httpx_requestso that CONNECT requests flow through the normal route matching system. This means CONNECT tunnels can be mocked just like any other request:Or mock proxy errors: