def test_custom_hook_policy_set_hook(): def test_callback(response): raise ValueError() custom_hook_policy = CustomHookPolicy() request = HttpRequest('GET', 'http://127.0.0.1/') pipeline_request = PipelineRequest(request, PipelineContext(None, raw_response_hook=test_callback)) custom_hook_policy.on_request(pipeline_request) assert custom_hook_policy._callback is test_callback
def _get_config(**kwargs): """Configuration common to a/sync pipelines""" config = Configuration(**kwargs) config.custom_hook_policy = CustomHookPolicy(**kwargs) config.headers_policy = HeadersPolicy(**kwargs) config.http_logging_policy = HttpLoggingPolicy(**kwargs) config.logging_policy = NetworkTraceLoggingPolicy(**kwargs) config.proxy_policy = ProxyPolicy(**kwargs) config.user_agent_policy = UserAgentPolicy(base_user_agent=USER_AGENT, **kwargs) return config
def test_request_hook_policy_in_request(): def test_callback(response): raise ValueError() url = "https://bing.com" custom_hook_policy = CustomHookPolicy() policies = [UserAgentPolicy("myuseragent"), custom_hook_policy] client = PipelineClient(base_url=url, policies=policies) request = client.get(url) with pytest.raises(ValueError): client._pipeline.run(request, raw_request_hook=test_callback)
def test_request_hook_policy_in_request(http_request): def test_callback(response): raise ValueError() transport = mock.MagicMock(spec=HttpTransport) url = "http://localhost" custom_hook_policy = CustomHookPolicy() policies = [UserAgentPolicy("myuseragent"), custom_hook_policy] client = PipelineClient(base_url=url, policies=policies, transport=transport) request = http_request("GET", url) with pytest.raises(ValueError): client._pipeline.run(request, raw_request_hook=test_callback)
def test_request_policies_raw_request_hook(port): # test that the request all the way through the pipeline is a new request request = HttpRequest("GET", "/headers") def callback(request): assert is_rest(request.http_request) raise ValueError("I entered the callback!") custom_hook_policy = CustomHookPolicy(raw_request_hook=callback) policies = [UserAgentPolicy("myuseragent"), custom_hook_policy] client = TestRestClient(port=port, policies=policies) with pytest.raises(ValueError) as ex: client.send_request(request) assert "I entered the callback!" in str(ex.value)
def test_custom_hook_policy_invoke_hook(): def test_callback(response): raise ValueError() custom_hook_policy = CustomHookPolicy() request = HttpRequest('GET', 'http://127.0.0.1/') pipeline_request = PipelineRequest(request, PipelineContext(None, raw_response_hook=test_callback)) custom_hook_policy.on_request(pipeline_request) with pytest.raises(ValueError): custom_hook_policy.on_response(pipeline_request, None)
def _configure_policies(self, **kwargs): return [ RequestIdPolicy(**kwargs), StorageHeadersPolicy(**kwargs), UserAgentPolicy(sdk_moniker=SDK_MONIKER, **kwargs), ProxyPolicy(**kwargs), self._credential_policy, ContentDecodePolicy(response_encoding="utf-8"), RedirectPolicy(**kwargs), StorageHosts(**kwargs), TablesRetryPolicy(**kwargs), CustomHookPolicy(**kwargs), NetworkTraceLoggingPolicy(**kwargs), DistributedTracingPolicy(**kwargs), HttpLoggingPolicy(**kwargs), ]
def _policies(credential, **kwargs): # type: (Union[AzureKeyCredential, EventGridSharedAccessSignatureCredential], Any) -> List[Any] auth_policy = _get_authentication_policy(credential) sdk_moniker = 'eventgrid/{}'.format(VERSION) policies = [ RequestIdPolicy(**kwargs), HeadersPolicy(**kwargs), UserAgentPolicy(sdk_moniker=sdk_moniker, **kwargs), ProxyPolicy(**kwargs), ContentDecodePolicy(**kwargs), RedirectPolicy(**kwargs), RetryPolicy(**kwargs), auth_policy, CustomHookPolicy(**kwargs), NetworkTraceLoggingPolicy(**kwargs), DistributedTracingPolicy(**kwargs), CloudEventDistributedTracingPolicy(), HttpLoggingPolicy(**kwargs) ] return policies
def _policies(credential: Union[AzureKeyCredential, AzureSasCredential], **kwargs: Any) -> List[Any]: auth_policy = _get_authentication_policy(credential) sdk_moniker = 'eventgridpublisherclient/{}'.format(VERSION) policies = [ RequestIdPolicy(**kwargs), HeadersPolicy(**kwargs), UserAgentPolicy(sdk_moniker=sdk_moniker, **kwargs), ProxyPolicy(**kwargs), ContentDecodePolicy(**kwargs), AsyncRedirectPolicy(**kwargs), AsyncRetryPolicy(**kwargs), auth_policy, CustomHookPolicy(**kwargs), NetworkTraceLoggingPolicy(**kwargs), DistributedTracingPolicy(**kwargs), CloudEventDistributedTracingPolicy(), HttpLoggingPolicy(**kwargs) ] return policies
def test_example_raw_response_hook(): def callback(response): response.http_response.status_code = 200 response.http_response.headers["custom_header"] = "CustomHeader" from azure.core.pipeline import Pipeline from azure.core.pipeline.policies import RedirectPolicy, UserAgentPolicy from azure.core.pipeline.transport import RequestsTransport, HttpRequest from azure.core.pipeline.policies import CustomHookPolicy request = HttpRequest("GET", "https://bing.com") policies = [ CustomHookPolicy(raw_response_hook=callback) ] with Pipeline(transport=RequestsTransport(), policies=policies) as pipeline: response = pipeline.run(request) assert response.http_response.status_code == 200 assert response.http_response.headers["custom_header"] == "CustomHeader"
def _configure( self, **kwargs ): from azure.core.pipeline.policies import UserAgentPolicy, HeadersPolicy, ProxyPolicy, \ RetryPolicy, CustomHookPolicy, RedirectPolicy, SansIOHTTPPolicy from azure.cli.core.sdk.policies import SafeNetworkTraceLoggingPolicy from ._http_policy import AAZBearerTokenCredentialPolicy self.user_agent_policy = kwargs.get('user_agent_policy') or UserAgentPolicy(**kwargs) self.headers_policy = kwargs.get('headers_policy') or HeadersPolicy(**kwargs) self.proxy_policy = kwargs.get('proxy_policy') or ProxyPolicy(**kwargs) self.logging_policy = kwargs.get('logging_policy') or SafeNetworkTraceLoggingPolicy(**kwargs) self.http_logging_policy = kwargs.get('http_logging_policy') or SansIOHTTPPolicy() self.retry_policy = kwargs.get('retry_policy') or RetryPolicy(**kwargs) self.custom_hook_policy = kwargs.get('custom_hook_policy') or CustomHookPolicy(**kwargs) self.redirect_policy = kwargs.get('redirect_policy') or RedirectPolicy(**kwargs) self.authentication_policy = kwargs.get('authentication_policy') if self.credential and not self.authentication_policy: self.authentication_policy = AAZBearerTokenCredentialPolicy( self.credential, *self.credential_scopes, **kwargs)