mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Create httpx helper to wrap a shared httpx.AsyncClient (#43877)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
143
tests/helpers/test_httpx_client.py
Normal file
143
tests/helpers/test_httpx_client.py
Normal file
@@ -0,0 +1,143 @@
|
||||
"""Test the httpx client helper."""
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import EVENT_HOMEASSISTANT_CLOSE
|
||||
import homeassistant.helpers.httpx_client as client
|
||||
|
||||
from tests.async_mock import Mock, patch
|
||||
|
||||
|
||||
async def test_get_async_client_with_ssl(hass):
|
||||
"""Test init async client with ssl."""
|
||||
client.get_async_client(hass)
|
||||
|
||||
assert isinstance(hass.data[client.DATA_ASYNC_CLIENT], httpx.AsyncClient)
|
||||
|
||||
|
||||
async def test_get_async_client_without_ssl(hass):
|
||||
"""Test init async client without ssl."""
|
||||
client.get_async_client(hass, verify_ssl=False)
|
||||
|
||||
assert isinstance(hass.data[client.DATA_ASYNC_CLIENT_NOVERIFY], httpx.AsyncClient)
|
||||
|
||||
|
||||
async def test_create_async_httpx_client_with_ssl_and_cookies(hass):
|
||||
"""Test init async client with ssl and cookies."""
|
||||
client.get_async_client(hass)
|
||||
|
||||
httpx_client = client.create_async_httpx_client(hass, cookies={"bla": True})
|
||||
assert isinstance(httpx_client, httpx.AsyncClient)
|
||||
assert hass.data[client.DATA_ASYNC_CLIENT] != httpx_client
|
||||
|
||||
|
||||
async def test_create_async_httpx_client_without_ssl_and_cookies(hass):
|
||||
"""Test init async client without ssl and cookies."""
|
||||
client.get_async_client(hass, verify_ssl=False)
|
||||
|
||||
httpx_client = client.create_async_httpx_client(
|
||||
hass, verify_ssl=False, cookies={"bla": True}
|
||||
)
|
||||
assert isinstance(httpx_client, httpx.AsyncClient)
|
||||
assert hass.data[client.DATA_ASYNC_CLIENT_NOVERIFY] != httpx_client
|
||||
|
||||
|
||||
async def test_get_async_client_cleanup(hass):
|
||||
"""Test init async client with ssl."""
|
||||
client.get_async_client(hass)
|
||||
|
||||
assert isinstance(hass.data[client.DATA_ASYNC_CLIENT], httpx.AsyncClient)
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_CLOSE)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.data[client.DATA_ASYNC_CLIENT].is_closed
|
||||
|
||||
|
||||
async def test_get_async_client_cleanup_without_ssl(hass):
|
||||
"""Test init async client without ssl."""
|
||||
client.get_async_client(hass, verify_ssl=False)
|
||||
|
||||
assert isinstance(hass.data[client.DATA_ASYNC_CLIENT_NOVERIFY], httpx.AsyncClient)
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_CLOSE)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.data[client.DATA_ASYNC_CLIENT_NOVERIFY].is_closed
|
||||
|
||||
|
||||
async def test_get_async_client_patched_close(hass):
|
||||
"""Test closing the async client does not work."""
|
||||
|
||||
with patch("httpx.AsyncClient.aclose") as mock_aclose:
|
||||
httpx_session = client.get_async_client(hass)
|
||||
assert isinstance(hass.data[client.DATA_ASYNC_CLIENT], httpx.AsyncClient)
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
await httpx_session.aclose()
|
||||
|
||||
assert mock_aclose.call_count == 0
|
||||
|
||||
|
||||
async def test_warning_close_session_integration(hass, caplog):
|
||||
"""Test log warning message when closing the session from integration context."""
|
||||
with patch(
|
||||
"homeassistant.helpers.frame.extract_stack",
|
||||
return_value=[
|
||||
Mock(
|
||||
filename="/home/paulus/homeassistant/core.py",
|
||||
lineno="23",
|
||||
line="do_something()",
|
||||
),
|
||||
Mock(
|
||||
filename="/home/paulus/homeassistant/components/hue/light.py",
|
||||
lineno="23",
|
||||
line="await session.aclose()",
|
||||
),
|
||||
Mock(
|
||||
filename="/home/paulus/aiohue/lights.py",
|
||||
lineno="2",
|
||||
line="something()",
|
||||
),
|
||||
],
|
||||
):
|
||||
httpx_session = client.get_async_client(hass)
|
||||
await httpx_session.aclose()
|
||||
|
||||
assert (
|
||||
"Detected integration that closes the Home Assistant httpx client. "
|
||||
"Please report issue for hue using this method at "
|
||||
"homeassistant/components/hue/light.py, line 23: await session.aclose()"
|
||||
) in caplog.text
|
||||
|
||||
|
||||
async def test_warning_close_session_custom(hass, caplog):
|
||||
"""Test log warning message when closing the session from custom context."""
|
||||
with patch(
|
||||
"homeassistant.helpers.frame.extract_stack",
|
||||
return_value=[
|
||||
Mock(
|
||||
filename="/home/paulus/homeassistant/core.py",
|
||||
lineno="23",
|
||||
line="do_something()",
|
||||
),
|
||||
Mock(
|
||||
filename="/home/paulus/config/custom_components/hue/light.py",
|
||||
lineno="23",
|
||||
line="await session.aclose()",
|
||||
),
|
||||
Mock(
|
||||
filename="/home/paulus/aiohue/lights.py",
|
||||
lineno="2",
|
||||
line="something()",
|
||||
),
|
||||
],
|
||||
):
|
||||
httpx_session = client.get_async_client(hass)
|
||||
await httpx_session.aclose()
|
||||
assert (
|
||||
"Detected integration that closes the Home Assistant httpx client. "
|
||||
"Please report issue to the custom component author for hue using this method at "
|
||||
"custom_components/hue/light.py, line 23: await session.aclose()" in caplog.text
|
||||
)
|
||||
Reference in New Issue
Block a user