From 31200040da1890c9c5225219922a2806c8a80eb7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 24 Sep 2024 01:51:08 -0500 Subject: [PATCH] Bump aiohttp to 3.10.6rc2 (#126468) --- homeassistant/package_constraints.txt | 2 +- homeassistant/util/aiohttp.py | 21 ++++++++++++++++++++- pyproject.toml | 2 +- requirements.txt | 2 +- tests/components/media_player/test_init.py | 11 ++++++++++- tests/components/motioneye/test_camera.py | 22 ++++++++-------------- tests/test_util/aiohttp.py | 15 ++++++++++++++- 7 files changed, 55 insertions(+), 20 deletions(-) diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index 9b41d3da10b..064034a1641 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -5,7 +5,7 @@ aiodiscover==2.1.0 aiodns==3.2.0 aiohasupervisor==0.1.0b1 aiohttp-fast-zlib==0.1.1 -aiohttp==3.10.5 +aiohttp==3.10.6rc2 aiohttp_cors==0.7.0 aiozoneinfo==0.2.1 astral==2.2 diff --git a/homeassistant/util/aiohttp.py b/homeassistant/util/aiohttp.py index 2a4616ee634..5571861f417 100644 --- a/homeassistant/util/aiohttp.py +++ b/homeassistant/util/aiohttp.py @@ -28,6 +28,19 @@ class MockStreamReader: return self._content.read(byte_count) +class MockPayloadWriter: + """Small mock to imitate payload writer.""" + + def enable_chunking(self) -> None: + """Enable chunking.""" + + async def write_headers(self, *args: Any, **kwargs: Any) -> None: + """Write headers.""" + + +_MOCK_PAYLOAD_WRITER = MockPayloadWriter() + + class MockRequest: """Mock an aiohttp request.""" @@ -49,8 +62,14 @@ class MockRequest: self.status = status self.headers: CIMultiDict[str] = CIMultiDict(headers or {}) self.query_string = query_string or "" + self.keep_alive = False + self.version = (1, 1) self._content = content self.mock_source = mock_source + self._payload_writer = _MOCK_PAYLOAD_WRITER + + async def _prepare_hook(self, response: Any) -> None: + """Prepare hook.""" @property def query(self) -> MultiDict[str]: @@ -90,7 +109,7 @@ def serialize_response(response: web.Response) -> dict[str, Any]: if (body := response.body) is None: body_decoded = None elif isinstance(body, payload.StringPayload): - body_decoded = body._value.decode(body.encoding) # noqa: SLF001 + body_decoded = body._value.decode(body.encoding or "utf-8") # noqa: SLF001 elif isinstance(body, bytes): body_decoded = body.decode(response.charset or "utf-8") else: diff --git a/pyproject.toml b/pyproject.toml index e0e1a25e610..c23da491db6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ dependencies = [ # Integrations may depend on hassio integration without listing it to # change behavior based on presence of supervisor "aiohasupervisor==0.1.0b1", - "aiohttp==3.10.5", + "aiohttp==3.10.6rc2", "aiohttp_cors==0.7.0", "aiohttp-fast-zlib==0.1.1", "aiozoneinfo==0.2.1", diff --git a/requirements.txt b/requirements.txt index 4f9e0ff622f..0d1464b01b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,7 @@ # Home Assistant Core aiodns==3.2.0 aiohasupervisor==0.1.0b1 -aiohttp==3.10.5 +aiohttp==3.10.6rc2 aiohttp_cors==0.7.0 aiohttp-fast-zlib==0.1.1 aiozoneinfo==0.2.1 diff --git a/tests/components/media_player/test_init.py b/tests/components/media_player/test_init.py index 11898edfc36..8909995a3ff 100644 --- a/tests/components/media_player/test_init.py +++ b/tests/components/media_player/test_init.py @@ -298,10 +298,20 @@ async def test_enqueue_alert_exclusive(hass: HomeAssistant) -> None: ) +@pytest.mark.parametrize( + "media_content_id", + [ + "a/b c/d+e%2Fg{}", + "a/b c/d+e%2D", + "a/b c/d+e%2E", + "2012-06%20Pool%20party%20%2F%20BBQ", + ], +) async def test_get_async_get_browse_image_quoting( hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator, hass_ws_client: WebSocketGenerator, + media_content_id: str, ) -> None: """Test get browse image using media_content_id with special characters. @@ -325,7 +335,6 @@ async def test_get_async_get_browse_image_quoting( "homeassistant.components.media_player.MediaPlayerEntity." "async_get_browse_image", ) as mock_browse_image: - media_content_id = "a/b c/d+e%2Fg{}" url = player.get_browse_image_url("album", media_content_id) await client.get(url) mock_browse_image.assert_called_with("album", media_content_id, None) diff --git a/tests/components/motioneye/test_camera.py b/tests/components/motioneye/test_camera.py index 0f3a7d6f904..8ef58cc968d 100644 --- a/tests/components/motioneye/test_camera.py +++ b/tests/components/motioneye/test_camera.py @@ -3,7 +3,6 @@ from asyncio import AbstractEventLoop from collections.abc import Callable import copy -from typing import cast from unittest.mock import AsyncMock, Mock, call from aiohttp import web @@ -46,6 +45,7 @@ from homeassistant.const import ATTR_DEVICE_ID, ATTR_ENTITY_ID, CONF_URL from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.util.aiohttp import MockRequest import homeassistant.util.dt as dt_util from . import ( @@ -231,7 +231,7 @@ async def test_get_still_image_from_camera( ) -> None: """Test getting a still image.""" - image_handler = AsyncMock(return_value="") + image_handler = AsyncMock(return_value=web.Response(body="")) app = web.Application() app.add_routes( @@ -273,7 +273,8 @@ async def test_get_stream_from_camera( ) -> None: """Test getting a stream.""" - stream_handler = AsyncMock(return_value="") + stream_handler = AsyncMock(return_value=web.Response(body="")) + app = web.Application() app.add_routes([web.get("/", stream_handler)]) stream_server = await aiohttp_server(app) @@ -297,12 +298,7 @@ async def test_get_stream_from_camera( ) await hass.async_block_till_done() - # It won't actually get a stream from the dummy handler, so just catch - # the expected exception, then verify the right handler was called. - with pytest.raises(HTTPBadGateway): - await async_get_mjpeg_stream( - hass, cast(web.Request, None), TEST_CAMERA_ENTITY_ID - ) + await async_get_mjpeg_stream(hass, MockRequest(b"", "test"), TEST_CAMERA_ENTITY_ID) assert stream_handler.called @@ -358,7 +354,8 @@ async def test_camera_option_stream_url_template( """Verify camera with a stream URL template option.""" client = create_mock_motioneye_client() - stream_handler = AsyncMock(return_value="") + stream_handler = AsyncMock(return_value=web.Response(body="")) + app = web.Application() app.add_routes([web.get(f"/{TEST_CAMERA_NAME}/{TEST_CAMERA_ID}", stream_handler)]) stream_server = await aiohttp_server(app) @@ -384,10 +381,7 @@ async def test_camera_option_stream_url_template( ) await hass.async_block_till_done() - # It won't actually get a stream from the dummy handler, so just catch - # the expected exception, then verify the right handler was called. - with pytest.raises(HTTPBadGateway): - await async_get_mjpeg_stream(hass, Mock(), TEST_CAMERA_ENTITY_ID) + await async_get_mjpeg_stream(hass, MockRequest(b"", "test"), TEST_CAMERA_ENTITY_ID) assert AsyncMock.called assert not client.get_camera_stream_url.called diff --git a/tests/test_util/aiohttp.py b/tests/test_util/aiohttp.py index 04d6db509e0..633f98dc5b3 100644 --- a/tests/test_util/aiohttp.py +++ b/tests/test_util/aiohttp.py @@ -5,6 +5,7 @@ from collections.abc import Iterator from contextlib import contextmanager from http import HTTPStatus import re +from types import TracebackType from typing import Any from unittest import mock from urllib.parse import parse_qs @@ -166,7 +167,7 @@ class AiohttpClientMockResponse: def __init__( self, method, - url, + url: URL, status=HTTPStatus.OK, response=None, json=None, @@ -297,6 +298,18 @@ class AiohttpClientMockResponse: raise ClientConnectionError("Connection closed") return self._response + async def __aenter__(self): + """Enter the context manager.""" + return self + + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, + ) -> None: + """Exit the context manager.""" + @contextmanager def mock_aiohttp_client() -> Iterator[AiohttpClientMocker]: