python / python/cpython

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

Offen
#148,919 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

tests type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in Lib/test/test_urllib2.py bei HandlerTests.test_redirect_no_path und überprüfe, wie der Test EnvironmentVarGuard verwendet. Führe ./python -m unittest test.test_urllib2.HandlerTests.test_redirect_no_path aus, wenn Proxy-Variablen gesetzt und wenn sie nicht gesetzt sind. Erledigt ist die Aufgabe, wenn der Test die relative Weiterleitung validiert und unabhängig von den Einstellungen der Proxy-Umgebung erfolgreich ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
testing-qa
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.