Skip to content

Commit 2a5c571

Browse files
Add offline tests for azure core streaming (#33800)
Thank you @HimanshuBarak for your contribution!
1 parent b6bfa06 commit 2a5c571

File tree

3 files changed

+177
-9
lines changed

3 files changed

+177
-9
lines changed

sdk/core/azure-core/tests/async_tests/test_streaming_async.py

+115-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#
2525
# --------------------------------------------------------------------------
2626
import os
27+
import zlib
2728
import pytest
2829
from azure.core import AsyncPipelineClient
2930
from azure.core.exceptions import DecodeError
@@ -121,7 +122,6 @@ async def test_compress_compressed_no_header(http_request):
121122
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
122123
async def test_decompress_plain_header(http_request):
123124
# expect error
124-
import zlib
125125

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

183183

184+
@pytest.mark.asyncio
185+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
186+
async def test_compress_compressed_no_header_offline(port, http_request):
187+
# expect compressed text
188+
client = AsyncPipelineClient("")
189+
async with client:
190+
request = http_request(method="GET", url="http://localhost:{}/streams/compressed_no_header".format(port))
191+
pipeline_response = await client._pipeline.run(request, stream=True)
192+
response = pipeline_response.http_response
193+
data = response.stream_download(client._pipeline, decompress=False)
194+
with pytest.raises(UnicodeDecodeError):
195+
b"".join([d async for d in data]).decode("utf-8")
196+
197+
184198
@pytest.mark.live_test_only
185199
@pytest.mark.asyncio
186200
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
@@ -195,11 +209,103 @@ async def test_compress_compressed_header(http_request):
195209
pipeline_response = await client._pipeline.run(request, stream=True)
196210
response = pipeline_response.http_response
197211
data = response.stream_download(client._pipeline, decompress=False)
198-
content = b""
199-
async for d in data:
200-
content += d
201-
try:
202-
decoded = content.decode("utf-8")
203-
assert False
204-
except UnicodeDecodeError:
205-
pass
212+
with pytest.raises(UnicodeDecodeError):
213+
b"".join([d async for d in data]).decode("utf-8")
214+
215+
216+
@pytest.mark.asyncio
217+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
218+
async def test_decompress_plain_no_header_offline(port, http_request):
219+
# expect plain text
220+
client = AsyncPipelineClient("")
221+
async with client:
222+
request = http_request(method="GET", url="http://localhost:{}/streams/string".format(port))
223+
pipeline_response = await client._pipeline.run(request, stream=True)
224+
response = pipeline_response.http_response
225+
data = response.stream_download(client._pipeline, decompress=True)
226+
decoded = b"".join([d async for d in data]).decode("utf-8")
227+
assert decoded == "test"
228+
229+
230+
@pytest.mark.asyncio
231+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
232+
async def test_compress_plain_header_offline(port, http_request):
233+
# expect plain text
234+
client = AsyncPipelineClient("")
235+
async with client:
236+
request = http_request(method="GET", url="http://localhost:{}/streams/plain_header".format(port))
237+
pipeline_response = await client._pipeline.run(request, stream=True)
238+
response = pipeline_response.http_response
239+
data = response.stream_download(client._pipeline, decompress=False)
240+
decoded = b"".join([d async for d in data]).decode("utf-8")
241+
assert decoded == "test"
242+
243+
244+
@pytest.mark.asyncio
245+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
246+
async def test_decompress_compressed_no_header_offline(port, http_request):
247+
# expect compressed text
248+
client = AsyncPipelineClient("")
249+
async with client:
250+
request = http_request(method="GET", url="http://localhost:{}/streams/compressed_no_header".format(port))
251+
pipeline_response = await client._pipeline.run(request, stream=True)
252+
response = pipeline_response.http_response
253+
data = response.stream_download(client._pipeline, decompress=True)
254+
255+
with pytest.raises(UnicodeDecodeError):
256+
b"".join([d async for d in data]).decode("utf-8")
257+
258+
259+
@pytest.mark.asyncio
260+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
261+
async def test_compress_compressed_header_offline(port, http_request):
262+
# expect compressed text
263+
client = AsyncPipelineClient("")
264+
async with client:
265+
request = http_request(method="GET", url="http://localhost:{}/streams/compressed_header".format(port))
266+
pipeline_response = await client._pipeline.run(request, stream=True)
267+
response = pipeline_response.http_response
268+
data = response.stream_download(client._pipeline, decompress=False)
269+
with pytest.raises(UnicodeDecodeError):
270+
b"".join([d async for d in data]).decode("utf-8")
271+
272+
273+
@pytest.mark.asyncio
274+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
275+
async def test_decompress_plain_header_offline(port, http_request):
276+
# expect error
277+
client = AsyncPipelineClient("")
278+
async with client:
279+
request = http_request(method="GET", url="http://localhost:{}/streams/compressed".format(port))
280+
pipeline_response = await client._pipeline.run(request, stream=True)
281+
response = pipeline_response.http_response
282+
data = response.stream_download(client._pipeline, decompress=True)
283+
with pytest.raises((zlib.error, DecodeError)):
284+
b"".join([d async for d in data])
285+
286+
287+
@pytest.mark.asyncio
288+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
289+
async def test_compress_plain_no_header_offline(port, http_request):
290+
client = AsyncPipelineClient("")
291+
async with client:
292+
request = http_request(method="GET", url="http://localhost:{}/streams/string".format(port))
293+
pipeline_response = await client._pipeline.run(request, stream=True)
294+
response = pipeline_response.http_response
295+
data = response.stream_download(client._pipeline, decompress=False)
296+
decoded = b"".join([d async for d in data]).decode("utf-8")
297+
assert decoded == "test"
298+
299+
300+
@pytest.mark.asyncio
301+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
302+
async def test_decompress_compressed_header_offline(port, http_request):
303+
# expect compressed text
304+
client = AsyncPipelineClient("")
305+
async with client:
306+
request = http_request(method="GET", url="http://localhost:{}/streams/decompress_header".format(port))
307+
pipeline_response = await client._pipeline.run(request, stream=True)
308+
response = pipeline_response.http_response
309+
data = response.stream_download(client._pipeline, decompress=True)
310+
decoded = b"".join([d async for d in data]).decode("utf-8")
311+
assert decoded == "test"

sdk/core/azure-core/tests/test_streaming.py

+52
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,55 @@ def test_compress_compressed_header(http_request):
221221
assert False
222222
except UnicodeDecodeError:
223223
pass
224+
225+
226+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
227+
def test_decompress_plain_no_header_offline(port, http_request):
228+
# expect plain text
229+
request = http_request(method="GET", url="http://localhost:{}/streams/string".format(port))
230+
with RequestsTransport() as sender:
231+
response = sender.send(request, stream=True)
232+
response.raise_for_status()
233+
data = response.stream_download(sender, decompress=True)
234+
content = b"".join(list(data))
235+
decoded = content.decode("utf-8")
236+
assert decoded == "test"
237+
238+
239+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
240+
def test_compress_plain_header_offline(port, http_request):
241+
# expect plain text
242+
request = http_request(method="GET", url="http://localhost:{}/streams/plain_header".format(port))
243+
with RequestsTransport() as sender:
244+
response = sender.send(request, stream=True)
245+
response.raise_for_status()
246+
data = response.stream_download(sender, decompress=False)
247+
content = b"".join(list(data))
248+
decoded = content.decode("utf-8")
249+
assert decoded == "test"
250+
251+
252+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
253+
def test_decompress_compressed_no_header_offline(port, http_request):
254+
# expect compressed text
255+
client = PipelineClient("")
256+
request = http_request(method="GET", url="http://localhost:{}/streams/compressed_no_header".format(port))
257+
response = client._pipeline.run(request, stream=True).http_response
258+
response.raise_for_status()
259+
data = response.stream_download(client._pipeline, decompress=True)
260+
content = b"".join(list(data))
261+
with pytest.raises(UnicodeDecodeError):
262+
content.decode("utf-8")
263+
264+
265+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
266+
def test_compress_compressed_header_offline(port, http_request):
267+
# expect compressed text
268+
client = PipelineClient("")
269+
request = http_request(method="GET", url="http://localhost:{}/streams/compressed_header".format(port))
270+
response = client._pipeline.run(request, stream=True).http_response
271+
response.raise_for_status()
272+
data = response.stream_download(client._pipeline, decompress=False)
273+
content = b"".join(list(data))
274+
with pytest.raises(UnicodeDecodeError):
275+
content.decode("utf-8")

sdk/core/azure-core/tests/testserver_tests/coretestserver/coretestserver/test_routes/streams.py

+10
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,21 @@ def string():
5959
return Response(streaming_test(), status=200, mimetype="text/plain")
6060

6161

62+
@streams_api.route("/plain_header", methods=["GET"])
63+
def plain_header():
64+
return Response(streaming_test(), status=200, mimetype="text/plain", headers={"Content-Encoding": "gzip"})
65+
66+
6267
@streams_api.route("/compressed_no_header", methods=["GET"])
6368
def compressed_no_header():
6469
return Response(compressed_stream(), status=300)
6570

6671

72+
@streams_api.route("/compressed_header", methods=["GET"])
73+
def compressed_header():
74+
return Response(compressed_stream(), status=200, headers={"Content-Encoding": "gzip"})
75+
76+
6777
@streams_api.route("/compressed", methods=["GET"])
6878
def compressed():
6979
return Response(stream_compressed_header_error(), status=300, headers={"Content-Encoding": "gzip"})

0 commit comments

Comments
 (0)