Add HEAD and PUT support to webhooks (#26299)

pull/26317/head
Paulus Schoutsen 2019-08-30 18:32:38 -07:00 committed by Andrew Sayre
parent f01e106e6d
commit 37a3d5fd85
2 changed files with 44 additions and 1 deletions

View File

@ -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):

View File

@ -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", {})