24
24
#
25
25
# --------------------------------------------------------------------------
26
26
import os
27
+ import zlib
27
28
import pytest
28
29
from azure .core import AsyncPipelineClient
29
30
from azure .core .exceptions import DecodeError
@@ -121,7 +122,6 @@ async def test_compress_compressed_no_header(http_request):
121
122
@pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
122
123
async def test_decompress_plain_header (http_request ):
123
124
# expect error
124
- import zlib
125
125
126
126
account_name = "coretests"
127
127
account_url = "https://{}.blob.core.windows.net" .format (account_name )
@@ -181,6 +181,20 @@ async def test_decompress_compressed_header(http_request):
181
181
assert decoded == "test"
182
182
183
183
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
+
184
198
@pytest .mark .live_test_only
185
199
@pytest .mark .asyncio
186
200
@pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
@@ -195,11 +209,103 @@ async def test_compress_compressed_header(http_request):
195
209
pipeline_response = await client ._pipeline .run (request , stream = True )
196
210
response = pipeline_response .http_response
197
211
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"
0 commit comments