Skip to content

test_urllib: two urlopen_HttpTests tests fail when an HTTPS proxy is configured #160511

Open

Description

@devdavidejesus

Bug report

Bug description:

Two tests in test_urllib.urlopen_HttpTests fail when https_proxy (or HTTPS_PROXY) is set in the environment, as it often is behind a corporate proxy. http_proxy alone does not trigger it.

$ https_proxy=http://127.0.0.1:9 ./python.exe -m test test_urllib -v
...
ERROR: test_url_host_with_control_char_rejected (test.test_urllib.urlopen_HttpTests.test_url_host_with_control_char_rejected)
...
urllib.error.URLError: <urlopen error [Errno 61] Connection refused>
...
ERROR: test_url_host_with_newline_header_injection_rejected (test.test_urllib.urlopen_HttpTests.test_url_host_with_newline_header_injection_rejected)
...
ValueError: Invalid header value b'localhost\r\nX-injected: header\r\n:8080'
...
FAILED (errors=2, skipped=2)

Both errors come from the https: half of each test, which expects http.client.InvalidURL from the host check in HTTPConnection.__init__(). With an HTTPS proxy, the connection is created for the proxy and the bad host is passed to set_tunnel(), so it is rejected later with a different error:

  • test_url_host_with_control_char_rejected: _tunnel() raises ValueError after connecting to the proxy. Here nothing listens on the proxy address, so the test gets URLError first.
  • test_url_host_with_newline_header_injection_rejected: putheader() raises ValueError for the Host header, before connecting.

The tests call urllib.request.urlopen(), whose default opener includes ProxyHandler(), so they depend on what getproxies() returns: the environment, or on macOS also the system proxy settings. The opener is also cached at module level: when the whole file runs, urlopen_FileTests has already built it with the proxies of that moment. ProxyTests in the same file already removes the proxy variables in setUp().

The tests pass when no proxy is set. The same tests and code are in 3.14 and 3.15.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    testsTests in the Lib/test dir
    on Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions