Skip to content

Conversation

@tboerstad
Copy link

@tboerstad tboerstad commented Jan 10, 2026

This PR addresses an issue where pass_through in a proxy environment cause requests to fail:

$ 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'

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 request and tries to parse it into an httpx.URL, the parser crashes.

This PR simply bypass CONNECT requests at the mocker level, before URL parsing occurs.

Note
This means users cannot mock CONNECT requests (e.g. to simulate a 407 Proxy Authentication Required).

    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.
@tboerstad
Copy link
Author

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.
I leave it up to the maintainer to make a decision here, full understanding if this PR is closed instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant