requests
requests copied to clipboard
Fix: Respect no_proxy in proxies dictionary and NO_PROXY env var
This commit addresses issue #5000 by ensuring that the no_proxy directive is respected, whether it's provided directly within the proxies dictionary passed to request functions or set as the NO_PROXY environment variable.
Previously, no_proxy in the proxies dictionary was not fully honored, and the NO_PROXY environment variable was not always checked when a proxies dictionary was provided.
This change includes:
-
src/requests/sessions.py: Modified theSession.sendmethod to check theno_proxykey within thekwargs['proxies']dictionary. If the request URL matches any pattern in theno_proxylist, the proxies are cleared for that request. -
src/requests/utils.py: Updated theselect_proxyfunction to check theNO_PROXYenvironment variable usingshould_bypass_proxiesbefore selecting a proxy from the providedproxiesdictionary. -
tests/test_requests.py: Added new test cases (test_no_proxy_in_proxies_dict,test_no_proxy_star_in_proxies_dict,test_no_proxy_not_matching_in_proxies_dict) to verify thatno_proxywithin theproxiesdictionary works as expected, using mocks to check if the proxy is bypassed. -
tests/test_utils.py: Added new test cases (test_select_proxy_with_no_proxy) to ensure theNO_PROXYenvironment variable is correctly handled byselect_proxy.
These changes ensure consistent behavior for proxy bypass logic, regardless of how the proxy settings are configured.
Closes #5000