Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion nexia/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,11 @@ async def _get_url(
# the connection leaks until garbage collection.
await response.release()
await self.login()
return await self._get_url(request_url)
# Re-pass headers so the caller's conditional-request header
# (e.g. If-None-Match) survives the retry; without it the next
# poll after a session expiry always refetches the full body
# instead of getting a cheap 304.
return await self._get_url(request_url, headers=headers)

response.raise_for_status()
return response
Expand Down
22 changes: 22 additions & 0 deletions tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -2443,6 +2443,28 @@ async def test_get_url_releases_redirect_response_before_retry() -> None:
redirect.release.assert_awaited_once()


async def test_get_url_preserves_headers_on_relogin_retry() -> None:
"""A 302 re-login retry must re-send the caller's headers.

`_update` passes an `If-None-Match` conditional-request header so an
unchanged house returns a cheap 304. If that header is dropped on the
post-relogin retry, every poll after a session expiry refetches the
full body. The retry GET must carry the same header through.
"""
session = MagicMock()
nexia = NexiaHome(session, state_file=Path("nexia_config_test.conf"))

redirect = _make_response(302)
final = _make_response(200)
session.get = AsyncMock(side_effect=[redirect, final])

with patch.object(nexia, "login", new=AsyncMock()):
await nexia._get_url("https://example/x", headers={"If-None-Match": "etag-123"})

retry_headers = session.get.await_args_list[1].kwargs["headers"]
assert retry_headers["If-None-Match"] == "etag-123"


async def test_check_heat_cool_setpoints_rejects_out_of_range(
aiohttp_session: aiohttp.ClientSession,
) -> None:
Expand Down
Loading