Skip to content

Commit f24bb00

Browse files
authored
[Core][Corehttp] Add streaming request tests (#34725)
We already have some tests that test creating HttpRequests with iterable/generator content, but none actually send the requests through our transports. This adds some tests that will do so. Signed-off-by: Paul Van Eck <[email protected]>
1 parent 04e6769 commit f24bb00

File tree

6 files changed

+152
-2
lines changed

6 files changed

+152
-2
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,35 @@ async def test_decompress_compressed_header_offline(port, http_request):
315315
data = response.stream_download(client._pipeline, decompress=True)
316316
decoded = b"".join([d async for d in data]).decode("utf-8")
317317
assert decoded == "test"
318+
319+
320+
@pytest.mark.asyncio
321+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
322+
async def test_streaming_request_iterable(port, http_request):
323+
url = "http://localhost:{}/streams/upload".format(port)
324+
325+
class Content:
326+
async def __aiter__(self):
327+
yield b"test 123"
328+
329+
client = AsyncPipelineClient("")
330+
request = http_request(method="POST", url=url, data=Content())
331+
response = await client.send_request(request)
332+
response.raise_for_status()
333+
assert response.text() == "test 123"
334+
335+
336+
@pytest.mark.asyncio
337+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
338+
async def test_streaming_request_generator(port, http_request):
339+
url = "http://localhost:{}/streams/upload".format(port)
340+
341+
async def content():
342+
yield b"test 123"
343+
yield b"test 456"
344+
345+
client = AsyncPipelineClient("")
346+
request = http_request(method="POST", url=url, data=content())
347+
response = await client.send_request(request)
348+
response.raise_for_status()
349+
assert response.text() == "test 123test 456"

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,33 @@ def test_compress_compressed_header_offline(port, http_request):
277277
content = b"".join(list(data))
278278
with pytest.raises(UnicodeDecodeError):
279279
content.decode("utf-8")
280+
281+
282+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
283+
def test_streaming_request_iterable(port, http_request):
284+
url = "http://localhost:{}/streams/upload".format(port)
285+
286+
class Content:
287+
def __iter__(self):
288+
yield b"test 123"
289+
290+
client = PipelineClient("")
291+
request = http_request(method="POST", url=url, data=Content())
292+
response = client.send_request(request)
293+
response.raise_for_status()
294+
assert response.text() == "test 123"
295+
296+
297+
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
298+
def test_streaming_request_generator(port, http_request):
299+
url = "http://localhost:{}/streams/upload".format(port)
300+
301+
def content():
302+
yield b"test 123"
303+
yield b"test 456"
304+
305+
client = PipelineClient("")
306+
request = http_request(method="POST", url=url, data=content())
307+
response = client.send_request(request)
308+
response.raise_for_status()
309+
assert response.text() == "test 123test 456"

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from flask import (
1111
Response,
1212
Blueprint,
13+
request,
1314
)
1415

1516
streams_api = Blueprint("streams_api", __name__)
@@ -91,3 +92,15 @@ def compressed_stream():
9192
@streams_api.route("/decompress_header", methods=["GET"])
9293
def decompress_header():
9394
return Response(compressed_stream(), status=200, headers={"Content-Encoding": "gzip"})
95+
96+
97+
@streams_api.route("/upload", methods=["POST"])
98+
def upload():
99+
chunk_size = 1024
100+
byte_content = b""
101+
while True:
102+
chunk = request.stream.read(chunk_size)
103+
if len(chunk) == 0:
104+
break
105+
byte_content += chunk
106+
return Response(byte_content, status=200)

sdk/core/corehttp/tests/async_tests/test_streaming_async.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77
Asynchronous streaming tests.
88
9-
Test naming convention: test_{1}_{2}
9+
Test naming convention for streaming response tests: test_{1}_{2}
1010
1111
1:
1212
compress or decompress. Refers to the stream that is returned from the testserver / streams.py
@@ -294,3 +294,35 @@ async def test_decompress_compressed_header_offline(port, transport):
294294
data = response.iter_bytes()
295295
decoded = b"".join([d async for d in data]).decode("utf-8")
296296
assert decoded == "test"
297+
298+
299+
@pytest.mark.asyncio
300+
@pytest.mark.parametrize("transport", ASYNC_TRANSPORTS)
301+
async def test_streaming_request_iterable(port, transport):
302+
url = "http://localhost:{}/streams/upload".format(port)
303+
304+
class Content:
305+
async def __aiter__(self):
306+
yield b"test 123"
307+
308+
client = AsyncPipelineClient(url, transport=transport())
309+
request = HttpRequest("POST", url=url, content=Content())
310+
response = await client.send_request(request)
311+
response.raise_for_status()
312+
assert response.text() == "test 123"
313+
314+
315+
@pytest.mark.asyncio
316+
@pytest.mark.parametrize("transport", ASYNC_TRANSPORTS)
317+
async def test_streaming_request_generator(port, transport):
318+
url = "http://localhost:{}/streams/upload".format(port)
319+
320+
async def content():
321+
yield b"test 123"
322+
yield b"test 456"
323+
324+
client = AsyncPipelineClient(url, transport=transport())
325+
request = HttpRequest("POST", url=url, content=content())
326+
response = await client.send_request(request)
327+
response.raise_for_status()
328+
assert response.text() == "test 123test 456"

sdk/core/corehttp/tests/test_streaming.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77
Streaming tests.
88
9-
Test naming convention: test_{1}_{2}
9+
Test naming convention for streaming response tests: test_{1}_{2}
1010
1111
1:
1212
compress or decompress. Refers to the stream that is returned from the testserver / streams.py
@@ -259,3 +259,33 @@ def test_compress_compressed_header_offline(port, transport):
259259
content = b"".join(list(data))
260260
with pytest.raises(UnicodeDecodeError):
261261
content.decode("utf-8")
262+
263+
264+
@pytest.mark.parametrize("transport", SYNC_TRANSPORTS)
265+
def test_streaming_request_iterable(port, transport):
266+
url = "http://localhost:{}/streams/upload".format(port)
267+
268+
class Content:
269+
def __iter__(self):
270+
yield b"test 123"
271+
272+
client = PipelineClient(url, transport=transport())
273+
request = HttpRequest("POST", url=url, content=Content())
274+
response = client.send_request(request)
275+
response.raise_for_status()
276+
assert response.text() == "test 123"
277+
278+
279+
@pytest.mark.parametrize("transport", SYNC_TRANSPORTS)
280+
def test_streaming_request_generator(port, transport):
281+
url = "http://localhost:{}/streams/upload".format(port)
282+
283+
def content():
284+
yield b"test 123"
285+
yield b"test 456"
286+
287+
client = PipelineClient(url, transport=transport())
288+
request = HttpRequest("POST", url=url, content=content())
289+
response = client.send_request(request)
290+
response.raise_for_status()
291+
assert response.text() == "test 123test 456"

sdk/core/corehttp/tests/testserver_tests/coretestserver/coretestserver/test_routes/streams.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from flask import (
1010
Response,
1111
Blueprint,
12+
request,
1213
)
1314

1415
streams_api = Blueprint("streams_api", __name__)
@@ -90,3 +91,15 @@ def compressed_stream():
9091
@streams_api.route("/decompress_header", methods=["GET"])
9192
def decompress_header():
9293
return Response(compressed_stream(), status=200, headers={"Content-Encoding": "gzip"})
94+
95+
96+
@streams_api.route("/upload", methods=["POST"])
97+
def upload():
98+
chunk_size = 1024
99+
byte_content = b""
100+
while True:
101+
chunk = request.stream.read(chunk_size)
102+
if len(chunk) == 0:
103+
break
104+
byte_content += chunk
105+
return Response(byte_content, status=200)

0 commit comments

Comments
 (0)