Skip to content

Commit e06b8aa

Browse files
authored
[rest] add decompression tests and fix multiple body() calls (Azure#21630)
1 parent 71b42a1 commit e06b8aa

File tree

4 files changed

+119
-37
lines changed

4 files changed

+119
-37
lines changed

sdk/core/azure-core/azure/core/rest/_requests_basic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def _body(self):
6868
# be able to access the body directly without loading it first (like we have to do
6969
# in aiohttp). So here, we set self._content to self._internal_response.content,
7070
# which is similar to read, without the async call.
71-
self._content = self._internal_response.content
71+
if self._content is None:
72+
self._content = self._internal_response.content
7273
return self._content
7374

7475
class _RestRequestsTransportResponseBase(_HttpResponseBaseImpl, _RestRequestsTransportResponseBaseMixin):

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

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@
1111
import pytest
1212
from utils import readonly_checks
1313

14+
@pytest.fixture
15+
async def client(port):
16+
async with AsyncioRequestsTransport() as transport:
17+
async with AsyncTestRestClient(port, transport=transport) as client:
18+
yield client
19+
1420
@pytest.mark.asyncio
15-
async def test_async_gen_data(port):
21+
async def test_async_gen_data(port, client):
1622
class AsyncGen:
1723
def __init__(self):
1824
self._range = iter([b"azerty"])
@@ -26,29 +32,52 @@ async def __anext__(self):
2632
except StopIteration:
2733
raise StopAsyncIteration
2834

29-
async with AsyncioRequestsTransport() as transport:
30-
client = AsyncTestRestClient(port, transport=transport)
31-
request = HttpRequest('GET', 'http://localhost:{}/basic/anything'.format(port), content=AsyncGen())
32-
response = await client.send_request(request)
33-
assert response.json()['data'] == "azerty"
35+
request = HttpRequest('GET', 'http://localhost:{}/basic/anything'.format(port), content=AsyncGen())
36+
response = await client.send_request(request)
37+
assert response.json()['data'] == "azerty"
3438

3539
@pytest.mark.asyncio
36-
async def test_send_data(port):
37-
async with AsyncioRequestsTransport() as transport:
38-
client = AsyncTestRestClient(port, transport=transport)
39-
request = HttpRequest('PUT', 'http://localhost:{}/basic/anything'.format(port), content=b"azerty")
40-
response = await client.send_request(request)
40+
async def test_send_data(port, client):
41+
request = HttpRequest('PUT', 'http://localhost:{}/basic/anything'.format(port), content=b"azerty")
42+
response = await client.send_request(request)
4143

42-
assert response.json()['data'] == "azerty"
44+
assert response.json()['data'] == "azerty"
4345

4446
@pytest.mark.asyncio
45-
async def test_readonly(port):
47+
async def test_readonly(client):
4648
"""Make sure everything that is readonly is readonly"""
47-
async with AsyncioRequestsTransport() as transport:
48-
client = AsyncTestRestClient(port, transport=transport)
49-
response = await client.send_request(HttpRequest("GET", "/health"))
50-
response.raise_for_status()
49+
response = await client.send_request(HttpRequest("GET", "/health"))
50+
response.raise_for_status()
5151

5252
assert isinstance(response, RestAsyncioRequestsTransportResponse)
5353
from azure.core.pipeline.transport import AsyncioRequestsTransportResponse
5454
readonly_checks(response, old_response_class=AsyncioRequestsTransportResponse)
55+
56+
@pytest.mark.asyncio
57+
async def test_decompress_compressed_header(client):
58+
# expect plain text
59+
request = HttpRequest("GET", "/encoding/gzip")
60+
response = await client.send_request(request)
61+
content = await response.read()
62+
assert content == b"hello world"
63+
assert response.content == content
64+
assert response.text() == "hello world"
65+
66+
@pytest.mark.asyncio
67+
async def test_decompress_compressed_header_stream(client):
68+
# expect plain text
69+
request = HttpRequest("GET", "/encoding/gzip")
70+
response = await client.send_request(request, stream=True)
71+
content = await response.read()
72+
assert content == b"hello world"
73+
assert response.content == content
74+
assert response.text() == "hello world"
75+
76+
@pytest.mark.asyncio
77+
async def test_decompress_compressed_header_stream_body_content(client):
78+
# expect plain text
79+
request = HttpRequest("GET", "/encoding/gzip")
80+
response = await client.send_request(request, stream=True)
81+
await response.read()
82+
content = response.content
83+
assert content == response.body()

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

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
from utils import readonly_checks
1111
import pytest
1212

13+
@pytest.fixture
14+
async def client(port):
15+
async with TrioRequestsTransport() as transport:
16+
async with AsyncTestRestClient(port, transport=transport) as client:
17+
yield client
1318

1419
@pytest.mark.trio
15-
async def test_async_gen_data(port):
20+
async def test_async_gen_data(client, port):
1621
class AsyncGen:
1722
def __init__(self):
1823
self._range = iter([b"azerty"])
@@ -26,30 +31,51 @@ async def __anext__(self):
2631
except StopIteration:
2732
raise StopAsyncIteration
2833

29-
async with TrioRequestsTransport() as transport:
30-
client = AsyncTestRestClient(port, transport=transport)
31-
request = HttpRequest('GET', 'http://localhost:{}/basic/anything'.format(port), content=AsyncGen())
32-
response = await client.send_request(request)
33-
assert response.json()['data'] == "azerty"
34+
request = HttpRequest('GET', 'http://localhost:{}/basic/anything'.format(port), content=AsyncGen())
35+
response = await client.send_request(request)
36+
assert response.json()['data'] == "azerty"
3437

3538
@pytest.mark.trio
36-
async def test_send_data(port):
37-
async with TrioRequestsTransport() as transport:
38-
request = HttpRequest('PUT', 'http://localhost:{}/basic/anything'.format(port), content=b"azerty")
39-
client = AsyncTestRestClient(port, transport=transport)
40-
response = await client.send_request(request)
41-
42-
assert response.json()['data'] == "azerty"
39+
async def test_send_data(port, client):
40+
request = HttpRequest('PUT', 'http://localhost:{}/basic/anything'.format(port), content=b"azerty")
41+
response = await client.send_request(request)
42+
assert response.json()['data'] == "azerty"
4343

4444
@pytest.mark.trio
45-
async def test_readonly(port):
45+
async def test_readonly(client):
4646
"""Make sure everything that is readonly is readonly"""
47-
async with TrioRequestsTransport() as transport:
48-
request = HttpRequest('GET', 'http://localhost:{}/health'.format(port))
49-
client = AsyncTestRestClient(port, transport=transport)
50-
response = await client.send_request(HttpRequest("GET", "/health"))
51-
response.raise_for_status()
47+
response = await client.send_request(HttpRequest("GET", "/health"))
48+
response.raise_for_status()
5249

5350
assert isinstance(response, RestTrioRequestsTransportResponse)
5451
from azure.core.pipeline.transport import TrioRequestsTransportResponse
5552
readonly_checks(response, old_response_class=TrioRequestsTransportResponse)
53+
54+
@pytest.mark.trio
55+
async def test_decompress_compressed_header(client):
56+
# expect plain text
57+
request = HttpRequest("GET", "/encoding/gzip")
58+
response = await client.send_request(request)
59+
content = await response.read()
60+
assert content == b"hello world"
61+
assert response.content == content
62+
assert response.text() == "hello world"
63+
64+
@pytest.mark.trio
65+
async def test_decompress_compressed_header_stream(client):
66+
# expect plain text
67+
request = HttpRequest("GET", "/encoding/gzip")
68+
response = await client.send_request(request, stream=True)
69+
content = await response.read()
70+
assert content == b"hello world"
71+
assert response.content == content
72+
assert response.text() == "hello world"
73+
74+
@pytest.mark.trio
75+
async def test_decompress_compressed_header_stream_body_content(client):
76+
# expect plain text
77+
request = HttpRequest("GET", "/encoding/gzip")
78+
response = await client.send_request(request, stream=True)
79+
await response.read()
80+
content = response.content
81+
assert content == response.body()

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,29 @@ def test_pass_kwarg_to_iter_raw(client):
241241
response = client.send_request(request, stream=True)
242242
for part in response.iter_raw(chunk_size=5):
243243
assert part
244+
245+
def test_decompress_compressed_header(client):
246+
# expect plain text
247+
request = HttpRequest("GET", "/encoding/gzip")
248+
response = client.send_request(request)
249+
content = response.read()
250+
assert content == b"hello world"
251+
assert response.content == content
252+
assert response.text() == "hello world"
253+
254+
def test_decompress_compressed_header_stream(client):
255+
# expect plain text
256+
request = HttpRequest("GET", "/encoding/gzip")
257+
response = client.send_request(request, stream=True)
258+
content = response.read()
259+
assert content == b"hello world"
260+
assert response.content == content
261+
assert response.text() == "hello world"
262+
263+
def test_decompress_compressed_header_stream_body_content(client):
264+
# expect plain text
265+
request = HttpRequest("GET", "/encoding/gzip")
266+
response = client.send_request(request, stream=True)
267+
response.read()
268+
content = response.content
269+
assert content == response.body()

0 commit comments

Comments
 (0)