コード例 #1
0
def test_response_stream_download(old_request, new_request):
    transport = RequestsTransport()
    pipeline = Pipeline(transport)

    old_response = transport.send(old_request, stream=True)
    old_string = b"".join(old_response.stream_download(pipeline=pipeline))

    new_response = transport.send(new_request, stream=True)
    new_string = b"".join(new_response.stream_download(pipeline))
    assert old_string == new_string == b"Hello, world!"
コード例 #2
0
    def _create_pipeline(self, credential, **kwargs):
        # type: (Any, **Any) -> Tuple[Configuration, Pipeline]
        self._credential_policy = None
        if hasattr(credential, "get_token"):
            self._credential_policy = BearerTokenCredentialPolicy(
                credential, STORAGE_OAUTH_SCOPE)
        elif isinstance(credential, SharedKeyCredentialPolicy):
            self._credential_policy = credential
        elif credential is not None:
            raise TypeError("Unsupported credential: {}".format(credential))

        config = kwargs.get("_configuration") or create_configuration(**kwargs)
        if kwargs.get("_pipeline"):
            return config, kwargs["_pipeline"]
        config.transport = kwargs.get("transport")  # type: ignore
        kwargs.setdefault("connection_timeout", CONNECTION_TIMEOUT)
        kwargs.setdefault("read_timeout", READ_TIMEOUT)
        if not config.transport:
            config.transport = RequestsTransport(**kwargs)
        policies = [
            QueueMessagePolicy(), config.headers_policy, config.proxy_policy,
            config.user_agent_policy,
            StorageContentValidation(),
            StorageRequestHook(**kwargs), self._credential_policy,
            ContentDecodePolicy(response_encoding="utf-8"),
            RedirectPolicy(**kwargs),
            StorageHosts(hosts=self._hosts,
                         **kwargs), config.retry_policy, config.logging_policy,
            StorageResponseHook(**kwargs),
            DistributedTracingPolicy(**kwargs),
            HttpLoggingPolicy(**kwargs)
        ]
        if kwargs.get("_additional_pipeline_policies"):
            policies = policies + kwargs.get("_additional_pipeline_policies")
        return config, Pipeline(config.transport, policies=policies)
    def _create_appconfig_pipeline(
        self, credential, base_url=None, aad_mode=False, **kwargs
    ):
        transport = kwargs.get("transport")
        policies = kwargs.get("policies")

        if policies is None:  # [] is a valid policy list
            if aad_mode:
                scope = base_url.strip("/") + "/.default"
                if hasattr(credential, "get_token"):
                    credential_policy = BearerTokenCredentialPolicy(credential, scope)
                else:
                    raise TypeError(
                        "Please provide an instance from azure-identity "
                        "or a class that implement the 'get_token protocol"
                    )
            else:
                credential_policy = AppConfigRequestsCredentialsPolicy(credential)
            policies = [
                self._config.headers_policy,
                self._config.user_agent_policy,
                self._config.retry_policy,
                self._sync_token_policy,
                credential_policy,
                self._config.logging_policy,  # HTTP request/response log
                DistributedTracingPolicy(**kwargs),
                HttpLoggingPolicy(**kwargs),
                ContentDecodePolicy(**kwargs),
            ]

        if not transport:
            transport = RequestsTransport(**kwargs)

        return Pipeline(transport, policies)
コード例 #4
0
def test_smoke(port):
    request = HttpRequest(method="GET",
                          url="http://localhost:{}/basic/string".format(port))
    with RequestsTransport() as sender:
        response = sender.send(request)
        response.raise_for_status()
        assert response.text() == "Hello, world!"
コード例 #5
0
 def _build_pipeline(self, **kwargs):  # pylint: disable=no-self-use
     transport = kwargs.get("transport")
     policies = kwargs.get("policies")
     credential_policy = (
         ServiceBusSharedKeyCredentialPolicy(
             self._endpoint, self._credential, "Authorization"
         )
         if isinstance(self._credential, ServiceBusSharedKeyCredential)
         else BearerTokenCredentialPolicy(self._credential, JWT_TOKEN_SCOPE)
     )
     if policies is None:  # [] is a valid policy list
         policies = [
             RequestIdPolicy(**kwargs),
             self._config.headers_policy,
             self._config.user_agent_policy,
             self._config.proxy_policy,
             ContentDecodePolicy(**kwargs),
             ServiceBusXMLWorkaroundPolicy(),
             self._config.redirect_policy,
             self._config.retry_policy,
             credential_policy,
             self._config.logging_policy,
             DistributedTracingPolicy(**kwargs),
             HttpLoggingPolicy(**kwargs),
         ]
     if not transport:
         transport = RequestsTransport(**kwargs)
     return Pipeline(transport, policies)
コード例 #6
0
def test_requests_auto_headers(port, http_request):
    request = http_request("POST",
                           "http://localhost:{}/basic/string".format(port))
    with RequestsTransport() as sender:
        response = sender.send(request)
        auto_headers = response.internal_response.request.headers
        assert 'Content-Type' not in auto_headers
コード例 #7
0
def test_browser_credential():
    transport = Mock(wraps=RequestsTransport())
    credential = InteractiveBrowserCredential(transport=transport)
    scope = "https://management.azure.com/.default"  # N.B. this is valid only in Public Cloud

    record = credential.authenticate(scopes=(scope, ))
    assert record.authority
    assert record.home_account_id
    assert record.tenant_id
    assert record.username

    # credential should have a cached access token for the scope used in authenticate
    with patch(
            WEBBROWSER_OPEN,
            Mock(side_effect=Exception(
                "credential should authenticate silently"))):
        token = credential.get_token(scope)
    assert token.token

    credential = InteractiveBrowserCredential(transport=transport)
    token = credential.get_token(scope)
    assert token.token

    with patch(
            WEBBROWSER_OPEN,
            Mock(side_effect=Exception(
                "credential should authenticate silently"))):
        second_token = credential.get_token(scope)
    assert second_token.token == token.token

    # every request should have the correct User-Agent
    for call in transport.send.call_args_list:
        args, _ = call
        request = args[0]
        assert request.headers["User-Agent"] == USER_AGENT
コード例 #8
0
ファイル: client_base.py プロジェクト: HMatthewsQA/key-app
def _build_pipeline(config, transport=None, **kwargs):
    # type: (Configuration, HttpTransport, **Any) -> Pipeline
    policies = _get_policies(config)
    if transport is None:
        transport = RequestsTransport(**kwargs)

    return Pipeline(transport, policies=policies)
コード例 #9
0
    def test_get_form_recognizer_client_v2(self, formrecognizer_test_endpoint,
                                           formrecognizer_test_api_key,
                                           **kwargs):
        # this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers
        set_custom_default_matcher(
            compare_bodies=False,
            excluded_headers=
            "Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id"
        )
        transport = RequestsTransport()
        ftc = FormTrainingClient(
            endpoint=formrecognizer_test_endpoint,
            credential=AzureKeyCredential(formrecognizer_test_api_key),
            transport=transport,
            api_version="2.1")

        with ftc:
            ftc.get_account_properties()
            assert transport.session is not None
            with ftc.get_form_recognizer_client() as frc:
                assert transport.session is not None
                frc.begin_recognize_receipts_from_url(
                    self.receipt_url_jpg).wait()
                assert frc._api_version == FormRecognizerApiVersion.V2_1
            ftc.get_account_properties()
            assert transport.session is not None
コード例 #10
0
 def __init__(self, vault_url, credentials, config=None, transport=None):
     # type: (str, Any, Configuration, HttpTransport) -> None
     self.vault_url = vault_url.strip("/")
     config = config or KeyClient.create_config()
     transport = RequestsTransport(config)
     policies = [
         config.user_agent_policy,
         config.headers_policy,
         BearerTokenCredentialPolicy(credentials),
         config.redirect_policy,
         config.retry_policy,
         config.logging_policy,
     ]
     self._pipeline = Pipeline(transport, policies=policies)
     models = {
         "DeletedKey": DeletedKey,
         "DeletedKeyItem": DeletedKeyItem,
         "DeletedKeyItemPaged": DeletedKeyItemPaged,
         "JsonWebKey": JsonWebKey,
         "Key": Key,
         "KeyAttributes": KeyAttributes,
         "KeyCreateParameters": KeyCreateParameters,
         "KeyItem": KeyItem,
         "KeyItemPaged": KeyItemPaged,
     }
     self._deserialize = Deserializer(models)
     self._serialize = Serializer(models)
コード例 #11
0
def test_conflict_timeout(caplog):
    transport = RequestsTransport()

    request = HttpRequest("GET", "https://www.bing.com")

    with pytest.raises(ValueError):
        with Pipeline(transport) as pipeline:
            pipeline.run(request, connection_timeout=(100, 100), read_timeout = 100)
コード例 #12
0
def test_basic_requests_separate_session(port, http_request):

    session = requests.Session()
    request = http_request("GET",
                           "http://localhost:{}/basic/string".format(port))
    policies = [UserAgentPolicy("myusergant"), RedirectPolicy()]
    transport = RequestsTransport(session=session, session_owner=False)
    with Pipeline(transport, policies=policies) as pipeline:
        response = pipeline.run(request)
        if is_rest(request):
            assert is_rest(response.http_response)

    assert transport.session
    assert isinstance(response.http_response.status_code, int)
    transport.close()
    assert transport.session
    transport.session.close()
コード例 #13
0
def test_response_streaming_error_behavior(http_response):
    # Test to reproduce https://github.com/Azure/azure-sdk-for-python/issues/16723
    block_size = 103
    total_response_size = 500
    req_response = requests.Response()
    req_request = requests.Request()

    class FakeStreamWithConnectionError:
        # fake object for urllib3.response.HTTPResponse
        def __init__(self):
            self.total_response_size = 500

        def stream(self, chunk_size, decode_content=False):
            assert chunk_size == block_size
            left = total_response_size
            while left > 0:
                if left <= block_size:
                    raise requests.exceptions.ConnectionError()
                data = b"X" * min(chunk_size, left)
                left -= len(data)
                yield data

        def read(self, chunk_size, decode_content=False):
            assert chunk_size == block_size
            if self.total_response_size > 0:
                if self.total_response_size <= block_size:
                    raise requests.exceptions.ConnectionError()
                data = b"X" * min(chunk_size, self.total_response_size)
                self.total_response_size -= len(data)
                return data

        def close(self):
            pass

    s = FakeStreamWithConnectionError()
    req_response.raw = FakeStreamWithConnectionError()

    response = create_transport_response(
        http_response,
        req_request,
        req_response,
        block_size,
    )

    def mock_run(self, *args, **kwargs):
        return PipelineResponse(
            None,
            requests.Response(),
            None,
        )

    transport = RequestsTransport()
    pipeline = Pipeline(transport)
    pipeline.run = mock_run
    downloader = response.stream_download(pipeline, decompress=False)
    with pytest.raises(requests.exceptions.ConnectionError):
        full_response = b"".join(downloader)
コード例 #14
0
def build_pipeline(transport=None, policies=None, **kwargs):
    if not policies:
        config = _get_config(**kwargs)
        config.retry_policy = RetryPolicy(**kwargs)
        policies = _get_policies(config, **kwargs)
    if not transport:
        transport = RequestsTransport(**kwargs)

    return Pipeline(transport, policies=policies)
コード例 #15
0
def _create_pipeline(account, credential, **kwargs):
    # type: (Any, **Any) -> Tuple[Configuration, Pipeline]
    credential_policy = SharedKeyCredentialPolicy(account_name=account.name, account_key=credential)
    transport = RequestsTransport(**kwargs)
    policies = [
        HeadersPolicy(),
        credential_policy,
        ContentDecodePolicy(response_encoding="utf-8")]
    return Pipeline(transport, policies=policies)
コード例 #16
0
    def test_basic_options_requests(self):

        request = HttpRequest("OPTIONS", "https://httpbin.org")
        policies = [UserAgentPolicy("myusergant"), RedirectPolicy()]
        with Pipeline(RequestsTransport(), policies=policies) as pipeline:
            response = pipeline.run(request)

        assert pipeline._transport.session is None
        assert isinstance(response.http_response.status_code, int)
コード例 #17
0
 def test_requests_socket_timeout(self):
     conf = Configuration()
     request = HttpRequest("GET", "https://bing.com")
     policies = [UserAgentPolicy("myusergant"), RedirectPolicy()]
     # Sometimes this will raise a read timeout, sometimes a socket timeout depending on timing.
     # Either way, the error should always be wrapped as an AzureError to ensure it's caught
     # by the retry policy.
     with pytest.raises(AzureError):
         with Pipeline(RequestsTransport(), policies=policies) as pipeline:
             response = pipeline.run(request, connection_timeout=0.000001)
コード例 #18
0
    def _build_pipeline(self, config, policies=None, transport=None, **kwargs):  # pylint:disable=no-self-use
        # type: (Configuration, Optional[List[PolicyType]], Optional[HttpTransport], **Any) -> Pipeline
        if policies is None:  # [] is a valid policy list
            policies = _get_policies(config, **kwargs)
        if not transport:
            from azure.core.pipeline.transport import RequestsTransport

            transport = RequestsTransport(**kwargs)

        return Pipeline(transport=transport, policies=policies)
コード例 #19
0
def test_tuple_timeout(caplog):
    transport = RequestsTransport()

    request = HttpRequest("GET", "https://www.bing.com")

    with caplog.at_level(logging.WARNING, logger="azure.core.pipeline.transport"):
        with Pipeline(transport) as pipeline:
            pipeline.run(request, connection_timeout=(100, 100))

    assert "Tuple timeout setting is deprecated" in caplog.text
コード例 #20
0
def test_conflict_timeout(caplog, port):
    transport = RequestsTransport()

    request = HttpRequest("GET",
                          "http://localhost:{}/basic/string".format(port))

    with pytest.raises(ValueError):
        with Pipeline(transport) as pipeline:
            pipeline.run(request,
                         connection_timeout=(100, 100),
                         read_timeout=100)
コード例 #21
0
 def test_transport_closed_only_once(self, resource_group, location, storage_account, storage_account_key):
     transport = RequestsTransport()
     prefix = TEST_QUEUE_PREFIX
     queue_name = self.get_resource_name(prefix)
     with QueueServiceClient(self.account_url(storage_account, "queue"), credential=storage_account_key, transport=transport) as qsc:
         qsc.get_service_properties()
         assert transport.session is not None
         with qsc.get_queue_client(queue_name) as qc:
             assert transport.session is not None
         qsc.get_service_properties()
         assert transport.session is not None
コード例 #22
0
def test_compress_plain_no_header_offline(port, http_request):
    # thanks to Daisy Cisneros for this test!
    # expect plain text
    request = http_request(method="GET", url="http://localhost:{}/streams/string".format(port))
    with RequestsTransport() as sender:
        response = sender.send(request, stream=True)
        response.raise_for_status()
        data = response.stream_download(sender, decompress=False)
        content = b"".join(list(data))
        decoded = content.decode('utf-8')
        assert decoded == "test"
コード例 #23
0
 def _build_pipeline(self, config=None, policies=None, transport=None, **kwargs):
     config = config or self._create_config(**kwargs)
     policies = policies or [
         ContentDecodePolicy(),
         config.retry_policy,
         config.logging_policy,
         DistributedTracingPolicy(),
     ]
     if not transport:
         transport = RequestsTransport(**kwargs)
     return Pipeline(transport=transport, policies=policies)