Skip to content

Commit

Permalink
tests: make urlllib3 transport tests more robust against local env
Browse files Browse the repository at this point in the history
When mocking os.environ pass clear=True to avoid getting host
configurations.

Fix #1968
  • Loading branch information
xrmx committed Feb 15, 2024
1 parent 460f141 commit 0541caf
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions tests/transports/test_urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,38 +115,46 @@ def test_generic_error(mock_urlopen, elasticapm_client):


def test_http_proxy_environment_variable(elasticapm_client):
with mock.patch.dict("os.environ", {"HTTP_PROXY": "http://example.com"}):
with mock.patch.dict("os.environ", {"HTTP_PROXY": "http://example.com"}, clear=True):
transport = Transport("http://localhost:9999", client=elasticapm_client)
assert isinstance(transport.http, urllib3.ProxyManager)


def test_https_proxy_environment_variable(elasticapm_client):
with mock.patch.dict("os.environ", {"HTTPS_PROXY": "https://example.com"}):
with mock.patch.dict(
"os.environ",
{
"HTTPS_PROXY": "https://example.com",
},
clear=True,
):
transport = Transport("http://localhost:9999", client=elasticapm_client)
assert isinstance(transport.http, urllib3.poolmanager.ProxyManager)


def test_https_proxy_environment_variable_is_preferred(elasticapm_client):
with mock.patch.dict("os.environ", {"https_proxy": "https://example.com", "HTTP_PROXY": "http://example.com"}):
with mock.patch.dict(
"os.environ", {"https_proxy": "https://example.com", "HTTP_PROXY": "http://example.com"}, clear=True
):
transport = Transport("http://localhost:9999", client=elasticapm_client)
assert isinstance(transport.http, urllib3.poolmanager.ProxyManager)
assert transport.http.proxy.scheme == "https"


def test_no_proxy_star(elasticapm_client):
with mock.patch.dict("os.environ", {"HTTPS_PROXY": "https://example.com", "NO_PROXY": "*"}):
with mock.patch.dict("os.environ", {"HTTPS_PROXY": "https://example.com", "NO_PROXY": "*"}, clear=True):
transport = Transport("http://localhost:9999", client=elasticapm_client)
assert not isinstance(transport.http, urllib3.poolmanager.ProxyManager)


def test_no_proxy_host(elasticapm_client):
with mock.patch.dict("os.environ", {"HTTPS_PROXY": "https://example.com", "NO_PROXY": "localhost"}):
with mock.patch.dict("os.environ", {"HTTPS_PROXY": "https://example.com", "NO_PROXY": "localhost"}, clear=True):
transport = Transport("http://localhost:9999", client=elasticapm_client)
assert not isinstance(transport.http, urllib3.poolmanager.ProxyManager)


def test_no_proxy_all(elasticapm_client):
with mock.patch.dict("os.environ", {"HTTPS_PROXY": "https://example.com", "NO_PROXY": "*"}):
with mock.patch.dict("os.environ", {"HTTPS_PROXY": "https://example.com", "NO_PROXY": "*"}, clear=True):
transport = Transport("http://localhost:9999", client=elasticapm_client)
assert not isinstance(transport.http, urllib3.poolmanager.ProxyManager)

Expand Down

0 comments on commit 0541caf

Please sign in to comment.