python / python/cpython

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

Aperta
#148,919 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

tests type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in Lib/test/test_urllib2.py, in HandlerTests.test_redirect_no_path, e verifica come il test utilizza EnvironmentVarGuard. Esegui ./python -m unittest test.test_urllib2.HandlerTests.test_redirect_no_path con le variabili proxy impostate e non impostate. Il lavoro è completato quando il test convalida il redirect relativo e supera il test indipendentemente dalle impostazioni dell'ambiente proxy.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
testing-qa
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.