Skip to content

Commit

Permalink
Merge pull request #5681 from mateusduboli/5677
Browse files Browse the repository at this point in the history
5677: Rebuild proxies on Session#send
  • Loading branch information
sigmavirus24 authored Jan 24, 2021
2 parents 74b7280 + 60ea7f0 commit 8c211a9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def send(self, request, **kwargs):
kwargs.setdefault('stream', self.stream)
kwargs.setdefault('verify', self.verify)
kwargs.setdefault('cert', self.cert)
kwargs.setdefault('proxies', self.proxies)
kwargs.setdefault('proxies', self.rebuild_proxies(request, self.proxies))

# It's possible that users might accidentally send a Request object.
# Guard against that specific failure case.
Expand Down
39 changes: 39 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
# listening on that port)
TARPIT = 'http://10.255.255.1'

# This is to avoid waiting the timeout of using TARPIT
INVALID_PROXY='http://localhost:1'

try:
from ssl import SSLContext
del SSLContext
Expand Down Expand Up @@ -551,6 +554,42 @@ def test_proxy_error_on_bad_url(self, httpbin, httpbin_secure):
with pytest.raises(InvalidProxyURL):
requests.get(httpbin(), proxies={'http': 'http:///example.com:8080'})

def test_respect_proxy_env_on_send_self_prepared_request(self, httpbin):
with override_environ(http_proxy=INVALID_PROXY):
with pytest.raises(ProxyError):
session = requests.Session()
request = requests.Request('GET', httpbin())
session.send(request.prepare())

def test_respect_proxy_env_on_send_session_prepared_request(self, httpbin):
with override_environ(http_proxy=INVALID_PROXY):
with pytest.raises(ProxyError):
session = requests.Session()
request = requests.Request('GET', httpbin())
prepared = session.prepare_request(request)
session.send(prepared)

def test_respect_proxy_env_on_send_with_redirects(self, httpbin):
with override_environ(http_proxy=INVALID_PROXY):
with pytest.raises(ProxyError):
session = requests.Session()
url = httpbin('redirect/1')
print(url)
request = requests.Request('GET', url)
session.send(request.prepare())

def test_respect_proxy_env_on_get(self, httpbin):
with override_environ(http_proxy=INVALID_PROXY):
with pytest.raises(ProxyError):
session = requests.Session()
session.get(httpbin())

def test_respect_proxy_env_on_request(self, httpbin):
with override_environ(http_proxy=INVALID_PROXY):
with pytest.raises(ProxyError):
session = requests.Session()
session.request(method='GET', url=httpbin())

def test_basicauth_with_netrc(self, httpbin):
auth = ('user', 'pass')
wrong_auth = ('wronguser', 'wrongpass')
Expand Down

0 comments on commit 8c211a9

Please sign in to comment.