test proxy bypass with config from registry
# -*- coding: utf-8 -*-
+import os
from io import BytesIO
import pytest
@@ -599,3 +600,50 @@ def test_should_bypass_proxies_no_proxy(
no_proxy = '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1'
# Test 'no_proxy' argument
assert should_bypass_proxies(url, no_proxy=no_proxy) == expected
+
+
+@pytest.mark.skipif(os.name != 'nt', reason='Test only on Windows')
+@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.22/', False),
+ ('http://172.16.1.22:5000/', False),
+ ('http://google.com:5000/v1.0/', False),
+ ))
+def test_should_bypass_proxies_win_registry(url, expected, monkeypatch):
+ """Tests for function should_bypass_proxies to check if proxy
+ can be bypassed or not with Windows registry settings
+ """
+ if compat.is_py3:
+ import winreg
+ else:
+ import _winreg as winreg
+
+ class RegHandle:
+ def Close(self):
+ pass
+
+ ie_settings = RegHandle()
+
+ def OpenKey(key, subkey):
+ return ie_settings
+
+ def QueryValueEx(key, value_name):
+ if key is ie_settings:
+ if value_name == 'ProxyEnable':
+ return [1]
+ elif value_name == 'ProxyOverride':
+ return ['192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1']
+
+ monkeypatch.setenv('http_proxy', '')
+ monkeypatch.setenv('https_proxy', '')
+ monkeypatch.setenv('ftp_proxy', '')
+ monkeypatch.setenv('no_proxy', '')
+ monkeypatch.setenv('NO_PROXY', '')
+ monkeypatch.setattr(winreg, 'OpenKey', OpenKey)
+ monkeypatch.setattr(winreg, 'QueryValueEx', QueryValueEx)
+ assert should_bypass_proxies(url, no_proxy=None) == expected
