def test_default_port(self): p = ProxyManager('http://something') self.addCleanup(p.clear) self.assertEqual(p.proxy.port, 80) p = ProxyManager('https://something') self.addCleanup(p.clear) self.assertEqual(p.proxy.port, 443)
def test_default_port(self) -> None: with ProxyManager("http://something") as p: assert p.proxy is not None assert p.proxy.port == 80 with ProxyManager("https://something") as p: assert p.proxy is not None assert p.proxy.port == 443
def test_proxy_tunnel(self): http_url = parse_url("http://example.com") https_url = parse_url("https://example.com") with ProxyManager("http://proxy:8080") as p: assert p._proxy_requires_url_absolute_form(http_url) assert p._proxy_requires_url_absolute_form(https_url) is False with ProxyManager("https://proxy:8080") as p: assert p._proxy_requires_url_absolute_form(http_url) assert p._proxy_requires_url_absolute_form(https_url)
def test_proxy_connect_retry(self): retry = Retry(total=None, connect=False) port = find_unused_port() with ProxyManager(f"http://localhost:{port}") as p: with pytest.raises(ProxyError) as ei: p.urlopen("HEAD", url="http://localhost/", retries=retry) assert isinstance(ei.value.original_error, NewConnectionError) retry = Retry(total=None, connect=2) with ProxyManager(f"http://localhost:{port}") as p: with pytest.raises(MaxRetryError) as ei: p.urlopen("HEAD", url="http://localhost/", retries=retry) assert isinstance(ei.value.reason.original_error, NewConnectionError)
def test_proxy_headers(self): url = "http://pypi.org/project/urllib3/" with ProxyManager("http://something:1234") as p: # Verify default headers default_headers = {"Accept": "*/*", "Host": "pypi.org"} headers = p._set_proxy_headers(url) assert headers == default_headers # Verify default headers don't overwrite provided headers provided_headers = { "Accept": "application/json", "custom": "header", "Host": "test.python.org", } headers = p._set_proxy_headers(url, provided_headers) assert headers == provided_headers # Verify proxy with nonstandard port provided_headers = {"Accept": "application/json"} expected_headers = provided_headers.copy() expected_headers.update({"Host": "pypi.org:8080"}) url_with_port = "http://pypi.org:8080/project/urllib3/" headers = p._set_proxy_headers(url_with_port, provided_headers) assert headers == expected_headers
def test_proxy_headers(self): p = ProxyManager('http://something:1234') self.addCleanup(p.clear) url = 'http://pypi.python.org/test' # Verify default headers default_headers = {'Accept': '*/*', 'Host': 'pypi.python.org'} headers = p._set_proxy_headers(url) self.assertEqual(headers, default_headers) # Verify default headers don't overwrite provided headers provided_headers = { 'Accept': 'application/json', 'custom': 'header', 'Host': 'test.python.org' } headers = p._set_proxy_headers(url, provided_headers) self.assertEqual(headers, provided_headers) # Verify proxy with nonstandard port provided_headers = {'Accept': 'application/json'} expected_headers = provided_headers.copy() expected_headers.update({'Host': 'pypi.python.org:8080'}) url_with_port = 'http://pypi.python.org:8080/test' headers = p._set_proxy_headers(url_with_port, provided_headers) self.assertEqual(headers, expected_headers)
def test_proxy_headers(self): url = 'http://pypi.python.org/test' with ProxyManager('http://something:1234') as p: # Verify default headers default_headers = {'Accept': '*/*', 'Host': 'pypi.python.org'} headers = p._set_proxy_headers(url) assert headers == default_headers # Verify default headers don't overwrite provided headers provided_headers = { 'Accept': 'application/json', 'custom': 'header', 'Host': 'test.python.org' } headers = p._set_proxy_headers(url, provided_headers) assert headers == provided_headers # Verify proxy with nonstandard port provided_headers = {'Accept': 'application/json'} expected_headers = provided_headers.copy() expected_headers.update({'Host': 'pypi.python.org:8080'}) url_with_port = 'http://pypi.python.org:8080/test' headers = p._set_proxy_headers(url_with_port, provided_headers) assert headers == expected_headers
def test_oldapi(self): with ProxyManager(connection_from_url(self.proxy_url), ca_certs=DEFAULT_CA) as http: r = http.request("GET", f"{self.http_url}/") assert r.status == 200 r = http.request("GET", f"{self.https_url}/") assert r.status == 200
def test_oldapi(self): http = ProxyManager(connection_from_url(self.proxy_url)) r = http.request('GET', '%s/' % self.http_url) self.assertEqual(r.status, 200) r = http.request('GET', '%s/' % self.https_url) self.assertEqual(r.status, 200)
def test_oldapi(self): http = ProxyManager(connection_from_url(self.proxy_url), ca_certs=DEFAULT_CA) self.addCleanup(http.clear) r = http.request("GET", "%s/" % self.http_url) assert r.status == 200 r = http.request("GET", "%s/" % self.https_url) assert r.status == 200
def test_oldapi(self): http = ProxyManager(connection_from_url(self.proxy_url), ca_certs=DEFAULT_CA) self.addCleanup(http.clear) r = http.request('GET', '%s/' % self.http_url) self.assertEqual(r.status, 200) r = http.request('GET', '%s/' % self.https_url) self.assertEqual(r.status, 200)
def test_nagle_proxy(self): """ Test that proxy connections do not have TCP_NODELAY turned on """ with ProxyManager(self.proxy_url) as http: hc2 = http.connection_from_host(self.http_host, self.http_port) conn = hc2._get_conn() try: hc2._make_request(conn, "GET", "/") tcp_nodelay_setting = conn.sock.getsockopt( socket.IPPROTO_TCP, socket.TCP_NODELAY) assert tcp_nodelay_setting == 0, ( "Expected TCP_NODELAY for proxies to be set " "to zero, instead was %s" % tcp_nodelay_setting) finally: conn.close()
def test_proxy_headers(self): p = ProxyManager(None) # Verify default headers default_headers = {'Accept': '*/*'} headers = p._set_proxy_headers() self.assertEqual(headers, default_headers) # Verify default headers don't overwrite provided headers provided_headers = {'Accept': 'application/json', 'custom': 'header'} headers = p._set_proxy_headers(provided_headers) self.assertEqual(headers, provided_headers)
def test_proxy_headers(self): p = ProxyManager(None) url = 'http://pypi.python.org/test' # Verify default headers default_headers = {'Accept': '*/*', 'Host': 'pypi.python.org'} headers = p._set_proxy_headers(url) self.assertEqual(headers, default_headers) # Verify default headers don't overwrite provided headers provided_headers = { 'Accept': 'application/json', 'custom': 'header', 'Host': 'test.python.org' } headers = p._set_proxy_headers(url, provided_headers) self.assertEqual(headers, provided_headers)
def test_invalid_scheme(self): with pytest.raises(AssertionError): ProxyManager("invalid://host/p") with pytest.raises(ValueError): ProxyManager("invalid://host/p")
# Handle keyboard exit before multi-thread operations print_results_worker = None try: # Resolve target host to avoid multiple dns lookups if not conf.proxy_url: resolved, port = dnscache.get_host_ip(conf.target_host, conf.target_port) # disable urllib'3 SSL warning (globally) urllib3.disable_warnings() # Benchmark target host if conf.proxy_url: database.connection_pool = ProxyManager( conf.proxy_url, timeout=conf.fetch_timeout_secs, maxsize=conf.thread_count, cert_reqs='CERT_NONE') elif not conf.proxy_url and is_ssl: database.connection_pool = HTTPSConnectionPool( resolved, port=str(port), timeout=conf.fetch_timeout_secs, maxsize=conf.thread_count) else: database.connection_pool = HTTPConnectionPool( resolved, port=str(port), timeout=conf.fetch_timeout_secs, maxsize=conf.thread_count)
def test_default_port(self): with ProxyManager('http://something') as p: assert p.proxy.port == 80 with ProxyManager('https://something') as p: assert p.proxy.port == 443