From 37a3d5fd8568502c9b592a80a8f68a399257a893 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 30 Aug 2019 18:32:38 -0700 Subject: [PATCH] Add HEAD and PUT support to webhooks (#26299) --- homeassistant/components/webhook/__init__.py | 6 ++- tests/components/webhook/test_init.py | 39 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/webhook/__init__.py b/homeassistant/components/webhook/__init__.py index 38540bbd307..a12e55c771a 100644 --- a/homeassistant/components/webhook/__init__.py +++ b/homeassistant/components/webhook/__init__.py @@ -99,11 +99,15 @@ class WebhookView(HomeAssistantView): name = "api:webhook" requires_auth = False - async def post(self, request, webhook_id): + async def _handle(self, request, webhook_id): """Handle webhook call.""" hass = request.app["hass"] return await async_handle_webhook(hass, webhook_id, request) + head = _handle + post = _handle + put = _handle + @callback def websocket_list(hass, connection, msg): diff --git a/tests/components/webhook/test_init.py b/tests/components/webhook/test_init.py index 307437f8c05..5f07ca9abc9 100644 --- a/tests/components/webhook/test_init.py +++ b/tests/components/webhook/test_init.py @@ -99,9 +99,48 @@ async def test_posting_webhook_no_data(hass, mock_client): assert len(hooks) == 1 assert hooks[0][0] is hass assert hooks[0][1] == webhook_id + assert hooks[0][2].method == "POST" assert await hooks[0][2].text() == "" +async def test_webhook_put(hass, mock_client): + """Test sending a put request to a webhook.""" + hooks = [] + webhook_id = hass.components.webhook.async_generate_id() + + async def handle(*args): + """Handle webhook.""" + hooks.append(args) + + hass.components.webhook.async_register("test", "Test hook", webhook_id, handle) + + resp = await mock_client.put("/api/webhook/{}".format(webhook_id)) + assert resp.status == 200 + assert len(hooks) == 1 + assert hooks[0][0] is hass + assert hooks[0][1] == webhook_id + assert hooks[0][2].method == "PUT" + + +async def test_webhook_head(hass, mock_client): + """Test sending a head request to a webhook.""" + hooks = [] + webhook_id = hass.components.webhook.async_generate_id() + + async def handle(*args): + """Handle webhook.""" + hooks.append(args) + + hass.components.webhook.async_register("test", "Test hook", webhook_id, handle) + + resp = await mock_client.head("/api/webhook/{}".format(webhook_id)) + assert resp.status == 200 + assert len(hooks) == 1 + assert hooks[0][0] is hass + assert hooks[0][1] == webhook_id + assert hooks[0][2].method == "HEAD" + + async def test_listing_webhook(hass, hass_ws_client, hass_access_token): """Test unregistering a webhook.""" assert await async_setup_component(hass, "webhook", {})