Check status code when downloading file and raise an error if someting bad happends

This commit is contained in:
Dmitry Anfimov 2022-12-13 22:27:58 +06:00
parent a0dbbcfd5d
commit eb2125d631
6 changed files with 34 additions and 7 deletions

View file

@ -37,7 +37,7 @@ class MockedSession(BaseSession):
return response.result # type: ignore
async def stream_content(
self, url: str, timeout: int, chunk_size: int
self, url: str, timeout: int, chunk_size: int, raise_for_status: bool,
) -> AsyncGenerator[bytes, None]: # pragma: no cover
yield b""

View file

@ -192,7 +192,10 @@ class TestAiohttpSession:
session = AiohttpSession()
stream = session.stream_content(
"https://www.python.org/static/img/python-logo.png", timeout=5, chunk_size=1
"https://www.python.org/static/img/python-logo.png",
timeout=5,
chunk_size=1,
raise_for_status=True,
)
assert isinstance(stream, AsyncGenerator)
@ -204,6 +207,25 @@ class TestAiohttpSession:
size += chunk_size
assert size == 10
async def test_stream_content_404(self, aresponses: ResponsesMockServer):
aresponses.add(
aresponses.ANY, aresponses.ANY, "get", aresponses.Response(
status=404,
body=b"File not found",
)
)
session = AiohttpSession()
stream = session.stream_content(
"https://www.python.org/static/img/python-logo.png",
timeout=5,
chunk_size=1,
raise_for_status=True,
)
with pytest.raises(ClientError):
async for _ in stream:
...
async def test_context_manager(self):
session = AiohttpSession()
assert isinstance(session, AsyncContextManager)