|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 |
|
| 3 | +import os |
3 | 4 | from io import BytesIO
|
4 | 5 |
|
5 | 6 | import pytest
|
@@ -599,3 +600,50 @@ def test_should_bypass_proxies_no_proxy(
|
599 | 600 | no_proxy = '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1'
|
600 | 601 | # Test 'no_proxy' argument
|
601 | 602 | assert should_bypass_proxies(url, no_proxy=no_proxy) == expected
|
| 603 | + |
| 604 | + |
| 605 | +@pytest.mark.skipif(os.name != 'nt', reason='Test only on Windows') |
| 606 | +@pytest.mark.parametrize( |
| 607 | + 'url, expected', ( |
| 608 | + ('http://192.168.0.1:5000/', True), |
| 609 | + ('http://192.168.0.1/', True), |
| 610 | + ('http://172.16.1.1/', True), |
| 611 | + ('http://172.16.1.1:5000/', True), |
| 612 | + ('http://localhost.localdomain:5000/v1.0/', True), |
| 613 | + ('http://172.16.1.22/', False), |
| 614 | + ('http://172.16.1.22:5000/', False), |
| 615 | + ('http://google.com:5000/v1.0/', False), |
| 616 | + )) |
| 617 | +def test_should_bypass_proxies_win_registry(url, expected, monkeypatch): |
| 618 | + """Tests for function should_bypass_proxies to check if proxy |
| 619 | + can be bypassed or not with Windows registry settings |
| 620 | + """ |
| 621 | + if compat.is_py3: |
| 622 | + import winreg |
| 623 | + else: |
| 624 | + import _winreg as winreg |
| 625 | + |
| 626 | + class RegHandle: |
| 627 | + def Close(self): |
| 628 | + pass |
| 629 | + |
| 630 | + ie_settings = RegHandle() |
| 631 | + |
| 632 | + def OpenKey(key, subkey): |
| 633 | + return ie_settings |
| 634 | + |
| 635 | + def QueryValueEx(key, value_name): |
| 636 | + if key is ie_settings: |
| 637 | + if value_name == 'ProxyEnable': |
| 638 | + return [1] |
| 639 | + elif value_name == 'ProxyOverride': |
| 640 | + return ['192.168.*;127.0.0.1;localhost.localdomain;172.16.1.1'] |
| 641 | + |
| 642 | + monkeypatch.setenv('http_proxy', '') |
| 643 | + monkeypatch.setenv('https_proxy', '') |
| 644 | + monkeypatch.setenv('ftp_proxy', '') |
| 645 | + monkeypatch.setenv('no_proxy', '') |
| 646 | + monkeypatch.setenv('NO_PROXY', '') |
| 647 | + monkeypatch.setattr(winreg, 'OpenKey', OpenKey) |
| 648 | + monkeypatch.setattr(winreg, 'QueryValueEx', QueryValueEx) |
| 649 | + assert should_bypass_proxies(url, no_proxy=None) == expected |
0 commit comments