Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass the session's proxies property to request #10680

Merged
merged 8 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/9691.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix pip install issues using a proxy due to an inconsistency in how Requests is currently handling variable precedence in session.
2 changes: 2 additions & 0 deletions src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ def is_secure_origin(self, location: Link) -> bool:
def request(self, method: str, url: str, *args: Any, **kwargs: Any) -> Response:
# Allow setting a default timeout on a session
kwargs.setdefault("timeout", self.timeout)
# Allow setting a default proxies on a session
kwargs.setdefault("proxies", self.proxies)

# Dispatch the actual request
return super().request(method, url, *args, **kwargs)
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def pytest_addoption(parser: Parser) -> None:
default=False,
help="run 'pip search' tests",
)
parser.addoption(
"--proxy",
action="store",
default=None,
help="use given proxy in session network tests",
)


def pytest_collection_modifyitems(config: Config, items: List[pytest.Item]) -> None:
Expand Down Expand Up @@ -624,3 +630,8 @@ def utc() -> Iterator[None]:
tzset()
yield
tzset()


@pytest.fixture
def proxy(request: pytest.FixtureRequest) -> str:
return request.config.getoption("proxy")
33 changes: 32 additions & 1 deletion tests/unit/test_network_session.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import logging
from typing import Any, List
from typing import Any, List, Optional
from urllib.parse import urlparse
from urllib.request import getproxies

import pytest
from pip._vendor import requests

from pip import __version__
from pip._internal.models.link import Link
Expand Down Expand Up @@ -242,3 +245,31 @@ def warning(self, *args: Any, **kwargs: Any) -> None:
actual_level, actual_message = log_records[0]
assert actual_level == "WARNING"
assert "is not a trusted or secure host" in actual_message

@pytest.mark.network
def test_proxy(self, proxy: Optional[str]) -> None:
session = PipSession(trusted_hosts=[])

if not proxy:
# if user didn't pass --proxy then try to get it from the system.
env_proxy = getproxies().get("http", None)
proxy = urlparse(env_proxy).netloc if env_proxy else None

if proxy:
# set proxy scheme to session.proxies
session.proxies = {
"http": f"{proxy}",
"https": f"{proxy}",
"ftp": f"{proxy}",
}

connection_error = None
try:
session.request("GET", "https://pypi.org", timeout=1)
except requests.exceptions.ConnectionError as e:
connection_error = e

assert connection_error is None, (
f"Invalid proxy {proxy} or session.proxies: "
f"{session.proxies} is not correctly passed to session.request."
)