Skip to content

Commit ddac9f5

Browse files
mateusduboliJonathan Fung
authored and
Jonathan Fung
committed
psf#5677: Rebuild proxies on Session#send
1 parent 9d242b0 commit ddac9f5

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

requests/sessions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def send(self, request, **kwargs):
630630
kwargs.setdefault('stream', self.stream)
631631
kwargs.setdefault('verify', self.verify)
632632
kwargs.setdefault('cert', self.cert)
633-
kwargs.setdefault('proxies', self.proxies)
633+
kwargs.setdefault('proxies', self.rebuild_proxies(request, self.proxies))
634634

635635
# It's possible that users might accidentally send a Request object.
636636
# Guard against that specific failure case.

tests/test_requests.py

+39
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
# listening on that port)
4141
TARPIT = 'http://10.255.255.1'
4242

43+
# This is to avoid waiting the timeout of using TARPIT
44+
INVALID_PROXY='http://localhost:1'
45+
4346
try:
4447
from ssl import SSLContext
4548
del SSLContext
@@ -551,6 +554,42 @@ def test_proxy_error_on_bad_url(self, httpbin, httpbin_secure):
551554
with pytest.raises(InvalidProxyURL):
552555
requests.get(httpbin(), proxies={'http': 'http:///example.com:8080'})
553556

557+
def test_respect_proxy_env_on_send_self_prepared_request(self, httpbin):
558+
with override_environ(http_proxy=INVALID_PROXY):
559+
with pytest.raises(ProxyError):
560+
session = requests.Session()
561+
request = requests.Request('GET', httpbin())
562+
session.send(request.prepare())
563+
564+
def test_respect_proxy_env_on_send_session_prepared_request(self, httpbin):
565+
with override_environ(http_proxy=INVALID_PROXY):
566+
with pytest.raises(ProxyError):
567+
session = requests.Session()
568+
request = requests.Request('GET', httpbin())
569+
prepared = session.prepare_request(request)
570+
session.send(prepared)
571+
572+
def test_respect_proxy_env_on_send_with_redirects(self, httpbin):
573+
with override_environ(http_proxy=INVALID_PROXY):
574+
with pytest.raises(ProxyError):
575+
session = requests.Session()
576+
url = httpbin('redirect/1')
577+
print(url)
578+
request = requests.Request('GET', url)
579+
session.send(request.prepare())
580+
581+
def test_respect_proxy_env_on_get(self, httpbin):
582+
with override_environ(http_proxy=INVALID_PROXY):
583+
with pytest.raises(ProxyError):
584+
session = requests.Session()
585+
session.get(httpbin())
586+
587+
def test_respect_proxy_env_on_request(self, httpbin):
588+
with override_environ(http_proxy=INVALID_PROXY):
589+
with pytest.raises(ProxyError):
590+
session = requests.Session()
591+
session.request(method='GET', url=httpbin())
592+
554593
def test_basicauth_with_netrc(self, httpbin):
555594
auth = ('user', 'pass')
556595
wrong_auth = ('wronguser', 'wrongpass')

0 commit comments

Comments
 (0)