def test_proxy_response_timeout(self): session = requests.Session() with pytest.raises(ProxyError): session.get(arbitrary_url, timeout=0.001, proxies=proxy_parameter_for_requests( "http://httpbin/delay/10"))
def test_proxy_connect_timeout(self): session = requests.Session() with pytest.raises(ConnectTimeout): session.get( arbitrary_url, timeout=1, proxies=proxy_parameter_for_requests("http://httpbin.org:99"))
def test_timeout_proxy(self): # Travis can refuse quickly, and trigger ProxyError instead. session = requests.Session() with pytest.raises(ConnectTimeout): session.get( arbitrary_url, timeout=0.001, proxies=proxy_parameter_for_requests('http://localhost'))
def test_pac_override_using_request_proxies_parameter(self): sess = PACSession() mock_ok = Mock(spec=requests.Response, status_code=204) with _patch_get_pac(PACFile(proxy_pac_js)), \ _patch_request_base(mock_ok) as request: for proxies_arg in ({}, proxy_parameter_for_requests('http://manual:80')): sess.get(arbitrary_url, proxies=proxies_arg) request.assert_called_with('GET', arbitrary_url, proxies=proxies_arg, allow_redirects=ANY)
def test_pac_override_using_request_proxies_parameter(self): sess = PACSession() mock_ok = Mock(spec=requests.Response, status_code=204) with _patch_get_pac(PACFile(proxy_pac_js)), \ _patch_request_base(mock_ok) as request: for proxies_arg in ({}, proxy_parameter_for_requests('http://manual:80')): sess.get(arbitrary_url, proxies=proxies_arg) request.assert_called_with('GET', arbitrary_url, proxies=proxies_arg, allow_redirects=ANY)
def test_unreachable_proxy(self, proxy_host): session = requests.Session() with pytest.raises(ProxyError): session.get(arbitrary_url, proxies=proxy_parameter_for_requests('http://' + proxy_host))
def get_call(url, proxy): return call( 'GET', url, proxies=proxy_parameter_for_requests(proxy) if proxy else proxy, allow_redirects=ANY)
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) proxy_pac_js = proxy_pac_js_tpl % 'PROXY fake.local:8080; DIRECT' fake_proxy_url = 'http://fake.local:8080' fake_proxies_requests_arg = proxy_parameter_for_requests(fake_proxy_url) def _patch_get_pac(return_value): return patch('pypac.api.get_pac', return_value=return_value) def get_call(url, proxy): return call( 'GET', url, proxies=proxy_parameter_for_requests(proxy) if proxy else proxy, allow_redirects=ANY) class TestRequests(object):
def test_timeout_proxy(self): # Travis can refuse quickly, and trigger ProxyError instead. session = requests.Session() with pytest.raises(ConnectTimeout): session.get(arbitrary_url, timeout=0.001, proxies=proxy_parameter_for_requests('http://localhost'))
def test_unreachable_proxy(self, proxy_host): session = requests.Session() with pytest.raises(ProxyError): session.get(arbitrary_url, proxies=proxy_parameter_for_requests('http://' + proxy_host))
def get_call(url, proxy): return call('GET', url, proxies=proxy_parameter_for_requests(proxy) if proxy else proxy, allow_redirects=ANY)
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) proxy_pac_js = proxy_pac_js_tpl % 'PROXY fake.local:8080; DIRECT' fake_proxy_url = 'http://fake.local:8080' fake_proxies_requests_arg = proxy_parameter_for_requests(fake_proxy_url) def _patch_get_pac(return_value): return patch('pypac.api.get_pac', return_value=return_value) def get_call(url, proxy): return call('GET', url, proxies=proxy_parameter_for_requests(proxy) if proxy else proxy, allow_redirects=ANY) class TestRequests(object): """Test the behaviour of the Requests library that PyPAC expects.""" @pytest.mark.parametrize('proxy_host', ['0.0.0.0', '127.0.0.1', 'unreachable.local', 'localhost'])