python / python/cpython

test_urllib2.HandlerTests.test_redirect_no_path fails when proxy environment variables are set

Open
#148,919 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

tests type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Running:

./python -m unittest test.test_urllib2.HandlerTests.test_redirect_no_path

can fail in environments where proxy-related variables are set (http_proxy, https_proxy, etc.).

The test output shows below:

F
======================================================================
FAIL: test_redirect_no_path (test.test_urllib2.HandlerTests.test_redirect_no_path)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/yansendao/git/cpython/Lib/test/test_urllib2.py", line 1400, in test_redirect_no_path
    fp = urllib.request.urlopen("http://python.org/path")
  File "/home/yansendao/git/cpython/Lib/urllib/request.py", line 187, in urlopen
    return opener.open(url, data, timeout)
           ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/home/yansendao/git/cpython/Lib/urllib/request.py", line 489, in open
    response = self._open(req, data)
  File "/home/yansendao/git/cpython/Lib/urllib/request.py", line 506, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
                              '_open', req)
  File "/home/yansendao/git/cpython/Lib/urllib/request.py", line 466, in _call_chain
    result = func(*args)
  File "/home/yansendao/git/cpython/Lib/urllib/request.py", line 1352, in http_open
    return self.do_open(http.client.HTTPConnection, req)
           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yansendao/git/cpython/Lib/urllib/request.py", line 1323, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
              encode_chunked=req.has_header('Transfer-encoding'))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yansendao/git/cpython/Lib/test/test_urllib2.py", line 1395, in request
    self.assertEqual(url, next(urls))
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
AssertionError: 'http://python.org/path' != '/path'
- http://python.org/path
+ /path


----------------------------------------------------------------------
Ran 1 test in 0.010s

FAILED (failures=1)

Expected behavior
This test should validate relative redirect behavior (Location: ?query) independently of external shell/network configuration. It should pass regardless of whether proxy env vars are present.

Root cause
urllib.request.urlopen() honors proxy settings from environment variables.
When proxying is enabled, the HTTP request target format changes to absolute-form, which does not match this test’s path-only assertion.

Proposed fix
Make the test environment-independent by clearing proxy-related environment variables inside test_redirect_no_path using EnvironmentVarGuard (both lower/upper case variants, plus no_proxy/NO_PROXY).

Why proxy configuration matters when running tests
Some environments (corporate/campus networks, CI runners) require proxy settings for outbound connectivity; without them, networked tests may fail due to connection/timeouts.
However, logic-focused unit tests should not depend on external proxy state, so they should explicitly isolate themselves from proxy env effects.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-148920

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Lib/test/test_urllib2.py at HandlerTests.test_redirect_no_path and review how the test uses EnvironmentVarGuard. Run ./python -m unittest test.test_urllib2.HandlerTests.test_redirect_no_path with proxy variables set and unset. Done means the test validates the relative redirect and passes independently of proxy environment settings.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.