Exemplo n.º 1
0
async def test_compress_plain_no_header():
    # expect plain text
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test.txt".format(account_name)
    client = AsyncPipelineClient(account_url)
    request = client.get(url)
    pipeline_response = await client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=False)
    content = b""
    async for d in data:
        content += d
    decoded = content.decode('utf-8')
    assert decoded == "test"
class PipelineClientGetTest(PerfStressTest):
    pipeline_client: PipelineClient = None
    async_pipeline_client: AsyncPipelineClient = None
    _first_run = True

    def __init__(self, arguments):
        super().__init__(arguments)
        self.pipeline_client = PipelineClient(base_url=self.args.url, **self._client_kwargs)
        self.async_pipeline_client = AsyncPipelineClient(base_url=self.args.url, **self._client_kwargs)

    def run_sync(self):
        if self._first_run:
            for _ in range(self.args.first_run_extra_requests):
                self._send_request_sync()
            self._first_run = False
        self._send_request_sync()

    async def run_async(self):
        if self._first_run:
            for _ in range(self.args.first_run_extra_requests):
                await self._send_request_async()
            self._first_run = False
        await self._send_request_async()

    def _send_request_sync(self):
        request = self.pipeline_client.get(self.args.url)
        response: PipelineResponse = self.pipeline_client._pipeline.run(request)
        # Consume response body
        http_response: HttpResponse = response.http_response
        data = http_response.body()

    async def _send_request_async(self):
        request = self.async_pipeline_client.get(self.args.url)
        response: PipelineResponse = await self.async_pipeline_client._pipeline.run(request)
        # Consume response body
        http_response: HttpResponse = response.http_response
        data = http_response.body()

    async def close(self):
        await self.async_pipeline_client.close()
        await super().close()

    @staticmethod
    def add_arguments(parser):
        parser.add_argument("--first-run-extra-requests", type=int, default=0, help='Extra requests to send on first run. ' +
            'Simulates SDKs which require extra requests (like authentication) on first API call.')
        parser.add_argument("-u", "--url", required=True)
Exemplo n.º 3
0
async def test_decompress_compressed_no_header():
    # expect compressed text
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test.tar.gz".format(account_name)
    client = AsyncPipelineClient(account_url)
    request = client.get(url)
    pipeline_response = await client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=True)
    content = b""
    async for d in data:
        content += d
    try:
        decoded = content.decode('utf-8')
        assert False
    except UnicodeDecodeError:
        pass
Exemplo n.º 4
0
async def test_decompress_plain_header():
    # expect error
    import zlib
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test_with_header.txt".format(account_name)
    client = AsyncPipelineClient(account_url)
    request = client.get(url)
    pipeline_response = await client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=True)
    try:
        content = b""
        async for d in data:
            content += d
        assert False
    except zlib.error:
        pass
Exemplo n.º 5
0
async def test_cloud_shell_live(cloud_shell):
    credential = ManagedIdentityCredential()
    token = credential.get_token("https://vault.azure.net")

    # Validate the token by sending a request to the Key Vault. The request is manual because azure-keyvault-secrets
    # can't authenticate in Cloud Shell; the MSI endpoint there doesn't support AADv2 scopes.
    policies = [
        ContentDecodePolicy(),
        AsyncRedirectPolicy(),
        AsyncRetryPolicy(),
        HttpLoggingPolicy()
    ]
    client = AsyncPipelineClient(cloud_shell["vault_url"], policies=policies)
    list_secrets = client.get(
        "secrets",
        headers={"Authorization": "Bearer " + token.token},
        params={"api-version": "7.0"})
    async with client:
        await client._pipeline.run(list_secrets)