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()
def __init__(self, key=None, token=None, token_details=None, **kwargs): """Create an AblyRest instance. :Parameters: **Credentials** - `key`: a valid key string **Or** - `token`: a valid token string - `token_details`: an instance of TokenDetails class **Optional Parameters** - `client_id`: Undocumented - `rest_host`: The host to connect to. Defaults to rest.ably.io - `environment`: The environment to use. Defaults to 'production' - `port`: The port to connect to. Defaults to 80 - `tls_port`: The tls_port to connect to. Defaults to 443 - `tls`: Specifies whether the client should use TLS. Defaults to True - `auth_token`: Undocumented - `auth_callback`: Undocumented - `auth_url`: Undocumented - `keep_alive`: use persistent connections. Defaults to True """ if key is not None and ('key_name' in kwargs or 'key_secret' in kwargs): raise ValueError( "key and key_name or key_secret are mutually exclusive. " "Provider either a key or key_name & key_secret") if key is not None: options = Options(key=key, **kwargs) elif token is not None: options = Options(auth_token=token, **kwargs) elif token_details is not None: if not isinstance(token_details, TokenDetails): raise ValueError( "token_details must be an instance of TokenDetails") options = Options(token_details=token_details, **kwargs) elif not ('auth_callback' in kwargs or 'auth_url' in kwargs or # and don't have both key_name and key_secret ('key_name' in kwargs and 'key_secret' in kwargs)): raise ValueError( "key is missing. Either an API key, token, or token auth method must be provided" ) else: options = Options(**kwargs) # if self.__keep_alive: # self.__session = requests.Session() # else: # self.__session = None self.__http = Http(self, options) self.__auth = Auth(self, options) self.__http.auth = self.__auth self.__channels = Channels(self) self.__options = options
def clear_test_vars(cls): test_vars = RestSetup.__test_vars options = Options(key=test_vars["keys"][0]["key_str"]) options.rest_host = test_vars["host"] options.port = test_vars["port"] options.tls_port = test_vars["tls_port"] options.tls = test_vars["tls"] ably = cls.get_ably_rest() ably.http.delete('/apps/' + test_vars['app_id']) RestSetup.__test_vars = None
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('requests.Request', wraps=requests.Request) as request_mock: with mock.patch('requests.sessions.Session.send', side_effect=requests.exceptions.RequestException ) as send_mock: with pytest.raises(requests.exceptions.RequestException): ably.http.make_request('GET', '/', skip_auth=True) assert send_mock.call_count == Defaults.http_max_retry_count expected_urls_set = 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)
def test_host_fallback(self): ably = AblyRest(token="foo") self.assertIn('http_max_retry_count', ably.http.CONNECTION_RETRY_DEFAULTS) 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('requests.Request', wraps=requests.Request) as request_mock: with mock.patch('requests.sessions.Session.send', side_effect=requests.exceptions.RequestException ) as send_mock: with self.assertRaises(requests.exceptions.RequestException): ably.http.make_request('GET', '/', skip_auth=True) self.assertEqual( send_mock.call_count, ably.http. CONNECTION_RETRY_DEFAULTS['http_max_retry_count']) expected_urls_set = set([ make_url(host) for host in ([ably.http.preferred_host] + Defaults.get_fallback_rest_hosts(Options())) ]) for ((__, url), ___) in request_mock.call_args_list: self.assertIn(url, expected_urls_set) expected_urls_set.remove(url)
def test_no_retry_if_not_500_to_599_http_code(self): default_host = Defaults.get_rest_host(Options()) ably = AblyRest(token="foo") self.assertIn('http_max_retry_count', ably.http.CONNECTION_RETRY_DEFAULTS) default_url = "%s://%s:%d/" % (ably.http.preferred_scheme, default_host, ably.http.preferred_port) def raise_ably_exception(*args, **kwagrs): raise AblyException(message="", status_code=600, code=50500) with mock.patch('requests.Request', wraps=requests.Request) as request_mock: with mock.patch( 'ably.util.exceptions.AblyException.raise_for_response', side_effect=raise_ably_exception) as send_mock: with self.assertRaises(AblyException): ably.http.make_request('GET', '/', skip_auth=True) self.assertEqual(send_mock.call_count, 1) self.assertEqual( request_mock.call_args, mock.call(mock.ANY, default_url, data=mock.ANY, headers=mock.ANY))
def clear_test_vars(): test_vars = RestSetup.__test_vars options = Options.with_key(test_vars["keys"][0]["key_str"]) options.host = test_vars["host"] options.port = test_vars["port"] options.tls_port = test_vars["tls_port"] options.tls = test_vars["tls"] ably = AblyRest(options) headers = HttpUtils.default_get_headers() ably.http.delete('/apps/' + test_vars['app_id'], headers) RestSetup.__test_vars = None
async def test_no_retry_if_not_500_to_599_http_code(self): default_host = Options().get_rest_host() ably = AblyRest(token="foo") default_url = "%s://%s:%d/" % (ably.http.preferred_scheme, default_host, ably.http.preferred_port) def raise_ably_exception(*args, **kwargs): raise AblyException(message="", status_code=600, code=50500) with mock.patch('httpx.Request', wraps=httpx.Request) as request_mock: with mock.patch( 'ably.util.exceptions.AblyException.raise_for_response', side_effect=raise_ably_exception) as send_mock: with pytest.raises(AblyException): await ably.http.make_request('GET', '/', skip_auth=True) assert send_mock.call_count == 1 assert request_mock.call_args == mock.call(mock.ANY, default_url, content=mock.ANY, headers=mock.ANY) await ably.close()
def with_key(cls, key): return cls(Options.with_key(key))