test_urllib2.HandlerTests.test_redirect_no_path fails when proxy environment variables are set
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 Lib/test/test_urllib2.py 中的 HandlerTests.test_redirect_no_path 開始,檢查測試如何使用 EnvironmentVarGuard。在設定和未設定 proxy 變數的情況下執行 ./python -m unittest test.test_urllib2.HandlerTests.test_redirect_no_path。完成的標準是測試驗證相對重新導向,並且不受 proxy 環境設定影響而通過。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- testing-qa
- Issue 類型
- 缺陷
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 35/100