Skip to content

Commit

Permalink
[py] Handle NO_Proxy for connections to the driver. Fixes #9925
Browse files Browse the repository at this point in the history
  • Loading branch information
AutomatedTester committed Oct 21, 2021
1 parent 833c6d6 commit 153298f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions py/selenium/webdriver/remote/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ def __init__(self, remote_server_addr, keep_alive=False, resolve_ip=None, ignore
DeprecationWarning)
self.keep_alive = keep_alive
self._url = remote_server_addr

# Env var NO_PROXY will override this part of the code
_no_proxy = os.environ.get('no_proxy', os.environ.get('NO_PROXY'))
if _no_proxy is not None:
ignore_proxy = _no_proxy

self._proxy_url = self._get_proxy_url() if not ignore_proxy else None
if keep_alive:
self._conn = self._get_connection_manager()
Expand Down
23 changes: 21 additions & 2 deletions py/test/unit/selenium/webdriver/remote/remote_connection_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.


from selenium.webdriver.remote import remote_connection
import urllib3
import pytest

Expand Down Expand Up @@ -103,6 +104,12 @@ def test_get_connection_manager_with_proxy(mock_proxy_settings):
assert conn.proxy.port == 8080


def test_get_connection_manager_when_no_proxy_set(mock_no_proxy_settings):
remote_connection = RemoteConnection("https://remote")
conn = remote_connection._get_connection_manager()
assert type(conn) == urllib3.PoolManager


def test_ignore_proxy_env_vars(mock_proxy_settings):
remote_connection = RemoteConnection("http://remote", ignore_proxy=True)
conn = remote_connection._get_connection_manager()
Expand All @@ -123,19 +130,31 @@ def getheader(self, *args, **kwargs):
pass


@pytest.fixture
@pytest.fixture(scope="function")
def mock_proxy_settings_missing(monkeypatch):
monkeypatch.delenv("HTTPS_PROXY", raising=False)
monkeypatch.delenv("HTTP_PROXY", raising=False)
monkeypatch.delenv("https_proxy", raising=False)
monkeypatch.delenv("http_proxy", raising=False)


@pytest.fixture
@pytest.fixture(scope="function")
def mock_proxy_settings(monkeypatch):
http_proxy = 'http://http_proxy.com:8080'
https_proxy = 'http://https_proxy.com:8080'
monkeypatch.setenv("HTTPS_PROXY", https_proxy)
monkeypatch.setenv("HTTP_PROXY", http_proxy)
monkeypatch.setenv("https_proxy", https_proxy)
monkeypatch.setenv("http_proxy", http_proxy)


@pytest.fixture(scope="function")
def mock_no_proxy_settings(monkeypatch):
http_proxy = 'http://http_proxy.com:8080'
https_proxy = 'http://https_proxy.com:8080'
monkeypatch.setenv("HTTPS_PROXY", https_proxy)
monkeypatch.setenv("HTTP_PROXY", http_proxy)
monkeypatch.setenv("https_proxy", https_proxy)
monkeypatch.setenv("http_proxy", http_proxy)
monkeypatch.setenv("no_proxy", "localhost")
monkeypatch.setenv("NO_PROXY", "localhost")

0 comments on commit 153298f

Please sign in to comment.