def test_fetch(): test_str = """ function FindProxyForURL(domain, host) { return "DIRECT; PROXY 127.0.0.1:8080; SOCKS 192.168.1.1:4444"; } """ class PACHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'application/x-ns-proxy-autoconfig') self.end_headers() self.wfile.write(test_str.encode("ascii")) ready_event = threading.Event() def serve(): httpd = http.server.HTTPServer(("127.0.0.1", 8081), PACHandler) ready_event.set() httpd.handle_request() httpd.server_close() serve_thread = threading.Thread(target=serve, daemon=True) serve_thread.start() try: ready_event.wait() res = pac.PACFetcher(QUrl("pac+http://127.0.0.1:8081")) assert res.fetch_error() is None finally: serve_thread.join() proxies = res.resolve(QNetworkProxyQuery(QUrl("https://example.com/test"))) assert len(proxies) == 3
def _handle_custom_proxies(self, request): proxy = None # proxies set in proxy profiles or `proxy` HTTP argument splash_proxy_factory = self._get_webpage_attribute( request, 'splash_proxy_factory') if splash_proxy_factory: proxy_query = QNetworkProxyQuery(request.url()) proxy = splash_proxy_factory.queryProxy(proxy_query)[0] self.setProxy(proxy) # proxies set in on_request if hasattr(request, 'custom_proxy'): proxy = request.custom_proxy self.setProxy(proxy) # Handle proxy auth. We're setting Proxy-Authorization header # explicitly because Qt loves to cache proxy credentials. if proxy is None: return user, password = proxy.user(), proxy.password() if not user and not password: return auth = b"Basic " + base64.b64encode("{}:{}".format( user, password).encode("utf-8")) request.setRawHeader(b"Proxy-Authorization", auth)
def test_fail_parse(value): test_str_f = """ function FindProxyForURL(domain, host) {{ return "{}"; }} """ res = pac.PACResolver(test_str_f.format(value)) with pytest.raises(pac.ParseProxyError): res.resolve(QNetworkProxyQuery(QUrl("https://example.com/test")))
def test_fail_return(): test_str = """ function FindProxyForURL(domain, host) { return null; } """ res = pac.PACResolver(test_str) with pytest.raises(pac.EvalProxyError): res.resolve(QNetworkProxyQuery(QUrl("https://example.com/test")))
def test_invalid_port(): test_str = """ function FindProxyForURL(domain, host) { return "PROXY 127.0.0.1:FOO"; } """ res = pac.PACResolver(test_str) with pytest.raises(pac.ParseProxyError): res.resolve(QNetworkProxyQuery(QUrl("https://example.com/test")))
def test_fetch_success(): test_str = """ function FindProxyForURL(domain, host) { return "DIRECT; PROXY 127.0.0.1:8080; SOCKS 192.168.1.1:4444"; } """ res = fetcher_test(test_str) proxies = res.resolve(QNetworkProxyQuery(QUrl("https://example.com/test"))) assert len(proxies) == 3
def test_logging(qtlog): """Make sure console.log() works for PAC files.""" test_str = """ function FindProxyForURL(domain, host) { console.log("logging test"); return "DIRECT"; } """ res = pac.PACResolver(test_str) res.resolve(QNetworkProxyQuery(QUrl("https://example.com/test"))) assert len(qtlog.records) == 1 assert qtlog.records[0].message == 'logging test'
def test_fetch_evalerror(caplog): test_str = """ function FindProxyForURL(domain, host) { return "FOO"; } """ res = fetcher_test(test_str) with caplog.at_level(logging.ERROR): proxies = res.resolve(QNetworkProxyQuery(QUrl("https://example.com/test"))) assert len(proxies) == 1 assert proxies[0].port() == 9
def _handle_custom_proxies(self, request): # proxies set in proxy profiles or `proxy` HTTP argument splash_proxy_factory = self._get_webpage_attribute(request, 'splash_proxy_factory') if splash_proxy_factory: proxy_query = QNetworkProxyQuery(request.url()) proxy = splash_proxy_factory.queryProxy(proxy_query)[0] self.setProxy(proxy) # proxies set in on_request if hasattr(request, 'custom_proxy'): self.setProxy(request.custom_proxy) user, password = request.custom_proxy.user(), request.custom_proxy.password() if not user and not password: return auth = b"Basic " + base64.b64encode("{}:{}".format(user, password).encode("utf-8")) request.setRawHeader(b"Proxy-Authorization", auth)
def _pac_common_test(test_str): fun_str_f = """ function FindProxyForURL(domain, host) {{ {} return "DIRECT; PROXY 127.0.0.1:8080; SOCKS 192.168.1.1:4444"; }} """ fun_str = fun_str_f.format(test_str) res = pac.PACResolver(fun_str) proxies = res.resolve(QNetworkProxyQuery(QUrl("https://example.com/test"))) assert len(proxies) == 3 assert proxies[0].type() == QNetworkProxy.NoProxy assert proxies[1].type() == QNetworkProxy.HttpProxy assert proxies[1].hostName() == "127.0.0.1" assert proxies[1].port() == 8080 assert proxies[2].type() == QNetworkProxy.Socks5Proxy assert proxies[2].hostName() == "192.168.1.1" assert proxies[2].port() == 4444
def test_secret_url(url, has_secret, from_file): """Make sure secret parts in an URL are stripped correctly. The following parts are considered secret: - If the PAC info is loaded from a local file, nothing. - If the URL to resolve is a HTTP URL, the username/password. - If the URL to resolve is a HTTPS URL, the username/password, query and path. """ test_str = """ function FindProxyForURL(domain, host) {{ has_secret = domain.indexOf("secret") !== -1; expected_secret = {}; if (has_secret !== expected_secret) {{ throw new Error("Expected secret: " + expected_secret + ", found: " + has_secret + " in " + domain); }} return "DIRECT"; }} """.format('true' if (has_secret or from_file) else 'false') res = pac.PACResolver(test_str) res.resolve(QNetworkProxyQuery(QUrl(url)), from_file=from_file)