python / python/cpython

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

未关闭
#148,919 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

tests type-bug
主要语言
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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Lib/test/test_urllib2.py 中的 HandlerTests.test_redirect_no_path 开始,检查测试如何使用 EnvironmentVarGuard。在设置和未设置代理变量的情况下运行 ./python -m unittest test.test_urllib2.HandlerTests.test_redirect_no_path。完成的标准是测试验证相对重定向,并且不受代理环境设置影响而通过。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
testing-qa
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。