def _on_request(self, adapter, request, **kwargs): match = self._find_match(request) # TODO(dcramer): find the correct class for this if match is None: error_msg = 'Connection refused: {0} {1}'.format(request.method, request.url) response = ConnectionError(error_msg) response.request = request self._calls.add(request, response) raise response if 'body' in match and isinstance(match['body'], Exception): self._calls.add(request, match['body']) raise match['body'] headers = {} if match['content_type'] is not None: headers['Content-Type'] = match['content_type'] if 'callback' in match: # use callback status, r_headers, body = match['callback'](request) if isinstance(body, six.text_type): body = body.encode('utf-8') body = BufferIO(body) headers.update(r_headers) elif 'body' in match: if match['adding_headers']: headers.update(match['adding_headers']) status = match['status'] body = BufferIO(match['body']) response = HTTPResponse( status=status, reason=six.moves.http_client.responses[status], body=body, headers=headers, preload_content=False, ) response = adapter.build_response(request, response) if not match.get('stream'): response.content # NOQA try: resp_cookies = Cookies.from_request(response.headers['set-cookie']) response.cookies = cookiejar_from_dict(dict( (v.name, v.value) for _, v in resp_cookies.items() )) except (KeyError, TypeError): pass self._calls.add(request, response) return response
def _patched_fake_send(session, request, **kwargs): try: return _fake_send(session, request, **kwargs) except NoMockAddress: request = _adapter.last_request error_msg = "Connection refused: {0} {1}".format(request.method, request.url) response = ConnectionError(error_msg) response.request = request raise response
def test_error(get_response): def error(msg, *args, **kwargs): global error_msg error_msg = msg % args exc = ConnectionError('Connection aborted') exc.request = Request(method='GET', url='http://www.google.com') get_response.side_effect = exc ret = main(['--ignore-stdin', 'www.google.com'], custom_log_error=error) assert ret == ExitStatus.ERROR assert error_msg == ( 'ConnectionError: ' 'Connection aborted while doing GET request to URL: ' 'http://www.google.com')
def _on_request(self, adapter, request, **kwargs): match = self._find_match(request) # TODO(dcramer): find the correct class for this if match is None: error_msg = "Connection refused: {0} {1}".format(request.method, request.url) response = ConnectionError(error_msg) self._calls.add(request, response) raise response if "body" in match and isinstance(match["body"], Exception): self._calls.add(request, match["body"]) raise match["body"] headers = {"Content-Type": match["content_type"]} if "callback" in match: # use callback status, r_headers, body = match["callback"](request) if isinstance(body, six.text_type): body = body.encode("utf-8") body = BufferIO(body) headers.update(r_headers) elif "body" in match: if match["adding_headers"]: headers.update(match["adding_headers"]) status = match["status"] body = BufferIO(match["body"]) response = HTTPResponse(status=status, body=body, headers=headers, preload_content=False) response = adapter.build_response(request, response) if not match.get("stream"): response.content # NOQA try: resp_cookies = Cookies.from_request(response.headers["set-cookie"]) response.cookies = cookiejar_from_dict(dict((v.name, v.value) for _, v in resp_cookies.items())) except (KeyError, TypeError): pass self._calls.add(request, response) return response
def _on_request(self, adapter, request, **kwargs): match = self._find_match(request) resp_callback = self.response_callback if match is None: if request.url.startswith(self.passthru_prefixes): logger.info("request.allowed-passthru", extra={"url": request.url}) return _real_send(adapter, request, **kwargs) error_msg = ( "Connection refused by Responses: {0} {1} doesn't " "match Responses Mock".format(request.method, request.url) ) response = ConnectionError(error_msg) response.request = request self._calls.add(request, response) response = resp_callback(response) if resp_callback else response raise response try: response = adapter.build_response(request, match.get_response(request)) except Exception as response: match.call_count += 1 self._calls.add(request, response) response = resp_callback(response) if resp_callback else response raise if not match.stream: response.content # NOQA try: response.cookies = _cookies_from_headers(response.headers) except (KeyError, TypeError): pass response = resp_callback(response) if resp_callback else response match.call_count += 1 self._calls.add(request, response) return response
def validate_connection(self, timeout_in_s=DEFAULT_TIMEOUT_IN_S): try: start = time.time() ok = False while not ok and (time.time() - start < timeout_in_s): try: self.list() ok = True except ConnectionError, e: logger.warn(e.message) time.sleep(0.5) if not ok: raise ConnectionError('Failed to connect to manager (%s) within %d seconds' % (self._hostname, timeout_in_s))
def test_connection_error(self): with self.mock_config_info({'key': '12345678'}), \ patch('requests.get', side_effect=ConnectionError()), \ patch('logging.warning'): self.initialize_bot() self.assert_bot_response( message={'content': 'world without chocolate'}, response={ 'content': ('Uh oh, sorry :slightly_frowning_face:, I ' 'cannot process your request right now. But, ' 'let\'s try again later! :grin:') }, expected_method='send_reply')
def test_build_menu_connection_error(self): exception = ConnectionError('Internet is down') url = self.base_url + 'droplets/' responses.add(responses.GET, url, body=exception) indicator = DoIndicator.Indicator(self.do_api_token) indicator.build_menu() error = indicator.menu.get_children()[0] self.assertEqual(len(indicator.menu), 10) self.assertEqual(error.get_label(), "No network connection.")
def pnk_request(self, url): pool = ThreadPool(processes = 1) async_result = pool.apply_async(self.pnk_request_raw, (url,)) try: ret_val = async_result.get(timeout = self.hard_timeout) except TimeoutError as te: traceback.print_exc() #raise requests ConnectionError for easier handling if there's a hard timeout raise ConnectionError("Request received a hard timeout") return ret_val
def init(repository, anaconda_owner=None, anaconda_label=None): authenticate() try: _fetch(repository) except: pass else: raise ConnectionError("Repository already initialized on AppVeyor") appveyor_url = "https://ci.appveyor.com/api/projects" appveyor_data = dict(repositoryProvider="gitHub", repositoryName=repository["owner"]["login"] + "/" + repository["name"]) appveyor_request = requests.post(appveyor_url, data=json.dumps(appveyor_data), headers=HEADERS) if not appveyor_request.ok: raise ConnectionError(appveyor_request.text) else: appveyor_request = appveyor_request.json() _settings(repository=repository, anaconda_owner=anaconda_owner, anaconda_label=anaconda_label)
def get_vault_token(vault_role, access_token): if access_token is None: raise ValueError( "Vault secret retrieval error. Access token is not provided." ) request = {'jwt': access_token, 'role': vault_role} secret_vault_login_uri = Settings.vault_login_uri token_request = session.post(secret_vault_login_uri, data=request) if not token_request.ok: raise ConnectionError( "Vault auth error. {}".format(token_request.text) ) return token_request.json()['auth']['client_token']
def test_get_worklist_connection_error(self, mock_get): mock_get.side_effect = ConnectionError(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.get_worklist() self.assertEqual( context.exception.message, 'Encountered an error connecting to verification_api: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E402') self.assertEqual(context.exception.http_code, 500)
def test_device_status_error_on_connection_error(self): responses.add( responses.GET, TEST_SUPERVISOR_DEVICE_STATUS_URL, body=ConnectionError('Unable to connect') ) bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY) with self.assertRaises(RuntimeError) as exp: bs.get_device_status('appState') assert str(exp.exception).startswith("Device status request failed")
def test_shutdown_gateway_error_on_connection_error(self): responses.add( responses.POST, TEST_SUPERVISOR_SHUTDOWN_URL, body=ConnectionError('Unable to connect') ) bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY) with self.assertRaises(Exception) as exp: bs.shutdown() assert str(exp.exception) == "supervisor API not accessible"
def test_invalid_when_handle_message(self) -> None: get_bot_message_handler(self.bot_name) StubBotHandler() with self.mock_config_info({ "auth_token": "someInvalidKey", "username": "******", "goalname": "goal" }), patch("requests.get", side_effect=ConnectionError()), self.mock_http_conversation( "test_invalid_when_handle_message"), patch( "logging.exception"): self.verify_reply("5", "Error. Check your key!")
def get_api(self): ''' get an api key string, requires admin api ''' try: response = requests.get(self.url + "get_api", headers=self.headers, verify=False) try: return response.json()["api"] except: logger.info(response.text) raise ValueError("Invalid admin api key") except ConnectionError as e: raise ConnectionError("Could not connect: " + str(e))
def revoke_api(self, api): ''' add a new user, requires admin api ''' try: response = requests.put(self.url + "revoke_api/" + api, headers=self.headers, verify=False) try: return response.json() except: logger.error(response.text) raise ValueError("Invalid admin api key") except ConnectionError as e: raise ConnectionError("Could not connect: " + str(e))
def _fetch(repository): if not validators.url(repository): repository = git.Repo(repository) return _fetch(repository.remote().url) else: github_url = repository.replace("http:", "https:").replace(".git", "").replace("/github.com/", "/api.github.com/repos/") github_request = requests.get(github_url, auth=GITHUB_AUTH) if not github_request.ok: raise ConnectionError(github_request.text) else: github_request = github_request.json() return github_request
def check_connection(self): """Checks wether the connection with the environments works The methods tries to get content of PIM system homepage. It returns True if the request is handled properly, or raises an exception. Note: this method does NOT check whether credentials are correct. """ resp = self.session.get(self.cfg.baseurl) if resp.status_code != 200: raise ConnectionError('Connection could not be validated during ' 'check_connection method call.for ' f'environment : \'{self.cfg.env}\'') return (True)
def test_update_user_details_connection_error(self, mock_post): mock_post.side_effect = ConnectionError(self.error_msg) with self.assertRaises(ApplicationError) as c: VerificationAPI().update_user_details(self.item_id, {}) self.assertEqual( c.exception.message, 'Encountered an error connecting to verification_api: {}'.format( self.error_msg)) self.assertEqual(c.exception.code, 'E402') self.assertEqual(c.exception.http_code, 500) self.assertEqual(mock_post.call_count, 1)
def test_change_jira_status_empty_url(self, jira_get): # Configure jira mock jira_get.side_effect = ConnectionError('exception error') # Configure jira module jira.enabled = True # Test method jira.change_jira_status('TOOLIUM-1', 'Pass', None, []) # Check logging error message expected_response = "Test Case 'TOOLIUM-1' can not be updated: execution_url is not configured" self.logger.warn.assert_called_once_with(expected_response)
def stream_open(self, params): """open the OANDA stream""" url = 'https://%s/v1/%s' % (stream_host, self.name) headers = {'Authorization': 'Bearer %s' % api_key} res = requests.get(url, headers=headers, params=params, stream=True, timeout=self.timeout) if res.status_code != 200: print "OANDA request error %s" % (res.status_code) raise ConnectionError(res.content) return res
async def test_coordinator_update_when_unreachable(hass: HomeAssistant, fritz: Mock): """Test coordinator after reboot.""" entry = MockConfigEntry( domain=FB_DOMAIN, data=MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], unique_id="any", ) entry.add_to_hass(hass) fritz().get_devices.side_effect = [ConnectionError(), ""] assert not await hass.config_entries.async_setup(entry.entry_id) assert entry.state is ConfigEntryState.SETUP_RETRY
def get_secret(secret_path, vault_role, access_token) -> dict: logger.debug("Obtaining secret from Vault") vault_token = get_vault_token(vault_role, access_token) headers = {'X-Vault-Token': vault_token} secret_vault_uri = Settings.vault_secret_storage_uri secret_request = session.get( urllib.parse.urljoin(secret_vault_uri, secret_path), headers=headers ) if not secret_request.ok: raise ConnectionError( "Vault secret retrieval error. {}".format(secret_request.text) ) return secret_request.json()["data"]
def _on_request(self, adapter, request, **kwargs): match = self._find_match(request) resp_callback = self.response_callback if match is None: error_msg = 'Connection refused: {0} {1}'.format( request.method, request.url) response = ConnectionError(error_msg) response.request = request self._calls.add(request, response) response = resp_callback(response) if resp_callback else response raise response try: response = adapter.build_response( request, match.get_response(request), ) except Exception as response: match.call_count += 1 self._calls.add(request, response) response = resp_callback(response) if resp_callback else response raise if not match.stream: response.content # NOQA try: resp_cookies = Cookies.from_request(response.headers['set-cookie']) response.cookies = cookiejar_from_dict( dict((v.name, v.value) for _, v in resp_cookies.items())) except (KeyError, TypeError): pass response = resp_callback(response) if resp_callback else response match.call_count += 1 self._calls.add(request, response) return response
def _settings(repository, anaconda_owner=None, anaconda_label=None): appveyor_data = _fetch(repository) appveyor_url = "https://ci.appveyor.com/api/projects/" + appveyor_data[ "accountName"] + "/" + appveyor_data["slug"] + "/settings" appveyor_request = requests.get(appveyor_url, headers=HEADERS) if not appveyor_request.ok: raise ConnectionError(appveyor_request.text) else: appveyor_request = appveyor_request.json() appveyor_url = "https://ci.appveyor.com/api/projects" appveyor_data = appveyor_request["settings"] appveyor_data["name"] = appveyor_data["repositoryName"] appveyor_data["slug"] = appveyor_data["repositoryName"] appveyor_subdata = appveyor_data["configuration"]["environmentVariables"] anaconda_login, anaconda_password = conda.retrieve() if anaconda_login: appveyor_subdata.append( dict(name="ANACONDA_LOGIN", value=dict(value=anaconda_login, isEncrypted=False))) if anaconda_password: appveyor_subdata.append( dict(name="ANACONDA_PASSWORD", value=dict(value=anaconda_password, isEncrypted=True))) if anaconda_label: appveyor_subdata.append( dict(name="ANACONDA_LABEL", value=dict(value=anaconda_label, isEncrypted=False))) if anaconda_login and anaconda_password: if not anaconda_owner: anaconda_owner = repository["owner"]["login"] appveyor_subdata.append( dict(name="ANACONDA_OWNER", value=dict(value=anaconda_owner, isEncrypted=False))) appveyor_data["configuration"]["environmentVariables"] = appveyor_subdata appveyor_request = requests.put(appveyor_url, data=json.dumps(appveyor_data), headers=HEADERS) if not appveyor_request.ok: raise ConnectionError(appveyor_request.text)
def __init__(self): # inherit from DataSharingRestClient to make sure all the helper methods are the same super().__init__(rest_client._profile) self.sleeps = [] self._sleeper = self.sleeps.append http_error = HTTPError() response = Response() response.status_code = 429 http_error.response = response self.http_error = http_error self.connection_error = ConnectionError()
def test_get_index_no_network(self): err_msg = 'Max retries exceeded with url...' with mock.patch('requests.get', side_effect=mock_index_get_generator(DEFAULT_INDEX_URL, ConnectionError(err_msg))): with self.assertRaises(CLIError) as err: get_index() self.assertEqual(str(err.exception), ERR_TMPL_NO_NETWORK.format(err_msg)) err_msg = 'Max retries exceeded with url...' with mock.patch('requests.get', side_effect=mock_index_get_generator(DEFAULT_INDEX_URL, HTTPError(err_msg))): with self.assertRaises(CLIError) as err: get_index() self.assertEqual(str(err.exception), ERR_TMPL_NO_NETWORK.format(err_msg))
def test_connection_error(self): content_id = 'uuid' archive_url = 'http://example.com' request = testing.DummyRequest() request.registry = mock.Mock() request.registry.settings = {'archive.url': archive_url} from ..utils import fetch_archive_content from ..models import ArchiveConnectionError from requests.exceptions import ConnectionError with mock.patch('requests.get', side_effect=ConnectionError()) as get: self.assertRaises(ArchiveConnectionError, fetch_archive_content, request, content_id)
def test_get_manifest_digests_connection_error(tmpdir): # Test that our code to handle falling back from https to http # doesn't do anything unexpected when a connection can't be # made at all. kwargs = {} kwargs['image'] = ImageName.parse('example.com/spam:latest') kwargs['registry'] = 'https://example.com' url = 'https://example.com/v2/spam/manifests/latest' responses.add(responses.GET, url, body=ConnectionError()) with pytest.raises(ConnectionError): get_manifest_digests(**kwargs)
async def generate(): async with self: async with finalize(self.raw.stream(chunk_size)) as gen: try: async for trunk in gen: yield trunk except ProtocolError as e: raise ChunkedEncodingError(e) except DecodeError as e: raise ContentDecodingError(e) except ReadTimeoutError as e: raise ConnectionError(e) self._content_consumed = True
def _issue(repository, number): github_url = repository.remote().url upstream = _fetch(github_url) if "parent" in upstream: upstream = upstream["parent"] github_url = upstream["issues_url"].replace('{/number}', '/' + number) github_request = requests.get(github_url, auth=GITHUB_AUTH) if not github_request.ok: raise ConnectionError(github_request.text) else: github_request = github_request.json() return github_request
def parse_response(resp): """ Check response from server for errors, and returned parsed json from response """ if resp.status_code == 503: raise ConnectionError("Server Busy") try: data = resp.json() except json.decoder.JSONDecodeError: raise ElmwareRequestError("Invalid Server Response") if resp.status_code > 200: raise ElmwareRequestError(data["message"]) return data
async def test_urlize_plain_host(hass, requests_mock): """Test that plain host or IP gets converted to a URL.""" requests_mock.request(ANY, ANY, exc=ConnectionError()) host = "192.168.100.1" user_input = {**FIXTURE_USER_INPUT, CONF_URL: host} result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}, data=user_input) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "user" assert user_input[CONF_URL] == f"http://{host}/"
def _on_request(self, adapter, request, **kwargs): match = self._find_match(request) resp_callback = self.response_callback if match is None: if request.url.startswith(self.passthru_prefixes): logger.info("request.allowed-passthru", extra={"url": request.url}) return _real_send(adapter, request, **kwargs) error_msg = "Connection refused: {0} {1}".format( request.method, request.url ) response = ConnectionError(error_msg) response.request = request self._calls.add(request, response) response = resp_callback(response) if resp_callback else response raise response try: response = adapter.build_response(request, match.get_response(request)) except Exception as response: match.call_count += 1 self._calls.add(request, response) response = resp_callback(response) if resp_callback else response raise if not match.stream: response.content # NOQA try: response.cookies = _cookies_from_headers(response.headers) except (KeyError, TypeError): pass response = resp_callback(response) if resp_callback else response match.call_count += 1 self._calls.add(request, response) return response
def test_error_traceback(get_response): exc = ConnectionError('Connection aborted') exc.request = Request(method='GET', url='http://www.google.com') get_response.side_effect = exc with raises(ConnectionError): main(['--ignore-stdin', '--traceback', 'www.google.com'])
def _on_request(self, session, request, **kwargs): match = self._find_match(request) # TODO(dcramer): find the correct class for this if match is None: error_msg = 'Connection refused: {0} {1}'.format(request.method, request.url) response = ConnectionError(error_msg) self._calls.add(request, response) raise response if 'body' in match and isinstance(match['body'], Exception): self._calls.add(request, match['body']) raise match['body'] headers = { 'Content-Type': match['content_type'], } if 'callback' in match: # use callback status, r_headers, body = match['callback'](request) if isinstance(body, six.text_type): body = body.encode('utf-8') body = BufferIO(body) headers.update(r_headers) elif 'body' in match: if match['adding_headers']: headers.update(match['adding_headers']) status = match['status'] body = BufferIO(match['body']) response = HTTPResponse( status=status, body=body, headers=headers, preload_content=False, ) adapter = session.get_adapter(request.url) response = adapter.build_response(request, response) if not match.get('stream'): response.content # NOQA try: resp_cookies = Cookies.from_request(response.headers['set-cookie']) response.cookies = cookiejar_from_dict(dict( (v.name, v.value) for _, v in resp_cookies.items() )) session.cookies = response.cookies except (KeyError, TypeError): pass self._calls.add(request, response) if kwargs.get('allow_redirects') and response.is_redirect: # include redirect resolving logic from requests.sessions.Session keep_kws = ('stream', 'timeout', 'cert', 'proxies') resolve_kwargs = dict([(k, v) for (k, v) in kwargs.items() if k in keep_kws]) # this recurses if response.is_redirect, # but limited by session.max_redirects gen = session.resolve_redirects(response, request, **resolve_kwargs) history = [resp for resp in gen] # Shuffle things around if there's history. if history: # Insert the first (original) request at the start history.insert(0, response) # Get the last request made response = history.pop() response.history = history return response