diff --git a/tests/helpers/test_network.py b/tests/helpers/test_network.py index c470fbd7834..3330c42d9fc 100644 --- a/tests/helpers/test_network.py +++ b/tests/helpers/test_network.py @@ -12,6 +12,7 @@ from homeassistant.helpers.network import ( _get_internal_url, _get_request_host, get_url, + is_internal_request, ) from tests.async_mock import Mock, patch @@ -860,3 +861,40 @@ async def test_get_current_request_url_with_known_host( "homeassistant.helpers.network._get_request_host", return_value="unknown.local" ), pytest.raises(NoURLAvailableError): get_url(hass, require_current_request=True) + + +async def test_is_internal_request(hass: HomeAssistant): + """Test if accessing an instance on its internal URL.""" + # Test with internal URL: http://example.local:8123 + await async_process_ha_core_config( + hass, + {"internal_url": "http://example.local:8123"}, + ) + + assert hass.config.internal_url == "http://example.local:8123" + assert not is_internal_request(hass) + + with patch( + "homeassistant.helpers.network._get_request_host", return_value="example.local" + ): + assert is_internal_request(hass) + + with patch( + "homeassistant.helpers.network._get_request_host", + return_value="no_match.example.local", + ): + assert not is_internal_request(hass) + + # Test with internal URL: http://192.168.0.1:8123 + await async_process_ha_core_config( + hass, + {"internal_url": "http://192.168.0.1:8123"}, + ) + + assert hass.config.internal_url == "http://192.168.0.1:8123" + assert not is_internal_request(hass) + + with patch( + "homeassistant.helpers.network._get_request_host", return_value="192.168.0.1" + ): + assert is_internal_request(hass)