diff --git a/requests/sessions.py b/requests/sessions.py index fdf7e9fe35..39281eb1f1 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -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. diff --git a/tests/test_requests.py b/tests/test_requests.py index 5b6a7f5847..223c36e363 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -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 @@ -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')