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:
J. Nick Koston 2024-09-06 08:34:52 -05:00 committed by GitHub
parent ff449e7741
commit 051a28b55a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 7 deletions

View File

@ -216,7 +216,7 @@ def _get_request_host() -> str | None:
"""Get the host address of the current request."""
if (request := http.current_request.get()) is None:
raise NoURLAvailableError
return yarl.URL(request.url).host
return request.url.host
@bind_hass

View File

@ -3,6 +3,7 @@
from unittest.mock import Mock, patch
import pytest
from yarl import URL
from homeassistant.components import cloud
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:
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)
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
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)
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)
# 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 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)
# Test for matching against local IP
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"):
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
# Test for matching against HassOS hostname
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