Add test for is_internal_request (#43841)

This commit is contained in:
Chris Talkington 2020-12-02 03:12:48 -06:00 committed by GitHub
parent cf286d1c51
commit 891edec73b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)