mirror of
https://github.com/home-assistant/supervisor.git
synced 2026-02-14 23:19:37 +00:00
Add DELETE method support to /core/api proxy (#6521)
The Supervisor's /core/api proxy previously only supported GET and POST methods, returning 405 Method Not Allowed for DELETE requests. This prevented addons from calling Home Assistant Core REST API endpoints that require DELETE methods, such as deleting automations, scripts, or scenes. The underlying proxy implementation already supported passing through any HTTP method via request.method.lower(), so only the route registration was needed. Fixes #6509 Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -522,6 +522,7 @@ class RestAPI(CoreSysAttributes):
|
||||
web.get("/core/api/stream", api_proxy.stream),
|
||||
web.post("/core/api/{path:.+}", api_proxy.api),
|
||||
web.get("/core/api/{path:.+}", api_proxy.api),
|
||||
web.delete("/core/api/{path:.+}", api_proxy.api),
|
||||
web.get("/core/api/", api_proxy.api),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -254,3 +254,75 @@ async def test_api_proxy_get_request(
|
||||
assert response.status == 200
|
||||
assert await response.text() == "mocked response"
|
||||
assert response.content_type == "application/json"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"path", ["config/automation/config/test_id", "services/light/turn_on"]
|
||||
)
|
||||
async def test_api_proxy_post_request(
|
||||
api_client: TestClient,
|
||||
install_addon_example: Addon,
|
||||
request: pytest.FixtureRequest,
|
||||
path: str,
|
||||
):
|
||||
"""Test the API proxy POST request."""
|
||||
install_addon_example.persist[ATTR_ACCESS_TOKEN] = "abc123"
|
||||
install_addon_example.data["homeassistant_api"] = True
|
||||
|
||||
request.param = "local_example"
|
||||
|
||||
with patch.object(HomeAssistantAPI, "make_request") as make_request:
|
||||
# Mock the response from make_request
|
||||
mock_response = AsyncMock()
|
||||
mock_response.status = 200
|
||||
mock_response.content_type = "application/json"
|
||||
mock_response.read.return_value = b'{"result": "ok"}'
|
||||
make_request.return_value.__aenter__.return_value = mock_response
|
||||
|
||||
response = await api_client.post(
|
||||
f"/core/api/{path}",
|
||||
headers={"Authorization": "Bearer abc123"},
|
||||
json={"test": "data"},
|
||||
)
|
||||
|
||||
assert make_request.call_args[0][0] == "post"
|
||||
assert make_request.call_args[0][1] == f"api/{path}"
|
||||
|
||||
assert response.status == 200
|
||||
assert await response.text() == '{"result": "ok"}'
|
||||
assert response.content_type == "application/json"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"path", ["config/automation/config/test_id", "states/light.test"]
|
||||
)
|
||||
async def test_api_proxy_delete_request(
|
||||
api_client: TestClient,
|
||||
install_addon_example: Addon,
|
||||
request: pytest.FixtureRequest,
|
||||
path: str,
|
||||
):
|
||||
"""Test the API proxy DELETE request."""
|
||||
install_addon_example.persist[ATTR_ACCESS_TOKEN] = "abc123"
|
||||
install_addon_example.data["homeassistant_api"] = True
|
||||
|
||||
request.param = "local_example"
|
||||
|
||||
with patch.object(HomeAssistantAPI, "make_request") as make_request:
|
||||
# Mock the response from make_request
|
||||
mock_response = AsyncMock()
|
||||
mock_response.status = 200
|
||||
mock_response.content_type = "application/json"
|
||||
mock_response.read.return_value = b'{"result": "ok"}'
|
||||
make_request.return_value.__aenter__.return_value = mock_response
|
||||
|
||||
response = await api_client.delete(
|
||||
f"/core/api/{path}", headers={"Authorization": "Bearer abc123"}
|
||||
)
|
||||
|
||||
assert make_request.call_args[0][0] == "delete"
|
||||
assert make_request.call_args[0][1] == f"api/{path}"
|
||||
|
||||
assert response.status == 200
|
||||
assert await response.text() == '{"result": "ok"}'
|
||||
assert response.content_type == "application/json"
|
||||
|
||||
Reference in New Issue
Block a user