Skip to content

Commit

Permalink
test: extend test_streaming_requests
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-locatelli committed Oct 7, 2024
1 parent 84651be commit ed4a16c
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions test/integration/base_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import asyncio
import pickle
from contextlib import asynccontextmanager

from test.conftest import (
ALL_METHODS,
CACHE_NAME,
Expand Down Expand Up @@ -174,28 +175,37 @@ async def test_include_headers(self):
async def test_streaming_requests(self):
"""Test that streaming requests work both for the original and cached responses"""
async with self.init_session() as session:
for _ in range(2):
response = cast(CachedResponse, await session.get(httpbin('stream-bytes/64')))
for i in range(2):
any_response = await session.get(httpbin('stream-bytes/64'))

# Can read multiple times.
assert {len(await any_response.read()) for _ in range(3)} == {64}

if i == 0:
continue

response = cast(CachedResponse, any_response)

lines = [line async for line in response.content]
assert len(b''.join(lines)) == 64

# Test some additional methods on the cached response
response.reset()
chunks = [c async for (c, _) in response.content.iter_chunks()]
assert len(b''.join(chunks)) == 64
response.reset()
# Test some additional methods on the cached response
response.reset()
chunks = [c async for (c, _) in response.content.iter_chunks()]
assert len(b''.join(chunks)) == 64
response.reset()

chunks = [c async for c in response.content.iter_chunked(2)]
assert len(b''.join(chunks)) == 64
response.reset()
chunks = [c async for c in response.content.iter_chunked(2)]
assert len(b''.join(chunks)) == 64
response.reset()

chunks = [c async for c in response.content.iter_any()]
assert len(b''.join(chunks)) == 64
response.reset()
chunks = [c async for c in response.content.iter_any()]
assert len(b''.join(chunks)) == 64
response.reset()

# readany() should return empty bytes after being consumed
assert len(await response.content.readany()) == 64
assert await response.content.readany() == b''
# readany() should return empty bytes after being consumed
assert len(await response.content.readany()) == 64
assert await response.content.readany() == b''

async def test_streaming_request__ignored(self):
"""If a streaming request is filtered out (expire_after=0), its body should be readable as usual"""
Expand Down

0 comments on commit ed4a16c

Please sign in to comment.