Skip to content

Commit 78d17d1

Browse files
crepererumuntitaker
authored andcommitted
Rework proxy selection (getsentry#309)
* split and extend proxy tests * fix HTTP fallback for proxy env vars Fixes getsentry#308 * Fine-grained control over proxy selection. Fixes getsentry#178
1 parent d25173d commit 78d17d1

File tree

2 files changed

+98
-11
lines changed

2 files changed

+98
-11
lines changed

sentry_sdk/transport.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,15 @@ def _make_pool(
156156
ca_certs, # type: Optional[Any]
157157
):
158158
# type: (...) -> Union[PoolManager, ProxyManager]
159-
# Use http_proxy if scheme is https and https_proxy is not set
160-
proxy = parsed_dsn.scheme == "https" and https_proxy or http_proxy
161-
if not proxy:
162-
proxy = getproxies().get(parsed_dsn.scheme)
159+
proxy = None
160+
161+
# try HTTPS first
162+
if parsed_dsn.scheme == "https" and (https_proxy != ""):
163+
proxy = https_proxy or getproxies().get("https")
164+
165+
# maybe fallback to HTTP proxy
166+
if not proxy and (http_proxy != ""):
167+
proxy = http_proxy or getproxies().get("http")
163168

164169
opts = self._get_pool_options(ca_certs)
165170

tests/test_client.py

+89-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
22
import json
33
import logging
4+
import os
45
import pytest
56
import subprocess
67
import sys
@@ -52,25 +53,106 @@ def test_transport_option(monkeypatch):
5253
assert str(Client(transport=transport).dsn) == dsn
5354

5455

55-
def test_http_proxy(monkeypatch):
56-
client = Client("https://foo@sentry.io/123", http_proxy="http://localhost/123")
56+
def test_proxy_http_use(monkeypatch):
57+
client = Client("http://foo@sentry.io/123", http_proxy="http://localhost/123")
5758
assert client.transport._pool.proxy.scheme == "http"
5859

60+
61+
def test_proxy_https_use(monkeypatch):
62+
client = Client("https://foo@sentry.io/123", http_proxy="https://localhost/123")
63+
assert client.transport._pool.proxy.scheme == "https"
64+
65+
66+
def test_proxy_both_select_http(monkeypatch):
5967
client = Client(
60-
"https://foo@sentry.io/123",
68+
"http://foo@sentry.io/123",
6169
https_proxy="https://localhost/123",
6270
http_proxy="http://localhost/123",
6371
)
64-
assert client.transport._pool.proxy.scheme == "https"
65-
66-
client = Client("http://foo@sentry.io/123", http_proxy="http://localhost/123")
6772
assert client.transport._pool.proxy.scheme == "http"
6873

74+
75+
def test_proxy_both_select_https(monkeypatch):
6976
client = Client(
70-
"http://foo@sentry.io/123",
77+
"https://foo@sentry.io/123",
7178
https_proxy="https://localhost/123",
7279
http_proxy="http://localhost/123",
7380
)
81+
assert client.transport._pool.proxy.scheme == "https"
82+
83+
84+
def test_proxy_http_fallback_http(monkeypatch):
85+
client = Client("https://foo@sentry.io/123", http_proxy="http://localhost/123")
86+
assert client.transport._pool.proxy.scheme == "http"
87+
88+
89+
def test_proxy_none_noenv(monkeypatch):
90+
client = Client("http://foo@sentry.io/123")
91+
assert client.transport._pool.proxy is None
92+
93+
94+
def test_proxy_none_httpenv_select(monkeypatch):
95+
os.environ["HTTP_PROXY"] = "http://localhost/123"
96+
client = Client("http://foo@sentry.io/123")
97+
assert client.transport._pool.proxy.scheme == "http"
98+
99+
100+
def test_proxy_none_httpsenv_select(monkeypatch):
101+
os.environ["HTTPS_PROXY"] = "https://localhost/123"
102+
client = Client("https://foo@sentry.io/123")
103+
assert client.transport._pool.proxy.scheme == "https"
104+
105+
106+
def test_proxy_none_httpenv_fallback(monkeypatch):
107+
os.environ["HTTP_PROXY"] = "http://localhost/123"
108+
client = Client("https://foo@sentry.io/123")
109+
assert client.transport._pool.proxy.scheme == "http"
110+
111+
112+
def test_proxy_bothselect_bothen(monkeypatch):
113+
os.environ["HTTP_PROXY"] = "http://localhost/123"
114+
os.environ["HTTPS_PROXY"] = "https://localhost/123"
115+
client = Client("https://foo@sentry.io/123", http_proxy="", https_proxy="")
116+
assert client.transport._pool.proxy is None
117+
118+
119+
def test_proxy_bothavoid_bothenv(monkeypatch):
120+
os.environ["HTTP_PROXY"] = "http://localhost/123"
121+
os.environ["HTTPS_PROXY"] = "https://localhost/123"
122+
client = Client("https://foo@sentry.io/123", http_proxy=None, https_proxy=None)
123+
assert client.transport._pool.proxy.scheme == "https"
124+
125+
126+
def test_proxy_bothselect_httpenv(monkeypatch):
127+
os.environ["HTTP_PROXY"] = "http://localhost/123"
128+
client = Client("https://foo@sentry.io/123", http_proxy=None, https_proxy=None)
129+
assert client.transport._pool.proxy.scheme == "http"
130+
131+
132+
def test_proxy_httpselect_bothenv(monkeypatch):
133+
os.environ["HTTP_PROXY"] = "http://localhost/123"
134+
os.environ["HTTPS_PROXY"] = "https://localhost/123"
135+
client = Client("https://foo@sentry.io/123", http_proxy=None, https_proxy="")
136+
assert client.transport._pool.proxy.scheme == "http"
137+
138+
139+
def test_proxy_httpsselect_bothenv(monkeypatch):
140+
os.environ["HTTP_PROXY"] = "http://localhost/123"
141+
os.environ["HTTPS_PROXY"] = "https://localhost/123"
142+
client = Client("https://foo@sentry.io/123", http_proxy="", https_proxy=None)
143+
assert client.transport._pool.proxy.scheme == "https"
144+
145+
146+
def test_proxy_httpselect_httpsenv(monkeypatch):
147+
os.environ["HTTPS_PROXY"] = "https://localhost/123"
148+
client = Client("https://foo@sentry.io/123", http_proxy=None, https_proxy="")
149+
assert client.transport._pool.proxy is None
150+
151+
152+
def test_proxy_httpsselect_bothenv_http(monkeypatch):
153+
os.environ["HTTP_PROXY"] = "http://localhost/123"
154+
os.environ["HTTPS_PROXY"] = "https://localhost/123"
155+
client = Client("http://foo@sentry.io/123", http_proxy=None, https_proxy=None)
74156
assert client.transport._pool.proxy.scheme == "http"
75157

76158

0 commit comments

Comments
 (0)