def stream(*args, **kwargs): nonlocal first if first: first = False raise httpx.RequestError('fake exception', request=None) return httpx.Response(status_code=200, text=TestNetworkStreamRetries.TEXT)
async def test_host_fallback(self): ably = AblyRest(token="foo") def make_url(host): base_url = "%s://%s:%d" % (ably.http.preferred_scheme, host, ably.http.preferred_port) return urljoin(base_url, '/') with mock.patch('httpx.Request', wraps=httpx.Request) as request_mock: with mock.patch('httpx.AsyncClient.send', side_effect=httpx.RequestError('')) as send_mock: with pytest.raises(httpx.RequestError): await ably.http.make_request('GET', '/', skip_auth=True) assert send_mock.call_count == Defaults.http_max_retry_count expected_urls_set = { make_url(host) for host in Options( http_max_retry_count=10).get_rest_hosts() } for ((_, url), _) in request_mock.call_args_list: assert url in expected_urls_set expected_urls_set.remove(url) expected_hosts_set = set( Options(http_max_retry_count=10).get_rest_hosts()) for (prep_request_tuple, _) in send_mock.call_args_list: assert prep_request_tuple[0].headers.get( 'host') in expected_hosts_set expected_hosts_set.remove( prep_request_tuple[0].headers.get('host')) await ably.close()
async def get_response(*args, **kwargs): nonlocal request_count request_count += 1 if request_count < 3: raise httpx.RequestError('fake exception', request=None) return httpx.Response(status_code=200, text=TestNetworkRequestRetries.TEXT)
async def _make_request( self, method: str, path: str, params: Dict[str, Any], url_format: str = REST_V2_FORMAT, ) -> httpx.Response: if path.startswith("http"): url = path else: url = url_format.format(path) try: if method == "GET": response = await self._session.get(url, params=params) elif method == "POST": response = await self._session.post(url, json=params) else: raise httpx.RequestError("only GET and POST") except httpx.RequestError as e: self._log.error(f"An Exception occurred when trying to perform " f"the {method} request!") self._log.error(f"Params: {params}") self._log.error(f"Method: {method}") self._log.error(f"Request: {e.request}") if isinstance(e, httpx.HTTPStatusError): self._log.error(f"Response: {e.response}") # pylint: disable=no-member raise (e) return response
def test_make_request__raises_Abort_if_client_request_raises_exception( respx_mock, dummy_client): """ Validate that the ``make_request()`` function will raise an Abort if the call to ``client.send`` raises an exception. """ client = dummy_client(headers={"content-type": "garbage"}) req_path = "/fake-path" original_error = httpx.RequestError("BOOM!") respx_mock.get(f"{DEFAULT_DOMAIN}{req_path}").mock( side_effect=original_error) with pytest.raises( Abort, match="There was a big problem: Communication with the API failed" ) as err_info: make_request(client, req_path, "GET", abort_message="There was a big problem", abort_subject="BIG PROBLEM", support=True) assert err_info.value.subject == "BIG PROBLEM" assert err_info.value.support is True assert err_info.value.log_message == "There was an error making the request to the API" assert err_info.value.original_error == original_error
async def test_max_retry_attempts_and_timeouts_defaults(self): ably = AblyRest(token="foo") assert 'http_open_timeout' in ably.http.CONNECTION_RETRY_DEFAULTS assert 'http_request_timeout' in ably.http.CONNECTION_RETRY_DEFAULTS with mock.patch('httpx.AsyncClient.send', side_effect=httpx.RequestError('')) as send_mock: with pytest.raises(httpx.RequestError): await ably.http.make_request('GET', '/', skip_auth=True) assert send_mock.call_count == Defaults.http_max_retry_count timeout = ( ably.http.CONNECTION_RETRY_DEFAULTS['http_open_timeout'], ably.http.CONNECTION_RETRY_DEFAULTS['http_request_timeout'], ) assert send_mock.call_args == mock.call(mock.ANY) await ably.close()
async def test_setup_failed_connect(hass): """Test setup when connection error occurs.""" respx.get("http://localhost", content=httpx.RequestError(message="any", request=Mock())) assert await async_setup_component( hass, sensor.DOMAIN, { "sensor": { "platform": "rest", "resource": "http://localhost", "method": "GET", } }, ) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0
async def test_setup_failed_connect(opp, caplog): """Test setup when connection error occurs.""" respx.get("http://localhost").mock( side_effect=httpx.RequestError("server offline", request=MagicMock())) assert await async_setup_component( opp, sensor.DOMAIN, { "sensor": { "platform": "rest", "resource": "http://localhost", "method": "GET", } }, ) await opp.async_block_till_done() assert len(opp.states.async_all()) == 0 assert "server offline" in caplog.text
async def test_no_host_fallback_nor_retries_if_custom_host(self): custom_host = 'example.org' ably = AblyRest(token="foo", rest_host=custom_host) custom_url = "%s://%s:%d/" % (ably.http.preferred_scheme, custom_host, ably.http.preferred_port) with mock.patch('httpx.Request', wraps=httpx.Request) as request_mock: with mock.patch('httpx.AsyncClient.send', side_effect=httpx.RequestError('')) as send_mock: with pytest.raises(httpx.RequestError): await ably.http.make_request('GET', '/', skip_auth=True) assert send_mock.call_count == 1 assert request_mock.call_args == mock.call(mock.ANY, custom_url, content=mock.ANY, headers=mock.ANY) await ably.close()
async def test_analyze_file_request_error(): respx.get("http://perdu.com/").mock( side_effect=httpx.RequestError("error")) persister = AsyncMock() home_dir = os.getenv("HOME") or os.getenv("USERPROFILE") base_dir = os.path.join(home_dir, ".wapiti") persister.CONFIG_DIR = os.path.join(base_dir, "config") request = Request("http://perdu.com/") request.path_id = 1 crawler = AsyncCrawler(Request("http://perdu.com/")) options = {"timeout": 10, "level": 2} module_htp = ModuleHtp(crawler, persister, options, Event()) await module_htp._analyze_file(Request("http://perdu.com")) assert len(module_htp.tech_versions) == 0 assert module_htp.network_errors == 1
def _get_movie_api_resource(self, url): """ _get_movie_api_resource private method(meant to be private) to communicate with external api this method directly calls Credy movie API using httpx lib, which also supports async http call. The main reason to make it a separate method is to make it easier to mock. It returns httpx response or blank dict """ api_resp = dict() retry = True for i in range(2): if not retry: break try: # actual api call api_resp = httpx.get(url, auth=(settings.API_USERNAME, settings.API_PASSWORD)) # we raise RequestError if there is any issue with the response if not isinstance( api_resp, httpx_response ) or api_resp.status_code != status.HTTP_200_OK: logger.error( "Error occurred while requesting for user {} url {}". format(self.request.user.username, url)) raise httpx.RequestError(message="Error occurred", request=api_resp.request) # if success we don't want any retry retry = False except httpx.RequestError as req_err: logger.error( "Error occurred while requesting for user {} url {} error {}" .format(self.request.user.username, url, req_err)) if i != 0: # log the retry cases(may be for metric) logger.warning("Retrying {} times for user {} url {}".format( i, self.request.user.username, url)) return api_resp
async def test_setup_with_exception(hass): """Test setup with exception.""" respx.get("http://localhost", status_code=200, content="{}") assert await async_setup_component( hass, "binary_sensor", { "binary_sensor": { "platform": "rest", "resource": "http://localhost", "method": "GET", "value_template": "{{ value_json.dog }}", "name": "foo", "verify_ssl": "true", "timeout": 30, } }, ) await hass.async_block_till_done() assert len(hass.states.async_all()) == 1 state = hass.states.get("binary_sensor.foo") assert state.state == STATE_OFF await async_setup_component(hass, "homeassistant", {}) await hass.async_block_till_done() respx.clear() respx.get("http://localhost", content=httpx.RequestError(message="any", request=Mock())) await hass.services.async_call( "homeassistant", "update_entity", {ATTR_ENTITY_ID: ["binary_sensor.foo"]}, blocking=True, ) await hass.async_block_till_done() state = hass.states.get("binary_sensor.foo") assert state.state == STATE_UNAVAILABLE
def error_response(request, ext): raise httpx.RequestError(message='Error from service', request=request)
async def get_response(*args, **kwargs): raise httpx.RequestError('fake exception', request=None)