mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Remove unneeded wrapping of URL in URL in network helper (#125265)
* Remove unneeded wrapping of URL in URL in network helper * fix mocks
This commit is contained in:
parent
ff449e7741
commit
051a28b55a
@ -216,7 +216,7 @@ def _get_request_host() -> str | None:
|
|||||||
"""Get the host address of the current request."""
|
"""Get the host address of the current request."""
|
||||||
if (request := http.current_request.get()) is None:
|
if (request := http.current_request.get()) is None:
|
||||||
raise NoURLAvailableError
|
raise NoURLAvailableError
|
||||||
return yarl.URL(request.url).host
|
return request.url.host
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from yarl import URL
|
||||||
|
|
||||||
from homeassistant.components import cloud
|
from homeassistant.components import cloud
|
||||||
from homeassistant.config import async_process_ha_core_config
|
from homeassistant.config import async_process_ha_core_config
|
||||||
@ -591,7 +592,7 @@ async def test_get_request_host(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
with patch("homeassistant.components.http.current_request") as mock_request_context:
|
with patch("homeassistant.components.http.current_request") as mock_request_context:
|
||||||
mock_request = Mock()
|
mock_request = Mock()
|
||||||
mock_request.url = "http://example.com:8123/test/request"
|
mock_request.url = URL("http://example.com:8123/test/request")
|
||||||
mock_request_context.get = Mock(return_value=mock_request)
|
mock_request_context.get = Mock(return_value=mock_request)
|
||||||
|
|
||||||
assert _get_request_host() == "example.com"
|
assert _get_request_host() == "example.com"
|
||||||
@ -682,10 +683,12 @@ async def test_is_internal_request(hass: HomeAssistant, mock_current_request) ->
|
|||||||
mock_current_request.return_value = None
|
mock_current_request.return_value = None
|
||||||
assert not is_internal_request(hass)
|
assert not is_internal_request(hass)
|
||||||
|
|
||||||
mock_current_request.return_value = Mock(url="http://example.local:8123")
|
mock_current_request.return_value = Mock(url=URL("http://example.local:8123"))
|
||||||
assert is_internal_request(hass)
|
assert is_internal_request(hass)
|
||||||
|
|
||||||
mock_current_request.return_value = Mock(url="http://no_match.example.local:8123")
|
mock_current_request.return_value = Mock(
|
||||||
|
url=URL("http://no_match.example.local:8123")
|
||||||
|
)
|
||||||
assert not is_internal_request(hass)
|
assert not is_internal_request(hass)
|
||||||
|
|
||||||
# Test with internal URL: http://192.168.0.1:8123
|
# Test with internal URL: http://192.168.0.1:8123
|
||||||
@ -697,18 +700,18 @@ async def test_is_internal_request(hass: HomeAssistant, mock_current_request) ->
|
|||||||
assert hass.config.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)
|
assert not is_internal_request(hass)
|
||||||
|
|
||||||
mock_current_request.return_value = Mock(url="http://192.168.0.1:8123")
|
mock_current_request.return_value = Mock(url=URL("http://192.168.0.1:8123"))
|
||||||
assert is_internal_request(hass)
|
assert is_internal_request(hass)
|
||||||
|
|
||||||
# Test for matching against local IP
|
# Test for matching against local IP
|
||||||
hass.config.api = Mock(use_ssl=False, local_ip="192.168.123.123", port=8123)
|
hass.config.api = Mock(use_ssl=False, local_ip="192.168.123.123", port=8123)
|
||||||
for allowed in ("127.0.0.1", "192.168.123.123"):
|
for allowed in ("127.0.0.1", "192.168.123.123"):
|
||||||
mock_current_request.return_value = Mock(url=f"http://{allowed}:8123")
|
mock_current_request.return_value = Mock(url=URL(f"http://{allowed}:8123"))
|
||||||
assert is_internal_request(hass), mock_current_request.return_value.url
|
assert is_internal_request(hass), mock_current_request.return_value.url
|
||||||
|
|
||||||
# Test for matching against HassOS hostname
|
# Test for matching against HassOS hostname
|
||||||
for allowed in ("hellohost", "hellohost.local"):
|
for allowed in ("hellohost", "hellohost.local"):
|
||||||
mock_current_request.return_value = Mock(url=f"http://{allowed}:8123")
|
mock_current_request.return_value = Mock(url=URL(f"http://{allowed}:8123"))
|
||||||
assert is_internal_request(hass), mock_current_request.return_value.url
|
assert is_internal_request(hass), mock_current_request.return_value.url
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user