mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 02:24:51 -05:00
Keep caller supplied default headers in async_create_clientsession (#179824)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
f64340cd9a
commit
d315c85a3d
@@ -16,6 +16,7 @@ from aiohttp import ClientMiddlewareType, hdrs, web
|
||||
from aiohttp.hdrs import CONTENT_TYPE, USER_AGENT
|
||||
from aiohttp.web_exceptions import HTTPBadGateway, HTTPGatewayTimeout
|
||||
from aiohttp_asyncmdnsresolver.api import AsyncDualMDNSResolver
|
||||
from multidict import CIMultiDict
|
||||
from yarl import URL
|
||||
|
||||
from homeassistant import config_entries
|
||||
@@ -300,8 +301,10 @@ def _async_create_clientsession(
|
||||
# It's important that we identify as Home Assistant
|
||||
# If a package requires a different user agent, override it by passing a headers
|
||||
# dictionary to the request method.
|
||||
default_headers = CIMultiDict(clientsession.headers)
|
||||
default_headers[USER_AGENT] = SERVER_SOFTWARE
|
||||
clientsession._default_headers = MappingProxyType( # type: ignore[assignment] # noqa: SLF001
|
||||
{USER_AGENT: SERVER_SOFTWARE},
|
||||
default_headers
|
||||
)
|
||||
|
||||
clientsession.close = warn_use( # type: ignore[method-assign]
|
||||
|
||||
@@ -393,6 +393,17 @@ async def test_client_session_immutable_headers(hass: HomeAssistant) -> None:
|
||||
session.headers.update({"user-agent": "bla"})
|
||||
|
||||
|
||||
async def test_create_clientsession_keeps_headers(hass: HomeAssistant) -> None:
|
||||
"""Test default headers passed to the session are kept."""
|
||||
session = client.async_create_clientsession(
|
||||
hass, headers={"Authorization": "Basic bla", "User-Agent": "bla"}
|
||||
)
|
||||
|
||||
assert session.headers["authorization"] == "Basic bla"
|
||||
# We always identify as Home Assistant
|
||||
assert session.headers["user-agent"] == client.SERVER_SOFTWARE
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("disable_mock_zeroconf_resolver")
|
||||
@pytest.mark.usefixtures("mock_async_zeroconf")
|
||||
async def test_async_mdnsresolver(
|
||||
|
||||
Reference in New Issue
Block a user