Skip to content

Add offline tests for azure core streaming #33800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 115 additions & 9 deletions sdk/core/azure-core/tests/async_tests/test_streaming_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#
# --------------------------------------------------------------------------
import os
import zlib
import pytest
from azure.core import AsyncPipelineClient
from azure.core.exceptions import DecodeError
Expand Down Expand Up @@ -121,7 +122,6 @@ async def test_compress_compressed_no_header(http_request):
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
async def test_decompress_plain_header(http_request):
# expect error
import zlib

account_name = "coretests"
account_url = "https://{}.blob.core.windows.net".format(account_name)
Expand Down Expand Up @@ -181,6 +181,20 @@ async def test_decompress_compressed_header(http_request):
assert decoded == "test"


@pytest.mark.asyncio
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
async def test_compress_compressed_no_header_offline(port, http_request):
# expect compressed text
client = AsyncPipelineClient("")
async with client:
request = http_request(method="GET", url="http://localhost:{}/streams/compressed_no_header".format(port))
pipeline_response = await client._pipeline.run(request, stream=True)
response = pipeline_response.http_response
data = response.stream_download(client._pipeline, decompress=False)
with pytest.raises(UnicodeDecodeError):
b"".join([d async for d in data]).decode("utf-8")


@pytest.mark.live_test_only
@pytest.mark.asyncio
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
Expand All @@ -195,11 +209,103 @@ async def test_compress_compressed_header(http_request):
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
try:
decoded = content.decode("utf-8")
assert False
except UnicodeDecodeError:
pass
with pytest.raises(UnicodeDecodeError):
b"".join([d async for d in data]).decode("utf-8")


@pytest.mark.asyncio
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
async def test_decompress_plain_no_header_offline(port, http_request):
# expect plain text
client = AsyncPipelineClient("")
async with client:
request = http_request(method="GET", url="http://localhost:{}/streams/string".format(port))
pipeline_response = await client._pipeline.run(request, stream=True)
response = pipeline_response.http_response
data = response.stream_download(client._pipeline, decompress=True)
decoded = b"".join([d async for d in data]).decode("utf-8")
assert decoded == "test"


@pytest.mark.asyncio
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
async def test_compress_plain_header_offline(port, http_request):
# expect plain text
client = AsyncPipelineClient("")
async with client:
request = http_request(method="GET", url="http://localhost:{}/streams/plain_header".format(port))
pipeline_response = await client._pipeline.run(request, stream=True)
response = pipeline_response.http_response
data = response.stream_download(client._pipeline, decompress=False)
decoded = b"".join([d async for d in data]).decode("utf-8")
assert decoded == "test"


@pytest.mark.asyncio
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
async def test_decompress_compressed_no_header_offline(port, http_request):
# expect compressed text
client = AsyncPipelineClient("")
async with client:
request = http_request(method="GET", url="http://localhost:{}/streams/compressed_no_header".format(port))
pipeline_response = await client._pipeline.run(request, stream=True)
response = pipeline_response.http_response
data = response.stream_download(client._pipeline, decompress=True)

with pytest.raises(UnicodeDecodeError):
b"".join([d async for d in data]).decode("utf-8")


@pytest.mark.asyncio
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
async def test_compress_compressed_header_offline(port, http_request):
# expect compressed text
client = AsyncPipelineClient("")
async with client:
request = http_request(method="GET", url="http://localhost:{}/streams/compressed_header".format(port))
pipeline_response = await client._pipeline.run(request, stream=True)
response = pipeline_response.http_response
data = response.stream_download(client._pipeline, decompress=False)
with pytest.raises(UnicodeDecodeError):
b"".join([d async for d in data]).decode("utf-8")


@pytest.mark.asyncio
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
async def test_decompress_plain_header_offline(port, http_request):
# expect error
client = AsyncPipelineClient("")
async with client:
request = http_request(method="GET", url="http://localhost:{}/streams/compressed".format(port))
pipeline_response = await client._pipeline.run(request, stream=True)
response = pipeline_response.http_response
data = response.stream_download(client._pipeline, decompress=True)
with pytest.raises((zlib.error, DecodeError)):
b"".join([d async for d in data])


@pytest.mark.asyncio
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
async def test_compress_plain_no_header_offline(port, http_request):
client = AsyncPipelineClient("")
async with client:
request = http_request(method="GET", url="http://localhost:{}/streams/string".format(port))
pipeline_response = await client._pipeline.run(request, stream=True)
response = pipeline_response.http_response
data = response.stream_download(client._pipeline, decompress=False)
decoded = b"".join([d async for d in data]).decode("utf-8")
assert decoded == "test"


@pytest.mark.asyncio
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
async def test_decompress_compressed_header_offline(port, http_request):
# expect compressed text
client = AsyncPipelineClient("")
async with client:
request = http_request(method="GET", url="http://localhost:{}/streams/decompress_header".format(port))
pipeline_response = await client._pipeline.run(request, stream=True)
response = pipeline_response.http_response
data = response.stream_download(client._pipeline, decompress=True)
decoded = b"".join([d async for d in data]).decode("utf-8")
assert decoded == "test"
52 changes: 52 additions & 0 deletions sdk/core/azure-core/tests/test_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,55 @@ def test_compress_compressed_header(http_request):
assert False
except UnicodeDecodeError:
pass


@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_decompress_plain_no_header_offline(port, http_request):
# 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=True)
content = b"".join(list(data))
decoded = content.decode("utf-8")
assert decoded == "test"


@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_compress_plain_header_offline(port, http_request):
# expect plain text
request = http_request(method="GET", url="http://localhost:{}/streams/plain_header".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"


@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_decompress_compressed_no_header_offline(port, http_request):
# expect compressed text
client = PipelineClient("")
request = http_request(method="GET", url="http://localhost:{}/streams/compressed_no_header".format(port))
response = client._pipeline.run(request, stream=True).http_response
response.raise_for_status()
data = response.stream_download(client._pipeline, decompress=True)
content = b"".join(list(data))
with pytest.raises(UnicodeDecodeError):
content.decode("utf-8")


@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_compress_compressed_header_offline(port, http_request):
# expect compressed text
client = PipelineClient("")
request = http_request(method="GET", url="http://localhost:{}/streams/compressed_header".format(port))
response = client._pipeline.run(request, stream=True).http_response
response.raise_for_status()
data = response.stream_download(client._pipeline, decompress=False)
content = b"".join(list(data))
with pytest.raises(UnicodeDecodeError):
content.decode("utf-8")
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ def string():
return Response(streaming_test(), status=200, mimetype="text/plain")


@streams_api.route("/plain_header", methods=["GET"])
def plain_header():
return Response(streaming_test(), status=200, mimetype="text/plain", headers={"Content-Encoding": "gzip"})


@streams_api.route("/compressed_no_header", methods=["GET"])
def compressed_no_header():
return Response(compressed_stream(), status=300)


@streams_api.route("/compressed_header", methods=["GET"])
def compressed_header():
return Response(compressed_stream(), status=200, headers={"Content-Encoding": "gzip"})


@streams_api.route("/compressed", methods=["GET"])
def compressed():
return Response(stream_compressed_header_error(), status=300, headers={"Content-Encoding": "gzip"})
Expand Down