Skip to content

Commit 6be16a4

Browse files
authored
tests: make urlllib3 transport tests more robust against local env (#1969)
When mocking os.environ pass clear=True to avoid getting host configurations. Fix #1968
1 parent a1bde22 commit 6be16a4

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

tests/transports/test_urllib3.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,38 +115,46 @@ def test_generic_error(mock_urlopen, elasticapm_client):
115115

116116

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

122122

123123
def test_https_proxy_environment_variable(elasticapm_client):
124-
with mock.patch.dict("os.environ", {"HTTPS_PROXY": "https://example.com"}):
124+
with mock.patch.dict(
125+
"os.environ",
126+
{
127+
"HTTPS_PROXY": "https://example.com",
128+
},
129+
clear=True,
130+
):
125131
transport = Transport("http://localhost:9999", client=elasticapm_client)
126132
assert isinstance(transport.http, urllib3.poolmanager.ProxyManager)
127133

128134

129135
def test_https_proxy_environment_variable_is_preferred(elasticapm_client):
130-
with mock.patch.dict("os.environ", {"https_proxy": "https://example.com", "HTTP_PROXY": "http://example.com"}):
136+
with mock.patch.dict(
137+
"os.environ", {"https_proxy": "https://example.com", "HTTP_PROXY": "http://example.com"}, clear=True
138+
):
131139
transport = Transport("http://localhost:9999", client=elasticapm_client)
132140
assert isinstance(transport.http, urllib3.poolmanager.ProxyManager)
133141
assert transport.http.proxy.scheme == "https"
134142

135143

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

141149

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

147155

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

0 commit comments

Comments
 (0)