Skip to content

Commit 59a7e95

Browse files
authored
Fix root path requests (home-assistant#5815)
* Fix root path requests Since home-assistant#5759 we've tried to access the path explicitly. However, this raises KeyError exception when trying to access the proxied root path (e.g. http://supervisor/core/api/). Before home-assistant#5759 get was used, which lead to no exception, but instead inserted a `None` into the path. It seems aiohttp doesn't provide a path when the root is accessed. So simply convert this to no path as well by setting path to an empty string. * Add rudimentary pytest for regular proxy requets
1 parent dedf5df commit 59a7e95

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

supervisor/api/ingress.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ async def handler(
154154

155155
# Process requests
156156
addon = self._extract_addon(request)
157-
path = request.match_info["path"]
157+
path = request.match_info.get("path", "")
158158
session_data = self.sys_ingress.get_session_data(session)
159159
try:
160160
# Websocket

supervisor/api/proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async def api(self, request: web.Request):
117117
raise HTTPBadGateway()
118118

119119
# Normal request
120-
path = request.match_info["path"]
120+
path = request.match_info.get("path", "")
121121
async with self._api_client(request, path) as client:
122122
data = await client.read()
123123
return web.Response(

tests/api/test_proxy.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from json import dumps
88
import logging
99
from typing import Any, cast
10-
from unittest.mock import patch
10+
from unittest.mock import AsyncMock, patch
1111

1212
from aiohttp import ClientWebSocketResponse, WSCloseCode
1313
from aiohttp.http_websocket import WSMessage, WSMsgType
@@ -17,6 +17,7 @@
1717
from supervisor.addons.addon import Addon
1818
from supervisor.api.proxy import APIProxy
1919
from supervisor.const import ATTR_ACCESS_TOKEN
20+
from supervisor.homeassistant.api import HomeAssistantAPI
2021

2122

2223
def id_generator() -> Generator[int]:
@@ -220,3 +221,36 @@ async def test_proxy_auth_abort_log(
220221
assert (
221222
"Unexpected message during authentication for WebSocket API" in caplog.text
222223
)
224+
225+
226+
@pytest.mark.parametrize("path", ["", "mock_path"])
227+
async def test_api_proxy_get_request(
228+
api_client: TestClient,
229+
install_addon_example: Addon,
230+
request: pytest.FixtureRequest,
231+
path: str,
232+
):
233+
"""Test the API proxy request using patch for make_request."""
234+
install_addon_example.persist[ATTR_ACCESS_TOKEN] = "abc123"
235+
install_addon_example.data["homeassistant_api"] = True
236+
237+
request.param = "local_example"
238+
239+
with patch.object(HomeAssistantAPI, "make_request") as make_request:
240+
# Mock the response from make_request
241+
mock_response = AsyncMock()
242+
mock_response.status = 200
243+
mock_response.content_type = "application/json"
244+
mock_response.read.return_value = b"mocked response"
245+
make_request.return_value.__aenter__.return_value = mock_response
246+
247+
response = await api_client.get(
248+
f"/core/api/{path}", headers={"Authorization": "Bearer abc123"}
249+
)
250+
251+
assert make_request.call_args[0][0] == "get"
252+
assert make_request.call_args[0][1] == f"api/{path}"
253+
254+
assert response.status == 200
255+
assert await response.text() == "mocked response"
256+
assert response.content_type == "application/json"

0 commit comments

Comments
 (0)