From 17b6c5742ca7118858962d73220968a2e281b82e Mon Sep 17 00:00:00 2001 From: Kumar Varadarajulu Date: Mon, 16 May 2016 06:11:25 +0000 Subject: [PATCH 1/3] consider plain ip notation of an ip in no_proxy if not in cidr notation --- requests/utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/requests/utils.py b/requests/utils.py index c08448cc..eaa89df2 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -539,6 +539,10 @@ def should_bypass_proxies(url): if is_valid_cidr(proxy_ip): if address_in_network(ip, proxy_ip): return True + elif ip == proxy_ip: + # If no_proxy ip was defined in plain IP notation instead of cidr notation & + # matches the IP of the index + return True else: for host in no_proxy: if netloc.endswith(host) or netloc.split(':')[0].endswith(host): From f59a95713ebefcbcf4baf247d9ef193d5e8b83b0 Mon Sep 17 00:00:00 2001 From: Kumar Varadarajulu Date: Mon, 16 May 2016 10:40:22 +0000 Subject: [PATCH 2/3] Added tests for should_bypass_proxies function --- tests/test_utils.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 13d44df9..3da5dc3c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -11,7 +11,7 @@ from requests.utils import ( guess_filename, guess_json_utf, is_ipv4_address, is_valid_cidr, iter_slices, parse_dict_header, parse_header_links, prepend_scheme_if_needed, - requote_uri, select_proxy, super_len, + requote_uri, select_proxy, should_bypass_proxies, super_len, to_key_val_list, to_native_string, unquote_header_value, unquote_unreserved, urldefragauth) @@ -131,6 +131,42 @@ class TestGetEnvironProxies: assert get_environ_proxies(url) != {} +class TestShouldBypassProxies: + """ + Tests for should_bypass_proxies function + """ + + @pytest.mark.parametrize( + 'url, expected', ( + ('http://192.168.0.1:5000/', True), + ('http://192.168.0.1/', True), + ('http://172.16.1.1/', True), + ('http://172.16.1.1:5000/', True), + ('http://localhost.localdomain:5000/v1.0/', True), + )) + def test_should_bypass_proxies(self, url, expected, monkeypatch): + """ + Test to check if proxy is bypassed + """ + monkeypatch.setenv('no_proxy', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1') + monkeypatch.setenv('NO_PROXY', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1') + assert should_bypass_proxies(url) == expected + + @pytest.mark.parametrize( + 'url, expected', ( + ('http://172.16.1.12/', False), + ('http://172.16.1.12:5000/', False), + ('http://google.com:5000/v1.0/', False), + )) + def test_should_bypass_proxies(self, url, expected, monkeypatch): + """ + Test to check if proxy is not bypassed + """ + monkeypatch.setenv('no_proxy', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1') + monkeypatch.setenv('NO_PROXY', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1') + assert should_bypass_proxies(url) == expected + + class TestIsIPv4Address: def test_valid(self): From b94decc47c1026067b99e4452ddabec1ad139845 Mon Sep 17 00:00:00 2001 From: Kumar Varadarajulu Date: Mon, 16 May 2016 10:49:37 +0000 Subject: [PATCH 3/3] Combined tests to one tests for should_bypass_proxies method --- tests/test_utils.py | 56 ++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 3da5dc3c..68e5a173 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -131,42 +131,6 @@ class TestGetEnvironProxies: assert get_environ_proxies(url) != {} -class TestShouldBypassProxies: - """ - Tests for should_bypass_proxies function - """ - - @pytest.mark.parametrize( - 'url, expected', ( - ('http://192.168.0.1:5000/', True), - ('http://192.168.0.1/', True), - ('http://172.16.1.1/', True), - ('http://172.16.1.1:5000/', True), - ('http://localhost.localdomain:5000/v1.0/', True), - )) - def test_should_bypass_proxies(self, url, expected, monkeypatch): - """ - Test to check if proxy is bypassed - """ - monkeypatch.setenv('no_proxy', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1') - monkeypatch.setenv('NO_PROXY', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1') - assert should_bypass_proxies(url) == expected - - @pytest.mark.parametrize( - 'url, expected', ( - ('http://172.16.1.12/', False), - ('http://172.16.1.12:5000/', False), - ('http://google.com:5000/v1.0/', False), - )) - def test_should_bypass_proxies(self, url, expected, monkeypatch): - """ - Test to check if proxy is not bypassed - """ - monkeypatch.setenv('no_proxy', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1') - monkeypatch.setenv('NO_PROXY', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1') - assert should_bypass_proxies(url) == expected - - class TestIsIPv4Address: def test_valid(self): @@ -464,3 +428,23 @@ def test_to_native_string(value, expected): )) def test_urldefragauth(url, expected): assert urldefragauth(url) == expected + + +@pytest.mark.parametrize( + 'url, expected', ( + ('http://192.168.0.1:5000/', True), + ('http://192.168.0.1/', True), + ('http://172.16.1.1/', True), + ('http://172.16.1.1:5000/', True), + ('http://localhost.localdomain:5000/v1.0/', True), + ('http://172.16.1.12/', False), + ('http://172.16.1.12:5000/', False), + ('http://google.com:5000/v1.0/', False), + )) +def test_should_bypass_proxies(url, expected, monkeypatch): + """ + Tests for function should_bypass_proxies to check if proxy can be bypassed or not + """ + monkeypatch.setenv('no_proxy', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1') + monkeypatch.setenv('NO_PROXY', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1') + assert should_bypass_proxies(url) == expected