def test_registry_filesystem_path(self): """ The AutoConfigURL from the Windows Registry can also be a filesystem path. Test that this works, but local PAC files with invalid contents raise an error. """ handle, fs_pac_path = mkstemp() os.close(handle) try: with patch('pypac.api.ON_WINDOWS', return_value=True), \ patch('pypac.api.autoconfig_url_from_registry', return_value=fs_pac_path): with pytest.raises(MalformedPacError): get_pac(from_os_settings=True) with open(fs_pac_path, 'w') as f: f.write(proxy_pac_js_tpl % 'DIRECT') assert isinstance(get_pac(from_os_settings=True), PACFile) finally: os.remove(fs_pac_path)
def resolve_proxy(self, target_url=None): if not self.proxy: return None elif self.proxy == PROXY_TYPE_PAC: pac_file = PacAPI.get_pac() if not pac_file: return None proxy_resolver = PacProxyResolver(pac_file) url_to_resolve = target_url if not url_to_resolve and self.api: url_to_resolve = self.api if not url_to_resolve: url_to_resolve = PROXY_PAC_DEFAULT_URL return proxy_resolver.get_proxy_for_requests(url_to_resolve) else: return {'http': self.proxy, 'https': self.proxy, 'ftp': self.proxy}
def test_get_pac_autodetect(self): with _patch_download_pac(None): assert get_pac() is None with _patch_download_pac(direct_pac_js): assert isinstance(get_pac(), PACFile)
def test_get_pac_via_js(self): assert isinstance(get_pac(js=direct_pac_js), PACFile)
def test_get_pac_via_url(self): with _patch_download_pac(None): assert get_pac(url=arbitrary_pac_url) is None with _patch_download_pac(direct_pac_js): assert isinstance(get_pac(url=arbitrary_pac_url), PACFile)